blob: b4946ec5ee845945f61cad5147f348e2c6a6d8fe [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 Rosieraa5656c2011-11-28 19:59:09 +000065STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
66 "target-independent selector");
67STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
68 "target-specific selector");
Chad Rosierae6f2cb2011-11-29 19:40:47 +000069STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosier053e69a2011-11-16 21:05:28 +000070
Dan Gohman84023e02010-07-10 09:00:22 +000071/// startNewBlock - Set the current block to which generated machine
72/// instructions will be appended, and clear the local CSE map.
73///
74void FastISel::startNewBlock() {
75 LocalValueMap.clear();
76
Ivan Krasin74af88a2011-08-18 22:06:10 +000077 EmitStartPt = 0;
Dan Gohman84023e02010-07-10 09:00:22 +000078
Ivan Krasin74af88a2011-08-18 22:06:10 +000079 // Advance the emit start point past any EH_LABEL instructions.
Dan Gohman84023e02010-07-10 09:00:22 +000080 MachineBasicBlock::iterator
81 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
82 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
Ivan Krasin74af88a2011-08-18 22:06:10 +000083 EmitStartPt = I;
Dan Gohman84023e02010-07-10 09:00:22 +000084 ++I;
85 }
Ivan Krasin74af88a2011-08-18 22:06:10 +000086 LastLocalValue = EmitStartPt;
87}
88
89void FastISel::flushLocalValueMap() {
90 LocalValueMap.clear();
91 LastLocalValue = EmitStartPt;
92 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +000093}
94
Dan Gohmana6cb6412010-05-11 23:54:07 +000095bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000096 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000097 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000098 if (!I)
99 return false;
100
101 // No-op casts are trivially coalesced by fast-isel.
102 if (const CastInst *Cast = dyn_cast<CastInst>(I))
103 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
104 !hasTrivialKill(Cast->getOperand(0)))
105 return false;
106
Chad Rosier22b34cc2011-11-15 23:34:05 +0000107 // GEPs with all zero indices are trivially coalesced by fast-isel.
108 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
109 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
110 return false;
111
Dan Gohman7f0d6952010-05-14 22:53:18 +0000112 // Only instructions with a single use in the same basic block are considered
113 // to have trivial kills.
114 return I->hasOneUse() &&
115 !(I->getOpcode() == Instruction::BitCast ||
116 I->getOpcode() == Instruction::PtrToInt ||
117 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000118 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000119}
120
Dan Gohman46510a72010-04-15 01:51:59 +0000121unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000122 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000123 // Don't handle non-simple values in FastISel.
124 if (!RealVT.isSimple())
125 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000126
127 // Ignore illegal types. We must do this before looking up the value
128 // in ValueMap because Arguments are given virtual registers regardless
129 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000131 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000132 // Handle integer promotions, though, because they're common and easy.
133 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000134 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000135 else
136 return 0;
137 }
138
Dan Gohman104e4ce2008-09-03 23:32:19 +0000139 // Look up the value to see if we already have a register for it. We
140 // cache values defined by Instructions across blocks, and other values
141 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000142 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000143 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
Chris Lattnerfff65b32011-04-17 01:16:47 +0000144 if (I != FuncInfo.ValueMap.end())
145 return I->second;
146
Dan Gohman104e4ce2008-09-03 23:32:19 +0000147 unsigned Reg = LocalValueMap[V];
148 if (Reg != 0)
149 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000150
Dan Gohman97c94b82010-05-06 00:02:14 +0000151 // In bottom-up mode, just create the virtual register which will be used
152 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000153 if (isa<Instruction>(V) &&
154 (!isa<AllocaInst>(V) ||
155 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
156 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000157
Dan Gohmana10b8492010-07-14 01:07:44 +0000158 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000159
160 // Materialize the value in a register. Emit any instructions in the
161 // local value area.
162 Reg = materializeRegForValue(V, VT);
163
164 leaveLocalValueArea(SaveInsertPt);
165
166 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000167}
168
Eric Christopher44a2c342010-08-17 01:30:33 +0000169/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000170/// called when the value isn't already available in a register and must
171/// be materialized with new instructions.
172unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
173 unsigned Reg = 0;
174
Dan Gohman46510a72010-04-15 01:51:59 +0000175 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000176 if (CI->getValue().getActiveBits() <= 64)
177 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000178 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000179 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000180 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000181 // Translate this as an integer zero so that it can be
182 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000183 Reg =
184 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000185 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000186 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000187 Reg = TargetMaterializeFloatZero(CF);
188 } else {
189 // Try to emit the constant directly.
190 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
191 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000192
193 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000194 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000195 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000196 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000197
198 uint64_t x[2];
199 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000200 bool isExact;
201 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
202 APFloat::rmTowardZero, &isExact);
203 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000204 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000205
Owen Andersone922c022009-07-22 00:24:57 +0000206 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000207 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000208 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000209 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
210 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000211 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000212 }
Dan Gohman46510a72010-04-15 01:51:59 +0000213 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000214 if (!SelectOperator(Op, Op->getOpcode()))
215 if (!isa<Instruction>(Op) ||
216 !TargetSelectInstruction(cast<Instruction>(Op)))
217 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000218 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000219 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000220 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000221 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
222 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000223 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000224
Dan Gohmandceffe62008-09-25 01:28:51 +0000225 // If target-independent code couldn't handle the value, give target-specific
226 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000227 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000228 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000229
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000230 // Don't cache constant materializations in the general ValueMap.
231 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000232 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000233 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000234 LastLocalValue = MRI.getVRegDef(Reg);
235 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000236 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000237}
238
Dan Gohman46510a72010-04-15 01:51:59 +0000239unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000240 // Look up the value to see if we already have a register for it. We
241 // cache values defined by Instructions across blocks, and other values
242 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000243 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000244 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
245 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000246 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000247 return LocalValueMap[V];
248}
249
Owen Andersoncc54e762008-08-30 00:38:46 +0000250/// UpdateValueMap - Update the value map to include the new mapping for this
251/// instruction, or insert an extra copy to get the result in a previous
252/// determined register.
253/// NOTE: This is only necessary because we might select a block that uses
254/// a value before we select the block that defines the value. It might be
255/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000256void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000257 if (!isa<Instruction>(I)) {
258 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000259 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000260 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000261
Dan Gohmana4160c32010-07-07 16:29:44 +0000262 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000263 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000264 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000265 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000266 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000267 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000268 for (unsigned i = 0; i < NumRegs; i++)
269 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000270
271 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000272 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000273}
274
Dan Gohmana6cb6412010-05-11 23:54:07 +0000275std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000276 unsigned IdxN = getRegForValue(Idx);
277 if (IdxN == 0)
278 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000279 return std::pair<unsigned, bool>(0, false);
280
281 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000282
283 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000284 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000285 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000286 if (IdxVT.bitsLT(PtrVT)) {
287 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
288 IdxN, IdxNIsKill);
289 IdxNIsKill = true;
290 }
291 else if (IdxVT.bitsGT(PtrVT)) {
292 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
293 IdxN, IdxNIsKill);
294 IdxNIsKill = true;
295 }
296 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000297}
298
Dan Gohman84023e02010-07-10 09:00:22 +0000299void FastISel::recomputeInsertPt() {
300 if (getLastLocalValue()) {
301 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000302 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000303 ++FuncInfo.InsertPt;
304 } else
305 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
306
307 // Now skip past any EH_LABELs, which must remain at the beginning.
308 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
309 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
310 ++FuncInfo.InsertPt;
311}
312
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000313void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
314 MachineBasicBlock::iterator E) {
315 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
316 while (I != E) {
317 MachineInstr *Dead = &*I;
318 ++I;
319 Dead->eraseFromParent();
320 ++NumFastIselDead;
321 }
322 recomputeInsertPt();
323}
324
Dan Gohmana10b8492010-07-14 01:07:44 +0000325FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000326 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Dan Gohman163f78e2010-07-14 22:01:31 +0000327 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000328 recomputeInsertPt();
Dan Gohmana10b8492010-07-14 01:07:44 +0000329 DL = DebugLoc();
Dan Gohman163f78e2010-07-14 22:01:31 +0000330 SavePoint SP = { OldInsertPt, OldDL };
Dan Gohmana10b8492010-07-14 01:07:44 +0000331 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000332}
333
Dan Gohmana10b8492010-07-14 01:07:44 +0000334void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000335 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
336 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
337
338 // Restore the previous insert position.
Dan Gohmana10b8492010-07-14 01:07:44 +0000339 FuncInfo.InsertPt = OldInsertPt.InsertPt;
340 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000341}
342
Dan Gohmanbdedd442008-08-20 00:11:48 +0000343/// SelectBinaryOp - Select and emit code for a binary operator instruction,
344/// which has an opcode which directly corresponds to the given ISD opcode.
345///
Dan Gohman46510a72010-04-15 01:51:59 +0000346bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000347 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000349 // Unhandled type. Halt "fast" selection and bail.
350 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000351
Dan Gohmanb71fea22008-08-26 20:52:40 +0000352 // We only handle legal types. For example, on x86-32 the instruction
353 // selector contains all of the 64-bit instructions from x86-64,
354 // under the assumption that i64 won't be used if the target doesn't
355 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000356 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000358 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000360 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
361 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000362 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000363 else
364 return false;
365 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000366
Chris Lattnerfff65b32011-04-17 01:16:47 +0000367 // Check if the first operand is a constant, and handle it as "ri". At -O0,
368 // we don't have anything that canonicalizes operand order.
369 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
370 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
371 unsigned Op1 = getRegForValue(I->getOperand(1));
372 if (Op1 == 0) return false;
373
374 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000375
Chris Lattner602fc062011-04-17 20:23:29 +0000376 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
377 Op1IsKill, CI->getZExtValue(),
378 VT.getSimpleVT());
379 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000380
Chris Lattner602fc062011-04-17 20:23:29 +0000381 // We successfully emitted code for the given LLVM Instruction.
382 UpdateValueMap(I, ResultReg);
383 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000384 }
Owen Andersond74ea772011-04-22 23:38:06 +0000385
386
Dan Gohman3df24e62008-09-03 23:12:08 +0000387 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000388 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000389 return false;
390
Dan Gohmana6cb6412010-05-11 23:54:07 +0000391 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
392
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000393 // Check if the second operand is a constant and handle it appropriately.
394 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000395 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000396
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000397 // Transform "sdiv exact X, 8" -> "sra X, 3".
398 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
399 cast<BinaryOperator>(I)->isExact() &&
400 isPowerOf2_64(Imm)) {
401 Imm = Log2_64(Imm);
402 ISDOpcode = ISD::SRA;
403 }
Owen Andersond74ea772011-04-22 23:38:06 +0000404
Chris Lattner602fc062011-04-17 20:23:29 +0000405 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
406 Op0IsKill, Imm, VT.getSimpleVT());
407 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000408
Chris Lattner602fc062011-04-17 20:23:29 +0000409 // We successfully emitted code for the given LLVM Instruction.
410 UpdateValueMap(I, ResultReg);
411 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000412 }
413
Dan Gohman10df0fa2008-08-27 01:09:54 +0000414 // Check if the second operand is a constant float.
415 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000416 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000417 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000418 if (ResultReg != 0) {
419 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000420 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000421 return true;
422 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000423 }
424
Dan Gohman3df24e62008-09-03 23:12:08 +0000425 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000426 if (Op1 == 0)
427 // Unhandled operand. Halt "fast" selection and bail.
428 return false;
429
Dan Gohmana6cb6412010-05-11 23:54:07 +0000430 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
431
Dan Gohmanad368ac2008-08-27 18:10:19 +0000432 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000433 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000434 ISDOpcode,
435 Op0, Op0IsKill,
436 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000437 if (ResultReg == 0)
438 // Target-specific code wasn't able to find a machine opcode for
439 // the given ISD opcode and type. Halt "fast" selection and bail.
440 return false;
441
Dan Gohman8014e862008-08-20 00:23:20 +0000442 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000443 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000444 return true;
445}
446
Dan Gohman46510a72010-04-15 01:51:59 +0000447bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000448 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000449 if (N == 0)
450 // Unhandled operand. Halt "fast" selection and bail.
451 return false;
452
Dan Gohmana6cb6412010-05-11 23:54:07 +0000453 bool NIsKill = hasTrivialKill(I->getOperand(0));
454
Chad Rosier478b06c2011-11-17 07:15:58 +0000455 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
456 // into a single N = N + TotalOffset.
457 uint64_t TotalOffs = 0;
458 // FIXME: What's a good SWAG number for MaxOffs?
459 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000460 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000462 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
463 E = I->op_end(); OI != E; ++OI) {
464 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000465 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000466 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
467 if (Field) {
468 // N = N + Offset
Chad Rosier478b06c2011-11-17 07:15:58 +0000469 TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
470 if (TotalOffs >= MaxOffs) {
471 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
472 if (N == 0)
473 // Unhandled operand. Halt "fast" selection and bail.
474 return false;
475 NIsKill = true;
476 TotalOffs = 0;
477 }
Evan Cheng83785c82008-08-20 22:45:34 +0000478 }
479 Ty = StTy->getElementType(Field);
480 } else {
481 Ty = cast<SequentialType>(Ty)->getElementType();
482
483 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000484 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000485 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000486 // N = N + Offset
487 TotalOffs +=
Duncan Sands777d2302009-05-09 07:06:46 +0000488 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000489 if (TotalOffs >= MaxOffs) {
490 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
491 if (N == 0)
492 // Unhandled operand. Halt "fast" selection and bail.
493 return false;
494 NIsKill = true;
495 TotalOffs = 0;
496 }
497 continue;
498 }
499 if (TotalOffs) {
500 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000501 if (N == 0)
502 // Unhandled operand. Halt "fast" selection and bail.
503 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000504 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000505 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000506 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000507
Evan Cheng83785c82008-08-20 22:45:34 +0000508 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000509 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000510 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
511 unsigned IdxN = Pair.first;
512 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000513 if (IdxN == 0)
514 // Unhandled operand. Halt "fast" selection and bail.
515 return false;
516
Dan Gohman80bc6e22008-08-26 20:57:08 +0000517 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000518 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000519 if (IdxN == 0)
520 // Unhandled operand. Halt "fast" selection and bail.
521 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000522 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000523 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000524 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000525 if (N == 0)
526 // Unhandled operand. Halt "fast" selection and bail.
527 return false;
528 }
529 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000530 if (TotalOffs) {
531 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
532 if (N == 0)
533 // Unhandled operand. Halt "fast" selection and bail.
534 return false;
535 }
Evan Cheng83785c82008-08-20 22:45:34 +0000536
537 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000538 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000539 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000540}
541
Dan Gohman46510a72010-04-15 01:51:59 +0000542bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000543 const CallInst *Call = cast<CallInst>(I);
544
545 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000546 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000547 // Don't attempt to handle constraints.
548 if (!IA->getConstraintString().empty())
549 return false;
550
551 unsigned ExtraInfo = 0;
552 if (IA->hasSideEffects())
553 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
554 if (IA->isAlignStack())
555 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
556
557 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
558 TII.get(TargetOpcode::INLINEASM))
559 .addExternalSymbol(IA->getAsmString().c_str())
560 .addImm(ExtraInfo);
561 return true;
562 }
563
564 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000565 if (!F) return false;
566
Dan Gohman4183e312010-04-13 17:07:06 +0000567 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000568 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000569 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000570 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000571 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000572 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000573 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000574 return true;
575
Dan Gohman46510a72010-04-15 01:51:59 +0000576 const Value *Address = DI->getAddress();
Devang Patel6fe75aa2010-09-14 20:29:31 +0000577 if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
Dale Johannesendc918562010-02-06 02:26:02 +0000578 return true;
Devang Patel6fe75aa2010-09-14 20:29:31 +0000579
580 unsigned Reg = 0;
581 unsigned Offset = 0;
582 if (const Argument *Arg = dyn_cast<Argument>(Address)) {
Devang Patel9aee3352011-09-08 22:59:09 +0000583 // Some arguments' frame index is recorded during argument lowering.
584 Offset = FuncInfo.getArgumentFrameIndex(Arg);
585 if (Offset)
586 Reg = TRI.getFrameRegister(*FuncInfo.MF);
Devang Patel4bafda92010-09-10 20:32:09 +0000587 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000588 if (!Reg)
589 Reg = getRegForValue(Address);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000590
Devang Patel6fe75aa2010-09-14 20:29:31 +0000591 if (Reg)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Devang Patel6fe75aa2010-09-14 20:29:31 +0000593 TII.get(TargetOpcode::DBG_VALUE))
594 .addReg(Reg, RegState::Debug).addImm(Offset)
595 .addMetadata(DI->getVariable());
Dan Gohman33134c42008-09-25 17:05:24 +0000596 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000597 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000598 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000599 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000600 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000601 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000602 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000603 if (!V) {
604 // Currently the optimizer can produce this; insert an undef to
605 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000606 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
607 .addReg(0U).addImm(DI->getOffset())
608 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000609 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000610 if (CI->getBitWidth() > 64)
611 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
612 .addCImm(CI).addImm(DI->getOffset())
613 .addMetadata(DI->getVariable());
614 else
615 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
616 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
617 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000618 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000619 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
620 .addFPImm(CF).addImm(DI->getOffset())
621 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000622 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
624 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
625 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000626 } else {
627 // We can't yet handle anything else here because it would require
628 // generating code, thus altering codegen because of debug info.
Devang Patelafeaae72010-12-06 22:39:26 +0000629 DEBUG(dbgs() << "Dropping debug info for " << DI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000630 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000631 return true;
632 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000633 case Intrinsic::eh_exception: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000634 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000635 if (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)!=TargetLowering::Expand)
636 break;
Owen Andersond74ea772011-04-22 23:38:06 +0000637
Chris Lattner832e4942011-04-19 05:52:03 +0000638 assert(FuncInfo.MBB->isLandingPad() &&
639 "Call to eh.exception not in landing pad!");
640 unsigned Reg = TLI.getExceptionAddressRegister();
641 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
642 unsigned ResultReg = createResultReg(RC);
643 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
644 ResultReg).addReg(Reg);
Dan Gohmana61e73b2011-04-26 17:18:34 +0000645 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000646 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000647 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000648 case Intrinsic::eh_selector: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000649 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000650 if (TLI.getOperationAction(ISD::EHSELECTION, VT) != TargetLowering::Expand)
651 break;
652 if (FuncInfo.MBB->isLandingPad())
Dan Gohmana61e73b2011-04-26 17:18:34 +0000653 AddCatchInfo(*Call, &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattner832e4942011-04-19 05:52:03 +0000654 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000655#ifndef NDEBUG
Dan Gohmana61e73b2011-04-26 17:18:34 +0000656 FuncInfo.CatchInfoLost.insert(Call);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000657#endif
Chris Lattner832e4942011-04-19 05:52:03 +0000658 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Chris Lattnered3a8062010-04-05 06:05:26 +0000659 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattner832e4942011-04-19 05:52:03 +0000660 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000661 }
Chris Lattner832e4942011-04-19 05:52:03 +0000662
663 unsigned Reg = TLI.getExceptionSelectorRegister();
664 EVT SrcVT = TLI.getPointerTy();
665 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
666 unsigned ResultReg = createResultReg(RC);
667 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
668 ResultReg).addReg(Reg);
669
Dan Gohmana61e73b2011-04-26 17:18:34 +0000670 bool ResultRegIsKill = hasTrivialKill(Call);
Chris Lattner832e4942011-04-19 05:52:03 +0000671
672 // Cast the register to the type of the selector.
673 if (SrcVT.bitsGT(MVT::i32))
674 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
675 ResultReg, ResultRegIsKill);
676 else if (SrcVT.bitsLT(MVT::i32))
677 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
678 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
679 if (ResultReg == 0)
680 // Unhandled operand. Halt "fast" selection and bail.
681 return false;
682
Dan Gohmana61e73b2011-04-26 17:18:34 +0000683 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000684
685 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000686 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000687 case Intrinsic::objectsize: {
688 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
689 unsigned long long Res = CI->isZero() ? -1ULL : 0;
690 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
691 unsigned ResultReg = getRegForValue(ResCI);
692 if (ResultReg == 0)
693 return false;
694 UpdateValueMap(Call, ResultReg);
695 return true;
696 }
Dan Gohman33134c42008-09-25 17:05:24 +0000697 }
Dan Gohman4183e312010-04-13 17:07:06 +0000698
Ivan Krasin74af88a2011-08-18 22:06:10 +0000699 // Usually, it does not make sense to initialize a value,
700 // make an unrelated function call and use the value, because
701 // it tends to be spilled on the stack. So, we move the pointer
702 // to the last local value to the beginning of the block, so that
703 // all the values which have already been materialized,
704 // appear after the call. It also makes sense to skip intrinsics
705 // since they tend to be inlined.
706 if (!isa<IntrinsicInst>(F))
707 flushLocalValueMap();
708
Dan Gohman4183e312010-04-13 17:07:06 +0000709 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000710 return false;
711}
712
Dan Gohman46510a72010-04-15 01:51:59 +0000713bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000714 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
715 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000716
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
718 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000719 // Unhandled type. Halt "fast" selection and bail.
720 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000721
Eli Friedman76927d732011-05-25 23:49:02 +0000722 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000723 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000724 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000725
Eli Friedman76927d732011-05-25 23:49:02 +0000726 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000727 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000728 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000729
Dan Gohman3df24e62008-09-03 23:12:08 +0000730 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000731 if (!InputReg)
732 // Unhandled operand. Halt "fast" selection and bail.
733 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000734
Dan Gohmana6cb6412010-05-11 23:54:07 +0000735 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
736
Owen Andersond0533c92008-08-26 23:46:32 +0000737 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
738 DstVT.getSimpleVT(),
739 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000740 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000741 if (!ResultReg)
742 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000743
Dan Gohman3df24e62008-09-03 23:12:08 +0000744 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000745 return true;
746}
747
Dan Gohman46510a72010-04-15 01:51:59 +0000748bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000749 // If the bitcast doesn't change the type, just use the operand value.
750 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000751 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000752 if (Reg == 0)
753 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000754 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000755 return true;
756 }
757
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000759 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
760 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000761
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
763 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000764 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
765 // Unhandled type. Halt "fast" selection and bail.
766 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767
Dan Gohman3df24e62008-09-03 23:12:08 +0000768 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000769 if (Op0 == 0)
770 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000771 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000772
773 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000774
Dan Gohmanad368ac2008-08-27 18:10:19 +0000775 // First, try to perform the bitcast by inserting a reg-reg copy.
776 unsigned ResultReg = 0;
777 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
778 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
779 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000780 // Don't attempt a cross-class copy. It will likely fail.
781 if (SrcClass == DstClass) {
782 ResultReg = createResultReg(DstClass);
783 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
784 ResultReg).addReg(Op0);
785 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000786 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000787
788 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000789 if (!ResultReg)
790 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000791 ISD::BITCAST, Op0, Op0IsKill);
792
Dan Gohmanad368ac2008-08-27 18:10:19 +0000793 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000794 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000795
Dan Gohman3df24e62008-09-03 23:12:08 +0000796 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000797 return true;
798}
799
Dan Gohman3df24e62008-09-03 23:12:08 +0000800bool
Dan Gohman46510a72010-04-15 01:51:59 +0000801FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000802 // Just before the terminator instruction, insert instructions to
803 // feed PHI nodes in successor blocks.
804 if (isa<TerminatorInst>(I))
805 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
806 return false;
807
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000808 DL = I->getDebugLoc();
809
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000810 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
811
Dan Gohman6e3ff372009-12-05 01:27:58 +0000812 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000813 if (SelectOperator(I, I->getOpcode())) {
Chad Rosier053e69a2011-11-16 21:05:28 +0000814 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000815 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000816 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000817 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000818 // Remove dead code. However, ignore call instructions since we've flushed
819 // the local value map and recomputed the insert point.
820 if (!isa<CallInst>(I)) {
821 recomputeInsertPt();
822 if (SavedInsertPt != FuncInfo.InsertPt)
823 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
824 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000825
826 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000827 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000828 if (TargetSelectInstruction(I)) {
Chad Rosier053e69a2011-11-16 21:05:28 +0000829 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000830 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000831 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000832 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000833 // Check for dead code and remove as necessary.
834 recomputeInsertPt();
835 if (SavedInsertPt != FuncInfo.InsertPt)
836 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000837
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000838 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000839 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000840}
841
Dan Gohmand98d6202008-10-02 22:15:21 +0000842/// FastEmitBranch - Emit an unconditional branch to the given block,
843/// unless it is the immediate (fall-through) successor, and update
844/// the CFG.
845void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000846FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohman84023e02010-07-10 09:00:22 +0000847 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000848 // The unconditional fall-through case, which needs no instructions.
849 } else {
850 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000851 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
852 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000853 }
Dan Gohman84023e02010-07-10 09:00:22 +0000854 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000855}
856
Dan Gohman3d45a852009-09-03 22:53:57 +0000857/// SelectFNeg - Emit an FNeg operation.
858///
859bool
Dan Gohman46510a72010-04-15 01:51:59 +0000860FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000861 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
862 if (OpReg == 0) return false;
863
Dan Gohmana6cb6412010-05-11 23:54:07 +0000864 bool OpRegIsKill = hasTrivialKill(I);
865
Dan Gohman4a215a12009-09-11 00:36:43 +0000866 // If the target has ISD::FNEG, use it.
867 EVT VT = TLI.getValueType(I->getType());
868 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000869 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000870 if (ResultReg != 0) {
871 UpdateValueMap(I, ResultReg);
872 return true;
873 }
874
Dan Gohman5e5abb72009-09-11 00:34:46 +0000875 // Bitcast the value to integer, twiddle the sign bit with xor,
876 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000877 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000878 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
879 if (!TLI.isTypeLegal(IntVT))
880 return false;
881
882 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000884 if (IntReg == 0)
885 return false;
886
Dan Gohmana6cb6412010-05-11 23:54:07 +0000887 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
888 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000889 UINT64_C(1) << (VT.getSizeInBits()-1),
890 IntVT.getSimpleVT());
891 if (IntResultReg == 0)
892 return false;
893
894 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000895 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000896 if (ResultReg == 0)
897 return false;
898
899 UpdateValueMap(I, ResultReg);
900 return true;
901}
902
Dan Gohman40b189e2008-09-05 18:18:20 +0000903bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000904FastISel::SelectExtractValue(const User *U) {
905 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000906 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000907 return false;
908
Eli Friedman482feb32011-05-16 21:06:17 +0000909 // Make sure we only try to handle extracts with a legal result. But also
910 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000911 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
912 if (!RealVT.isSimple())
913 return false;
914 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000915 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000916 return false;
917
918 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000919 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000920
921 // Get the base result register.
922 unsigned ResultReg;
923 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
924 if (I != FuncInfo.ValueMap.end())
925 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000926 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000927 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000928 else
929 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000930
931 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000932 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000933
934 SmallVector<EVT, 4> AggValueVTs;
935 ComputeValueVTs(TLI, AggTy, AggValueVTs);
936
937 for (unsigned i = 0; i < VTIndex; i++)
938 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
939
940 UpdateValueMap(EVI, ResultReg);
941 return true;
942}
943
944bool
Dan Gohman46510a72010-04-15 01:51:59 +0000945FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000946 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000947 case Instruction::Add:
948 return SelectBinaryOp(I, ISD::ADD);
949 case Instruction::FAdd:
950 return SelectBinaryOp(I, ISD::FADD);
951 case Instruction::Sub:
952 return SelectBinaryOp(I, ISD::SUB);
953 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000954 // FNeg is currently represented in LLVM IR as a special case of FSub.
955 if (BinaryOperator::isFNeg(I))
956 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000957 return SelectBinaryOp(I, ISD::FSUB);
958 case Instruction::Mul:
959 return SelectBinaryOp(I, ISD::MUL);
960 case Instruction::FMul:
961 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000962 case Instruction::SDiv:
963 return SelectBinaryOp(I, ISD::SDIV);
964 case Instruction::UDiv:
965 return SelectBinaryOp(I, ISD::UDIV);
966 case Instruction::FDiv:
967 return SelectBinaryOp(I, ISD::FDIV);
968 case Instruction::SRem:
969 return SelectBinaryOp(I, ISD::SREM);
970 case Instruction::URem:
971 return SelectBinaryOp(I, ISD::UREM);
972 case Instruction::FRem:
973 return SelectBinaryOp(I, ISD::FREM);
974 case Instruction::Shl:
975 return SelectBinaryOp(I, ISD::SHL);
976 case Instruction::LShr:
977 return SelectBinaryOp(I, ISD::SRL);
978 case Instruction::AShr:
979 return SelectBinaryOp(I, ISD::SRA);
980 case Instruction::And:
981 return SelectBinaryOp(I, ISD::AND);
982 case Instruction::Or:
983 return SelectBinaryOp(I, ISD::OR);
984 case Instruction::Xor:
985 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000986
Dan Gohman3df24e62008-09-03 23:12:08 +0000987 case Instruction::GetElementPtr:
988 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000989
Dan Gohman3df24e62008-09-03 23:12:08 +0000990 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000991 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000992
Dan Gohman3df24e62008-09-03 23:12:08 +0000993 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000994 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000995 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000996 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000997 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000998 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000999
1000 // Conditional branches are not handed yet.
1001 // Halt "fast" selection and bail.
1002 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001003 }
1004
Dan Gohman087c8502008-09-05 01:08:41 +00001005 case Instruction::Unreachable:
1006 // Nothing to emit.
1007 return true;
1008
Dan Gohman0586d912008-09-10 20:11:02 +00001009 case Instruction::Alloca:
1010 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001011 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001012 return true;
1013
1014 // Dynamic-sized alloca is not handled yet.
1015 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001016
Dan Gohman33134c42008-09-25 17:05:24 +00001017 case Instruction::Call:
1018 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001019
Dan Gohman3df24e62008-09-03 23:12:08 +00001020 case Instruction::BitCast:
1021 return SelectBitCast(I);
1022
1023 case Instruction::FPToSI:
1024 return SelectCast(I, ISD::FP_TO_SINT);
1025 case Instruction::ZExt:
1026 return SelectCast(I, ISD::ZERO_EXTEND);
1027 case Instruction::SExt:
1028 return SelectCast(I, ISD::SIGN_EXTEND);
1029 case Instruction::Trunc:
1030 return SelectCast(I, ISD::TRUNCATE);
1031 case Instruction::SIToFP:
1032 return SelectCast(I, ISD::SINT_TO_FP);
1033
1034 case Instruction::IntToPtr: // Deliberate fall-through.
1035 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001036 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1037 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001038 if (DstVT.bitsGT(SrcVT))
1039 return SelectCast(I, ISD::ZERO_EXTEND);
1040 if (DstVT.bitsLT(SrcVT))
1041 return SelectCast(I, ISD::TRUNCATE);
1042 unsigned Reg = getRegForValue(I->getOperand(0));
1043 if (Reg == 0) return false;
1044 UpdateValueMap(I, Reg);
1045 return true;
1046 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001047
Eli Friedman2586b8f2011-05-16 20:27:46 +00001048 case Instruction::ExtractValue:
1049 return SelectExtractValue(I);
1050
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001051 case Instruction::PHI:
1052 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1053
Dan Gohman3df24e62008-09-03 23:12:08 +00001054 default:
1055 // Unhandled instruction. Halt "fast" selection and bail.
1056 return false;
1057 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001058}
1059
Dan Gohmana4160c32010-07-07 16:29:44 +00001060FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001061 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001062 MRI(FuncInfo.MF->getRegInfo()),
1063 MFI(*FuncInfo.MF->getFrameInfo()),
1064 MCP(*FuncInfo.MF->getConstantPool()),
1065 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001066 TD(*TM.getTargetData()),
1067 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001068 TLI(*TM.getTargetLowering()),
Dan Gohman84023e02010-07-10 09:00:22 +00001069 TRI(*TM.getRegisterInfo()) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001070}
1071
Dan Gohmane285a742008-08-14 21:51:29 +00001072FastISel::~FastISel() {}
1073
Owen Anderson825b72b2009-08-11 20:47:22 +00001074unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001075 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001076 return 0;
1077}
1078
Owen Anderson825b72b2009-08-11 20:47:22 +00001079unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001080 unsigned,
1081 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001082 return 0;
1083}
1084
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001085unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001086 unsigned,
1087 unsigned /*Op0*/, bool /*Op0IsKill*/,
1088 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001089 return 0;
1090}
1091
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001092unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001093 return 0;
1094}
1095
Owen Anderson825b72b2009-08-11 20:47:22 +00001096unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001097 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001098 return 0;
1099}
1100
Owen Anderson825b72b2009-08-11 20:47:22 +00001101unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001102 unsigned,
1103 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001104 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001105 return 0;
1106}
1107
Owen Anderson825b72b2009-08-11 20:47:22 +00001108unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001109 unsigned,
1110 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001111 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001112 return 0;
1113}
1114
Owen Anderson825b72b2009-08-11 20:47:22 +00001115unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001116 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001117 unsigned /*Op0*/, bool /*Op0IsKill*/,
1118 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001119 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001120 return 0;
1121}
1122
1123/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1124/// to emit an instruction with an immediate operand using FastEmit_ri.
1125/// If that fails, it materializes the immediate into a register and try
1126/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001127unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001128 unsigned Op0, bool Op0IsKill,
1129 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001130 // If this is a multiply by a power of two, emit this as a shift left.
1131 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1132 Opcode = ISD::SHL;
1133 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001134 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1135 // div x, 8 -> srl x, 3
1136 Opcode = ISD::SRL;
1137 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001138 }
Owen Andersond74ea772011-04-22 23:38:06 +00001139
Chris Lattner602fc062011-04-17 20:23:29 +00001140 // Horrible hack (to be removed), check to make sure shift amounts are
1141 // in-range.
1142 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1143 Imm >= VT.getSizeInBits())
1144 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001145
Evan Cheng83785c82008-08-20 22:45:34 +00001146 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001147 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001148 if (ResultReg != 0)
1149 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001150 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001151 if (MaterialReg == 0) {
1152 // This is a bit ugly/slow, but failing here means falling out of
1153 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001154 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001155 VT.getSizeInBits());
1156 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
1157 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001158 return FastEmit_rr(VT, VT, Opcode,
1159 Op0, Op0IsKill,
1160 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001161}
1162
1163unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1164 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001165}
1166
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001167unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001168 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001169 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001170 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001171
Dan Gohman84023e02010-07-10 09:00:22 +00001172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001173 return ResultReg;
1174}
1175
1176unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1177 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001178 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001179 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001180 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001181
Evan Cheng5960e4e2008-09-08 08:38:20 +00001182 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001183 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1184 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001185 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001186 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1187 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001188 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1189 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001190 }
1191
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001192 return ResultReg;
1193}
1194
1195unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1196 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001197 unsigned Op0, bool Op0IsKill,
1198 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001199 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001200 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001201
Evan Cheng5960e4e2008-09-08 08:38:20 +00001202 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001203 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001204 .addReg(Op0, Op0IsKill * RegState::Kill)
1205 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001206 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001207 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001208 .addReg(Op0, Op0IsKill * RegState::Kill)
1209 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001210 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1211 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001212 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001213 return ResultReg;
1214}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001215
Owen Andersond71867a2011-05-05 17:59:04 +00001216unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1217 const TargetRegisterClass *RC,
1218 unsigned Op0, bool Op0IsKill,
1219 unsigned Op1, bool Op1IsKill,
1220 unsigned Op2, bool Op2IsKill) {
1221 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001222 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001223
1224 if (II.getNumDefs() >= 1)
1225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1226 .addReg(Op0, Op0IsKill * RegState::Kill)
1227 .addReg(Op1, Op1IsKill * RegState::Kill)
1228 .addReg(Op2, Op2IsKill * RegState::Kill);
1229 else {
1230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1231 .addReg(Op0, Op0IsKill * RegState::Kill)
1232 .addReg(Op1, Op1IsKill * RegState::Kill)
1233 .addReg(Op2, Op2IsKill * RegState::Kill);
1234 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1235 ResultReg).addReg(II.ImplicitDefs[0]);
1236 }
1237 return ResultReg;
1238}
1239
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001240unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1241 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001242 unsigned Op0, bool Op0IsKill,
1243 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001244 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001245 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001246
Evan Cheng5960e4e2008-09-08 08:38:20 +00001247 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001249 .addReg(Op0, Op0IsKill * RegState::Kill)
1250 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001251 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001252 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001253 .addReg(Op0, Op0IsKill * RegState::Kill)
1254 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001255 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1256 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001257 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001258 return ResultReg;
1259}
1260
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001261unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1262 const TargetRegisterClass *RC,
1263 unsigned Op0, bool Op0IsKill,
1264 uint64_t Imm1, uint64_t Imm2) {
1265 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001266 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001267
1268 if (II.getNumDefs() >= 1)
1269 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1270 .addReg(Op0, Op0IsKill * RegState::Kill)
1271 .addImm(Imm1)
1272 .addImm(Imm2);
1273 else {
1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1275 .addReg(Op0, Op0IsKill * RegState::Kill)
1276 .addImm(Imm1)
1277 .addImm(Imm2);
1278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1279 ResultReg).addReg(II.ImplicitDefs[0]);
1280 }
1281 return ResultReg;
1282}
1283
Dan Gohman10df0fa2008-08-27 01:09:54 +00001284unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1285 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001286 unsigned Op0, bool Op0IsKill,
1287 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001288 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001289 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001290
Evan Cheng5960e4e2008-09-08 08:38:20 +00001291 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001293 .addReg(Op0, Op0IsKill * RegState::Kill)
1294 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001295 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001296 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001297 .addReg(Op0, Op0IsKill * RegState::Kill)
1298 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1300 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001301 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001302 return ResultReg;
1303}
1304
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001305unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1306 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001307 unsigned Op0, bool Op0IsKill,
1308 unsigned Op1, bool Op1IsKill,
1309 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001310 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001311 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001312
Evan Cheng5960e4e2008-09-08 08:38:20 +00001313 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001314 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001315 .addReg(Op0, Op0IsKill * RegState::Kill)
1316 .addReg(Op1, Op1IsKill * RegState::Kill)
1317 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001318 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001319 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001320 .addReg(Op0, Op0IsKill * RegState::Kill)
1321 .addReg(Op1, Op1IsKill * RegState::Kill)
1322 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001323 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1324 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001325 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001326 return ResultReg;
1327}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001328
1329unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1330 const TargetRegisterClass *RC,
1331 uint64_t Imm) {
1332 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001333 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001334
Evan Cheng5960e4e2008-09-08 08:38:20 +00001335 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001337 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001338 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001339 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1340 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001341 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001342 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001343}
Owen Anderson8970f002008-08-27 22:30:02 +00001344
Owen Andersond74ea772011-04-22 23:38:06 +00001345unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1346 const TargetRegisterClass *RC,
1347 uint64_t Imm1, uint64_t Imm2) {
1348 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001349 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001350
1351 if (II.getNumDefs() >= 1)
1352 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1353 .addImm(Imm1).addImm(Imm2);
1354 else {
1355 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1356 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1357 ResultReg).addReg(II.ImplicitDefs[0]);
1358 }
1359 return ResultReg;
1360}
1361
Owen Anderson825b72b2009-08-11 20:47:22 +00001362unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001363 unsigned Op0, bool Op0IsKill,
1364 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001365 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001366 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1367 "Cannot yet extract from physregs");
Dan Gohman84023e02010-07-10 09:00:22 +00001368 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1369 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001370 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001371 return ResultReg;
1372}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001373
1374/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1375/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001376unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1377 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001378}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001379
1380/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1381/// Emit code to ensure constants are copied into registers when needed.
1382/// Remember the virtual registers that need to be added to the Machine PHI
1383/// nodes as input. We cannot just directly add them, because expansion
1384/// might result in multiple MBB's for one BB. As such, the start of the
1385/// BB might correspond to a different MBB than the end.
1386bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1387 const TerminatorInst *TI = LLVMBB->getTerminator();
1388
1389 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001390 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001391
1392 // Check successor nodes' PHI nodes that expect a constant to be available
1393 // from this block.
1394 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1395 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1396 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001397 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001398
1399 // If this terminator has multiple identical successors (common for
1400 // switches), only handle each succ once.
1401 if (!SuccsHandled.insert(SuccMBB)) continue;
1402
1403 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1404
1405 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1406 // nodes and Machine PHI nodes, but the incoming operands have not been
1407 // emitted yet.
1408 for (BasicBlock::const_iterator I = SuccBB->begin();
1409 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001410
Dan Gohmanf81eca02010-04-22 20:46:50 +00001411 // Ignore dead phi's.
1412 if (PN->use_empty()) continue;
1413
1414 // Only handle legal types. Two interesting things to note here. First,
1415 // by bailing out early, we may leave behind some dead instructions,
1416 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001417 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001418 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001419 // exactly one register for each non-void instruction.
1420 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1421 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1422 // Promote MVT::i1.
1423 if (VT == MVT::i1)
1424 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1425 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001426 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001427 return false;
1428 }
1429 }
1430
1431 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1432
Dan Gohmanfb95f892010-05-07 01:10:20 +00001433 // Set the DebugLoc for the copy. Prefer the location of the operand
1434 // if there is one; use the location of the PHI otherwise.
1435 DL = PN->getDebugLoc();
1436 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1437 DL = Inst->getDebugLoc();
1438
Dan Gohmanf81eca02010-04-22 20:46:50 +00001439 unsigned Reg = getRegForValue(PHIOp);
1440 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001441 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001442 return false;
1443 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001444 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001445 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001446 }
1447 }
1448
1449 return true;
1450}