blob: 9ac738e507265f3f2b7824e7c239a3d1bcb2ede5 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000043#include "llvm/CodeGen/FastISel.h"
44#include "llvm/ADT/Statistic.h"
45#include "llvm/Analysis/Loads.h"
46#include "llvm/CodeGen/Analysis.h"
47#include "llvm/CodeGen/FunctionLoweringInfo.h"
48#include "llvm/CodeGen/MachineInstrBuilder.h"
49#include "llvm/CodeGen/MachineModuleInfo.h"
50#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000051#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000052#include "llvm/IR/DataLayout.h"
53#include "llvm/IR/Function.h"
54#include "llvm/IR/GlobalVariable.h"
55#include "llvm/IR/Instructions.h"
56#include "llvm/IR/IntrinsicInst.h"
57#include "llvm/IR/Operator.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000058#include "llvm/Support/Debug.h"
59#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000060#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsond49edb72012-08-03 04:06:28 +000061#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000062#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000063#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000064using namespace llvm;
65
Chad Rosieraa5656c2011-11-28 19:59:09 +000066STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
67 "target-independent selector");
68STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
69 "target-specific selector");
Chad Rosierae6f2cb2011-11-29 19:40:47 +000070STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosier053e69a2011-11-16 21:05:28 +000071
Dan Gohman84023e02010-07-10 09:00:22 +000072/// startNewBlock - Set the current block to which generated machine
73/// instructions will be appended, and clear the local CSE map.
74///
75void FastISel::startNewBlock() {
76 LocalValueMap.clear();
77
Ivan Krasin74af88a2011-08-18 22:06:10 +000078 EmitStartPt = 0;
Dan Gohman84023e02010-07-10 09:00:22 +000079
Ivan Krasin74af88a2011-08-18 22:06:10 +000080 // Advance the emit start point past any EH_LABEL instructions.
Dan Gohman84023e02010-07-10 09:00:22 +000081 MachineBasicBlock::iterator
82 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
83 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
Ivan Krasin74af88a2011-08-18 22:06:10 +000084 EmitStartPt = I;
Dan Gohman84023e02010-07-10 09:00:22 +000085 ++I;
86 }
Ivan Krasin74af88a2011-08-18 22:06:10 +000087 LastLocalValue = EmitStartPt;
88}
89
Evan Cheng092e5e72013-02-11 01:27:15 +000090bool FastISel::LowerArguments() {
91 if (!FuncInfo.CanLowerReturn)
92 // Fallback to SDISel argument lowering code to deal with sret pointer
93 // parameter.
94 return false;
95
96 if (!FastLowerArguments())
97 return false;
98
99 // Enter non-dead arguments into ValueMap for uses in non-entry BBs.
100 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
101 E = FuncInfo.Fn->arg_end(); I != E; ++I) {
102 if (!I->use_empty()) {
103 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
104 assert(VI != LocalValueMap.end() && "Missed an argument?");
105 FuncInfo.ValueMap[I] = VI->second;
106 }
107 }
108 return true;
109}
110
Ivan Krasin74af88a2011-08-18 22:06:10 +0000111void FastISel::flushLocalValueMap() {
112 LocalValueMap.clear();
113 LastLocalValue = EmitStartPt;
114 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +0000115}
116
Dan Gohmana6cb6412010-05-11 23:54:07 +0000117bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +0000118 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000119 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +0000120 if (!I)
121 return false;
122
123 // No-op casts are trivially coalesced by fast-isel.
124 if (const CastInst *Cast = dyn_cast<CastInst>(I))
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000125 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
126 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman7f0d6952010-05-14 22:53:18 +0000127 return false;
128
Chad Rosier22b34cc2011-11-15 23:34:05 +0000129 // GEPs with all zero indices are trivially coalesced by fast-isel.
130 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
131 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
132 return false;
133
Dan Gohman7f0d6952010-05-14 22:53:18 +0000134 // Only instructions with a single use in the same basic block are considered
135 // to have trivial kills.
136 return I->hasOneUse() &&
137 !(I->getOpcode() == Instruction::BitCast ||
138 I->getOpcode() == Instruction::PtrToInt ||
139 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000140 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000141}
142
Dan Gohman46510a72010-04-15 01:51:59 +0000143unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000144 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000145 // Don't handle non-simple values in FastISel.
146 if (!RealVT.isSimple())
147 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000148
149 // Ignore illegal types. We must do this before looking up the value
150 // in ValueMap because Arguments are given virtual registers regardless
151 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000153 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000154 // Handle integer promotions, though, because they're common and easy.
155 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000156 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000157 else
158 return 0;
159 }
160
Eric Christopher4e270272012-03-20 01:07:47 +0000161 // Look up the value to see if we already have a register for it.
162 unsigned Reg = lookUpRegForValue(V);
Dan Gohman104e4ce2008-09-03 23:32:19 +0000163 if (Reg != 0)
164 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000165
Dan Gohman97c94b82010-05-06 00:02:14 +0000166 // In bottom-up mode, just create the virtual register which will be used
167 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000168 if (isa<Instruction>(V) &&
169 (!isa<AllocaInst>(V) ||
170 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
171 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000172
Eric Christopher76ad43c2012-10-03 08:10:01 +0000173 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000174
175 // Materialize the value in a register. Emit any instructions in the
176 // local value area.
177 Reg = materializeRegForValue(V, VT);
178
Eric Christopher76ad43c2012-10-03 08:10:01 +0000179 leaveLocalValueArea(SaveInsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000180
181 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000182}
183
Eric Christopher44a2c342010-08-17 01:30:33 +0000184/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000185/// called when the value isn't already available in a register and must
186/// be materialized with new instructions.
187unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
188 unsigned Reg = 0;
189
Dan Gohman46510a72010-04-15 01:51:59 +0000190 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000191 if (CI->getValue().getActiveBits() <= 64)
192 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000193 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000194 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000195 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000196 // Translate this as an integer zero so that it can be
197 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000198 Reg =
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000199 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000200 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000201 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000202 Reg = TargetMaterializeFloatZero(CF);
203 } else {
204 // Try to emit the constant directly.
205 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
206 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000207
208 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000209 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000210 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000211 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000212
213 uint64_t x[2];
214 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000215 bool isExact;
216 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopherc415af22012-03-20 01:07:56 +0000217 APFloat::rmTowardZero, &isExact);
Dale Johannesen23a98552008-10-09 23:00:39 +0000218 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000219 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000220
Owen Andersone922c022009-07-22 00:24:57 +0000221 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000222 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000223 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000224 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
225 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000226 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000227 }
Dan Gohman46510a72010-04-15 01:51:59 +0000228 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000229 if (!SelectOperator(Op, Op->getOpcode()))
230 if (!isa<Instruction>(Op) ||
231 !TargetSelectInstruction(cast<Instruction>(Op)))
232 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000233 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000234 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000235 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
237 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000238 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239
Dan Gohmandceffe62008-09-25 01:28:51 +0000240 // If target-independent code couldn't handle the value, give target-specific
241 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000242 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000243 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000245 // Don't cache constant materializations in the general ValueMap.
246 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000247 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000248 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000249 LastLocalValue = MRI.getVRegDef(Reg);
250 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000251 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000252}
253
Dan Gohman46510a72010-04-15 01:51:59 +0000254unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000255 // Look up the value to see if we already have a register for it. We
256 // cache values defined by Instructions across blocks, and other values
257 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000258 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000259 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
260 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000261 return I->second;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000262 return LocalValueMap[V];
Evan Cheng59fbc802008-09-09 01:26:59 +0000263}
264
Owen Andersoncc54e762008-08-30 00:38:46 +0000265/// UpdateValueMap - Update the value map to include the new mapping for this
266/// instruction, or insert an extra copy to get the result in a previous
267/// determined register.
268/// NOTE: This is only necessary because we might select a block that uses
269/// a value before we select the block that defines the value. It might be
270/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000271void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000272 if (!isa<Instruction>(I)) {
273 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000274 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000275 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000276
Dan Gohmana4160c32010-07-07 16:29:44 +0000277 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000278 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000279 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000280 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000281 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000282 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000283 for (unsigned i = 0; i < NumRegs; i++)
284 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000285
286 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000287 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000288}
289
Dan Gohmana6cb6412010-05-11 23:54:07 +0000290std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000291 unsigned IdxN = getRegForValue(Idx);
292 if (IdxN == 0)
293 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000294 return std::pair<unsigned, bool>(0, false);
295
296 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000297
298 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000299 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000300 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000301 if (IdxVT.bitsLT(PtrVT)) {
302 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
303 IdxN, IdxNIsKill);
304 IdxNIsKill = true;
305 }
306 else if (IdxVT.bitsGT(PtrVT)) {
307 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
308 IdxN, IdxNIsKill);
309 IdxNIsKill = true;
310 }
311 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000312}
313
Dan Gohman84023e02010-07-10 09:00:22 +0000314void FastISel::recomputeInsertPt() {
315 if (getLastLocalValue()) {
316 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000317 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000318 ++FuncInfo.InsertPt;
319 } else
320 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
321
322 // Now skip past any EH_LABELs, which must remain at the beginning.
323 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
324 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
325 ++FuncInfo.InsertPt;
326}
327
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000328void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
329 MachineBasicBlock::iterator E) {
330 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
331 while (I != E) {
332 MachineInstr *Dead = &*I;
333 ++I;
334 Dead->eraseFromParent();
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000335 ++NumFastIselDead;
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000336 }
337 recomputeInsertPt();
338}
339
Eric Christopher76ad43c2012-10-03 08:10:01 +0000340FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000341 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000342 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000343 recomputeInsertPt();
Eric Christopher76ad43c2012-10-03 08:10:01 +0000344 DL = DebugLoc();
345 SavePoint SP = { OldInsertPt, OldDL };
346 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000347}
348
Eric Christopher76ad43c2012-10-03 08:10:01 +0000349void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000350 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
351 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
352
353 // Restore the previous insert position.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000354 FuncInfo.InsertPt = OldInsertPt.InsertPt;
355 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000356}
357
Dan Gohmanbdedd442008-08-20 00:11:48 +0000358/// SelectBinaryOp - Select and emit code for a binary operator instruction,
359/// which has an opcode which directly corresponds to the given ISD opcode.
360///
Dan Gohman46510a72010-04-15 01:51:59 +0000361bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000362 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000364 // Unhandled type. Halt "fast" selection and bail.
365 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000366
Dan Gohmanb71fea22008-08-26 20:52:40 +0000367 // We only handle legal types. For example, on x86-32 the instruction
368 // selector contains all of the 64-bit instructions from x86-64,
369 // under the assumption that i64 won't be used if the target doesn't
370 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000371 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000373 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000375 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
376 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000377 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000378 else
379 return false;
380 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000381
Chris Lattnerfff65b32011-04-17 01:16:47 +0000382 // Check if the first operand is a constant, and handle it as "ri". At -O0,
383 // we don't have anything that canonicalizes operand order.
384 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
385 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
386 unsigned Op1 = getRegForValue(I->getOperand(1));
387 if (Op1 == 0) return false;
388
389 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000390
Chris Lattner602fc062011-04-17 20:23:29 +0000391 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
392 Op1IsKill, CI->getZExtValue(),
393 VT.getSimpleVT());
394 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000395
Chris Lattner602fc062011-04-17 20:23:29 +0000396 // We successfully emitted code for the given LLVM Instruction.
397 UpdateValueMap(I, ResultReg);
398 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000399 }
Owen Andersond74ea772011-04-22 23:38:06 +0000400
401
Dan Gohman3df24e62008-09-03 23:12:08 +0000402 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000403 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000404 return false;
405
Dan Gohmana6cb6412010-05-11 23:54:07 +0000406 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
407
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000408 // Check if the second operand is a constant and handle it appropriately.
409 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000410 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000411
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000412 // Transform "sdiv exact X, 8" -> "sra X, 3".
413 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
414 cast<BinaryOperator>(I)->isExact() &&
415 isPowerOf2_64(Imm)) {
416 Imm = Log2_64(Imm);
417 ISDOpcode = ISD::SRA;
418 }
Owen Andersond74ea772011-04-22 23:38:06 +0000419
Chad Rosier544b9b42012-03-22 00:21:17 +0000420 // Transform "urem x, pow2" -> "and x, pow2-1".
421 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
422 isPowerOf2_64(Imm)) {
423 --Imm;
424 ISDOpcode = ISD::AND;
425 }
426
Chris Lattner602fc062011-04-17 20:23:29 +0000427 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
428 Op0IsKill, Imm, VT.getSimpleVT());
429 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000430
Chris Lattner602fc062011-04-17 20:23:29 +0000431 // We successfully emitted code for the given LLVM Instruction.
432 UpdateValueMap(I, ResultReg);
433 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000434 }
435
Dan Gohman10df0fa2008-08-27 01:09:54 +0000436 // Check if the second operand is a constant float.
437 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000438 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000439 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000440 if (ResultReg != 0) {
441 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000442 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000443 return true;
444 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000445 }
446
Dan Gohman3df24e62008-09-03 23:12:08 +0000447 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000448 if (Op1 == 0)
449 // Unhandled operand. Halt "fast" selection and bail.
450 return false;
451
Dan Gohmana6cb6412010-05-11 23:54:07 +0000452 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
453
Dan Gohmanad368ac2008-08-27 18:10:19 +0000454 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000455 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000456 ISDOpcode,
457 Op0, Op0IsKill,
458 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000459 if (ResultReg == 0)
460 // Target-specific code wasn't able to find a machine opcode for
461 // the given ISD opcode and type. Halt "fast" selection and bail.
462 return false;
463
Dan Gohman8014e862008-08-20 00:23:20 +0000464 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000465 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000466 return true;
467}
468
Dan Gohman46510a72010-04-15 01:51:59 +0000469bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000470 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000471 if (N == 0)
472 // Unhandled operand. Halt "fast" selection and bail.
473 return false;
474
Dan Gohmana6cb6412010-05-11 23:54:07 +0000475 bool NIsKill = hasTrivialKill(I->getOperand(0));
476
Chad Rosier478b06c2011-11-17 07:15:58 +0000477 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
478 // into a single N = N + TotalOffset.
479 uint64_t TotalOffs = 0;
480 // FIXME: What's a good SWAG number for MaxOffs?
481 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000482 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000484 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
485 E = I->op_end(); OI != E; ++OI) {
486 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000487 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000488 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
489 if (Field) {
490 // N = N + Offset
Chad Rosier478b06c2011-11-17 07:15:58 +0000491 TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
492 if (TotalOffs >= MaxOffs) {
493 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
494 if (N == 0)
495 // Unhandled operand. Halt "fast" selection and bail.
496 return false;
497 NIsKill = true;
498 TotalOffs = 0;
499 }
Evan Cheng83785c82008-08-20 22:45:34 +0000500 }
501 Ty = StTy->getElementType(Field);
502 } else {
503 Ty = cast<SequentialType>(Ty)->getElementType();
504
505 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000506 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000507 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000508 // N = N + Offset
Chad Rosier6016a4a2012-07-06 17:44:22 +0000509 TotalOffs +=
Duncan Sands777d2302009-05-09 07:06:46 +0000510 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000511 if (TotalOffs >= MaxOffs) {
512 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
513 if (N == 0)
514 // Unhandled operand. Halt "fast" selection and bail.
515 return false;
516 NIsKill = true;
517 TotalOffs = 0;
518 }
519 continue;
520 }
521 if (TotalOffs) {
522 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000523 if (N == 0)
524 // Unhandled operand. Halt "fast" selection and bail.
525 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000526 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000527 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000528 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000529
Evan Cheng83785c82008-08-20 22:45:34 +0000530 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000531 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000532 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
533 unsigned IdxN = Pair.first;
534 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000535 if (IdxN == 0)
536 // Unhandled operand. Halt "fast" selection and bail.
537 return false;
538
Dan Gohman80bc6e22008-08-26 20:57:08 +0000539 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000540 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000541 if (IdxN == 0)
542 // Unhandled operand. Halt "fast" selection and bail.
543 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000544 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000545 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000546 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000547 if (N == 0)
548 // Unhandled operand. Halt "fast" selection and bail.
549 return false;
550 }
551 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000552 if (TotalOffs) {
553 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
554 if (N == 0)
555 // Unhandled operand. Halt "fast" selection and bail.
556 return false;
557 }
Evan Cheng83785c82008-08-20 22:45:34 +0000558
559 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000560 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000561 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000562}
563
Dan Gohman46510a72010-04-15 01:51:59 +0000564bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000565 const CallInst *Call = cast<CallInst>(I);
566
567 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000568 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000569 // Don't attempt to handle constraints.
570 if (!IA->getConstraintString().empty())
571 return false;
572
573 unsigned ExtraInfo = 0;
574 if (IA->hasSideEffects())
575 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
576 if (IA->isAlignStack())
577 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
578
579 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
580 TII.get(TargetOpcode::INLINEASM))
581 .addExternalSymbol(IA->getAsmString().c_str())
582 .addImm(ExtraInfo);
583 return true;
584 }
585
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000586 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
587 ComputeUsesVAFloatArgument(*Call, &MMI);
588
Dan Gohmana61e73b2011-04-26 17:18:34 +0000589 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000590 if (!F) return false;
591
Dan Gohman4183e312010-04-13 17:07:06 +0000592 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000593 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000594 default: break;
Chad Rosieraefd36b2012-05-11 23:21:01 +0000595 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000596 case Intrinsic::lifetime_start:
597 case Intrinsic::lifetime_end:
Chad Rosierfd065bb2012-07-06 17:33:39 +0000598 // The donothing intrinsic does, well, nothing.
599 case Intrinsic::donothing:
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000600 return true;
Chad Rosierfd065bb2012-07-06 17:33:39 +0000601
Bill Wendling92c1e122009-02-13 02:16:35 +0000602 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000603 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000604 if (!DIVariable(DI->getVariable()).Verify() ||
Eric Christopherbb54d212012-03-15 21:33:44 +0000605 !FuncInfo.MF->getMMI().hasDebugInfo()) {
606 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel7e1e31f2009-07-02 22:43:26 +0000607 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000608 }
Devang Patel7e1e31f2009-07-02 22:43:26 +0000609
Dan Gohman46510a72010-04-15 01:51:59 +0000610 const Value *Address = DI->getAddress();
Eric Christopherccaea7d2012-03-15 21:33:47 +0000611 if (!Address || isa<UndefValue>(Address)) {
Eric Christopherbb54d212012-03-15 21:33:44 +0000612 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendc918562010-02-06 02:26:02 +0000613 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000614 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000615
616 unsigned Reg = 0;
617 unsigned Offset = 0;
618 if (const Argument *Arg = dyn_cast<Argument>(Address)) {
Devang Patel9aee3352011-09-08 22:59:09 +0000619 // Some arguments' frame index is recorded during argument lowering.
620 Offset = FuncInfo.getArgumentFrameIndex(Arg);
621 if (Offset)
Eric Christopherc415af22012-03-20 01:07:56 +0000622 Reg = TRI.getFrameRegister(*FuncInfo.MF);
Devang Patel4bafda92010-09-10 20:32:09 +0000623 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000624 if (!Reg)
Eric Christopher8c5293c2012-03-20 01:07:58 +0000625 Reg = lookUpRegForValue(Address);
626
Bill Wendling84364a42012-03-30 00:02:55 +0000627 // If we have a VLA that has a "use" in a metadata node that's then used
628 // here but it has no other uses, then we have a problem. E.g.,
629 //
630 // int foo (const int *x) {
631 // char a[*x];
632 // return 0;
633 // }
634 //
635 // If we assign 'a' a vreg and fast isel later on has to use the selection
636 // DAG isel, it will want to copy the value to the vreg. However, there are
637 // no uses, which goes counter to what selection DAG isel expects.
638 if (!Reg && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher8c5293c2012-03-20 01:07:58 +0000639 (!isa<AllocaInst>(Address) ||
640 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
641 Reg = FuncInfo.InitializeRegForValue(Address);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000642
Devang Patel6fe75aa2010-09-14 20:29:31 +0000643 if (Reg)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000644 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Devang Patel6fe75aa2010-09-14 20:29:31 +0000645 TII.get(TargetOpcode::DBG_VALUE))
646 .addReg(Reg, RegState::Debug).addImm(Offset)
647 .addMetadata(DI->getVariable());
Eric Christopher4476bae2012-03-20 01:07:53 +0000648 else
649 // We can't yet handle anything else here because it would require
650 // generating code, thus altering codegen because of debug info.
651 DEBUG(dbgs() << "Dropping debug info for " << DI);
Dan Gohman33134c42008-09-25 17:05:24 +0000652 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000653 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000654 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000655 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000656 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000657 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000658 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000659 if (!V) {
660 // Currently the optimizer can produce this; insert an undef to
661 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000662 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
663 .addReg(0U).addImm(DI->getOffset())
664 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000665 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000666 if (CI->getBitWidth() > 64)
667 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
668 .addCImm(CI).addImm(DI->getOffset())
669 .addMetadata(DI->getVariable());
Chad Rosier6016a4a2012-07-06 17:44:22 +0000670 else
Devang Patel8594d422011-06-24 20:46:11 +0000671 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
672 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
673 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000674 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000675 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
676 .addFPImm(CF).addImm(DI->getOffset())
677 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000678 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
680 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
681 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000682 } else {
683 // We can't yet handle anything else here because it would require
684 // generating code, thus altering codegen because of debug info.
Devang Patelafeaae72010-12-06 22:39:26 +0000685 DEBUG(dbgs() << "Dropping debug info for " << DI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000686 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000687 return true;
688 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000689 case Intrinsic::objectsize: {
690 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
691 unsigned long long Res = CI->isZero() ? -1ULL : 0;
692 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
693 unsigned ResultReg = getRegForValue(ResCI);
694 if (ResultReg == 0)
695 return false;
696 UpdateValueMap(Call, ResultReg);
697 return true;
698 }
Chad Rosier33947b42013-03-07 20:42:17 +0000699 case Intrinsic::expect: {
Chad Rosier4fde76d2013-03-07 21:38:33 +0000700 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky33cdfe92013-03-11 21:44:37 +0000701 if (ResultReg == 0)
702 return false;
Chad Rosier4fde76d2013-03-07 21:38:33 +0000703 UpdateValueMap(Call, ResultReg);
704 return true;
Chad Rosier33947b42013-03-07 20:42:17 +0000705 }
Dan Gohman33134c42008-09-25 17:05:24 +0000706 }
Dan Gohman4183e312010-04-13 17:07:06 +0000707
Ivan Krasin74af88a2011-08-18 22:06:10 +0000708 // Usually, it does not make sense to initialize a value,
709 // make an unrelated function call and use the value, because
710 // it tends to be spilled on the stack. So, we move the pointer
711 // to the last local value to the beginning of the block, so that
712 // all the values which have already been materialized,
713 // appear after the call. It also makes sense to skip intrinsics
714 // since they tend to be inlined.
Pete Cooperb704ffb2013-02-22 01:50:38 +0000715 if (!isa<IntrinsicInst>(Call))
Ivan Krasin74af88a2011-08-18 22:06:10 +0000716 flushLocalValueMap();
717
Dan Gohman4183e312010-04-13 17:07:06 +0000718 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000719 return false;
720}
721
Dan Gohman46510a72010-04-15 01:51:59 +0000722bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000723 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
724 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000725
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
727 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000728 // Unhandled type. Halt "fast" selection and bail.
729 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730
Eli Friedman76927d732011-05-25 23:49:02 +0000731 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000732 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000733 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000734
Eli Friedman76927d732011-05-25 23:49:02 +0000735 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000736 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000737 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000738
Dan Gohman3df24e62008-09-03 23:12:08 +0000739 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000740 if (!InputReg)
741 // Unhandled operand. Halt "fast" selection and bail.
742 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000743
Dan Gohmana6cb6412010-05-11 23:54:07 +0000744 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
745
Owen Andersond0533c92008-08-26 23:46:32 +0000746 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
747 DstVT.getSimpleVT(),
748 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000749 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000750 if (!ResultReg)
751 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000752
Dan Gohman3df24e62008-09-03 23:12:08 +0000753 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000754 return true;
755}
756
Dan Gohman46510a72010-04-15 01:51:59 +0000757bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000758 // If the bitcast doesn't change the type, just use the operand value.
759 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000760 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000761 if (Reg == 0)
762 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000763 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000764 return true;
765 }
766
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000768 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
769 EVT DstEVT = TLI.getValueType(I->getType());
770 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
771 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000772 // Unhandled type. Halt "fast" selection and bail.
773 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000774
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000775 MVT SrcVT = SrcEVT.getSimpleVT();
776 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman3df24e62008-09-03 23:12:08 +0000777 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000778 if (Op0 == 0)
779 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000780 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000781
782 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783
Dan Gohmanad368ac2008-08-27 18:10:19 +0000784 // First, try to perform the bitcast by inserting a reg-reg copy.
785 unsigned ResultReg = 0;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000786 if (SrcVT == DstVT) {
Craig Topper44d23822012-02-22 05:59:10 +0000787 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
788 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000789 // Don't attempt a cross-class copy. It will likely fail.
790 if (SrcClass == DstClass) {
791 ResultReg = createResultReg(DstClass);
792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
793 ResultReg).addReg(Op0);
794 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000795 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000796
797 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000798 if (!ResultReg)
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000799 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000800
Dan Gohmanad368ac2008-08-27 18:10:19 +0000801 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000802 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000803
Dan Gohman3df24e62008-09-03 23:12:08 +0000804 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000805 return true;
806}
807
Dan Gohman3df24e62008-09-03 23:12:08 +0000808bool
Dan Gohman46510a72010-04-15 01:51:59 +0000809FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000810 // Just before the terminator instruction, insert instructions to
811 // feed PHI nodes in successor blocks.
812 if (isa<TerminatorInst>(I))
813 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
814 return false;
815
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000816 DL = I->getDebugLoc();
817
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000818 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
819
Bob Wilson982dc842012-08-03 21:26:24 +0000820 // As a special case, don't handle calls to builtin library functions that
821 // may be translated directly to target instructions.
Bob Wilsond49edb72012-08-03 04:06:28 +0000822 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
823 const Function *F = Call->getCalledFunction();
824 LibFunc::Func Func;
825 if (F && !F->hasLocalLinkage() && F->hasName() &&
826 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson982dc842012-08-03 21:26:24 +0000827 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilsond49edb72012-08-03 04:06:28 +0000828 return false;
829 }
830
Dan Gohman6e3ff372009-12-05 01:27:58 +0000831 // First, try doing target-independent selection.
Michael Ilseman7dbd34b2013-02-27 19:54:00 +0000832 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000833 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000834 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000835 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000836 }
Chad Rosier6016a4a2012-07-06 17:44:22 +0000837 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000838 // the local value map and recomputed the insert point.
839 if (!isa<CallInst>(I)) {
840 recomputeInsertPt();
841 if (SavedInsertPt != FuncInfo.InsertPt)
842 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
843 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000844
845 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000846 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000847 if (TargetSelectInstruction(I)) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000848 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000849 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000850 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000851 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000852 // Check for dead code and remove as necessary.
853 recomputeInsertPt();
854 if (SavedInsertPt != FuncInfo.InsertPt)
855 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000856
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000857 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000858 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000859}
860
Dan Gohmand98d6202008-10-02 22:15:21 +0000861/// FastEmitBranch - Emit an unconditional branch to the given block,
862/// unless it is the immediate (fall-through) successor, and update
863/// the CFG.
864void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000865FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Eric Christopher18112d82012-04-10 18:18:10 +0000866
Evan Cheng092e5e72013-02-11 01:27:15 +0000867 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
868 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christopher18112d82012-04-10 18:18:10 +0000869 // For more accurate line information if this is the only instruction
870 // in the block then emit it, otherwise we have the unconditional
871 // fall-through case, which needs no instructions.
Dan Gohmand98d6202008-10-02 22:15:21 +0000872 } else {
873 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000874 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
875 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000876 }
Dan Gohman84023e02010-07-10 09:00:22 +0000877 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000878}
879
Dan Gohman3d45a852009-09-03 22:53:57 +0000880/// SelectFNeg - Emit an FNeg operation.
881///
882bool
Dan Gohman46510a72010-04-15 01:51:59 +0000883FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000884 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
885 if (OpReg == 0) return false;
886
Dan Gohmana6cb6412010-05-11 23:54:07 +0000887 bool OpRegIsKill = hasTrivialKill(I);
888
Dan Gohman4a215a12009-09-11 00:36:43 +0000889 // If the target has ISD::FNEG, use it.
890 EVT VT = TLI.getValueType(I->getType());
891 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000892 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000893 if (ResultReg != 0) {
894 UpdateValueMap(I, ResultReg);
895 return true;
896 }
897
Dan Gohman5e5abb72009-09-11 00:34:46 +0000898 // Bitcast the value to integer, twiddle the sign bit with xor,
899 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000900 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000901 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
902 if (!TLI.isTypeLegal(IntVT))
903 return false;
904
905 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000906 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000907 if (IntReg == 0)
908 return false;
909
Dan Gohmana6cb6412010-05-11 23:54:07 +0000910 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
911 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000912 UINT64_C(1) << (VT.getSizeInBits()-1),
913 IntVT.getSimpleVT());
914 if (IntResultReg == 0)
915 return false;
916
917 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000918 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000919 if (ResultReg == 0)
920 return false;
921
922 UpdateValueMap(I, ResultReg);
923 return true;
924}
925
Dan Gohman40b189e2008-09-05 18:18:20 +0000926bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000927FastISel::SelectExtractValue(const User *U) {
928 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000929 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000930 return false;
931
Eli Friedman482feb32011-05-16 21:06:17 +0000932 // Make sure we only try to handle extracts with a legal result. But also
933 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000934 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
935 if (!RealVT.isSimple())
936 return false;
937 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000938 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000939 return false;
940
941 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000942 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000943
944 // Get the base result register.
945 unsigned ResultReg;
946 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
947 if (I != FuncInfo.ValueMap.end())
948 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000949 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000950 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000951 else
952 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000953
954 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000955 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000956
957 SmallVector<EVT, 4> AggValueVTs;
958 ComputeValueVTs(TLI, AggTy, AggValueVTs);
959
960 for (unsigned i = 0; i < VTIndex; i++)
961 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
962
963 UpdateValueMap(EVI, ResultReg);
964 return true;
965}
966
967bool
Dan Gohman46510a72010-04-15 01:51:59 +0000968FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000969 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000970 case Instruction::Add:
971 return SelectBinaryOp(I, ISD::ADD);
972 case Instruction::FAdd:
973 return SelectBinaryOp(I, ISD::FADD);
974 case Instruction::Sub:
975 return SelectBinaryOp(I, ISD::SUB);
976 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000977 // FNeg is currently represented in LLVM IR as a special case of FSub.
978 if (BinaryOperator::isFNeg(I))
979 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000980 return SelectBinaryOp(I, ISD::FSUB);
981 case Instruction::Mul:
982 return SelectBinaryOp(I, ISD::MUL);
983 case Instruction::FMul:
984 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000985 case Instruction::SDiv:
986 return SelectBinaryOp(I, ISD::SDIV);
987 case Instruction::UDiv:
988 return SelectBinaryOp(I, ISD::UDIV);
989 case Instruction::FDiv:
990 return SelectBinaryOp(I, ISD::FDIV);
991 case Instruction::SRem:
992 return SelectBinaryOp(I, ISD::SREM);
993 case Instruction::URem:
994 return SelectBinaryOp(I, ISD::UREM);
995 case Instruction::FRem:
996 return SelectBinaryOp(I, ISD::FREM);
997 case Instruction::Shl:
998 return SelectBinaryOp(I, ISD::SHL);
999 case Instruction::LShr:
1000 return SelectBinaryOp(I, ISD::SRL);
1001 case Instruction::AShr:
1002 return SelectBinaryOp(I, ISD::SRA);
1003 case Instruction::And:
1004 return SelectBinaryOp(I, ISD::AND);
1005 case Instruction::Or:
1006 return SelectBinaryOp(I, ISD::OR);
1007 case Instruction::Xor:
1008 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001009
Dan Gohman3df24e62008-09-03 23:12:08 +00001010 case Instruction::GetElementPtr:
1011 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001012
Dan Gohman3df24e62008-09-03 23:12:08 +00001013 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +00001014 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001015
Dan Gohman3df24e62008-09-03 23:12:08 +00001016 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001017 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +00001018 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +00001019 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +00001020 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +00001021 }
Dan Gohman3df24e62008-09-03 23:12:08 +00001022
1023 // Conditional branches are not handed yet.
1024 // Halt "fast" selection and bail.
1025 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001026 }
1027
Dan Gohman087c8502008-09-05 01:08:41 +00001028 case Instruction::Unreachable:
1029 // Nothing to emit.
1030 return true;
1031
Dan Gohman0586d912008-09-10 20:11:02 +00001032 case Instruction::Alloca:
1033 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001034 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001035 return true;
1036
1037 // Dynamic-sized alloca is not handled yet.
1038 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001039
Dan Gohman33134c42008-09-25 17:05:24 +00001040 case Instruction::Call:
1041 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001042
Dan Gohman3df24e62008-09-03 23:12:08 +00001043 case Instruction::BitCast:
1044 return SelectBitCast(I);
1045
1046 case Instruction::FPToSI:
1047 return SelectCast(I, ISD::FP_TO_SINT);
1048 case Instruction::ZExt:
1049 return SelectCast(I, ISD::ZERO_EXTEND);
1050 case Instruction::SExt:
1051 return SelectCast(I, ISD::SIGN_EXTEND);
1052 case Instruction::Trunc:
1053 return SelectCast(I, ISD::TRUNCATE);
1054 case Instruction::SIToFP:
1055 return SelectCast(I, ISD::SINT_TO_FP);
1056
1057 case Instruction::IntToPtr: // Deliberate fall-through.
1058 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001059 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1060 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001061 if (DstVT.bitsGT(SrcVT))
1062 return SelectCast(I, ISD::ZERO_EXTEND);
1063 if (DstVT.bitsLT(SrcVT))
1064 return SelectCast(I, ISD::TRUNCATE);
1065 unsigned Reg = getRegForValue(I->getOperand(0));
1066 if (Reg == 0) return false;
1067 UpdateValueMap(I, Reg);
1068 return true;
1069 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001070
Eli Friedman2586b8f2011-05-16 20:27:46 +00001071 case Instruction::ExtractValue:
1072 return SelectExtractValue(I);
1073
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001074 case Instruction::PHI:
1075 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1076
Dan Gohman3df24e62008-09-03 23:12:08 +00001077 default:
1078 // Unhandled instruction. Halt "fast" selection and bail.
1079 return false;
1080 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001081}
1082
Bob Wilsond49edb72012-08-03 04:06:28 +00001083FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1084 const TargetLibraryInfo *libInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001085 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001086 MRI(FuncInfo.MF->getRegInfo()),
1087 MFI(*FuncInfo.MF->getFrameInfo()),
1088 MCP(*FuncInfo.MF->getConstantPool()),
1089 TM(FuncInfo.MF->getTarget()),
Micah Villmow3574eca2012-10-08 16:38:25 +00001090 TD(*TM.getDataLayout()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001091 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001092 TLI(*TM.getTargetLowering()),
Bob Wilsond49edb72012-08-03 04:06:28 +00001093 TRI(*TM.getRegisterInfo()),
1094 LibInfo(libInfo) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001095}
1096
Dan Gohmane285a742008-08-14 21:51:29 +00001097FastISel::~FastISel() {}
1098
Evan Cheng092e5e72013-02-11 01:27:15 +00001099bool FastISel::FastLowerArguments() {
1100 return false;
1101}
1102
Owen Anderson825b72b2009-08-11 20:47:22 +00001103unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001104 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001105 return 0;
1106}
1107
Owen Anderson825b72b2009-08-11 20:47:22 +00001108unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001109 unsigned,
1110 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001111 return 0;
1112}
1113
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001114unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001115 unsigned,
1116 unsigned /*Op0*/, bool /*Op0IsKill*/,
1117 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001118 return 0;
1119}
1120
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001121unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001122 return 0;
1123}
1124
Owen Anderson825b72b2009-08-11 20:47:22 +00001125unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001126 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001127 return 0;
1128}
1129
Owen Anderson825b72b2009-08-11 20:47:22 +00001130unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001131 unsigned,
1132 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001133 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001134 return 0;
1135}
1136
Owen Anderson825b72b2009-08-11 20:47:22 +00001137unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001138 unsigned,
1139 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001140 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001141 return 0;
1142}
1143
Owen Anderson825b72b2009-08-11 20:47:22 +00001144unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001145 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001146 unsigned /*Op0*/, bool /*Op0IsKill*/,
1147 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001148 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001149 return 0;
1150}
1151
1152/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1153/// to emit an instruction with an immediate operand using FastEmit_ri.
1154/// If that fails, it materializes the immediate into a register and try
1155/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001156unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001157 unsigned Op0, bool Op0IsKill,
1158 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001159 // If this is a multiply by a power of two, emit this as a shift left.
1160 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1161 Opcode = ISD::SHL;
1162 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001163 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1164 // div x, 8 -> srl x, 3
1165 Opcode = ISD::SRL;
1166 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001167 }
Owen Andersond74ea772011-04-22 23:38:06 +00001168
Chris Lattner602fc062011-04-17 20:23:29 +00001169 // Horrible hack (to be removed), check to make sure shift amounts are
1170 // in-range.
1171 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1172 Imm >= VT.getSizeInBits())
1173 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001174
Evan Cheng83785c82008-08-20 22:45:34 +00001175 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001176 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001177 if (ResultReg != 0)
1178 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001179 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001180 if (MaterialReg == 0) {
1181 // This is a bit ugly/slow, but failing here means falling out of
1182 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001183 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001184 VT.getSizeInBits());
1185 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosier7ae3bb82013-03-28 23:04:47 +00001186 assert (MaterialReg != 0 && "Unable to materialize imm.");
1187 if (MaterialReg == 0) return 0;
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001188 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001189 return FastEmit_rr(VT, VT, Opcode,
1190 Op0, Op0IsKill,
1191 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001192}
1193
1194unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1195 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001196}
1197
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001198unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001199 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001200 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001201 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001202
Dan Gohman84023e02010-07-10 09:00:22 +00001203 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001204 return ResultReg;
1205}
1206
1207unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1208 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001209 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001210 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001211 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001212
Evan Cheng5960e4e2008-09-08 08:38:20 +00001213 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1215 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001216 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1218 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1220 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001221 }
1222
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001223 return ResultReg;
1224}
1225
1226unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1227 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001228 unsigned Op0, bool Op0IsKill,
1229 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001230 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001231 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001232
Evan Cheng5960e4e2008-09-08 08:38:20 +00001233 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001234 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001235 .addReg(Op0, Op0IsKill * RegState::Kill)
1236 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001237 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001239 .addReg(Op0, Op0IsKill * RegState::Kill)
1240 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1242 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001243 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001244 return ResultReg;
1245}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001246
Owen Andersond71867a2011-05-05 17:59:04 +00001247unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1248 const TargetRegisterClass *RC,
1249 unsigned Op0, bool Op0IsKill,
1250 unsigned Op1, bool Op1IsKill,
1251 unsigned Op2, bool Op2IsKill) {
1252 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001253 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001254
1255 if (II.getNumDefs() >= 1)
1256 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1257 .addReg(Op0, Op0IsKill * RegState::Kill)
1258 .addReg(Op1, Op1IsKill * RegState::Kill)
1259 .addReg(Op2, Op2IsKill * RegState::Kill);
1260 else {
1261 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1262 .addReg(Op0, Op0IsKill * RegState::Kill)
1263 .addReg(Op1, Op1IsKill * RegState::Kill)
1264 .addReg(Op2, Op2IsKill * RegState::Kill);
1265 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1266 ResultReg).addReg(II.ImplicitDefs[0]);
1267 }
1268 return ResultReg;
1269}
1270
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001271unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1272 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001273 unsigned Op0, bool Op0IsKill,
1274 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001275 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001276 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001277
Evan Cheng5960e4e2008-09-08 08:38:20 +00001278 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001280 .addReg(Op0, Op0IsKill * RegState::Kill)
1281 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001282 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001284 .addReg(Op0, Op0IsKill * RegState::Kill)
1285 .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 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001289 return ResultReg;
1290}
1291
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001292unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1293 const TargetRegisterClass *RC,
1294 unsigned Op0, bool Op0IsKill,
1295 uint64_t Imm1, uint64_t Imm2) {
1296 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001297 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001298
1299 if (II.getNumDefs() >= 1)
1300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1301 .addReg(Op0, Op0IsKill * RegState::Kill)
1302 .addImm(Imm1)
1303 .addImm(Imm2);
1304 else {
1305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1306 .addReg(Op0, Op0IsKill * RegState::Kill)
1307 .addImm(Imm1)
1308 .addImm(Imm2);
1309 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1310 ResultReg).addReg(II.ImplicitDefs[0]);
1311 }
1312 return ResultReg;
1313}
1314
Dan Gohman10df0fa2008-08-27 01:09:54 +00001315unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1316 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001317 unsigned Op0, bool Op0IsKill,
1318 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001319 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001320 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001321
Evan Cheng5960e4e2008-09-08 08:38:20 +00001322 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001323 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001324 .addReg(Op0, Op0IsKill * RegState::Kill)
1325 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001326 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001328 .addReg(Op0, Op0IsKill * RegState::Kill)
1329 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1331 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001332 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001333 return ResultReg;
1334}
1335
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001336unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1337 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001338 unsigned Op0, bool Op0IsKill,
1339 unsigned Op1, bool Op1IsKill,
1340 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001341 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001342 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001343
Evan Cheng5960e4e2008-09-08 08:38:20 +00001344 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001345 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001346 .addReg(Op0, Op0IsKill * RegState::Kill)
1347 .addReg(Op1, Op1IsKill * RegState::Kill)
1348 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001349 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001350 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001351 .addReg(Op0, Op0IsKill * RegState::Kill)
1352 .addReg(Op1, Op1IsKill * RegState::Kill)
1353 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001354 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1355 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001356 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001357 return ResultReg;
1358}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001359
Manman Ren68f25572012-06-01 19:33:18 +00001360unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1361 const TargetRegisterClass *RC,
1362 unsigned Op0, bool Op0IsKill,
1363 unsigned Op1, bool Op1IsKill,
1364 uint64_t Imm1, uint64_t Imm2) {
1365 unsigned ResultReg = createResultReg(RC);
1366 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1367
1368 if (II.getNumDefs() >= 1)
1369 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1370 .addReg(Op0, Op0IsKill * RegState::Kill)
1371 .addReg(Op1, Op1IsKill * RegState::Kill)
1372 .addImm(Imm1).addImm(Imm2);
1373 else {
1374 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1375 .addReg(Op0, Op0IsKill * RegState::Kill)
1376 .addReg(Op1, Op1IsKill * RegState::Kill)
1377 .addImm(Imm1).addImm(Imm2);
1378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1379 ResultReg).addReg(II.ImplicitDefs[0]);
1380 }
1381 return ResultReg;
1382}
1383
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001384unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1385 const TargetRegisterClass *RC,
1386 uint64_t Imm) {
1387 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001388 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001389
Evan Cheng5960e4e2008-09-08 08:38:20 +00001390 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001392 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1395 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001396 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001397 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001398}
Owen Anderson8970f002008-08-27 22:30:02 +00001399
Owen Andersond74ea772011-04-22 23:38:06 +00001400unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1401 const TargetRegisterClass *RC,
1402 uint64_t Imm1, uint64_t Imm2) {
1403 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001404 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001405
1406 if (II.getNumDefs() >= 1)
1407 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1408 .addImm(Imm1).addImm(Imm2);
1409 else {
1410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1411 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1412 ResultReg).addReg(II.ImplicitDefs[0]);
1413 }
1414 return ResultReg;
1415}
1416
Owen Anderson825b72b2009-08-11 20:47:22 +00001417unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001418 unsigned Op0, bool Op0IsKill,
1419 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001420 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001421 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1422 "Cannot yet extract from physregs");
Jakob Stoklund Olesenee0d5d42012-05-20 06:38:37 +00001423 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1424 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohman84023e02010-07-10 09:00:22 +00001425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1426 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001427 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001428 return ResultReg;
1429}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001430
1431/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1432/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001433unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1434 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001435}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001436
1437/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1438/// Emit code to ensure constants are copied into registers when needed.
1439/// Remember the virtual registers that need to be added to the Machine PHI
1440/// nodes as input. We cannot just directly add them, because expansion
1441/// might result in multiple MBB's for one BB. As such, the start of the
1442/// BB might correspond to a different MBB than the end.
1443bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1444 const TerminatorInst *TI = LLVMBB->getTerminator();
1445
1446 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001447 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001448
1449 // Check successor nodes' PHI nodes that expect a constant to be available
1450 // from this block.
1451 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1452 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1453 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001454 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001455
1456 // If this terminator has multiple identical successors (common for
1457 // switches), only handle each succ once.
1458 if (!SuccsHandled.insert(SuccMBB)) continue;
1459
1460 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1461
1462 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1463 // nodes and Machine PHI nodes, but the incoming operands have not been
1464 // emitted yet.
1465 for (BasicBlock::const_iterator I = SuccBB->begin();
1466 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001467
Dan Gohmanf81eca02010-04-22 20:46:50 +00001468 // Ignore dead phi's.
1469 if (PN->use_empty()) continue;
1470
1471 // Only handle legal types. Two interesting things to note here. First,
1472 // by bailing out early, we may leave behind some dead instructions,
1473 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001474 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001475 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001476 // exactly one register for each non-void instruction.
1477 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1478 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier2f2d1d72012-02-04 00:39:19 +00001479 // Handle integer promotions, though, because they're common and easy.
1480 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanf81eca02010-04-22 20:46:50 +00001481 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1482 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001483 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001484 return false;
1485 }
1486 }
1487
1488 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1489
Dan Gohmanfb95f892010-05-07 01:10:20 +00001490 // Set the DebugLoc for the copy. Prefer the location of the operand
1491 // if there is one; use the location of the PHI otherwise.
1492 DL = PN->getDebugLoc();
1493 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1494 DL = Inst->getDebugLoc();
1495
Dan Gohmanf81eca02010-04-22 20:46:50 +00001496 unsigned Reg = getRegForValue(PHIOp);
1497 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001498 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001499 return false;
1500 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001501 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001502 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001503 }
1504 }
1505
1506 return true;
1507}