blob: f0fe179e050829d8cdc085c0c22d12e8f16d23ad [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
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000440 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000442 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
443 E = I->op_end(); OI != E; ++OI) {
444 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000445 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000446 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
447 if (Field) {
448 // N = N + Offset
449 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
450 // FIXME: This can be optimized by combining the add with a
451 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000452 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000453 if (N == 0)
454 // Unhandled operand. Halt "fast" selection and bail.
455 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000456 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000457 }
458 Ty = StTy->getElementType(Field);
459 } else {
460 Ty = cast<SequentialType>(Ty)->getElementType();
461
462 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000463 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000464 if (CI->isZero()) continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000465 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000466 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000467 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000468 if (N == 0)
469 // Unhandled operand. Halt "fast" selection and bail.
470 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000471 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000472 continue;
473 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000474
Evan Cheng83785c82008-08-20 22:45:34 +0000475 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000476 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000477 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
478 unsigned IdxN = Pair.first;
479 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000480 if (IdxN == 0)
481 // Unhandled operand. Halt "fast" selection and bail.
482 return false;
483
Dan Gohman80bc6e22008-08-26 20:57:08 +0000484 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000485 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000486 if (IdxN == 0)
487 // Unhandled operand. Halt "fast" selection and bail.
488 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000489 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000490 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000491 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000492 if (N == 0)
493 // Unhandled operand. Halt "fast" selection and bail.
494 return false;
495 }
496 }
497
498 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000499 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000500 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000501}
502
Dan Gohman46510a72010-04-15 01:51:59 +0000503bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000504 const CallInst *Call = cast<CallInst>(I);
505
506 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000507 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000508 // Don't attempt to handle constraints.
509 if (!IA->getConstraintString().empty())
510 return false;
511
512 unsigned ExtraInfo = 0;
513 if (IA->hasSideEffects())
514 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
515 if (IA->isAlignStack())
516 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
517
518 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
519 TII.get(TargetOpcode::INLINEASM))
520 .addExternalSymbol(IA->getAsmString().c_str())
521 .addImm(ExtraInfo);
522 return true;
523 }
524
525 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000526 if (!F) return false;
527
Dan Gohman4183e312010-04-13 17:07:06 +0000528 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000529 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000530 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000531 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000532 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000533 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000534 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000535 return true;
536
Dan Gohman46510a72010-04-15 01:51:59 +0000537 const Value *Address = DI->getAddress();
Devang Patel6fe75aa2010-09-14 20:29:31 +0000538 if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
Dale Johannesendc918562010-02-06 02:26:02 +0000539 return true;
Devang Patel6fe75aa2010-09-14 20:29:31 +0000540
541 unsigned Reg = 0;
542 unsigned Offset = 0;
543 if (const Argument *Arg = dyn_cast<Argument>(Address)) {
Devang Patel9aee3352011-09-08 22:59:09 +0000544 // Some arguments' frame index is recorded during argument lowering.
545 Offset = FuncInfo.getArgumentFrameIndex(Arg);
546 if (Offset)
547 Reg = TRI.getFrameRegister(*FuncInfo.MF);
Devang Patel4bafda92010-09-10 20:32:09 +0000548 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000549 if (!Reg)
550 Reg = getRegForValue(Address);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000551
Devang Patel6fe75aa2010-09-14 20:29:31 +0000552 if (Reg)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000553 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Devang Patel6fe75aa2010-09-14 20:29:31 +0000554 TII.get(TargetOpcode::DBG_VALUE))
555 .addReg(Reg, RegState::Debug).addImm(Offset)
556 .addMetadata(DI->getVariable());
Dan Gohman33134c42008-09-25 17:05:24 +0000557 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000558 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000559 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000560 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000561 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000562 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000563 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000564 if (!V) {
565 // Currently the optimizer can produce this; insert an undef to
566 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000567 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
568 .addReg(0U).addImm(DI->getOffset())
569 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000570 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000571 if (CI->getBitWidth() > 64)
572 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
573 .addCImm(CI).addImm(DI->getOffset())
574 .addMetadata(DI->getVariable());
575 else
576 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
577 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
578 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000579 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000580 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
581 .addFPImm(CF).addImm(DI->getOffset())
582 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000583 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000584 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
585 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
586 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000587 } else {
588 // We can't yet handle anything else here because it would require
589 // generating code, thus altering codegen because of debug info.
Devang Patelafeaae72010-12-06 22:39:26 +0000590 DEBUG(dbgs() << "Dropping debug info for " << DI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000591 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000592 return true;
593 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000594 case Intrinsic::eh_exception: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000595 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000596 if (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)!=TargetLowering::Expand)
597 break;
Owen Andersond74ea772011-04-22 23:38:06 +0000598
Chris Lattner832e4942011-04-19 05:52:03 +0000599 assert(FuncInfo.MBB->isLandingPad() &&
600 "Call to eh.exception not in landing pad!");
601 unsigned Reg = TLI.getExceptionAddressRegister();
602 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
603 unsigned ResultReg = createResultReg(RC);
604 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
605 ResultReg).addReg(Reg);
Dan Gohmana61e73b2011-04-26 17:18:34 +0000606 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000607 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000608 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000609 case Intrinsic::eh_selector: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000610 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000611 if (TLI.getOperationAction(ISD::EHSELECTION, VT) != TargetLowering::Expand)
612 break;
613 if (FuncInfo.MBB->isLandingPad())
Dan Gohmana61e73b2011-04-26 17:18:34 +0000614 AddCatchInfo(*Call, &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattner832e4942011-04-19 05:52:03 +0000615 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000616#ifndef NDEBUG
Dan Gohmana61e73b2011-04-26 17:18:34 +0000617 FuncInfo.CatchInfoLost.insert(Call);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000618#endif
Chris Lattner832e4942011-04-19 05:52:03 +0000619 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Chris Lattnered3a8062010-04-05 06:05:26 +0000620 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattner832e4942011-04-19 05:52:03 +0000621 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000622 }
Chris Lattner832e4942011-04-19 05:52:03 +0000623
624 unsigned Reg = TLI.getExceptionSelectorRegister();
625 EVT SrcVT = TLI.getPointerTy();
626 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
627 unsigned ResultReg = createResultReg(RC);
628 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
629 ResultReg).addReg(Reg);
630
Dan Gohmana61e73b2011-04-26 17:18:34 +0000631 bool ResultRegIsKill = hasTrivialKill(Call);
Chris Lattner832e4942011-04-19 05:52:03 +0000632
633 // Cast the register to the type of the selector.
634 if (SrcVT.bitsGT(MVT::i32))
635 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
636 ResultReg, ResultRegIsKill);
637 else if (SrcVT.bitsLT(MVT::i32))
638 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
639 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
640 if (ResultReg == 0)
641 // Unhandled operand. Halt "fast" selection and bail.
642 return false;
643
Dan Gohmana61e73b2011-04-26 17:18:34 +0000644 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000645
646 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000647 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000648 case Intrinsic::objectsize: {
649 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
650 unsigned long long Res = CI->isZero() ? -1ULL : 0;
651 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
652 unsigned ResultReg = getRegForValue(ResCI);
653 if (ResultReg == 0)
654 return false;
655 UpdateValueMap(Call, ResultReg);
656 return true;
657 }
Dan Gohman33134c42008-09-25 17:05:24 +0000658 }
Dan Gohman4183e312010-04-13 17:07:06 +0000659
Ivan Krasin74af88a2011-08-18 22:06:10 +0000660 // Usually, it does not make sense to initialize a value,
661 // make an unrelated function call and use the value, because
662 // it tends to be spilled on the stack. So, we move the pointer
663 // to the last local value to the beginning of the block, so that
664 // all the values which have already been materialized,
665 // appear after the call. It also makes sense to skip intrinsics
666 // since they tend to be inlined.
667 if (!isa<IntrinsicInst>(F))
668 flushLocalValueMap();
669
Dan Gohman4183e312010-04-13 17:07:06 +0000670 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000671 return false;
672}
673
Dan Gohman46510a72010-04-15 01:51:59 +0000674bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000675 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
676 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
679 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000680 // Unhandled type. Halt "fast" selection and bail.
681 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682
Eli Friedman76927d732011-05-25 23:49:02 +0000683 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000684 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000685 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000686
Eli Friedman76927d732011-05-25 23:49:02 +0000687 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000688 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000689 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000690
Dan Gohman3df24e62008-09-03 23:12:08 +0000691 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000692 if (!InputReg)
693 // Unhandled operand. Halt "fast" selection and bail.
694 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000695
Dan Gohmana6cb6412010-05-11 23:54:07 +0000696 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
697
Owen Andersond0533c92008-08-26 23:46:32 +0000698 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
699 DstVT.getSimpleVT(),
700 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000701 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000702 if (!ResultReg)
703 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000704
Dan Gohman3df24e62008-09-03 23:12:08 +0000705 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000706 return true;
707}
708
Dan Gohman46510a72010-04-15 01:51:59 +0000709bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000710 // If the bitcast doesn't change the type, just use the operand value.
711 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000712 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000713 if (Reg == 0)
714 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000715 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000716 return true;
717 }
718
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000719 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000720 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
721 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000722
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
724 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000725 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
726 // Unhandled type. Halt "fast" selection and bail.
727 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728
Dan Gohman3df24e62008-09-03 23:12:08 +0000729 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000730 if (Op0 == 0)
731 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000732 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000733
734 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735
Dan Gohmanad368ac2008-08-27 18:10:19 +0000736 // First, try to perform the bitcast by inserting a reg-reg copy.
737 unsigned ResultReg = 0;
738 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
739 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
740 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000741 // Don't attempt a cross-class copy. It will likely fail.
742 if (SrcClass == DstClass) {
743 ResultReg = createResultReg(DstClass);
744 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
745 ResultReg).addReg(Op0);
746 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000747 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000748
749 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000750 if (!ResultReg)
751 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752 ISD::BITCAST, Op0, Op0IsKill);
753
Dan Gohmanad368ac2008-08-27 18:10:19 +0000754 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000755 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000756
Dan Gohman3df24e62008-09-03 23:12:08 +0000757 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000758 return true;
759}
760
Dan Gohman3df24e62008-09-03 23:12:08 +0000761bool
Dan Gohman46510a72010-04-15 01:51:59 +0000762FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000763 // Just before the terminator instruction, insert instructions to
764 // feed PHI nodes in successor blocks.
765 if (isa<TerminatorInst>(I))
766 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
767 return false;
768
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000769 DL = I->getDebugLoc();
770
Dan Gohman6e3ff372009-12-05 01:27:58 +0000771 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000772 if (SelectOperator(I, I->getOpcode())) {
Chad Rosier053e69a2011-11-16 21:05:28 +0000773 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000774 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000775 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000776 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000777
778 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000779 if (TargetSelectInstruction(I)) {
Chad Rosier053e69a2011-11-16 21:05:28 +0000780 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000781 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000782 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000783 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000784
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000785 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000786 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000787}
788
Dan Gohmand98d6202008-10-02 22:15:21 +0000789/// FastEmitBranch - Emit an unconditional branch to the given block,
790/// unless it is the immediate (fall-through) successor, and update
791/// the CFG.
792void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000793FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohman84023e02010-07-10 09:00:22 +0000794 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000795 // The unconditional fall-through case, which needs no instructions.
796 } else {
797 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000798 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
799 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000800 }
Dan Gohman84023e02010-07-10 09:00:22 +0000801 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000802}
803
Dan Gohman3d45a852009-09-03 22:53:57 +0000804/// SelectFNeg - Emit an FNeg operation.
805///
806bool
Dan Gohman46510a72010-04-15 01:51:59 +0000807FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000808 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
809 if (OpReg == 0) return false;
810
Dan Gohmana6cb6412010-05-11 23:54:07 +0000811 bool OpRegIsKill = hasTrivialKill(I);
812
Dan Gohman4a215a12009-09-11 00:36:43 +0000813 // If the target has ISD::FNEG, use it.
814 EVT VT = TLI.getValueType(I->getType());
815 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000816 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000817 if (ResultReg != 0) {
818 UpdateValueMap(I, ResultReg);
819 return true;
820 }
821
Dan Gohman5e5abb72009-09-11 00:34:46 +0000822 // Bitcast the value to integer, twiddle the sign bit with xor,
823 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000824 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000825 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
826 if (!TLI.isTypeLegal(IntVT))
827 return false;
828
829 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000830 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000831 if (IntReg == 0)
832 return false;
833
Dan Gohmana6cb6412010-05-11 23:54:07 +0000834 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
835 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000836 UINT64_C(1) << (VT.getSizeInBits()-1),
837 IntVT.getSimpleVT());
838 if (IntResultReg == 0)
839 return false;
840
841 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000842 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000843 if (ResultReg == 0)
844 return false;
845
846 UpdateValueMap(I, ResultReg);
847 return true;
848}
849
Dan Gohman40b189e2008-09-05 18:18:20 +0000850bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000851FastISel::SelectExtractValue(const User *U) {
852 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000853 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000854 return false;
855
Eli Friedman482feb32011-05-16 21:06:17 +0000856 // Make sure we only try to handle extracts with a legal result. But also
857 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000858 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
859 if (!RealVT.isSimple())
860 return false;
861 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000862 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000863 return false;
864
865 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000866 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000867
868 // Get the base result register.
869 unsigned ResultReg;
870 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
871 if (I != FuncInfo.ValueMap.end())
872 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000873 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000874 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000875 else
876 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000877
878 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000879 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000880
881 SmallVector<EVT, 4> AggValueVTs;
882 ComputeValueVTs(TLI, AggTy, AggValueVTs);
883
884 for (unsigned i = 0; i < VTIndex; i++)
885 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
886
887 UpdateValueMap(EVI, ResultReg);
888 return true;
889}
890
891bool
Dan Gohman46510a72010-04-15 01:51:59 +0000892FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000893 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000894 case Instruction::Add:
895 return SelectBinaryOp(I, ISD::ADD);
896 case Instruction::FAdd:
897 return SelectBinaryOp(I, ISD::FADD);
898 case Instruction::Sub:
899 return SelectBinaryOp(I, ISD::SUB);
900 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000901 // FNeg is currently represented in LLVM IR as a special case of FSub.
902 if (BinaryOperator::isFNeg(I))
903 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000904 return SelectBinaryOp(I, ISD::FSUB);
905 case Instruction::Mul:
906 return SelectBinaryOp(I, ISD::MUL);
907 case Instruction::FMul:
908 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000909 case Instruction::SDiv:
910 return SelectBinaryOp(I, ISD::SDIV);
911 case Instruction::UDiv:
912 return SelectBinaryOp(I, ISD::UDIV);
913 case Instruction::FDiv:
914 return SelectBinaryOp(I, ISD::FDIV);
915 case Instruction::SRem:
916 return SelectBinaryOp(I, ISD::SREM);
917 case Instruction::URem:
918 return SelectBinaryOp(I, ISD::UREM);
919 case Instruction::FRem:
920 return SelectBinaryOp(I, ISD::FREM);
921 case Instruction::Shl:
922 return SelectBinaryOp(I, ISD::SHL);
923 case Instruction::LShr:
924 return SelectBinaryOp(I, ISD::SRL);
925 case Instruction::AShr:
926 return SelectBinaryOp(I, ISD::SRA);
927 case Instruction::And:
928 return SelectBinaryOp(I, ISD::AND);
929 case Instruction::Or:
930 return SelectBinaryOp(I, ISD::OR);
931 case Instruction::Xor:
932 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000933
Dan Gohman3df24e62008-09-03 23:12:08 +0000934 case Instruction::GetElementPtr:
935 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000936
Dan Gohman3df24e62008-09-03 23:12:08 +0000937 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000938 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000939
Dan Gohman3df24e62008-09-03 23:12:08 +0000940 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000941 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000942 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000943 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000944 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000945 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000946
947 // Conditional branches are not handed yet.
948 // Halt "fast" selection and bail.
949 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000950 }
951
Dan Gohman087c8502008-09-05 01:08:41 +0000952 case Instruction::Unreachable:
953 // Nothing to emit.
954 return true;
955
Dan Gohman0586d912008-09-10 20:11:02 +0000956 case Instruction::Alloca:
957 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000958 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000959 return true;
960
961 // Dynamic-sized alloca is not handled yet.
962 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000963
Dan Gohman33134c42008-09-25 17:05:24 +0000964 case Instruction::Call:
965 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000966
Dan Gohman3df24e62008-09-03 23:12:08 +0000967 case Instruction::BitCast:
968 return SelectBitCast(I);
969
970 case Instruction::FPToSI:
971 return SelectCast(I, ISD::FP_TO_SINT);
972 case Instruction::ZExt:
973 return SelectCast(I, ISD::ZERO_EXTEND);
974 case Instruction::SExt:
975 return SelectCast(I, ISD::SIGN_EXTEND);
976 case Instruction::Trunc:
977 return SelectCast(I, ISD::TRUNCATE);
978 case Instruction::SIToFP:
979 return SelectCast(I, ISD::SINT_TO_FP);
980
981 case Instruction::IntToPtr: // Deliberate fall-through.
982 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000983 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
984 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000985 if (DstVT.bitsGT(SrcVT))
986 return SelectCast(I, ISD::ZERO_EXTEND);
987 if (DstVT.bitsLT(SrcVT))
988 return SelectCast(I, ISD::TRUNCATE);
989 unsigned Reg = getRegForValue(I->getOperand(0));
990 if (Reg == 0) return false;
991 UpdateValueMap(I, Reg);
992 return true;
993 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000994
Eli Friedman2586b8f2011-05-16 20:27:46 +0000995 case Instruction::ExtractValue:
996 return SelectExtractValue(I);
997
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000998 case Instruction::PHI:
999 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1000
Dan Gohman3df24e62008-09-03 23:12:08 +00001001 default:
1002 // Unhandled instruction. Halt "fast" selection and bail.
1003 return false;
1004 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001005}
1006
Dan Gohmana4160c32010-07-07 16:29:44 +00001007FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001008 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001009 MRI(FuncInfo.MF->getRegInfo()),
1010 MFI(*FuncInfo.MF->getFrameInfo()),
1011 MCP(*FuncInfo.MF->getConstantPool()),
1012 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001013 TD(*TM.getTargetData()),
1014 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001015 TLI(*TM.getTargetLowering()),
Dan Gohman84023e02010-07-10 09:00:22 +00001016 TRI(*TM.getRegisterInfo()) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001017}
1018
Dan Gohmane285a742008-08-14 21:51:29 +00001019FastISel::~FastISel() {}
1020
Owen Anderson825b72b2009-08-11 20:47:22 +00001021unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001022 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001023 return 0;
1024}
1025
Owen Anderson825b72b2009-08-11 20:47:22 +00001026unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001027 unsigned,
1028 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001029 return 0;
1030}
1031
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001032unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001033 unsigned,
1034 unsigned /*Op0*/, bool /*Op0IsKill*/,
1035 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001036 return 0;
1037}
1038
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001039unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001040 return 0;
1041}
1042
Owen Anderson825b72b2009-08-11 20:47:22 +00001043unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001044 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001045 return 0;
1046}
1047
Owen Anderson825b72b2009-08-11 20:47:22 +00001048unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001049 unsigned,
1050 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001051 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001052 return 0;
1053}
1054
Owen Anderson825b72b2009-08-11 20:47:22 +00001055unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001056 unsigned,
1057 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001058 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001059 return 0;
1060}
1061
Owen Anderson825b72b2009-08-11 20:47:22 +00001062unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001063 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001064 unsigned /*Op0*/, bool /*Op0IsKill*/,
1065 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001066 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001067 return 0;
1068}
1069
1070/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1071/// to emit an instruction with an immediate operand using FastEmit_ri.
1072/// If that fails, it materializes the immediate into a register and try
1073/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001074unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001075 unsigned Op0, bool Op0IsKill,
1076 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001077 // If this is a multiply by a power of two, emit this as a shift left.
1078 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1079 Opcode = ISD::SHL;
1080 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001081 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1082 // div x, 8 -> srl x, 3
1083 Opcode = ISD::SRL;
1084 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001085 }
Owen Andersond74ea772011-04-22 23:38:06 +00001086
Chris Lattner602fc062011-04-17 20:23:29 +00001087 // Horrible hack (to be removed), check to make sure shift amounts are
1088 // in-range.
1089 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1090 Imm >= VT.getSizeInBits())
1091 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001092
Evan Cheng83785c82008-08-20 22:45:34 +00001093 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001094 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001095 if (ResultReg != 0)
1096 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001097 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001098 if (MaterialReg == 0) {
1099 // This is a bit ugly/slow, but failing here means falling out of
1100 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001101 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001102 VT.getSizeInBits());
1103 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
1104 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001105 return FastEmit_rr(VT, VT, Opcode,
1106 Op0, Op0IsKill,
1107 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001108}
1109
1110unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1111 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001112}
1113
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001114unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001115 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001116 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001117 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001118
Dan Gohman84023e02010-07-10 09:00:22 +00001119 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001120 return ResultReg;
1121}
1122
1123unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1124 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001125 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001126 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001127 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001128
Evan Cheng5960e4e2008-09-08 08:38:20 +00001129 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001130 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1131 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001132 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1134 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001135 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1136 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001137 }
1138
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001139 return ResultReg;
1140}
1141
1142unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1143 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001144 unsigned Op0, bool Op0IsKill,
1145 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001146 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001147 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001148
Evan Cheng5960e4e2008-09-08 08:38:20 +00001149 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001151 .addReg(Op0, Op0IsKill * RegState::Kill)
1152 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001153 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001155 .addReg(Op0, Op0IsKill * RegState::Kill)
1156 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001157 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1158 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001159 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001160 return ResultReg;
1161}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001162
Owen Andersond71867a2011-05-05 17:59:04 +00001163unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1164 const TargetRegisterClass *RC,
1165 unsigned Op0, bool Op0IsKill,
1166 unsigned Op1, bool Op1IsKill,
1167 unsigned Op2, bool Op2IsKill) {
1168 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001169 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001170
1171 if (II.getNumDefs() >= 1)
1172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1173 .addReg(Op0, Op0IsKill * RegState::Kill)
1174 .addReg(Op1, Op1IsKill * RegState::Kill)
1175 .addReg(Op2, Op2IsKill * RegState::Kill);
1176 else {
1177 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1178 .addReg(Op0, Op0IsKill * RegState::Kill)
1179 .addReg(Op1, Op1IsKill * RegState::Kill)
1180 .addReg(Op2, Op2IsKill * RegState::Kill);
1181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1182 ResultReg).addReg(II.ImplicitDefs[0]);
1183 }
1184 return ResultReg;
1185}
1186
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001187unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1188 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001189 unsigned Op0, bool Op0IsKill,
1190 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001191 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001192 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001193
Evan Cheng5960e4e2008-09-08 08:38:20 +00001194 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001196 .addReg(Op0, Op0IsKill * RegState::Kill)
1197 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001198 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001200 .addReg(Op0, Op0IsKill * RegState::Kill)
1201 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001202 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1203 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001204 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001205 return ResultReg;
1206}
1207
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001208unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1209 const TargetRegisterClass *RC,
1210 unsigned Op0, bool Op0IsKill,
1211 uint64_t Imm1, uint64_t Imm2) {
1212 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001213 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001214
1215 if (II.getNumDefs() >= 1)
1216 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1217 .addReg(Op0, Op0IsKill * RegState::Kill)
1218 .addImm(Imm1)
1219 .addImm(Imm2);
1220 else {
1221 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1222 .addReg(Op0, Op0IsKill * RegState::Kill)
1223 .addImm(Imm1)
1224 .addImm(Imm2);
1225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1226 ResultReg).addReg(II.ImplicitDefs[0]);
1227 }
1228 return ResultReg;
1229}
1230
Dan Gohman10df0fa2008-08-27 01:09:54 +00001231unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1232 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001233 unsigned Op0, bool Op0IsKill,
1234 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001235 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001236 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001237
Evan Cheng5960e4e2008-09-08 08:38:20 +00001238 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001239 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001240 .addReg(Op0, Op0IsKill * RegState::Kill)
1241 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001242 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001244 .addReg(Op0, Op0IsKill * RegState::Kill)
1245 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1247 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001248 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001249 return ResultReg;
1250}
1251
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001252unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1253 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001254 unsigned Op0, bool Op0IsKill,
1255 unsigned Op1, bool Op1IsKill,
1256 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001257 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001258 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001259
Evan Cheng5960e4e2008-09-08 08:38:20 +00001260 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001261 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001262 .addReg(Op0, Op0IsKill * RegState::Kill)
1263 .addReg(Op1, Op1IsKill * RegState::Kill)
1264 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001265 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001266 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001267 .addReg(Op0, Op0IsKill * RegState::Kill)
1268 .addReg(Op1, Op1IsKill * RegState::Kill)
1269 .addImm(Imm);
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 Gohmand5fe57d2008-08-21 01:41:07 +00001273 return ResultReg;
1274}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001275
1276unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1277 const TargetRegisterClass *RC,
1278 uint64_t Imm) {
1279 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001280 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001281
Evan Cheng5960e4e2008-09-08 08:38:20 +00001282 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001284 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001286 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1287 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001288 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001289 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001290}
Owen Anderson8970f002008-08-27 22:30:02 +00001291
Owen Andersond74ea772011-04-22 23:38:06 +00001292unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1293 const TargetRegisterClass *RC,
1294 uint64_t Imm1, uint64_t Imm2) {
1295 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001296 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001297
1298 if (II.getNumDefs() >= 1)
1299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1300 .addImm(Imm1).addImm(Imm2);
1301 else {
1302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1303 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1304 ResultReg).addReg(II.ImplicitDefs[0]);
1305 }
1306 return ResultReg;
1307}
1308
Owen Anderson825b72b2009-08-11 20:47:22 +00001309unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001310 unsigned Op0, bool Op0IsKill,
1311 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001312 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001313 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1314 "Cannot yet extract from physregs");
Dan Gohman84023e02010-07-10 09:00:22 +00001315 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1316 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001317 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001318 return ResultReg;
1319}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001320
1321/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1322/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001323unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1324 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001325}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001326
1327/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1328/// Emit code to ensure constants are copied into registers when needed.
1329/// Remember the virtual registers that need to be added to the Machine PHI
1330/// nodes as input. We cannot just directly add them, because expansion
1331/// might result in multiple MBB's for one BB. As such, the start of the
1332/// BB might correspond to a different MBB than the end.
1333bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1334 const TerminatorInst *TI = LLVMBB->getTerminator();
1335
1336 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001337 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001338
1339 // Check successor nodes' PHI nodes that expect a constant to be available
1340 // from this block.
1341 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1342 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1343 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001344 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001345
1346 // If this terminator has multiple identical successors (common for
1347 // switches), only handle each succ once.
1348 if (!SuccsHandled.insert(SuccMBB)) continue;
1349
1350 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1351
1352 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1353 // nodes and Machine PHI nodes, but the incoming operands have not been
1354 // emitted yet.
1355 for (BasicBlock::const_iterator I = SuccBB->begin();
1356 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001357
Dan Gohmanf81eca02010-04-22 20:46:50 +00001358 // Ignore dead phi's.
1359 if (PN->use_empty()) continue;
1360
1361 // Only handle legal types. Two interesting things to note here. First,
1362 // by bailing out early, we may leave behind some dead instructions,
1363 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001364 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001365 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001366 // exactly one register for each non-void instruction.
1367 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1368 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1369 // Promote MVT::i1.
1370 if (VT == MVT::i1)
1371 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1372 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001373 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001374 return false;
1375 }
1376 }
1377
1378 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1379
Dan Gohmanfb95f892010-05-07 01:10:20 +00001380 // Set the DebugLoc for the copy. Prefer the location of the operand
1381 // if there is one; use the location of the PHI otherwise.
1382 DL = PN->getDebugLoc();
1383 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1384 DL = Inst->getDebugLoc();
1385
Dan Gohmanf81eca02010-04-22 20:46:50 +00001386 unsigned Reg = getRegForValue(PHIOp);
1387 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001388 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001389 return false;
1390 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001391 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001392 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001393 }
1394 }
1395
1396 return true;
1397}