blob: 82f9f76baba954b03a260fbe8af7c5b25da6d1d4 [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"
David Blaikie6d9dbd52013-06-16 20:34:15 +000044#include "llvm/ADT/Optional.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000045#include "llvm/ADT/Statistic.h"
46#include "llvm/Analysis/Loads.h"
47#include "llvm/CodeGen/Analysis.h"
48#include "llvm/CodeGen/FunctionLoweringInfo.h"
49#include "llvm/CodeGen/MachineInstrBuilder.h"
50#include "llvm/CodeGen/MachineModuleInfo.h"
51#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000052#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000053#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/Function.h"
55#include "llvm/IR/GlobalVariable.h"
56#include "llvm/IR/Instructions.h"
57#include "llvm/IR/IntrinsicInst.h"
58#include "llvm/IR/Operator.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000059#include "llvm/Support/Debug.h"
60#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000061#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsond49edb72012-08-03 04:06:28 +000062#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000063#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000064#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000065using namespace llvm;
66
Chad Rosieraa5656c2011-11-28 19:59:09 +000067STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
68 "target-independent selector");
69STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
70 "target-specific selector");
Chad Rosierae6f2cb2011-11-29 19:40:47 +000071STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosier053e69a2011-11-16 21:05:28 +000072
Dan Gohman84023e02010-07-10 09:00:22 +000073/// startNewBlock - Set the current block to which generated machine
74/// instructions will be appended, and clear the local CSE map.
75///
76void FastISel::startNewBlock() {
77 LocalValueMap.clear();
78
Ivan Krasin74af88a2011-08-18 22:06:10 +000079 EmitStartPt = 0;
Dan Gohman84023e02010-07-10 09:00:22 +000080
Ivan Krasin74af88a2011-08-18 22:06:10 +000081 // Advance the emit start point past any EH_LABEL instructions.
Dan Gohman84023e02010-07-10 09:00:22 +000082 MachineBasicBlock::iterator
83 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
84 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
Ivan Krasin74af88a2011-08-18 22:06:10 +000085 EmitStartPt = I;
Dan Gohman84023e02010-07-10 09:00:22 +000086 ++I;
87 }
Ivan Krasin74af88a2011-08-18 22:06:10 +000088 LastLocalValue = EmitStartPt;
89}
90
Evan Cheng092e5e72013-02-11 01:27:15 +000091bool FastISel::LowerArguments() {
92 if (!FuncInfo.CanLowerReturn)
93 // Fallback to SDISel argument lowering code to deal with sret pointer
94 // parameter.
95 return false;
96
97 if (!FastLowerArguments())
98 return false;
99
100 // Enter non-dead arguments into ValueMap for uses in non-entry BBs.
101 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
102 E = FuncInfo.Fn->arg_end(); I != E; ++I) {
103 if (!I->use_empty()) {
104 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
105 assert(VI != LocalValueMap.end() && "Missed an argument?");
106 FuncInfo.ValueMap[I] = VI->second;
107 }
108 }
109 return true;
110}
111
Ivan Krasin74af88a2011-08-18 22:06:10 +0000112void FastISel::flushLocalValueMap() {
113 LocalValueMap.clear();
114 LastLocalValue = EmitStartPt;
115 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +0000116}
117
Dan Gohmana6cb6412010-05-11 23:54:07 +0000118bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +0000119 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000120 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +0000121 if (!I)
122 return false;
123
124 // No-op casts are trivially coalesced by fast-isel.
125 if (const CastInst *Cast = dyn_cast<CastInst>(I))
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000126 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
127 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman7f0d6952010-05-14 22:53:18 +0000128 return false;
129
Chad Rosier22b34cc2011-11-15 23:34:05 +0000130 // GEPs with all zero indices are trivially coalesced by fast-isel.
131 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
132 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
133 return false;
134
Dan Gohman7f0d6952010-05-14 22:53:18 +0000135 // Only instructions with a single use in the same basic block are considered
136 // to have trivial kills.
137 return I->hasOneUse() &&
138 !(I->getOpcode() == Instruction::BitCast ||
139 I->getOpcode() == Instruction::PtrToInt ||
140 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000141 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000142}
143
Dan Gohman46510a72010-04-15 01:51:59 +0000144unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000145 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000146 // Don't handle non-simple values in FastISel.
147 if (!RealVT.isSimple())
148 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000149
150 // Ignore illegal types. We must do this before looking up the value
151 // in ValueMap because Arguments are given virtual registers regardless
152 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000154 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000155 // Handle integer promotions, though, because they're common and easy.
156 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000157 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000158 else
159 return 0;
160 }
161
Eric Christopher4e270272012-03-20 01:07:47 +0000162 // Look up the value to see if we already have a register for it.
163 unsigned Reg = lookUpRegForValue(V);
Dan Gohman104e4ce2008-09-03 23:32:19 +0000164 if (Reg != 0)
165 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000166
Dan Gohman97c94b82010-05-06 00:02:14 +0000167 // In bottom-up mode, just create the virtual register which will be used
168 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000169 if (isa<Instruction>(V) &&
170 (!isa<AllocaInst>(V) ||
171 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
172 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000173
Eric Christopher76ad43c2012-10-03 08:10:01 +0000174 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000175
176 // Materialize the value in a register. Emit any instructions in the
177 // local value area.
178 Reg = materializeRegForValue(V, VT);
179
Eric Christopher76ad43c2012-10-03 08:10:01 +0000180 leaveLocalValueArea(SaveInsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000181
182 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000183}
184
Eric Christopher44a2c342010-08-17 01:30:33 +0000185/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000186/// called when the value isn't already available in a register and must
187/// be materialized with new instructions.
188unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
189 unsigned Reg = 0;
190
Dan Gohman46510a72010-04-15 01:51:59 +0000191 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000192 if (CI->getValue().getActiveBits() <= 64)
193 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000194 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000195 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000196 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000197 // Translate this as an integer zero so that it can be
198 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000199 Reg =
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000200 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000201 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000202 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000203 Reg = TargetMaterializeFloatZero(CF);
204 } else {
205 // Try to emit the constant directly.
206 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
207 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000208
209 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000210 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000211 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000212 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000213
214 uint64_t x[2];
215 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000216 bool isExact;
217 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopherc415af22012-03-20 01:07:56 +0000218 APFloat::rmTowardZero, &isExact);
Dale Johannesen23a98552008-10-09 23:00:39 +0000219 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000220 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000221
Owen Andersone922c022009-07-22 00:24:57 +0000222 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000223 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000224 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000225 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
226 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000227 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000228 }
Dan Gohman46510a72010-04-15 01:51:59 +0000229 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000230 if (!SelectOperator(Op, Op->getOpcode()))
231 if (!isa<Instruction>(Op) ||
232 !TargetSelectInstruction(cast<Instruction>(Op)))
233 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000234 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000235 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000236 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
238 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000239 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000240
Dan Gohmandceffe62008-09-25 01:28:51 +0000241 // If target-independent code couldn't handle the value, give target-specific
242 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000243 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000244 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000245
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000246 // Don't cache constant materializations in the general ValueMap.
247 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000248 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000249 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000250 LastLocalValue = MRI.getVRegDef(Reg);
251 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000252 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000253}
254
Dan Gohman46510a72010-04-15 01:51:59 +0000255unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000256 // Look up the value to see if we already have a register for it. We
257 // cache values defined by Instructions across blocks, and other values
258 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000259 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000260 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
261 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000262 return I->second;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000263 return LocalValueMap[V];
Evan Cheng59fbc802008-09-09 01:26:59 +0000264}
265
Owen Andersoncc54e762008-08-30 00:38:46 +0000266/// UpdateValueMap - Update the value map to include the new mapping for this
267/// instruction, or insert an extra copy to get the result in a previous
268/// determined register.
269/// NOTE: This is only necessary because we might select a block that uses
270/// a value before we select the block that defines the value. It might be
271/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000272void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000273 if (!isa<Instruction>(I)) {
274 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000275 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000276 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000277
Dan Gohmana4160c32010-07-07 16:29:44 +0000278 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000279 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000280 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000281 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000282 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000283 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000284 for (unsigned i = 0; i < NumRegs; i++)
285 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000286
287 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000288 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000289}
290
Dan Gohmana6cb6412010-05-11 23:54:07 +0000291std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000292 unsigned IdxN = getRegForValue(Idx);
293 if (IdxN == 0)
294 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000295 return std::pair<unsigned, bool>(0, false);
296
297 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000298
299 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000300 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000301 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000302 if (IdxVT.bitsLT(PtrVT)) {
303 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
304 IdxN, IdxNIsKill);
305 IdxNIsKill = true;
306 }
307 else if (IdxVT.bitsGT(PtrVT)) {
308 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
309 IdxN, IdxNIsKill);
310 IdxNIsKill = true;
311 }
312 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000313}
314
Dan Gohman84023e02010-07-10 09:00:22 +0000315void FastISel::recomputeInsertPt() {
316 if (getLastLocalValue()) {
317 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000318 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000319 ++FuncInfo.InsertPt;
320 } else
321 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
322
323 // Now skip past any EH_LABELs, which must remain at the beginning.
324 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
325 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
326 ++FuncInfo.InsertPt;
327}
328
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000329void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
330 MachineBasicBlock::iterator E) {
331 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
332 while (I != E) {
333 MachineInstr *Dead = &*I;
334 ++I;
335 Dead->eraseFromParent();
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000336 ++NumFastIselDead;
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000337 }
338 recomputeInsertPt();
339}
340
Eric Christopher76ad43c2012-10-03 08:10:01 +0000341FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000342 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000343 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000344 recomputeInsertPt();
Eric Christopher76ad43c2012-10-03 08:10:01 +0000345 DL = DebugLoc();
346 SavePoint SP = { OldInsertPt, OldDL };
347 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000348}
349
Eric Christopher76ad43c2012-10-03 08:10:01 +0000350void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000351 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
352 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
353
354 // Restore the previous insert position.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000355 FuncInfo.InsertPt = OldInsertPt.InsertPt;
356 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000357}
358
Dan Gohmanbdedd442008-08-20 00:11:48 +0000359/// SelectBinaryOp - Select and emit code for a binary operator instruction,
360/// which has an opcode which directly corresponds to the given ISD opcode.
361///
Dan Gohman46510a72010-04-15 01:51:59 +0000362bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000363 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000365 // Unhandled type. Halt "fast" selection and bail.
366 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000367
Dan Gohmanb71fea22008-08-26 20:52:40 +0000368 // We only handle legal types. For example, on x86-32 the instruction
369 // selector contains all of the 64-bit instructions from x86-64,
370 // under the assumption that i64 won't be used if the target doesn't
371 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000372 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000374 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000376 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
377 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000378 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000379 else
380 return false;
381 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000382
Chris Lattnerfff65b32011-04-17 01:16:47 +0000383 // Check if the first operand is a constant, and handle it as "ri". At -O0,
384 // we don't have anything that canonicalizes operand order.
385 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
386 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
387 unsigned Op1 = getRegForValue(I->getOperand(1));
388 if (Op1 == 0) return false;
389
390 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000391
Chris Lattner602fc062011-04-17 20:23:29 +0000392 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
393 Op1IsKill, CI->getZExtValue(),
394 VT.getSimpleVT());
395 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000396
Chris Lattner602fc062011-04-17 20:23:29 +0000397 // We successfully emitted code for the given LLVM Instruction.
398 UpdateValueMap(I, ResultReg);
399 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000400 }
Owen Andersond74ea772011-04-22 23:38:06 +0000401
402
Dan Gohman3df24e62008-09-03 23:12:08 +0000403 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000404 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000405 return false;
406
Dan Gohmana6cb6412010-05-11 23:54:07 +0000407 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
408
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000409 // Check if the second operand is a constant and handle it appropriately.
410 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000411 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000412
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000413 // Transform "sdiv exact X, 8" -> "sra X, 3".
414 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
415 cast<BinaryOperator>(I)->isExact() &&
416 isPowerOf2_64(Imm)) {
417 Imm = Log2_64(Imm);
418 ISDOpcode = ISD::SRA;
419 }
Owen Andersond74ea772011-04-22 23:38:06 +0000420
Chad Rosier544b9b42012-03-22 00:21:17 +0000421 // Transform "urem x, pow2" -> "and x, pow2-1".
422 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
423 isPowerOf2_64(Imm)) {
424 --Imm;
425 ISDOpcode = ISD::AND;
426 }
427
Chris Lattner602fc062011-04-17 20:23:29 +0000428 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
429 Op0IsKill, Imm, VT.getSimpleVT());
430 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000431
Chris Lattner602fc062011-04-17 20:23:29 +0000432 // We successfully emitted code for the given LLVM Instruction.
433 UpdateValueMap(I, ResultReg);
434 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000435 }
436
Dan Gohman10df0fa2008-08-27 01:09:54 +0000437 // Check if the second operand is a constant float.
438 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000439 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000440 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000441 if (ResultReg != 0) {
442 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000443 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000444 return true;
445 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000446 }
447
Dan Gohman3df24e62008-09-03 23:12:08 +0000448 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000449 if (Op1 == 0)
450 // Unhandled operand. Halt "fast" selection and bail.
451 return false;
452
Dan Gohmana6cb6412010-05-11 23:54:07 +0000453 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
454
Dan Gohmanad368ac2008-08-27 18:10:19 +0000455 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000456 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000457 ISDOpcode,
458 Op0, Op0IsKill,
459 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000460 if (ResultReg == 0)
461 // Target-specific code wasn't able to find a machine opcode for
462 // the given ISD opcode and type. Halt "fast" selection and bail.
463 return false;
464
Dan Gohman8014e862008-08-20 00:23:20 +0000465 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000466 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000467 return true;
468}
469
Dan Gohman46510a72010-04-15 01:51:59 +0000470bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000471 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000472 if (N == 0)
473 // Unhandled operand. Halt "fast" selection and bail.
474 return false;
475
Dan Gohmana6cb6412010-05-11 23:54:07 +0000476 bool NIsKill = hasTrivialKill(I->getOperand(0));
477
Chad Rosier478b06c2011-11-17 07:15:58 +0000478 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
479 // into a single N = N + TotalOffset.
480 uint64_t TotalOffs = 0;
481 // FIXME: What's a good SWAG number for MaxOffs?
482 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000483 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000485 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
486 E = I->op_end(); OI != E; ++OI) {
487 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000488 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000489 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
490 if (Field) {
491 // N = N + Offset
Chad Rosier478b06c2011-11-17 07:15:58 +0000492 TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
493 if (TotalOffs >= MaxOffs) {
494 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
495 if (N == 0)
496 // Unhandled operand. Halt "fast" selection and bail.
497 return false;
498 NIsKill = true;
499 TotalOffs = 0;
500 }
Evan Cheng83785c82008-08-20 22:45:34 +0000501 }
502 Ty = StTy->getElementType(Field);
503 } else {
504 Ty = cast<SequentialType>(Ty)->getElementType();
505
506 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000507 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000508 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000509 // N = N + Offset
Chad Rosier6016a4a2012-07-06 17:44:22 +0000510 TotalOffs +=
Duncan Sands777d2302009-05-09 07:06:46 +0000511 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000512 if (TotalOffs >= MaxOffs) {
513 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
514 if (N == 0)
515 // Unhandled operand. Halt "fast" selection and bail.
516 return false;
517 NIsKill = true;
518 TotalOffs = 0;
519 }
520 continue;
521 }
522 if (TotalOffs) {
523 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000524 if (N == 0)
525 // Unhandled operand. Halt "fast" selection and bail.
526 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000527 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000528 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000529 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000530
Evan Cheng83785c82008-08-20 22:45:34 +0000531 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000532 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000533 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
534 unsigned IdxN = Pair.first;
535 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000536 if (IdxN == 0)
537 // Unhandled operand. Halt "fast" selection and bail.
538 return false;
539
Dan Gohman80bc6e22008-08-26 20:57:08 +0000540 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000541 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000542 if (IdxN == 0)
543 // Unhandled operand. Halt "fast" selection and bail.
544 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000545 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000546 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000547 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000548 if (N == 0)
549 // Unhandled operand. Halt "fast" selection and bail.
550 return false;
551 }
552 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000553 if (TotalOffs) {
554 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
555 if (N == 0)
556 // Unhandled operand. Halt "fast" selection and bail.
557 return false;
558 }
Evan Cheng83785c82008-08-20 22:45:34 +0000559
560 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000561 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000562 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000563}
564
Dan Gohman46510a72010-04-15 01:51:59 +0000565bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000566 const CallInst *Call = cast<CallInst>(I);
567
568 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000569 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000570 // Don't attempt to handle constraints.
571 if (!IA->getConstraintString().empty())
572 return false;
573
574 unsigned ExtraInfo = 0;
575 if (IA->hasSideEffects())
576 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
577 if (IA->isAlignStack())
578 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
579
580 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
581 TII.get(TargetOpcode::INLINEASM))
582 .addExternalSymbol(IA->getAsmString().c_str())
583 .addImm(ExtraInfo);
584 return true;
585 }
586
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000587 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
588 ComputeUsesVAFloatArgument(*Call, &MMI);
589
Dan Gohmana61e73b2011-04-26 17:18:34 +0000590 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000591 if (!F) return false;
592
Dan Gohman4183e312010-04-13 17:07:06 +0000593 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000594 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000595 default: break;
Chad Rosieraefd36b2012-05-11 23:21:01 +0000596 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000597 case Intrinsic::lifetime_start:
598 case Intrinsic::lifetime_end:
Chad Rosierfd065bb2012-07-06 17:33:39 +0000599 // The donothing intrinsic does, well, nothing.
600 case Intrinsic::donothing:
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000601 return true;
Chad Rosierfd065bb2012-07-06 17:33:39 +0000602
Bill Wendling92c1e122009-02-13 02:16:35 +0000603 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000604 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000605 if (!DIVariable(DI->getVariable()).Verify() ||
Eric Christopherbb54d212012-03-15 21:33:44 +0000606 !FuncInfo.MF->getMMI().hasDebugInfo()) {
607 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel7e1e31f2009-07-02 22:43:26 +0000608 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000609 }
Devang Patel7e1e31f2009-07-02 22:43:26 +0000610
Dan Gohman46510a72010-04-15 01:51:59 +0000611 const Value *Address = DI->getAddress();
Eric Christopherccaea7d2012-03-15 21:33:47 +0000612 if (!Address || isa<UndefValue>(Address)) {
Eric Christopherbb54d212012-03-15 21:33:44 +0000613 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendc918562010-02-06 02:26:02 +0000614 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000615 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000616
David Blaikie6d9dbd52013-06-16 20:34:15 +0000617 Optional<MachineOperand> Op;
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.
David Blaikie6d9dbd52013-06-16 20:34:15 +0000620 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
621 Op = MachineOperand::CreateFI(FI);
622 if (!Op)
623 if (unsigned Reg = lookUpRegForValue(Address))
624 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher8c5293c2012-03-20 01:07:58 +0000625
Bill Wendling84364a42012-03-30 00:02:55 +0000626 // If we have a VLA that has a "use" in a metadata node that's then used
627 // here but it has no other uses, then we have a problem. E.g.,
628 //
629 // int foo (const int *x) {
630 // char a[*x];
631 // return 0;
632 // }
633 //
634 // If we assign 'a' a vreg and fast isel later on has to use the selection
635 // DAG isel, it will want to copy the value to the vreg. However, there are
636 // no uses, which goes counter to what selection DAG isel expects.
David Blaikie6d9dbd52013-06-16 20:34:15 +0000637 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher8c5293c2012-03-20 01:07:58 +0000638 (!isa<AllocaInst>(Address) ||
639 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie6d9dbd52013-06-16 20:34:15 +0000640 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
641 false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000642
David Blaikie6d9dbd52013-06-16 20:34:15 +0000643 if (Op && Op->isReg())
644 Op->setIsDebug(true);
645
646 if (Op)
Adrian Prantl86a87d92013-04-30 22:35:14 +0000647 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
David Blaikie6d9dbd52013-06-16 20:34:15 +0000648 TII.get(TargetOpcode::DBG_VALUE)).addOperand(*Op).addImm(0)
649 .addMetadata(DI->getVariable());
Adrian Prantl86a87d92013-04-30 22:35:14 +0000650 else
Eric Christopher4476bae2012-03-20 01:07:53 +0000651 // We can't yet handle anything else here because it would require
652 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000653 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dan Gohman33134c42008-09-25 17:05:24 +0000654 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000655 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000656 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000657 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000658 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000659 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000660 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000661 if (!V) {
662 // Currently the optimizer can produce this; insert an undef to
663 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000664 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
665 .addReg(0U).addImm(DI->getOffset())
666 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000667 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000668 if (CI->getBitWidth() > 64)
669 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
670 .addCImm(CI).addImm(DI->getOffset())
671 .addMetadata(DI->getVariable());
Chad Rosier6016a4a2012-07-06 17:44:22 +0000672 else
Devang Patel8594d422011-06-24 20:46:11 +0000673 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
674 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
675 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000676 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000677 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
678 .addFPImm(CF).addImm(DI->getOffset())
679 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000680 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantl86a87d92013-04-30 22:35:14 +0000681 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
682 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
683 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000684 } else {
685 // We can't yet handle anything else here because it would require
686 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000687 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000688 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000689 return true;
690 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000691 case Intrinsic::objectsize: {
692 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
693 unsigned long long Res = CI->isZero() ? -1ULL : 0;
694 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
695 unsigned ResultReg = getRegForValue(ResCI);
696 if (ResultReg == 0)
697 return false;
698 UpdateValueMap(Call, ResultReg);
699 return true;
700 }
Chad Rosier33947b42013-03-07 20:42:17 +0000701 case Intrinsic::expect: {
Chad Rosier4fde76d2013-03-07 21:38:33 +0000702 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky33cdfe92013-03-11 21:44:37 +0000703 if (ResultReg == 0)
704 return false;
Chad Rosier4fde76d2013-03-07 21:38:33 +0000705 UpdateValueMap(Call, ResultReg);
706 return true;
Chad Rosier33947b42013-03-07 20:42:17 +0000707 }
Dan Gohman33134c42008-09-25 17:05:24 +0000708 }
Dan Gohman4183e312010-04-13 17:07:06 +0000709
Ivan Krasin74af88a2011-08-18 22:06:10 +0000710 // Usually, it does not make sense to initialize a value,
711 // make an unrelated function call and use the value, because
712 // it tends to be spilled on the stack. So, we move the pointer
713 // to the last local value to the beginning of the block, so that
714 // all the values which have already been materialized,
715 // appear after the call. It also makes sense to skip intrinsics
716 // since they tend to be inlined.
Pete Cooperb704ffb2013-02-22 01:50:38 +0000717 if (!isa<IntrinsicInst>(Call))
Ivan Krasin74af88a2011-08-18 22:06:10 +0000718 flushLocalValueMap();
719
Dan Gohman4183e312010-04-13 17:07:06 +0000720 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000721 return false;
722}
723
Dan Gohman46510a72010-04-15 01:51:59 +0000724bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000725 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
726 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
729 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000730 // Unhandled type. Halt "fast" selection and bail.
731 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000732
Eli Friedman76927d732011-05-25 23:49:02 +0000733 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000734 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000735 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000736
Eli Friedman76927d732011-05-25 23:49:02 +0000737 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000738 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000739 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000740
Dan Gohman3df24e62008-09-03 23:12:08 +0000741 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000742 if (!InputReg)
743 // Unhandled operand. Halt "fast" selection and bail.
744 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000745
Dan Gohmana6cb6412010-05-11 23:54:07 +0000746 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
747
Owen Andersond0533c92008-08-26 23:46:32 +0000748 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
749 DstVT.getSimpleVT(),
750 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000751 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000752 if (!ResultReg)
753 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000754
Dan Gohman3df24e62008-09-03 23:12:08 +0000755 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000756 return true;
757}
758
Dan Gohman46510a72010-04-15 01:51:59 +0000759bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000760 // If the bitcast doesn't change the type, just use the operand value.
761 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000762 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000763 if (Reg == 0)
764 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000765 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000766 return true;
767 }
768
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000769 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000770 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
771 EVT DstEVT = TLI.getValueType(I->getType());
772 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
773 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000774 // Unhandled type. Halt "fast" selection and bail.
775 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000776
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000777 MVT SrcVT = SrcEVT.getSimpleVT();
778 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman3df24e62008-09-03 23:12:08 +0000779 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000780 if (Op0 == 0)
781 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000782 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000783
784 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000785
Dan Gohmanad368ac2008-08-27 18:10:19 +0000786 // First, try to perform the bitcast by inserting a reg-reg copy.
787 unsigned ResultReg = 0;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000788 if (SrcVT == DstVT) {
Craig Topper44d23822012-02-22 05:59:10 +0000789 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
790 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000791 // Don't attempt a cross-class copy. It will likely fail.
792 if (SrcClass == DstClass) {
793 ResultReg = createResultReg(DstClass);
794 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
795 ResultReg).addReg(Op0);
796 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000797 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000798
799 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000800 if (!ResultReg)
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000801 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000802
Dan Gohmanad368ac2008-08-27 18:10:19 +0000803 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000804 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000805
Dan Gohman3df24e62008-09-03 23:12:08 +0000806 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000807 return true;
808}
809
Dan Gohman3df24e62008-09-03 23:12:08 +0000810bool
Dan Gohman46510a72010-04-15 01:51:59 +0000811FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000812 // Just before the terminator instruction, insert instructions to
813 // feed PHI nodes in successor blocks.
814 if (isa<TerminatorInst>(I))
815 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
816 return false;
817
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000818 DL = I->getDebugLoc();
819
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000820 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
821
Bob Wilson982dc842012-08-03 21:26:24 +0000822 // As a special case, don't handle calls to builtin library functions that
823 // may be translated directly to target instructions.
Bob Wilsond49edb72012-08-03 04:06:28 +0000824 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
825 const Function *F = Call->getCalledFunction();
826 LibFunc::Func Func;
827 if (F && !F->hasLocalLinkage() && F->hasName() &&
828 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson982dc842012-08-03 21:26:24 +0000829 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilsond49edb72012-08-03 04:06:28 +0000830 return false;
831 }
832
Dan Gohman6e3ff372009-12-05 01:27:58 +0000833 // First, try doing target-independent selection.
Michael Ilseman7dbd34b2013-02-27 19:54:00 +0000834 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000835 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000836 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000837 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000838 }
Chad Rosier6016a4a2012-07-06 17:44:22 +0000839 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000840 // the local value map and recomputed the insert point.
841 if (!isa<CallInst>(I)) {
842 recomputeInsertPt();
843 if (SavedInsertPt != FuncInfo.InsertPt)
844 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
845 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000846
847 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000848 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000849 if (TargetSelectInstruction(I)) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000850 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000851 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000852 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000853 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000854 // Check for dead code and remove as necessary.
855 recomputeInsertPt();
856 if (SavedInsertPt != FuncInfo.InsertPt)
857 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000858
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000859 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000860 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000861}
862
Dan Gohmand98d6202008-10-02 22:15:21 +0000863/// FastEmitBranch - Emit an unconditional branch to the given block,
864/// unless it is the immediate (fall-through) successor, and update
865/// the CFG.
866void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000867FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Eric Christopher18112d82012-04-10 18:18:10 +0000868
Evan Cheng092e5e72013-02-11 01:27:15 +0000869 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
870 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christopher18112d82012-04-10 18:18:10 +0000871 // For more accurate line information if this is the only instruction
872 // in the block then emit it, otherwise we have the unconditional
873 // fall-through case, which needs no instructions.
Dan Gohmand98d6202008-10-02 22:15:21 +0000874 } else {
875 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000876 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
877 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000878 }
Dan Gohman84023e02010-07-10 09:00:22 +0000879 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000880}
881
Dan Gohman3d45a852009-09-03 22:53:57 +0000882/// SelectFNeg - Emit an FNeg operation.
883///
884bool
Dan Gohman46510a72010-04-15 01:51:59 +0000885FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000886 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
887 if (OpReg == 0) return false;
888
Dan Gohmana6cb6412010-05-11 23:54:07 +0000889 bool OpRegIsKill = hasTrivialKill(I);
890
Dan Gohman4a215a12009-09-11 00:36:43 +0000891 // If the target has ISD::FNEG, use it.
892 EVT VT = TLI.getValueType(I->getType());
893 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000894 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000895 if (ResultReg != 0) {
896 UpdateValueMap(I, ResultReg);
897 return true;
898 }
899
Dan Gohman5e5abb72009-09-11 00:34:46 +0000900 // Bitcast the value to integer, twiddle the sign bit with xor,
901 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000902 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000903 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
904 if (!TLI.isTypeLegal(IntVT))
905 return false;
906
907 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000908 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000909 if (IntReg == 0)
910 return false;
911
Dan Gohmana6cb6412010-05-11 23:54:07 +0000912 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
913 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000914 UINT64_C(1) << (VT.getSizeInBits()-1),
915 IntVT.getSimpleVT());
916 if (IntResultReg == 0)
917 return false;
918
919 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000920 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000921 if (ResultReg == 0)
922 return false;
923
924 UpdateValueMap(I, ResultReg);
925 return true;
926}
927
Dan Gohman40b189e2008-09-05 18:18:20 +0000928bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000929FastISel::SelectExtractValue(const User *U) {
930 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000931 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000932 return false;
933
Eli Friedman482feb32011-05-16 21:06:17 +0000934 // Make sure we only try to handle extracts with a legal result. But also
935 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000936 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
937 if (!RealVT.isSimple())
938 return false;
939 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000940 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000941 return false;
942
943 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000944 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000945
946 // Get the base result register.
947 unsigned ResultReg;
948 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
949 if (I != FuncInfo.ValueMap.end())
950 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000951 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000952 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000953 else
954 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000955
956 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000957 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000958
959 SmallVector<EVT, 4> AggValueVTs;
960 ComputeValueVTs(TLI, AggTy, AggValueVTs);
961
962 for (unsigned i = 0; i < VTIndex; i++)
963 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
964
965 UpdateValueMap(EVI, ResultReg);
966 return true;
967}
968
969bool
Dan Gohman46510a72010-04-15 01:51:59 +0000970FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000971 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000972 case Instruction::Add:
973 return SelectBinaryOp(I, ISD::ADD);
974 case Instruction::FAdd:
975 return SelectBinaryOp(I, ISD::FADD);
976 case Instruction::Sub:
977 return SelectBinaryOp(I, ISD::SUB);
978 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000979 // FNeg is currently represented in LLVM IR as a special case of FSub.
980 if (BinaryOperator::isFNeg(I))
981 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000982 return SelectBinaryOp(I, ISD::FSUB);
983 case Instruction::Mul:
984 return SelectBinaryOp(I, ISD::MUL);
985 case Instruction::FMul:
986 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000987 case Instruction::SDiv:
988 return SelectBinaryOp(I, ISD::SDIV);
989 case Instruction::UDiv:
990 return SelectBinaryOp(I, ISD::UDIV);
991 case Instruction::FDiv:
992 return SelectBinaryOp(I, ISD::FDIV);
993 case Instruction::SRem:
994 return SelectBinaryOp(I, ISD::SREM);
995 case Instruction::URem:
996 return SelectBinaryOp(I, ISD::UREM);
997 case Instruction::FRem:
998 return SelectBinaryOp(I, ISD::FREM);
999 case Instruction::Shl:
1000 return SelectBinaryOp(I, ISD::SHL);
1001 case Instruction::LShr:
1002 return SelectBinaryOp(I, ISD::SRL);
1003 case Instruction::AShr:
1004 return SelectBinaryOp(I, ISD::SRA);
1005 case Instruction::And:
1006 return SelectBinaryOp(I, ISD::AND);
1007 case Instruction::Or:
1008 return SelectBinaryOp(I, ISD::OR);
1009 case Instruction::Xor:
1010 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001011
Dan Gohman3df24e62008-09-03 23:12:08 +00001012 case Instruction::GetElementPtr:
1013 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001014
Dan Gohman3df24e62008-09-03 23:12:08 +00001015 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +00001016 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001017
Dan Gohman3df24e62008-09-03 23:12:08 +00001018 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001019 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +00001020 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +00001021 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +00001022 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +00001023 }
Dan Gohman3df24e62008-09-03 23:12:08 +00001024
1025 // Conditional branches are not handed yet.
1026 // Halt "fast" selection and bail.
1027 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001028 }
1029
Dan Gohman087c8502008-09-05 01:08:41 +00001030 case Instruction::Unreachable:
1031 // Nothing to emit.
1032 return true;
1033
Dan Gohman0586d912008-09-10 20:11:02 +00001034 case Instruction::Alloca:
1035 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001036 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001037 return true;
1038
1039 // Dynamic-sized alloca is not handled yet.
1040 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001041
Dan Gohman33134c42008-09-25 17:05:24 +00001042 case Instruction::Call:
1043 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001044
Dan Gohman3df24e62008-09-03 23:12:08 +00001045 case Instruction::BitCast:
1046 return SelectBitCast(I);
1047
1048 case Instruction::FPToSI:
1049 return SelectCast(I, ISD::FP_TO_SINT);
1050 case Instruction::ZExt:
1051 return SelectCast(I, ISD::ZERO_EXTEND);
1052 case Instruction::SExt:
1053 return SelectCast(I, ISD::SIGN_EXTEND);
1054 case Instruction::Trunc:
1055 return SelectCast(I, ISD::TRUNCATE);
1056 case Instruction::SIToFP:
1057 return SelectCast(I, ISD::SINT_TO_FP);
1058
1059 case Instruction::IntToPtr: // Deliberate fall-through.
1060 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001061 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1062 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001063 if (DstVT.bitsGT(SrcVT))
1064 return SelectCast(I, ISD::ZERO_EXTEND);
1065 if (DstVT.bitsLT(SrcVT))
1066 return SelectCast(I, ISD::TRUNCATE);
1067 unsigned Reg = getRegForValue(I->getOperand(0));
1068 if (Reg == 0) return false;
1069 UpdateValueMap(I, Reg);
1070 return true;
1071 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001072
Eli Friedman2586b8f2011-05-16 20:27:46 +00001073 case Instruction::ExtractValue:
1074 return SelectExtractValue(I);
1075
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001076 case Instruction::PHI:
1077 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1078
Dan Gohman3df24e62008-09-03 23:12:08 +00001079 default:
1080 // Unhandled instruction. Halt "fast" selection and bail.
1081 return false;
1082 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001083}
1084
Bob Wilsond49edb72012-08-03 04:06:28 +00001085FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1086 const TargetLibraryInfo *libInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001087 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001088 MRI(FuncInfo.MF->getRegInfo()),
1089 MFI(*FuncInfo.MF->getFrameInfo()),
1090 MCP(*FuncInfo.MF->getConstantPool()),
1091 TM(FuncInfo.MF->getTarget()),
Micah Villmow3574eca2012-10-08 16:38:25 +00001092 TD(*TM.getDataLayout()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001093 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001094 TLI(*TM.getTargetLowering()),
Bob Wilsond49edb72012-08-03 04:06:28 +00001095 TRI(*TM.getRegisterInfo()),
1096 LibInfo(libInfo) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001097}
1098
Dan Gohmane285a742008-08-14 21:51:29 +00001099FastISel::~FastISel() {}
1100
Evan Cheng092e5e72013-02-11 01:27:15 +00001101bool FastISel::FastLowerArguments() {
1102 return false;
1103}
1104
Owen Anderson825b72b2009-08-11 20:47:22 +00001105unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001106 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001107 return 0;
1108}
1109
Owen Anderson825b72b2009-08-11 20:47:22 +00001110unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001111 unsigned,
1112 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001113 return 0;
1114}
1115
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001116unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001117 unsigned,
1118 unsigned /*Op0*/, bool /*Op0IsKill*/,
1119 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001120 return 0;
1121}
1122
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001123unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001124 return 0;
1125}
1126
Owen Anderson825b72b2009-08-11 20:47:22 +00001127unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001128 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001129 return 0;
1130}
1131
Owen Anderson825b72b2009-08-11 20:47:22 +00001132unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001133 unsigned,
1134 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001135 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001136 return 0;
1137}
1138
Owen Anderson825b72b2009-08-11 20:47:22 +00001139unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001140 unsigned,
1141 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001142 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001143 return 0;
1144}
1145
Owen Anderson825b72b2009-08-11 20:47:22 +00001146unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001147 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001148 unsigned /*Op0*/, bool /*Op0IsKill*/,
1149 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001150 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001151 return 0;
1152}
1153
1154/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1155/// to emit an instruction with an immediate operand using FastEmit_ri.
1156/// If that fails, it materializes the immediate into a register and try
1157/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001158unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001159 unsigned Op0, bool Op0IsKill,
1160 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001161 // If this is a multiply by a power of two, emit this as a shift left.
1162 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1163 Opcode = ISD::SHL;
1164 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001165 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1166 // div x, 8 -> srl x, 3
1167 Opcode = ISD::SRL;
1168 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001169 }
Owen Andersond74ea772011-04-22 23:38:06 +00001170
Chris Lattner602fc062011-04-17 20:23:29 +00001171 // Horrible hack (to be removed), check to make sure shift amounts are
1172 // in-range.
1173 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1174 Imm >= VT.getSizeInBits())
1175 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001176
Evan Cheng83785c82008-08-20 22:45:34 +00001177 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001178 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001179 if (ResultReg != 0)
1180 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001181 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001182 if (MaterialReg == 0) {
1183 // This is a bit ugly/slow, but failing here means falling out of
1184 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001185 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001186 VT.getSizeInBits());
1187 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosier7ae3bb82013-03-28 23:04:47 +00001188 assert (MaterialReg != 0 && "Unable to materialize imm.");
1189 if (MaterialReg == 0) return 0;
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001190 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001191 return FastEmit_rr(VT, VT, Opcode,
1192 Op0, Op0IsKill,
1193 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001194}
1195
1196unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1197 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001198}
1199
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001200unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001201 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001202 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001203 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001204
Dan Gohman84023e02010-07-10 09:00:22 +00001205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001206 return ResultReg;
1207}
1208
1209unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1210 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001211 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001212 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001213 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001214
Evan Cheng5960e4e2008-09-08 08:38:20 +00001215 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001216 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1217 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001218 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1220 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001221 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1222 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001223 }
1224
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001225 return ResultReg;
1226}
1227
1228unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1229 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001230 unsigned Op0, bool Op0IsKill,
1231 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001232 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001233 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001234
Evan Cheng5960e4e2008-09-08 08:38:20 +00001235 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001237 .addReg(Op0, Op0IsKill * RegState::Kill)
1238 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001239 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001241 .addReg(Op0, Op0IsKill * RegState::Kill)
1242 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1244 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001245 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001246 return ResultReg;
1247}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001248
Owen Andersond71867a2011-05-05 17:59:04 +00001249unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1250 const TargetRegisterClass *RC,
1251 unsigned Op0, bool Op0IsKill,
1252 unsigned Op1, bool Op1IsKill,
1253 unsigned Op2, bool Op2IsKill) {
1254 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001255 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001256
1257 if (II.getNumDefs() >= 1)
1258 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1259 .addReg(Op0, Op0IsKill * RegState::Kill)
1260 .addReg(Op1, Op1IsKill * RegState::Kill)
1261 .addReg(Op2, Op2IsKill * RegState::Kill);
1262 else {
1263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1264 .addReg(Op0, Op0IsKill * RegState::Kill)
1265 .addReg(Op1, Op1IsKill * RegState::Kill)
1266 .addReg(Op2, Op2IsKill * RegState::Kill);
1267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1268 ResultReg).addReg(II.ImplicitDefs[0]);
1269 }
1270 return ResultReg;
1271}
1272
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001273unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1274 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001275 unsigned Op0, bool Op0IsKill,
1276 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001277 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001278 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001279
Evan Cheng5960e4e2008-09-08 08:38:20 +00001280 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001282 .addReg(Op0, Op0IsKill * RegState::Kill)
1283 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001284 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001286 .addReg(Op0, Op0IsKill * RegState::Kill)
1287 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1289 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001290 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001291 return ResultReg;
1292}
1293
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001294unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1295 const TargetRegisterClass *RC,
1296 unsigned Op0, bool Op0IsKill,
1297 uint64_t Imm1, uint64_t Imm2) {
1298 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001299 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001300
1301 if (II.getNumDefs() >= 1)
1302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1303 .addReg(Op0, Op0IsKill * RegState::Kill)
1304 .addImm(Imm1)
1305 .addImm(Imm2);
1306 else {
1307 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1308 .addReg(Op0, Op0IsKill * RegState::Kill)
1309 .addImm(Imm1)
1310 .addImm(Imm2);
1311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1312 ResultReg).addReg(II.ImplicitDefs[0]);
1313 }
1314 return ResultReg;
1315}
1316
Dan Gohman10df0fa2008-08-27 01:09:54 +00001317unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1318 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001319 unsigned Op0, bool Op0IsKill,
1320 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001321 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001322 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001323
Evan Cheng5960e4e2008-09-08 08:38:20 +00001324 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001325 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001326 .addReg(Op0, Op0IsKill * RegState::Kill)
1327 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001328 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001329 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001330 .addReg(Op0, Op0IsKill * RegState::Kill)
1331 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001332 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1333 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001334 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001335 return ResultReg;
1336}
1337
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001338unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1339 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001340 unsigned Op0, bool Op0IsKill,
1341 unsigned Op1, bool Op1IsKill,
1342 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001343 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001344 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001345
Evan Cheng5960e4e2008-09-08 08:38:20 +00001346 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001348 .addReg(Op0, Op0IsKill * RegState::Kill)
1349 .addReg(Op1, Op1IsKill * RegState::Kill)
1350 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001351 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001352 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001353 .addReg(Op0, Op0IsKill * RegState::Kill)
1354 .addReg(Op1, Op1IsKill * RegState::Kill)
1355 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001356 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1357 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001358 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001359 return ResultReg;
1360}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001361
Manman Ren68f25572012-06-01 19:33:18 +00001362unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1363 const TargetRegisterClass *RC,
1364 unsigned Op0, bool Op0IsKill,
1365 unsigned Op1, bool Op1IsKill,
1366 uint64_t Imm1, uint64_t Imm2) {
1367 unsigned ResultReg = createResultReg(RC);
1368 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1369
1370 if (II.getNumDefs() >= 1)
1371 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1372 .addReg(Op0, Op0IsKill * RegState::Kill)
1373 .addReg(Op1, Op1IsKill * RegState::Kill)
1374 .addImm(Imm1).addImm(Imm2);
1375 else {
1376 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1377 .addReg(Op0, Op0IsKill * RegState::Kill)
1378 .addReg(Op1, Op1IsKill * RegState::Kill)
1379 .addImm(Imm1).addImm(Imm2);
1380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1381 ResultReg).addReg(II.ImplicitDefs[0]);
1382 }
1383 return ResultReg;
1384}
1385
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001386unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1387 const TargetRegisterClass *RC,
1388 uint64_t Imm) {
1389 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001390 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001391
Evan Cheng5960e4e2008-09-08 08:38:20 +00001392 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001394 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001395 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1397 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001398 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001399 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001400}
Owen Anderson8970f002008-08-27 22:30:02 +00001401
Owen Andersond74ea772011-04-22 23:38:06 +00001402unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1403 const TargetRegisterClass *RC,
1404 uint64_t Imm1, uint64_t Imm2) {
1405 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001406 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001407
1408 if (II.getNumDefs() >= 1)
1409 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1410 .addImm(Imm1).addImm(Imm2);
1411 else {
1412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1414 ResultReg).addReg(II.ImplicitDefs[0]);
1415 }
1416 return ResultReg;
1417}
1418
Owen Anderson825b72b2009-08-11 20:47:22 +00001419unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001420 unsigned Op0, bool Op0IsKill,
1421 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001422 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001423 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1424 "Cannot yet extract from physregs");
Jakob Stoklund Olesenee0d5d42012-05-20 06:38:37 +00001425 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1426 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohman84023e02010-07-10 09:00:22 +00001427 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1428 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001429 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001430 return ResultReg;
1431}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001432
1433/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1434/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001435unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1436 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001437}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001438
1439/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1440/// Emit code to ensure constants are copied into registers when needed.
1441/// Remember the virtual registers that need to be added to the Machine PHI
1442/// nodes as input. We cannot just directly add them, because expansion
1443/// might result in multiple MBB's for one BB. As such, the start of the
1444/// BB might correspond to a different MBB than the end.
1445bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1446 const TerminatorInst *TI = LLVMBB->getTerminator();
1447
1448 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001449 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001450
1451 // Check successor nodes' PHI nodes that expect a constant to be available
1452 // from this block.
1453 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1454 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1455 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001456 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001457
1458 // If this terminator has multiple identical successors (common for
1459 // switches), only handle each succ once.
1460 if (!SuccsHandled.insert(SuccMBB)) continue;
1461
1462 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1463
1464 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1465 // nodes and Machine PHI nodes, but the incoming operands have not been
1466 // emitted yet.
1467 for (BasicBlock::const_iterator I = SuccBB->begin();
1468 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001469
Dan Gohmanf81eca02010-04-22 20:46:50 +00001470 // Ignore dead phi's.
1471 if (PN->use_empty()) continue;
1472
1473 // Only handle legal types. Two interesting things to note here. First,
1474 // by bailing out early, we may leave behind some dead instructions,
1475 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001476 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001477 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001478 // exactly one register for each non-void instruction.
1479 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1480 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier2f2d1d72012-02-04 00:39:19 +00001481 // Handle integer promotions, though, because they're common and easy.
1482 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanf81eca02010-04-22 20:46:50 +00001483 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1484 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001485 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001486 return false;
1487 }
1488 }
1489
1490 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1491
Dan Gohmanfb95f892010-05-07 01:10:20 +00001492 // Set the DebugLoc for the copy. Prefer the location of the operand
1493 // if there is one; use the location of the PHI otherwise.
1494 DL = PN->getDebugLoc();
1495 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1496 DL = Inst->getDebugLoc();
1497
Dan Gohmanf81eca02010-04-22 20:46:50 +00001498 unsigned Reg = getRegForValue(PHIOp);
1499 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001500 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001501 return false;
1502 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001503 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001504 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001505 }
1506 }
1507
1508 return true;
1509}
Eli Bendersky75299e32013-04-19 22:29:18 +00001510
1511bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Bendersky462123f2013-04-19 23:26:18 +00001512 assert(LI->hasOneUse() &&
1513 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky75299e32013-04-19 22:29:18 +00001514 // We know that the load has a single use, but don't know what it is. If it
1515 // isn't one of the folded instructions, then we can't succeed here. Handle
1516 // this by scanning the single-use users of the load until we get to FoldInst.
1517 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1518
1519 const Instruction *TheUser = LI->use_back();
1520 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1521 // Stay in the right block.
1522 TheUser->getParent() == FoldInst->getParent() &&
1523 --MaxUsers) { // Don't scan too far.
1524 // If there are multiple or no uses of this instruction, then bail out.
1525 if (!TheUser->hasOneUse())
1526 return false;
1527
1528 TheUser = TheUser->use_back();
1529 }
1530
1531 // If we didn't find the fold instruction, then we failed to collapse the
1532 // sequence.
1533 if (TheUser != FoldInst)
1534 return false;
1535
1536 // Don't try to fold volatile loads. Target has to deal with alignment
1537 // constraints.
Eli Bendersky462123f2013-04-19 23:26:18 +00001538 if (LI->isVolatile())
1539 return false;
Eli Bendersky75299e32013-04-19 22:29:18 +00001540
1541 // Figure out which vreg this is going into. If there is no assigned vreg yet
1542 // then there actually was no reference to it. Perhaps the load is referenced
1543 // by a dead instruction.
1544 unsigned LoadReg = getRegForValue(LI);
1545 if (LoadReg == 0)
1546 return false;
1547
Eli Bendersky462123f2013-04-19 23:26:18 +00001548 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1549 // may mean that the instruction got lowered to multiple MIs, or the use of
1550 // the loaded value ended up being multiple operands of the result.
1551 if (!MRI.hasOneUse(LoadReg))
1552 return false;
1553
Eli Bendersky75299e32013-04-19 22:29:18 +00001554 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Eli Bendersky75299e32013-04-19 22:29:18 +00001555 MachineInstr *User = &*RI;
1556
1557 // Set the insertion point properly. Folding the load can cause generation of
Eli Bendersky462123f2013-04-19 23:26:18 +00001558 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky75299e32013-04-19 22:29:18 +00001559 // sure they get inserted in a logical place before the new instruction.
1560 FuncInfo.InsertPt = User;
1561 FuncInfo.MBB = User->getParent();
1562
1563 // Ask the target to try folding the load.
1564 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1565}
1566
1567