blob: cff37c2c860bb806c61b9f25f55b7ecf2ba11318 [file] [log] [blame]
Dan Gohman3b172f12010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000012// "Fast" instruction selection is designed to emit very poor code quickly.
13// Also, it is not designed to be able to do much lowering, so most illegal
Chris Lattner44d2a982008-10-13 01:59:13 +000014// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
15// also not intended to be able to do much optimization, except in a few cases
16// where doing optimizations reduces overall compile time. For example, folding
17// constants into immediate fields is often done, because it's cheap and it
18// reduces the number of instructions later phases have to examine.
Dan Gohman5ec9efd2008-09-30 20:48:29 +000019//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
Chris Lattner44d2a982008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohman5ec9efd2008-09-30 20:48:29 +000023// the complicated lowering logic that SelectionDAG currently has.
24//
25// The intended use for "fast" instruction selection is "-O0" mode
26// compilation, where the quality of the generated code is irrelevant when
Chris Lattner44d2a982008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohman5ec9efd2008-09-30 20:48:29 +000028// at -O0, the LLVM optimizers are not running, and this makes the
29// compile time of codegen a much higher portion of the overall compile
Chris Lattner44d2a982008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohman5ec9efd2008-09-30 20:48:29 +000031// handle enough code on its own to provide noticeable overall speedups
32// in -O0 compiles.
33//
34// Basic operations are supported in a target-independent way, by reading
35// the same instruction descriptions that the SelectionDAG selector reads,
36// and identifying simple arithmetic operations that can be directly selected
Chris Lattner44d2a982008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohman5ec9efd2008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Chad Rosier053e69a2011-11-16 21:05:28 +000042#define DEBUG_TYPE "isel"
Dan Gohman33134c42008-09-25 17:05:24 +000043#include "llvm/Function.h"
44#include "llvm/GlobalVariable.h"
Dan Gohman6f2766d2008-08-19 22:31:46 +000045#include "llvm/Instructions.h"
Dan Gohman33134c42008-09-25 17:05:24 +000046#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000047#include "llvm/Operator.h"
Eli Friedman2586b8f2011-05-16 20:27:46 +000048#include "llvm/CodeGen/Analysis.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000049#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000050#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000051#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000052#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000053#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000054#include "llvm/Analysis/DebugInfo.h"
Dan Gohman7fbcc982010-07-01 03:49:38 +000055#include "llvm/Analysis/Loads.h"
Evan Cheng83785c82008-08-20 22:45:34 +000056#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000057#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000058#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000059#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000060#include "llvm/Support/ErrorHandling.h"
Devang Patelafeaae72010-12-06 22:39:26 +000061#include "llvm/Support/Debug.h"
Chad Rosier053e69a2011-11-16 21:05:28 +000062#include "llvm/ADT/Statistic.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000063using namespace llvm;
64
Chad Rosier053e69a2011-11-16 21:05:28 +000065STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by target-independent selector");
66STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by target-specific selector");
67
Dan Gohman84023e02010-07-10 09:00:22 +000068/// startNewBlock - Set the current block to which generated machine
69/// instructions will be appended, and clear the local CSE map.
70///
71void FastISel::startNewBlock() {
72 LocalValueMap.clear();
73
Ivan Krasin74af88a2011-08-18 22:06:10 +000074 EmitStartPt = 0;
Dan Gohman84023e02010-07-10 09:00:22 +000075
Ivan Krasin74af88a2011-08-18 22:06:10 +000076 // Advance the emit start point past any EH_LABEL instructions.
Dan Gohman84023e02010-07-10 09:00:22 +000077 MachineBasicBlock::iterator
78 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
79 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
Ivan Krasin74af88a2011-08-18 22:06:10 +000080 EmitStartPt = I;
Dan Gohman84023e02010-07-10 09:00:22 +000081 ++I;
82 }
Ivan Krasin74af88a2011-08-18 22:06:10 +000083 LastLocalValue = EmitStartPt;
84}
85
86void FastISel::flushLocalValueMap() {
87 LocalValueMap.clear();
88 LastLocalValue = EmitStartPt;
89 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +000090}
91
Dan Gohmana6cb6412010-05-11 23:54:07 +000092bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000093 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000094 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000095 if (!I)
96 return false;
97
98 // No-op casts are trivially coalesced by fast-isel.
99 if (const CastInst *Cast = dyn_cast<CastInst>(I))
100 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
101 !hasTrivialKill(Cast->getOperand(0)))
102 return false;
103
Chad Rosier22b34cc2011-11-15 23:34:05 +0000104 // GEPs with all zero indices are trivially coalesced by fast-isel.
105 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
106 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
107 return false;
108
Dan Gohman7f0d6952010-05-14 22:53:18 +0000109 // Only instructions with a single use in the same basic block are considered
110 // to have trivial kills.
111 return I->hasOneUse() &&
112 !(I->getOpcode() == Instruction::BitCast ||
113 I->getOpcode() == Instruction::PtrToInt ||
114 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000115 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000116}
117
Dan Gohman46510a72010-04-15 01:51:59 +0000118unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000119 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000120 // Don't handle non-simple values in FastISel.
121 if (!RealVT.isSimple())
122 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000123
124 // Ignore illegal types. We must do this before looking up the value
125 // in ValueMap because Arguments are given virtual registers regardless
126 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000128 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000129 // Handle integer promotions, though, because they're common and easy.
130 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000131 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000132 else
133 return 0;
134 }
135
Dan Gohman104e4ce2008-09-03 23:32:19 +0000136 // Look up the value to see if we already have a register for it. We
137 // cache values defined by Instructions across blocks, and other values
138 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000139 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000140 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
Chris Lattnerfff65b32011-04-17 01:16:47 +0000141 if (I != FuncInfo.ValueMap.end())
142 return I->second;
143
Dan Gohman104e4ce2008-09-03 23:32:19 +0000144 unsigned Reg = LocalValueMap[V];
145 if (Reg != 0)
146 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000147
Dan Gohman97c94b82010-05-06 00:02:14 +0000148 // In bottom-up mode, just create the virtual register which will be used
149 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000150 if (isa<Instruction>(V) &&
151 (!isa<AllocaInst>(V) ||
152 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
153 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000154
Dan Gohmana10b8492010-07-14 01:07:44 +0000155 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000156
157 // Materialize the value in a register. Emit any instructions in the
158 // local value area.
159 Reg = materializeRegForValue(V, VT);
160
161 leaveLocalValueArea(SaveInsertPt);
162
163 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000164}
165
Eric Christopher44a2c342010-08-17 01:30:33 +0000166/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000167/// called when the value isn't already available in a register and must
168/// be materialized with new instructions.
169unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
170 unsigned Reg = 0;
171
Dan Gohman46510a72010-04-15 01:51:59 +0000172 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000173 if (CI->getValue().getActiveBits() <= 64)
174 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000175 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000176 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000177 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000178 // Translate this as an integer zero so that it can be
179 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000180 Reg =
181 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000182 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000183 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000184 Reg = TargetMaterializeFloatZero(CF);
185 } else {
186 // Try to emit the constant directly.
187 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
188 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000189
190 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000191 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000192 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000193 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000194
195 uint64_t x[2];
196 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000197 bool isExact;
198 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
199 APFloat::rmTowardZero, &isExact);
200 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000201 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000202
Owen Andersone922c022009-07-22 00:24:57 +0000203 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000204 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000205 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000206 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
207 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000208 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000209 }
Dan Gohman46510a72010-04-15 01:51:59 +0000210 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000211 if (!SelectOperator(Op, Op->getOpcode()))
212 if (!isa<Instruction>(Op) ||
213 !TargetSelectInstruction(cast<Instruction>(Op)))
214 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000215 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000216 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000217 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
219 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000220 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000221
Dan Gohmandceffe62008-09-25 01:28:51 +0000222 // If target-independent code couldn't handle the value, give target-specific
223 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000224 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000225 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000226
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000227 // Don't cache constant materializations in the general ValueMap.
228 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000229 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000230 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000231 LastLocalValue = MRI.getVRegDef(Reg);
232 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000233 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000234}
235
Dan Gohman46510a72010-04-15 01:51:59 +0000236unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000237 // Look up the value to see if we already have a register for it. We
238 // cache values defined by Instructions across blocks, and other values
239 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000240 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000241 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
242 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000243 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000244 return LocalValueMap[V];
245}
246
Owen Andersoncc54e762008-08-30 00:38:46 +0000247/// UpdateValueMap - Update the value map to include the new mapping for this
248/// instruction, or insert an extra copy to get the result in a previous
249/// determined register.
250/// NOTE: This is only necessary because we might select a block that uses
251/// a value before we select the block that defines the value. It might be
252/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000253void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000254 if (!isa<Instruction>(I)) {
255 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000256 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000257 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000258
Dan Gohmana4160c32010-07-07 16:29:44 +0000259 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000260 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000261 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000262 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000263 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000264 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000265 for (unsigned i = 0; i < NumRegs; i++)
266 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000267
268 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000269 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000270}
271
Dan Gohmana6cb6412010-05-11 23:54:07 +0000272std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000273 unsigned IdxN = getRegForValue(Idx);
274 if (IdxN == 0)
275 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000276 return std::pair<unsigned, bool>(0, false);
277
278 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000279
280 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000281 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000282 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000283 if (IdxVT.bitsLT(PtrVT)) {
284 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
285 IdxN, IdxNIsKill);
286 IdxNIsKill = true;
287 }
288 else if (IdxVT.bitsGT(PtrVT)) {
289 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
290 IdxN, IdxNIsKill);
291 IdxNIsKill = true;
292 }
293 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000294}
295
Dan Gohman84023e02010-07-10 09:00:22 +0000296void FastISel::recomputeInsertPt() {
297 if (getLastLocalValue()) {
298 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000299 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000300 ++FuncInfo.InsertPt;
301 } else
302 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
303
304 // Now skip past any EH_LABELs, which must remain at the beginning.
305 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
306 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
307 ++FuncInfo.InsertPt;
308}
309
Dan Gohmana10b8492010-07-14 01:07:44 +0000310FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000311 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Dan Gohman163f78e2010-07-14 22:01:31 +0000312 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000313 recomputeInsertPt();
Dan Gohmana10b8492010-07-14 01:07:44 +0000314 DL = DebugLoc();
Dan Gohman163f78e2010-07-14 22:01:31 +0000315 SavePoint SP = { OldInsertPt, OldDL };
Dan Gohmana10b8492010-07-14 01:07:44 +0000316 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000317}
318
Dan Gohmana10b8492010-07-14 01:07:44 +0000319void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000320 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
321 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
322
323 // Restore the previous insert position.
Dan Gohmana10b8492010-07-14 01:07:44 +0000324 FuncInfo.InsertPt = OldInsertPt.InsertPt;
325 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000326}
327
Dan Gohmanbdedd442008-08-20 00:11:48 +0000328/// SelectBinaryOp - Select and emit code for a binary operator instruction,
329/// which has an opcode which directly corresponds to the given ISD opcode.
330///
Dan Gohman46510a72010-04-15 01:51:59 +0000331bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000332 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000334 // Unhandled type. Halt "fast" selection and bail.
335 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000336
Dan Gohmanb71fea22008-08-26 20:52:40 +0000337 // We only handle legal types. For example, on x86-32 the instruction
338 // selector contains all of the 64-bit instructions from x86-64,
339 // under the assumption that i64 won't be used if the target doesn't
340 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000341 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000343 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000345 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
346 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000347 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000348 else
349 return false;
350 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000351
Chris Lattnerfff65b32011-04-17 01:16:47 +0000352 // Check if the first operand is a constant, and handle it as "ri". At -O0,
353 // we don't have anything that canonicalizes operand order.
354 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
355 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
356 unsigned Op1 = getRegForValue(I->getOperand(1));
357 if (Op1 == 0) return false;
358
359 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000360
Chris Lattner602fc062011-04-17 20:23:29 +0000361 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
362 Op1IsKill, CI->getZExtValue(),
363 VT.getSimpleVT());
364 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000365
Chris Lattner602fc062011-04-17 20:23:29 +0000366 // We successfully emitted code for the given LLVM Instruction.
367 UpdateValueMap(I, ResultReg);
368 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000369 }
Owen Andersond74ea772011-04-22 23:38:06 +0000370
371
Dan Gohman3df24e62008-09-03 23:12:08 +0000372 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000373 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000374 return false;
375
Dan Gohmana6cb6412010-05-11 23:54:07 +0000376 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
377
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000378 // Check if the second operand is a constant and handle it appropriately.
379 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000380 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000381
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000382 // Transform "sdiv exact X, 8" -> "sra X, 3".
383 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
384 cast<BinaryOperator>(I)->isExact() &&
385 isPowerOf2_64(Imm)) {
386 Imm = Log2_64(Imm);
387 ISDOpcode = ISD::SRA;
388 }
Owen Andersond74ea772011-04-22 23:38:06 +0000389
Chris Lattner602fc062011-04-17 20:23:29 +0000390 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
391 Op0IsKill, Imm, VT.getSimpleVT());
392 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000393
Chris Lattner602fc062011-04-17 20:23:29 +0000394 // We successfully emitted code for the given LLVM Instruction.
395 UpdateValueMap(I, ResultReg);
396 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000397 }
398
Dan Gohman10df0fa2008-08-27 01:09:54 +0000399 // Check if the second operand is a constant float.
400 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000401 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000402 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000403 if (ResultReg != 0) {
404 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000405 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000406 return true;
407 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000408 }
409
Dan Gohman3df24e62008-09-03 23:12:08 +0000410 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000411 if (Op1 == 0)
412 // Unhandled operand. Halt "fast" selection and bail.
413 return false;
414
Dan Gohmana6cb6412010-05-11 23:54:07 +0000415 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
416
Dan Gohmanad368ac2008-08-27 18:10:19 +0000417 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000418 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000419 ISDOpcode,
420 Op0, Op0IsKill,
421 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000422 if (ResultReg == 0)
423 // Target-specific code wasn't able to find a machine opcode for
424 // the given ISD opcode and type. Halt "fast" selection and bail.
425 return false;
426
Dan Gohman8014e862008-08-20 00:23:20 +0000427 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000428 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000429 return true;
430}
431
Dan Gohman46510a72010-04-15 01:51:59 +0000432bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000433 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000434 if (N == 0)
435 // Unhandled operand. Halt "fast" selection and bail.
436 return false;
437
Dan Gohmana6cb6412010-05-11 23:54:07 +0000438 bool NIsKill = hasTrivialKill(I->getOperand(0));
439
Chad Rosier478b06c2011-11-17 07:15:58 +0000440 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
441 // into a single N = N + TotalOffset.
442 uint64_t TotalOffs = 0;
443 // FIXME: What's a good SWAG number for MaxOffs?
444 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000445 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000447 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
448 E = I->op_end(); OI != E; ++OI) {
449 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000450 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000451 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
452 if (Field) {
453 // N = N + Offset
Chad Rosier478b06c2011-11-17 07:15:58 +0000454 TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
455 if (TotalOffs >= MaxOffs) {
456 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
457 if (N == 0)
458 // Unhandled operand. Halt "fast" selection and bail.
459 return false;
460 NIsKill = true;
461 TotalOffs = 0;
462 }
Evan Cheng83785c82008-08-20 22:45:34 +0000463 }
464 Ty = StTy->getElementType(Field);
465 } else {
466 Ty = cast<SequentialType>(Ty)->getElementType();
467
468 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000469 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000470 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000471 // N = N + Offset
472 TotalOffs +=
Duncan Sands777d2302009-05-09 07:06:46 +0000473 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000474 if (TotalOffs >= MaxOffs) {
475 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
476 if (N == 0)
477 // Unhandled operand. Halt "fast" selection and bail.
478 return false;
479 NIsKill = true;
480 TotalOffs = 0;
481 }
482 continue;
483 }
484 if (TotalOffs) {
485 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000486 if (N == 0)
487 // Unhandled operand. Halt "fast" selection and bail.
488 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000489 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000490 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000491 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000492
Evan Cheng83785c82008-08-20 22:45:34 +0000493 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000494 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000495 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
496 unsigned IdxN = Pair.first;
497 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000498 if (IdxN == 0)
499 // Unhandled operand. Halt "fast" selection and bail.
500 return false;
501
Dan Gohman80bc6e22008-08-26 20:57:08 +0000502 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000503 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000504 if (IdxN == 0)
505 // Unhandled operand. Halt "fast" selection and bail.
506 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000507 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000508 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000509 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000510 if (N == 0)
511 // Unhandled operand. Halt "fast" selection and bail.
512 return false;
513 }
514 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000515 if (TotalOffs) {
516 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
517 if (N == 0)
518 // Unhandled operand. Halt "fast" selection and bail.
519 return false;
520 }
Evan Cheng83785c82008-08-20 22:45:34 +0000521
522 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000523 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000524 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000525}
526
Dan Gohman46510a72010-04-15 01:51:59 +0000527bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000528 const CallInst *Call = cast<CallInst>(I);
529
530 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000531 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000532 // Don't attempt to handle constraints.
533 if (!IA->getConstraintString().empty())
534 return false;
535
536 unsigned ExtraInfo = 0;
537 if (IA->hasSideEffects())
538 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
539 if (IA->isAlignStack())
540 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
541
542 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
543 TII.get(TargetOpcode::INLINEASM))
544 .addExternalSymbol(IA->getAsmString().c_str())
545 .addImm(ExtraInfo);
546 return true;
547 }
548
549 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000550 if (!F) return false;
551
Dan Gohman4183e312010-04-13 17:07:06 +0000552 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000553 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000554 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000555 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000556 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000557 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000558 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000559 return true;
560
Dan Gohman46510a72010-04-15 01:51:59 +0000561 const Value *Address = DI->getAddress();
Devang Patel6fe75aa2010-09-14 20:29:31 +0000562 if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
Dale Johannesendc918562010-02-06 02:26:02 +0000563 return true;
Devang Patel6fe75aa2010-09-14 20:29:31 +0000564
565 unsigned Reg = 0;
566 unsigned Offset = 0;
567 if (const Argument *Arg = dyn_cast<Argument>(Address)) {
Devang Patel9aee3352011-09-08 22:59:09 +0000568 // Some arguments' frame index is recorded during argument lowering.
569 Offset = FuncInfo.getArgumentFrameIndex(Arg);
570 if (Offset)
571 Reg = TRI.getFrameRegister(*FuncInfo.MF);
Devang Patel4bafda92010-09-10 20:32:09 +0000572 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000573 if (!Reg)
574 Reg = getRegForValue(Address);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000575
Devang Patel6fe75aa2010-09-14 20:29:31 +0000576 if (Reg)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000577 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Devang Patel6fe75aa2010-09-14 20:29:31 +0000578 TII.get(TargetOpcode::DBG_VALUE))
579 .addReg(Reg, RegState::Debug).addImm(Offset)
580 .addMetadata(DI->getVariable());
Dan Gohman33134c42008-09-25 17:05:24 +0000581 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000582 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000583 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000584 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000585 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000586 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000587 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000588 if (!V) {
589 // Currently the optimizer can produce this; insert an undef to
590 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000591 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
592 .addReg(0U).addImm(DI->getOffset())
593 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000594 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000595 if (CI->getBitWidth() > 64)
596 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
597 .addCImm(CI).addImm(DI->getOffset())
598 .addMetadata(DI->getVariable());
599 else
600 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
601 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
602 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000603 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000604 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
605 .addFPImm(CF).addImm(DI->getOffset())
606 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000607 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
609 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
610 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000611 } else {
612 // We can't yet handle anything else here because it would require
613 // generating code, thus altering codegen because of debug info.
Devang Patelafeaae72010-12-06 22:39:26 +0000614 DEBUG(dbgs() << "Dropping debug info for " << DI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000615 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000616 return true;
617 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000618 case Intrinsic::eh_exception: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000619 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000620 if (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)!=TargetLowering::Expand)
621 break;
Owen Andersond74ea772011-04-22 23:38:06 +0000622
Chris Lattner832e4942011-04-19 05:52:03 +0000623 assert(FuncInfo.MBB->isLandingPad() &&
624 "Call to eh.exception not in landing pad!");
625 unsigned Reg = TLI.getExceptionAddressRegister();
626 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
627 unsigned ResultReg = createResultReg(RC);
628 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
629 ResultReg).addReg(Reg);
Dan Gohmana61e73b2011-04-26 17:18:34 +0000630 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000631 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000632 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000633 case Intrinsic::eh_selector: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000634 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000635 if (TLI.getOperationAction(ISD::EHSELECTION, VT) != TargetLowering::Expand)
636 break;
637 if (FuncInfo.MBB->isLandingPad())
Dan Gohmana61e73b2011-04-26 17:18:34 +0000638 AddCatchInfo(*Call, &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattner832e4942011-04-19 05:52:03 +0000639 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000640#ifndef NDEBUG
Dan Gohmana61e73b2011-04-26 17:18:34 +0000641 FuncInfo.CatchInfoLost.insert(Call);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000642#endif
Chris Lattner832e4942011-04-19 05:52:03 +0000643 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Chris Lattnered3a8062010-04-05 06:05:26 +0000644 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattner832e4942011-04-19 05:52:03 +0000645 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000646 }
Chris Lattner832e4942011-04-19 05:52:03 +0000647
648 unsigned Reg = TLI.getExceptionSelectorRegister();
649 EVT SrcVT = TLI.getPointerTy();
650 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
651 unsigned ResultReg = createResultReg(RC);
652 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
653 ResultReg).addReg(Reg);
654
Dan Gohmana61e73b2011-04-26 17:18:34 +0000655 bool ResultRegIsKill = hasTrivialKill(Call);
Chris Lattner832e4942011-04-19 05:52:03 +0000656
657 // Cast the register to the type of the selector.
658 if (SrcVT.bitsGT(MVT::i32))
659 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
660 ResultReg, ResultRegIsKill);
661 else if (SrcVT.bitsLT(MVT::i32))
662 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
663 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
664 if (ResultReg == 0)
665 // Unhandled operand. Halt "fast" selection and bail.
666 return false;
667
Dan Gohmana61e73b2011-04-26 17:18:34 +0000668 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000669
670 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000671 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000672 case Intrinsic::objectsize: {
673 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
674 unsigned long long Res = CI->isZero() ? -1ULL : 0;
675 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
676 unsigned ResultReg = getRegForValue(ResCI);
677 if (ResultReg == 0)
678 return false;
679 UpdateValueMap(Call, ResultReg);
680 return true;
681 }
Dan Gohman33134c42008-09-25 17:05:24 +0000682 }
Dan Gohman4183e312010-04-13 17:07:06 +0000683
Ivan Krasin74af88a2011-08-18 22:06:10 +0000684 // Usually, it does not make sense to initialize a value,
685 // make an unrelated function call and use the value, because
686 // it tends to be spilled on the stack. So, we move the pointer
687 // to the last local value to the beginning of the block, so that
688 // all the values which have already been materialized,
689 // appear after the call. It also makes sense to skip intrinsics
690 // since they tend to be inlined.
691 if (!isa<IntrinsicInst>(F))
692 flushLocalValueMap();
693
Dan Gohman4183e312010-04-13 17:07:06 +0000694 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000695 return false;
696}
697
Dan Gohman46510a72010-04-15 01:51:59 +0000698bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000699 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
700 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
703 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000704 // Unhandled type. Halt "fast" selection and bail.
705 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000706
Eli Friedman76927d732011-05-25 23:49:02 +0000707 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000708 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000709 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000710
Eli Friedman76927d732011-05-25 23:49:02 +0000711 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000712 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000713 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000714
Dan Gohman3df24e62008-09-03 23:12:08 +0000715 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000716 if (!InputReg)
717 // Unhandled operand. Halt "fast" selection and bail.
718 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000719
Dan Gohmana6cb6412010-05-11 23:54:07 +0000720 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
721
Owen Andersond0533c92008-08-26 23:46:32 +0000722 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
723 DstVT.getSimpleVT(),
724 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000725 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000726 if (!ResultReg)
727 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728
Dan Gohman3df24e62008-09-03 23:12:08 +0000729 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000730 return true;
731}
732
Dan Gohman46510a72010-04-15 01:51:59 +0000733bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000734 // If the bitcast doesn't change the type, just use the operand value.
735 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000736 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000737 if (Reg == 0)
738 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000739 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000740 return true;
741 }
742
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000743 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000744 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
745 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000746
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
748 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000749 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
750 // Unhandled type. Halt "fast" selection and bail.
751 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752
Dan Gohman3df24e62008-09-03 23:12:08 +0000753 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000754 if (Op0 == 0)
755 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000756 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000757
758 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000759
Dan Gohmanad368ac2008-08-27 18:10:19 +0000760 // First, try to perform the bitcast by inserting a reg-reg copy.
761 unsigned ResultReg = 0;
762 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
763 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
764 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000765 // Don't attempt a cross-class copy. It will likely fail.
766 if (SrcClass == DstClass) {
767 ResultReg = createResultReg(DstClass);
768 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
769 ResultReg).addReg(Op0);
770 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000771 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000772
773 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000774 if (!ResultReg)
775 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000776 ISD::BITCAST, Op0, Op0IsKill);
777
Dan Gohmanad368ac2008-08-27 18:10:19 +0000778 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000779 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000780
Dan Gohman3df24e62008-09-03 23:12:08 +0000781 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000782 return true;
783}
784
Dan Gohman3df24e62008-09-03 23:12:08 +0000785bool
Dan Gohman46510a72010-04-15 01:51:59 +0000786FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000787 // Just before the terminator instruction, insert instructions to
788 // feed PHI nodes in successor blocks.
789 if (isa<TerminatorInst>(I))
790 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
791 return false;
792
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000793 DL = I->getDebugLoc();
794
Dan Gohman6e3ff372009-12-05 01:27:58 +0000795 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000796 if (SelectOperator(I, I->getOpcode())) {
Chad Rosier053e69a2011-11-16 21:05:28 +0000797 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000798 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000799 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000800 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000801
802 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000803 if (TargetSelectInstruction(I)) {
Chad Rosier053e69a2011-11-16 21:05:28 +0000804 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000805 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000806 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000807 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000808
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000809 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000810 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000811}
812
Dan Gohmand98d6202008-10-02 22:15:21 +0000813/// FastEmitBranch - Emit an unconditional branch to the given block,
814/// unless it is the immediate (fall-through) successor, and update
815/// the CFG.
816void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000817FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohman84023e02010-07-10 09:00:22 +0000818 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000819 // The unconditional fall-through case, which needs no instructions.
820 } else {
821 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000822 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
823 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000824 }
Dan Gohman84023e02010-07-10 09:00:22 +0000825 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000826}
827
Dan Gohman3d45a852009-09-03 22:53:57 +0000828/// SelectFNeg - Emit an FNeg operation.
829///
830bool
Dan Gohman46510a72010-04-15 01:51:59 +0000831FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000832 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
833 if (OpReg == 0) return false;
834
Dan Gohmana6cb6412010-05-11 23:54:07 +0000835 bool OpRegIsKill = hasTrivialKill(I);
836
Dan Gohman4a215a12009-09-11 00:36:43 +0000837 // If the target has ISD::FNEG, use it.
838 EVT VT = TLI.getValueType(I->getType());
839 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000840 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000841 if (ResultReg != 0) {
842 UpdateValueMap(I, ResultReg);
843 return true;
844 }
845
Dan Gohman5e5abb72009-09-11 00:34:46 +0000846 // Bitcast the value to integer, twiddle the sign bit with xor,
847 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000848 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000849 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
850 if (!TLI.isTypeLegal(IntVT))
851 return false;
852
853 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000855 if (IntReg == 0)
856 return false;
857
Dan Gohmana6cb6412010-05-11 23:54:07 +0000858 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
859 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000860 UINT64_C(1) << (VT.getSizeInBits()-1),
861 IntVT.getSimpleVT());
862 if (IntResultReg == 0)
863 return false;
864
865 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000866 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000867 if (ResultReg == 0)
868 return false;
869
870 UpdateValueMap(I, ResultReg);
871 return true;
872}
873
Dan Gohman40b189e2008-09-05 18:18:20 +0000874bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000875FastISel::SelectExtractValue(const User *U) {
876 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000877 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000878 return false;
879
Eli Friedman482feb32011-05-16 21:06:17 +0000880 // Make sure we only try to handle extracts with a legal result. But also
881 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000882 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
883 if (!RealVT.isSimple())
884 return false;
885 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000886 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000887 return false;
888
889 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000890 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000891
892 // Get the base result register.
893 unsigned ResultReg;
894 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
895 if (I != FuncInfo.ValueMap.end())
896 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000897 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000898 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000899 else
900 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000901
902 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000903 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000904
905 SmallVector<EVT, 4> AggValueVTs;
906 ComputeValueVTs(TLI, AggTy, AggValueVTs);
907
908 for (unsigned i = 0; i < VTIndex; i++)
909 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
910
911 UpdateValueMap(EVI, ResultReg);
912 return true;
913}
914
915bool
Dan Gohman46510a72010-04-15 01:51:59 +0000916FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000917 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000918 case Instruction::Add:
919 return SelectBinaryOp(I, ISD::ADD);
920 case Instruction::FAdd:
921 return SelectBinaryOp(I, ISD::FADD);
922 case Instruction::Sub:
923 return SelectBinaryOp(I, ISD::SUB);
924 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000925 // FNeg is currently represented in LLVM IR as a special case of FSub.
926 if (BinaryOperator::isFNeg(I))
927 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000928 return SelectBinaryOp(I, ISD::FSUB);
929 case Instruction::Mul:
930 return SelectBinaryOp(I, ISD::MUL);
931 case Instruction::FMul:
932 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000933 case Instruction::SDiv:
934 return SelectBinaryOp(I, ISD::SDIV);
935 case Instruction::UDiv:
936 return SelectBinaryOp(I, ISD::UDIV);
937 case Instruction::FDiv:
938 return SelectBinaryOp(I, ISD::FDIV);
939 case Instruction::SRem:
940 return SelectBinaryOp(I, ISD::SREM);
941 case Instruction::URem:
942 return SelectBinaryOp(I, ISD::UREM);
943 case Instruction::FRem:
944 return SelectBinaryOp(I, ISD::FREM);
945 case Instruction::Shl:
946 return SelectBinaryOp(I, ISD::SHL);
947 case Instruction::LShr:
948 return SelectBinaryOp(I, ISD::SRL);
949 case Instruction::AShr:
950 return SelectBinaryOp(I, ISD::SRA);
951 case Instruction::And:
952 return SelectBinaryOp(I, ISD::AND);
953 case Instruction::Or:
954 return SelectBinaryOp(I, ISD::OR);
955 case Instruction::Xor:
956 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000957
Dan Gohman3df24e62008-09-03 23:12:08 +0000958 case Instruction::GetElementPtr:
959 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000960
Dan Gohman3df24e62008-09-03 23:12:08 +0000961 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000962 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000963
Dan Gohman3df24e62008-09-03 23:12:08 +0000964 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000965 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000966 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000967 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000968 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000969 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000970
971 // Conditional branches are not handed yet.
972 // Halt "fast" selection and bail.
973 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000974 }
975
Dan Gohman087c8502008-09-05 01:08:41 +0000976 case Instruction::Unreachable:
977 // Nothing to emit.
978 return true;
979
Dan Gohman0586d912008-09-10 20:11:02 +0000980 case Instruction::Alloca:
981 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000982 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000983 return true;
984
985 // Dynamic-sized alloca is not handled yet.
986 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000987
Dan Gohman33134c42008-09-25 17:05:24 +0000988 case Instruction::Call:
989 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000990
Dan Gohman3df24e62008-09-03 23:12:08 +0000991 case Instruction::BitCast:
992 return SelectBitCast(I);
993
994 case Instruction::FPToSI:
995 return SelectCast(I, ISD::FP_TO_SINT);
996 case Instruction::ZExt:
997 return SelectCast(I, ISD::ZERO_EXTEND);
998 case Instruction::SExt:
999 return SelectCast(I, ISD::SIGN_EXTEND);
1000 case Instruction::Trunc:
1001 return SelectCast(I, ISD::TRUNCATE);
1002 case Instruction::SIToFP:
1003 return SelectCast(I, ISD::SINT_TO_FP);
1004
1005 case Instruction::IntToPtr: // Deliberate fall-through.
1006 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001007 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1008 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001009 if (DstVT.bitsGT(SrcVT))
1010 return SelectCast(I, ISD::ZERO_EXTEND);
1011 if (DstVT.bitsLT(SrcVT))
1012 return SelectCast(I, ISD::TRUNCATE);
1013 unsigned Reg = getRegForValue(I->getOperand(0));
1014 if (Reg == 0) return false;
1015 UpdateValueMap(I, Reg);
1016 return true;
1017 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001018
Eli Friedman2586b8f2011-05-16 20:27:46 +00001019 case Instruction::ExtractValue:
1020 return SelectExtractValue(I);
1021
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001022 case Instruction::PHI:
1023 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1024
Dan Gohman3df24e62008-09-03 23:12:08 +00001025 default:
1026 // Unhandled instruction. Halt "fast" selection and bail.
1027 return false;
1028 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001029}
1030
Dan Gohmana4160c32010-07-07 16:29:44 +00001031FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001032 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001033 MRI(FuncInfo.MF->getRegInfo()),
1034 MFI(*FuncInfo.MF->getFrameInfo()),
1035 MCP(*FuncInfo.MF->getConstantPool()),
1036 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001037 TD(*TM.getTargetData()),
1038 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001039 TLI(*TM.getTargetLowering()),
Dan Gohman84023e02010-07-10 09:00:22 +00001040 TRI(*TM.getRegisterInfo()) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001041}
1042
Dan Gohmane285a742008-08-14 21:51:29 +00001043FastISel::~FastISel() {}
1044
Owen Anderson825b72b2009-08-11 20:47:22 +00001045unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001046 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001047 return 0;
1048}
1049
Owen Anderson825b72b2009-08-11 20:47:22 +00001050unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001051 unsigned,
1052 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001053 return 0;
1054}
1055
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001056unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001057 unsigned,
1058 unsigned /*Op0*/, bool /*Op0IsKill*/,
1059 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001060 return 0;
1061}
1062
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001063unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001064 return 0;
1065}
1066
Owen Anderson825b72b2009-08-11 20:47:22 +00001067unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001068 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001069 return 0;
1070}
1071
Owen Anderson825b72b2009-08-11 20:47:22 +00001072unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001073 unsigned,
1074 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001075 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001076 return 0;
1077}
1078
Owen Anderson825b72b2009-08-11 20:47:22 +00001079unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001080 unsigned,
1081 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001082 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001083 return 0;
1084}
1085
Owen Anderson825b72b2009-08-11 20:47:22 +00001086unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001087 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001088 unsigned /*Op0*/, bool /*Op0IsKill*/,
1089 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001090 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001091 return 0;
1092}
1093
1094/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1095/// to emit an instruction with an immediate operand using FastEmit_ri.
1096/// If that fails, it materializes the immediate into a register and try
1097/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001098unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001099 unsigned Op0, bool Op0IsKill,
1100 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001101 // If this is a multiply by a power of two, emit this as a shift left.
1102 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1103 Opcode = ISD::SHL;
1104 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001105 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1106 // div x, 8 -> srl x, 3
1107 Opcode = ISD::SRL;
1108 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001109 }
Owen Andersond74ea772011-04-22 23:38:06 +00001110
Chris Lattner602fc062011-04-17 20:23:29 +00001111 // Horrible hack (to be removed), check to make sure shift amounts are
1112 // in-range.
1113 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1114 Imm >= VT.getSizeInBits())
1115 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001116
Evan Cheng83785c82008-08-20 22:45:34 +00001117 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001118 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001119 if (ResultReg != 0)
1120 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001121 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001122 if (MaterialReg == 0) {
1123 // This is a bit ugly/slow, but failing here means falling out of
1124 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001125 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001126 VT.getSizeInBits());
1127 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
1128 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001129 return FastEmit_rr(VT, VT, Opcode,
1130 Op0, Op0IsKill,
1131 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001132}
1133
1134unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1135 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001136}
1137
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001138unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001139 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001140 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001141 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001142
Dan Gohman84023e02010-07-10 09:00:22 +00001143 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001144 return ResultReg;
1145}
1146
1147unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1148 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001149 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001150 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001151 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001152
Evan Cheng5960e4e2008-09-08 08:38:20 +00001153 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1155 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001156 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001157 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1158 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001159 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1160 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001161 }
1162
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001163 return ResultReg;
1164}
1165
1166unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1167 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001168 unsigned Op0, bool Op0IsKill,
1169 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001170 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001171 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001172
Evan Cheng5960e4e2008-09-08 08:38:20 +00001173 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001175 .addReg(Op0, Op0IsKill * RegState::Kill)
1176 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001177 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001178 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001179 .addReg(Op0, Op0IsKill * RegState::Kill)
1180 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1182 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001183 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001184 return ResultReg;
1185}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001186
Owen Andersond71867a2011-05-05 17:59:04 +00001187unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1188 const TargetRegisterClass *RC,
1189 unsigned Op0, bool Op0IsKill,
1190 unsigned Op1, bool Op1IsKill,
1191 unsigned Op2, bool Op2IsKill) {
1192 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001193 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001194
1195 if (II.getNumDefs() >= 1)
1196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1197 .addReg(Op0, Op0IsKill * RegState::Kill)
1198 .addReg(Op1, Op1IsKill * RegState::Kill)
1199 .addReg(Op2, Op2IsKill * RegState::Kill);
1200 else {
1201 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1202 .addReg(Op0, Op0IsKill * RegState::Kill)
1203 .addReg(Op1, Op1IsKill * RegState::Kill)
1204 .addReg(Op2, Op2IsKill * RegState::Kill);
1205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1206 ResultReg).addReg(II.ImplicitDefs[0]);
1207 }
1208 return ResultReg;
1209}
1210
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001211unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1212 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001213 unsigned Op0, bool Op0IsKill,
1214 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001215 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001216 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001217
Evan Cheng5960e4e2008-09-08 08:38:20 +00001218 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001220 .addReg(Op0, Op0IsKill * RegState::Kill)
1221 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001222 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001224 .addReg(Op0, Op0IsKill * RegState::Kill)
1225 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001226 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1227 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001228 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001229 return ResultReg;
1230}
1231
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001232unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1233 const TargetRegisterClass *RC,
1234 unsigned Op0, bool Op0IsKill,
1235 uint64_t Imm1, uint64_t Imm2) {
1236 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001237 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001238
1239 if (II.getNumDefs() >= 1)
1240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1241 .addReg(Op0, Op0IsKill * RegState::Kill)
1242 .addImm(Imm1)
1243 .addImm(Imm2);
1244 else {
1245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1246 .addReg(Op0, Op0IsKill * RegState::Kill)
1247 .addImm(Imm1)
1248 .addImm(Imm2);
1249 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1250 ResultReg).addReg(II.ImplicitDefs[0]);
1251 }
1252 return ResultReg;
1253}
1254
Dan Gohman10df0fa2008-08-27 01:09:54 +00001255unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1256 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001257 unsigned Op0, bool Op0IsKill,
1258 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001259 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001260 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001261
Evan Cheng5960e4e2008-09-08 08:38:20 +00001262 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001264 .addReg(Op0, Op0IsKill * RegState::Kill)
1265 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001266 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001268 .addReg(Op0, Op0IsKill * RegState::Kill)
1269 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001270 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1271 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001272 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001273 return ResultReg;
1274}
1275
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001276unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1277 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001278 unsigned Op0, bool Op0IsKill,
1279 unsigned Op1, bool Op1IsKill,
1280 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001281 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001282 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001283
Evan Cheng5960e4e2008-09-08 08:38:20 +00001284 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001286 .addReg(Op0, Op0IsKill * RegState::Kill)
1287 .addReg(Op1, Op1IsKill * RegState::Kill)
1288 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001289 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001290 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001291 .addReg(Op0, Op0IsKill * RegState::Kill)
1292 .addReg(Op1, Op1IsKill * RegState::Kill)
1293 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001294 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1295 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001296 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001297 return ResultReg;
1298}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001299
1300unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1301 const TargetRegisterClass *RC,
1302 uint64_t Imm) {
1303 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001304 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001305
Evan Cheng5960e4e2008-09-08 08:38:20 +00001306 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001307 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001308 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001309 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001310 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1311 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001312 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001313 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001314}
Owen Anderson8970f002008-08-27 22:30:02 +00001315
Owen Andersond74ea772011-04-22 23:38:06 +00001316unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1317 const TargetRegisterClass *RC,
1318 uint64_t Imm1, uint64_t Imm2) {
1319 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001320 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001321
1322 if (II.getNumDefs() >= 1)
1323 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1324 .addImm(Imm1).addImm(Imm2);
1325 else {
1326 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1328 ResultReg).addReg(II.ImplicitDefs[0]);
1329 }
1330 return ResultReg;
1331}
1332
Owen Anderson825b72b2009-08-11 20:47:22 +00001333unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001334 unsigned Op0, bool Op0IsKill,
1335 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001336 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001337 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1338 "Cannot yet extract from physregs");
Dan Gohman84023e02010-07-10 09:00:22 +00001339 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1340 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001341 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001342 return ResultReg;
1343}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001344
1345/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1346/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001347unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1348 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001349}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001350
1351/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1352/// Emit code to ensure constants are copied into registers when needed.
1353/// Remember the virtual registers that need to be added to the Machine PHI
1354/// nodes as input. We cannot just directly add them, because expansion
1355/// might result in multiple MBB's for one BB. As such, the start of the
1356/// BB might correspond to a different MBB than the end.
1357bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1358 const TerminatorInst *TI = LLVMBB->getTerminator();
1359
1360 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001361 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001362
1363 // Check successor nodes' PHI nodes that expect a constant to be available
1364 // from this block.
1365 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1366 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1367 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001368 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001369
1370 // If this terminator has multiple identical successors (common for
1371 // switches), only handle each succ once.
1372 if (!SuccsHandled.insert(SuccMBB)) continue;
1373
1374 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1375
1376 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1377 // nodes and Machine PHI nodes, but the incoming operands have not been
1378 // emitted yet.
1379 for (BasicBlock::const_iterator I = SuccBB->begin();
1380 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001381
Dan Gohmanf81eca02010-04-22 20:46:50 +00001382 // Ignore dead phi's.
1383 if (PN->use_empty()) continue;
1384
1385 // Only handle legal types. Two interesting things to note here. First,
1386 // by bailing out early, we may leave behind some dead instructions,
1387 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001388 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001389 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001390 // exactly one register for each non-void instruction.
1391 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1392 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1393 // Promote MVT::i1.
1394 if (VT == MVT::i1)
1395 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1396 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001397 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001398 return false;
1399 }
1400 }
1401
1402 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1403
Dan Gohmanfb95f892010-05-07 01:10:20 +00001404 // Set the DebugLoc for the copy. Prefer the location of the operand
1405 // if there is one; use the location of the PHI otherwise.
1406 DL = PN->getDebugLoc();
1407 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1408 DL = Inst->getDebugLoc();
1409
Dan Gohmanf81eca02010-04-22 20:46:50 +00001410 unsigned Reg = getRegForValue(PHIOp);
1411 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001412 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001413 return false;
1414 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001415 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001416 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001417 }
1418 }
1419
1420 return true;
1421}