blob: b25e0317839002b855618f128ab26537ee0c2f02 [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
Jakob Stoklund Olesen1ab111e2013-07-04 04:53:49 +000079 // Instructions are appended to FuncInfo.MBB. If the basic block already
Jakob Stoklund Olesenef22e0e2013-07-04 04:32:39 +000080 // contains labels or copies, use the last instruction as the last local
81 // value.
Ivan Krasin74af88a2011-08-18 22:06:10 +000082 EmitStartPt = 0;
Jakob Stoklund Olesenef22e0e2013-07-04 04:32:39 +000083 if (!FuncInfo.MBB->empty())
84 EmitStartPt = &FuncInfo.MBB->back();
Ivan Krasin74af88a2011-08-18 22:06:10 +000085 LastLocalValue = EmitStartPt;
86}
87
Evan Cheng092e5e72013-02-11 01:27:15 +000088bool FastISel::LowerArguments() {
89 if (!FuncInfo.CanLowerReturn)
90 // Fallback to SDISel argument lowering code to deal with sret pointer
91 // parameter.
92 return false;
Stephen Lin155615d2013-07-08 00:37:03 +000093
Evan Cheng092e5e72013-02-11 01:27:15 +000094 if (!FastLowerArguments())
95 return false;
96
David Blaikie19489102013-06-21 22:56:30 +000097 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng092e5e72013-02-11 01:27:15 +000098 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
99 E = FuncInfo.Fn->arg_end(); I != E; ++I) {
David Blaikie19489102013-06-21 22:56:30 +0000100 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
101 assert(VI != LocalValueMap.end() && "Missed an argument?");
102 FuncInfo.ValueMap[I] = VI->second;
Evan Cheng092e5e72013-02-11 01:27:15 +0000103 }
104 return true;
105}
106
Ivan Krasin74af88a2011-08-18 22:06:10 +0000107void FastISel::flushLocalValueMap() {
108 LocalValueMap.clear();
109 LastLocalValue = EmitStartPt;
110 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +0000111}
112
Dan Gohmana6cb6412010-05-11 23:54:07 +0000113bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +0000114 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000115 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +0000116 if (!I)
117 return false;
118
119 // No-op casts are trivially coalesced by fast-isel.
120 if (const CastInst *Cast = dyn_cast<CastInst>(I))
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000121 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
122 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman7f0d6952010-05-14 22:53:18 +0000123 return false;
124
Chad Rosier22b34cc2011-11-15 23:34:05 +0000125 // GEPs with all zero indices are trivially coalesced by fast-isel.
126 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
127 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
128 return false;
129
Dan Gohman7f0d6952010-05-14 22:53:18 +0000130 // Only instructions with a single use in the same basic block are considered
131 // to have trivial kills.
132 return I->hasOneUse() &&
133 !(I->getOpcode() == Instruction::BitCast ||
134 I->getOpcode() == Instruction::PtrToInt ||
135 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000136 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000137}
138
Dan Gohman46510a72010-04-15 01:51:59 +0000139unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000140 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000141 // Don't handle non-simple values in FastISel.
142 if (!RealVT.isSimple())
143 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000144
145 // Ignore illegal types. We must do this before looking up the value
146 // in ValueMap because Arguments are given virtual registers regardless
147 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000149 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000150 // Handle integer promotions, though, because they're common and easy.
151 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000152 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000153 else
154 return 0;
155 }
156
Eric Christopher4e270272012-03-20 01:07:47 +0000157 // Look up the value to see if we already have a register for it.
158 unsigned Reg = lookUpRegForValue(V);
Dan Gohman104e4ce2008-09-03 23:32:19 +0000159 if (Reg != 0)
160 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000161
Dan Gohman97c94b82010-05-06 00:02:14 +0000162 // In bottom-up mode, just create the virtual register which will be used
163 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000164 if (isa<Instruction>(V) &&
165 (!isa<AllocaInst>(V) ||
166 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
167 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000168
Eric Christopher76ad43c2012-10-03 08:10:01 +0000169 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000170
171 // Materialize the value in a register. Emit any instructions in the
172 // local value area.
173 Reg = materializeRegForValue(V, VT);
174
Eric Christopher76ad43c2012-10-03 08:10:01 +0000175 leaveLocalValueArea(SaveInsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000176
177 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000178}
179
Eric Christopher44a2c342010-08-17 01:30:33 +0000180/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000181/// called when the value isn't already available in a register and must
182/// be materialized with new instructions.
183unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
184 unsigned Reg = 0;
185
Dan Gohman46510a72010-04-15 01:51:59 +0000186 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000187 if (CI->getValue().getActiveBits() <= 64)
188 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000189 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000190 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000191 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000192 // Translate this as an integer zero so that it can be
193 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000194 Reg =
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000195 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000196 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000197 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000198 Reg = TargetMaterializeFloatZero(CF);
199 } else {
200 // Try to emit the constant directly.
201 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
202 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000203
204 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000205 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000206 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000207 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000208
209 uint64_t x[2];
210 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000211 bool isExact;
212 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopherc415af22012-03-20 01:07:56 +0000213 APFloat::rmTowardZero, &isExact);
Dale Johannesen23a98552008-10-09 23:00:39 +0000214 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000215 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000216
Owen Andersone922c022009-07-22 00:24:57 +0000217 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000218 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000219 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000220 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
221 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000222 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000223 }
Dan Gohman46510a72010-04-15 01:51:59 +0000224 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000225 if (!SelectOperator(Op, Op->getOpcode()))
226 if (!isa<Instruction>(Op) ||
227 !TargetSelectInstruction(cast<Instruction>(Op)))
228 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000229 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000230 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000231 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
233 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000234 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235
Dan Gohmandceffe62008-09-25 01:28:51 +0000236 // If target-independent code couldn't handle the value, give target-specific
237 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000238 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000239 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000240
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000241 // Don't cache constant materializations in the general ValueMap.
242 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000243 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000244 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000245 LastLocalValue = MRI.getVRegDef(Reg);
246 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000247 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000248}
249
Dan Gohman46510a72010-04-15 01:51:59 +0000250unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000251 // Look up the value to see if we already have a register for it. We
252 // cache values defined by Instructions across blocks, and other values
253 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000254 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000255 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
256 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000257 return I->second;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000258 return LocalValueMap[V];
Evan Cheng59fbc802008-09-09 01:26:59 +0000259}
260
Owen Andersoncc54e762008-08-30 00:38:46 +0000261/// UpdateValueMap - Update the value map to include the new mapping for this
262/// instruction, or insert an extra copy to get the result in a previous
263/// determined register.
264/// NOTE: This is only necessary because we might select a block that uses
265/// a value before we select the block that defines the value. It might be
266/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000267void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000268 if (!isa<Instruction>(I)) {
269 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000270 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000271 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000272
Dan Gohmana4160c32010-07-07 16:29:44 +0000273 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000274 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000275 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000276 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000277 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000278 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000279 for (unsigned i = 0; i < NumRegs; i++)
280 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000281
282 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000283 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000284}
285
Dan Gohmana6cb6412010-05-11 23:54:07 +0000286std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000287 unsigned IdxN = getRegForValue(Idx);
288 if (IdxN == 0)
289 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000290 return std::pair<unsigned, bool>(0, false);
291
292 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000293
294 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000295 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000296 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000297 if (IdxVT.bitsLT(PtrVT)) {
298 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
299 IdxN, IdxNIsKill);
300 IdxNIsKill = true;
301 }
302 else if (IdxVT.bitsGT(PtrVT)) {
303 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
304 IdxN, IdxNIsKill);
305 IdxNIsKill = true;
306 }
307 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000308}
309
Dan Gohman84023e02010-07-10 09:00:22 +0000310void FastISel::recomputeInsertPt() {
311 if (getLastLocalValue()) {
312 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000313 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000314 ++FuncInfo.InsertPt;
315 } else
316 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
317
318 // Now skip past any EH_LABELs, which must remain at the beginning.
319 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
320 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
321 ++FuncInfo.InsertPt;
322}
323
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000324void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
325 MachineBasicBlock::iterator E) {
326 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
327 while (I != E) {
328 MachineInstr *Dead = &*I;
329 ++I;
330 Dead->eraseFromParent();
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000331 ++NumFastIselDead;
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000332 }
333 recomputeInsertPt();
334}
335
Eric Christopher76ad43c2012-10-03 08:10:01 +0000336FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000337 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000338 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000339 recomputeInsertPt();
Eric Christopher76ad43c2012-10-03 08:10:01 +0000340 DL = DebugLoc();
341 SavePoint SP = { OldInsertPt, OldDL };
342 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000343}
344
Eric Christopher76ad43c2012-10-03 08:10:01 +0000345void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000346 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
347 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
348
349 // Restore the previous insert position.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000350 FuncInfo.InsertPt = OldInsertPt.InsertPt;
351 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000352}
353
Dan Gohmanbdedd442008-08-20 00:11:48 +0000354/// SelectBinaryOp - Select and emit code for a binary operator instruction,
355/// which has an opcode which directly corresponds to the given ISD opcode.
356///
Dan Gohman46510a72010-04-15 01:51:59 +0000357bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000358 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000360 // Unhandled type. Halt "fast" selection and bail.
361 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000362
Dan Gohmanb71fea22008-08-26 20:52:40 +0000363 // We only handle legal types. For example, on x86-32 the instruction
364 // selector contains all of the 64-bit instructions from x86-64,
365 // under the assumption that i64 won't be used if the target doesn't
366 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000367 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000369 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000371 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
372 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000373 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000374 else
375 return false;
376 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000377
Chris Lattnerfff65b32011-04-17 01:16:47 +0000378 // Check if the first operand is a constant, and handle it as "ri". At -O0,
379 // we don't have anything that canonicalizes operand order.
380 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
381 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
382 unsigned Op1 = getRegForValue(I->getOperand(1));
383 if (Op1 == 0) return false;
384
385 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000386
Chris Lattner602fc062011-04-17 20:23:29 +0000387 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
388 Op1IsKill, CI->getZExtValue(),
389 VT.getSimpleVT());
390 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000391
Chris Lattner602fc062011-04-17 20:23:29 +0000392 // We successfully emitted code for the given LLVM Instruction.
393 UpdateValueMap(I, ResultReg);
394 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000395 }
Owen Andersond74ea772011-04-22 23:38:06 +0000396
397
Dan Gohman3df24e62008-09-03 23:12:08 +0000398 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000399 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000400 return false;
401
Dan Gohmana6cb6412010-05-11 23:54:07 +0000402 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
403
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000404 // Check if the second operand is a constant and handle it appropriately.
405 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000406 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000407
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000408 // Transform "sdiv exact X, 8" -> "sra X, 3".
409 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
410 cast<BinaryOperator>(I)->isExact() &&
411 isPowerOf2_64(Imm)) {
412 Imm = Log2_64(Imm);
413 ISDOpcode = ISD::SRA;
414 }
Owen Andersond74ea772011-04-22 23:38:06 +0000415
Chad Rosier544b9b42012-03-22 00:21:17 +0000416 // Transform "urem x, pow2" -> "and x, pow2-1".
417 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
418 isPowerOf2_64(Imm)) {
419 --Imm;
420 ISDOpcode = ISD::AND;
421 }
422
Chris Lattner602fc062011-04-17 20:23:29 +0000423 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
424 Op0IsKill, Imm, VT.getSimpleVT());
425 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000426
Chris Lattner602fc062011-04-17 20:23:29 +0000427 // We successfully emitted code for the given LLVM Instruction.
428 UpdateValueMap(I, ResultReg);
429 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000430 }
431
Dan Gohman10df0fa2008-08-27 01:09:54 +0000432 // Check if the second operand is a constant float.
433 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000434 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000435 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000436 if (ResultReg != 0) {
437 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000438 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000439 return true;
440 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000441 }
442
Dan Gohman3df24e62008-09-03 23:12:08 +0000443 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000444 if (Op1 == 0)
445 // Unhandled operand. Halt "fast" selection and bail.
446 return false;
447
Dan Gohmana6cb6412010-05-11 23:54:07 +0000448 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
449
Dan Gohmanad368ac2008-08-27 18:10:19 +0000450 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000451 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000452 ISDOpcode,
453 Op0, Op0IsKill,
454 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000455 if (ResultReg == 0)
456 // Target-specific code wasn't able to find a machine opcode for
457 // the given ISD opcode and type. Halt "fast" selection and bail.
458 return false;
459
Dan Gohman8014e862008-08-20 00:23:20 +0000460 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000461 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000462 return true;
463}
464
Dan Gohman46510a72010-04-15 01:51:59 +0000465bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000466 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000467 if (N == 0)
468 // Unhandled operand. Halt "fast" selection and bail.
469 return false;
470
Dan Gohmana6cb6412010-05-11 23:54:07 +0000471 bool NIsKill = hasTrivialKill(I->getOperand(0));
472
Chad Rosier478b06c2011-11-17 07:15:58 +0000473 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
474 // into a single N = N + TotalOffset.
475 uint64_t TotalOffs = 0;
476 // FIXME: What's a good SWAG number for MaxOffs?
477 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000478 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000480 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
481 E = I->op_end(); OI != E; ++OI) {
482 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000483 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000484 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
485 if (Field) {
486 // N = N + Offset
Chad Rosier478b06c2011-11-17 07:15:58 +0000487 TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
488 if (TotalOffs >= MaxOffs) {
489 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
490 if (N == 0)
491 // Unhandled operand. Halt "fast" selection and bail.
492 return false;
493 NIsKill = true;
494 TotalOffs = 0;
495 }
Evan Cheng83785c82008-08-20 22:45:34 +0000496 }
497 Ty = StTy->getElementType(Field);
498 } else {
499 Ty = cast<SequentialType>(Ty)->getElementType();
500
501 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000503 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000504 // N = N + Offset
Chad Rosier6016a4a2012-07-06 17:44:22 +0000505 TotalOffs +=
Duncan Sands777d2302009-05-09 07:06:46 +0000506 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000507 if (TotalOffs >= MaxOffs) {
508 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
509 if (N == 0)
510 // Unhandled operand. Halt "fast" selection and bail.
511 return false;
512 NIsKill = true;
513 TotalOffs = 0;
514 }
515 continue;
516 }
517 if (TotalOffs) {
518 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000519 if (N == 0)
520 // Unhandled operand. Halt "fast" selection and bail.
521 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000522 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000523 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000524 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000525
Evan Cheng83785c82008-08-20 22:45:34 +0000526 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000527 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000528 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
529 unsigned IdxN = Pair.first;
530 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000531 if (IdxN == 0)
532 // Unhandled operand. Halt "fast" selection and bail.
533 return false;
534
Dan Gohman80bc6e22008-08-26 20:57:08 +0000535 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000536 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000537 if (IdxN == 0)
538 // Unhandled operand. Halt "fast" selection and bail.
539 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000540 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000541 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000542 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000543 if (N == 0)
544 // Unhandled operand. Halt "fast" selection and bail.
545 return false;
546 }
547 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000548 if (TotalOffs) {
549 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
550 if (N == 0)
551 // Unhandled operand. Halt "fast" selection and bail.
552 return false;
553 }
Evan Cheng83785c82008-08-20 22:45:34 +0000554
555 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000556 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000557 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000558}
559
Dan Gohman46510a72010-04-15 01:51:59 +0000560bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000561 const CallInst *Call = cast<CallInst>(I);
562
563 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000564 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000565 // Don't attempt to handle constraints.
566 if (!IA->getConstraintString().empty())
567 return false;
568
569 unsigned ExtraInfo = 0;
570 if (IA->hasSideEffects())
571 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
572 if (IA->isAlignStack())
573 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
574
575 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
576 TII.get(TargetOpcode::INLINEASM))
577 .addExternalSymbol(IA->getAsmString().c_str())
578 .addImm(ExtraInfo);
579 return true;
580 }
581
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000582 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
583 ComputeUsesVAFloatArgument(*Call, &MMI);
584
Dan Gohmana61e73b2011-04-26 17:18:34 +0000585 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000586 if (!F) return false;
587
Dan Gohman4183e312010-04-13 17:07:06 +0000588 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000589 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000590 default: break;
Chad Rosieraefd36b2012-05-11 23:21:01 +0000591 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000592 case Intrinsic::lifetime_start:
593 case Intrinsic::lifetime_end:
Chad Rosierfd065bb2012-07-06 17:33:39 +0000594 // The donothing intrinsic does, well, nothing.
595 case Intrinsic::donothing:
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000596 return true;
Chad Rosierfd065bb2012-07-06 17:33:39 +0000597
Bill Wendling92c1e122009-02-13 02:16:35 +0000598 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000599 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Manman Rencbafae62013-06-28 05:43:10 +0000600 DIVariable DIVar(DI->getVariable());
Stephen Lin155615d2013-07-08 00:37:03 +0000601 assert((!DIVar || DIVar.isVariable()) &&
Manman Rencbafae62013-06-28 05:43:10 +0000602 "Variable in DbgDeclareInst should be either null or a DIVariable.");
603 if (!DIVar ||
Eric Christopherbb54d212012-03-15 21:33:44 +0000604 !FuncInfo.MF->getMMI().hasDebugInfo()) {
605 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel7e1e31f2009-07-02 22:43:26 +0000606 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000607 }
Devang Patel7e1e31f2009-07-02 22:43:26 +0000608
Dan Gohman46510a72010-04-15 01:51:59 +0000609 const Value *Address = DI->getAddress();
Eric Christopherccaea7d2012-03-15 21:33:47 +0000610 if (!Address || isa<UndefValue>(Address)) {
Eric Christopherbb54d212012-03-15 21:33:44 +0000611 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendc918562010-02-06 02:26:02 +0000612 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000613 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000614
Adrian Prantl35176402013-07-09 20:28:37 +0000615 unsigned Offset = 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +0000616 Optional<MachineOperand> Op;
617 if (const Argument *Arg = dyn_cast<Argument>(Address))
Devang Patel9aee3352011-09-08 22:59:09 +0000618 // Some arguments' frame index is recorded during argument lowering.
Adrian Prantl35176402013-07-09 20:28:37 +0000619 Offset = FuncInfo.getArgumentFrameIndex(Arg);
620 if (Offset)
621 Op = MachineOperand::CreateFI(Offset);
David Blaikie6d9dbd52013-06-16 20:34:15 +0000622 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)
Adrian Prantl35176402013-07-09 20:28:37 +0000644 if (Op->isReg()) {
Adrian Prantl45ff7092013-07-10 01:53:37 +0000645 // Set the indirect flag if the type and the DIVariable's
646 // indirect field are in disagreement: Indirectly-addressed
647 // variables that are nonpointer types should be marked as
648 // indirect, and VLAs should be marked as indirect eventhough
649 // they are a pointer type.
650 bool IsIndirect = DI->getAddress()->getType()->isPointerTy()
651 ^ DIVar.isIndirect();
Adrian Prantl35176402013-07-09 20:28:37 +0000652 Op->setIsDebug(true);
653 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
654 TII.get(TargetOpcode::DBG_VALUE),
Adrian Prantl45ff7092013-07-10 01:53:37 +0000655 IsIndirect, Op->getReg(), Offset, DI->getVariable());
Adrian Prantl35176402013-07-09 20:28:37 +0000656 } else
657 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
658 TII.get(TargetOpcode::DBG_VALUE)).addOperand(*Op).addImm(0)
David Blaikie6d9dbd52013-06-16 20:34:15 +0000659 .addMetadata(DI->getVariable());
Adrian Prantl86a87d92013-04-30 22:35:14 +0000660 else
Eric Christopher4476bae2012-03-20 01:07:53 +0000661 // We can't yet handle anything else here because it would require
662 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000663 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dan Gohman33134c42008-09-25 17:05:24 +0000664 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000665 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000666 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000667 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000668 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000669 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000670 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000671 if (!V) {
672 // Currently the optimizer can produce this; insert an undef to
673 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000674 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
675 .addReg(0U).addImm(DI->getOffset())
676 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000677 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000678 if (CI->getBitWidth() > 64)
679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
680 .addCImm(CI).addImm(DI->getOffset())
681 .addMetadata(DI->getVariable());
Chad Rosier6016a4a2012-07-06 17:44:22 +0000682 else
Devang Patel8594d422011-06-24 20:46:11 +0000683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
684 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
685 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000686 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000687 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
688 .addFPImm(CF).addImm(DI->getOffset())
689 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000690 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantl818833f2013-09-16 23:29:03 +0000691 // FIXME: This does not handle register-indirect values at offset 0.
Adrian Prantl35176402013-07-09 20:28:37 +0000692 bool IsIndirect = DI->getOffset() != 0;
693 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, IsIndirect,
694 Reg, DI->getOffset(), DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000695 } else {
696 // We can't yet handle anything else here because it would require
697 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000698 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000700 return true;
701 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000702 case Intrinsic::objectsize: {
703 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
704 unsigned long long Res = CI->isZero() ? -1ULL : 0;
705 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
706 unsigned ResultReg = getRegForValue(ResCI);
707 if (ResultReg == 0)
708 return false;
709 UpdateValueMap(Call, ResultReg);
710 return true;
711 }
Chad Rosier33947b42013-03-07 20:42:17 +0000712 case Intrinsic::expect: {
Chad Rosier4fde76d2013-03-07 21:38:33 +0000713 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky33cdfe92013-03-11 21:44:37 +0000714 if (ResultReg == 0)
715 return false;
Chad Rosier4fde76d2013-03-07 21:38:33 +0000716 UpdateValueMap(Call, ResultReg);
717 return true;
Chad Rosier33947b42013-03-07 20:42:17 +0000718 }
Dan Gohman33134c42008-09-25 17:05:24 +0000719 }
Dan Gohman4183e312010-04-13 17:07:06 +0000720
Ivan Krasin74af88a2011-08-18 22:06:10 +0000721 // Usually, it does not make sense to initialize a value,
722 // make an unrelated function call and use the value, because
723 // it tends to be spilled on the stack. So, we move the pointer
724 // to the last local value to the beginning of the block, so that
725 // all the values which have already been materialized,
726 // appear after the call. It also makes sense to skip intrinsics
727 // since they tend to be inlined.
Pete Cooperb704ffb2013-02-22 01:50:38 +0000728 if (!isa<IntrinsicInst>(Call))
Ivan Krasin74af88a2011-08-18 22:06:10 +0000729 flushLocalValueMap();
730
Dan Gohman4183e312010-04-13 17:07:06 +0000731 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000732 return false;
733}
734
Dan Gohman46510a72010-04-15 01:51:59 +0000735bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000736 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
737 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000738
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
740 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000741 // Unhandled type. Halt "fast" selection and bail.
742 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000743
Eli Friedman76927d732011-05-25 23:49:02 +0000744 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000745 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000746 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000747
Eli Friedman76927d732011-05-25 23:49:02 +0000748 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000749 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000750 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000751
Dan Gohman3df24e62008-09-03 23:12:08 +0000752 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000753 if (!InputReg)
754 // Unhandled operand. Halt "fast" selection and bail.
755 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000756
Dan Gohmana6cb6412010-05-11 23:54:07 +0000757 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
758
Owen Andersond0533c92008-08-26 23:46:32 +0000759 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
760 DstVT.getSimpleVT(),
761 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000762 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000763 if (!ResultReg)
764 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000765
Dan Gohman3df24e62008-09-03 23:12:08 +0000766 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000767 return true;
768}
769
Dan Gohman46510a72010-04-15 01:51:59 +0000770bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000771 // If the bitcast doesn't change the type, just use the operand value.
772 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000773 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000774 if (Reg == 0)
775 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000776 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000777 return true;
778 }
779
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000780 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000781 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
782 EVT DstEVT = TLI.getValueType(I->getType());
783 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
784 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000785 // Unhandled type. Halt "fast" selection and bail.
786 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000787
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000788 MVT SrcVT = SrcEVT.getSimpleVT();
789 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman3df24e62008-09-03 23:12:08 +0000790 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000791 if (Op0 == 0)
792 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000793 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000794
795 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000796
Dan Gohmanad368ac2008-08-27 18:10:19 +0000797 // First, try to perform the bitcast by inserting a reg-reg copy.
798 unsigned ResultReg = 0;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000799 if (SrcVT == DstVT) {
Craig Topper44d23822012-02-22 05:59:10 +0000800 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
801 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000802 // Don't attempt a cross-class copy. It will likely fail.
803 if (SrcClass == DstClass) {
804 ResultReg = createResultReg(DstClass);
805 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
806 ResultReg).addReg(Op0);
807 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000808 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000809
810 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000811 if (!ResultReg)
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000812 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000813
Dan Gohmanad368ac2008-08-27 18:10:19 +0000814 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000815 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000816
Dan Gohman3df24e62008-09-03 23:12:08 +0000817 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000818 return true;
819}
820
Dan Gohman3df24e62008-09-03 23:12:08 +0000821bool
Dan Gohman46510a72010-04-15 01:51:59 +0000822FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000823 // Just before the terminator instruction, insert instructions to
824 // feed PHI nodes in successor blocks.
825 if (isa<TerminatorInst>(I))
826 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
827 return false;
828
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000829 DL = I->getDebugLoc();
830
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000831 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
832
Bob Wilson982dc842012-08-03 21:26:24 +0000833 // As a special case, don't handle calls to builtin library functions that
834 // may be translated directly to target instructions.
Bob Wilsond49edb72012-08-03 04:06:28 +0000835 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
836 const Function *F = Call->getCalledFunction();
837 LibFunc::Func Func;
838 if (F && !F->hasLocalLinkage() && F->hasName() &&
839 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson982dc842012-08-03 21:26:24 +0000840 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilsond49edb72012-08-03 04:06:28 +0000841 return false;
842 }
843
Dan Gohman6e3ff372009-12-05 01:27:58 +0000844 // First, try doing target-independent selection.
Michael Ilseman7dbd34b2013-02-27 19:54:00 +0000845 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000846 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000847 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000848 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000849 }
Chad Rosier6016a4a2012-07-06 17:44:22 +0000850 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000851 // the local value map and recomputed the insert point.
852 if (!isa<CallInst>(I)) {
853 recomputeInsertPt();
854 if (SavedInsertPt != FuncInfo.InsertPt)
855 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
856 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000857
858 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000859 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000860 if (TargetSelectInstruction(I)) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000861 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000862 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000863 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000864 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000865 // Check for dead code and remove as necessary.
866 recomputeInsertPt();
867 if (SavedInsertPt != FuncInfo.InsertPt)
868 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000869
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000870 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000871 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000872}
873
Dan Gohmand98d6202008-10-02 22:15:21 +0000874/// FastEmitBranch - Emit an unconditional branch to the given block,
875/// unless it is the immediate (fall-through) successor, and update
876/// the CFG.
877void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000878FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Eric Christopher18112d82012-04-10 18:18:10 +0000879
Evan Cheng092e5e72013-02-11 01:27:15 +0000880 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
881 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christopher18112d82012-04-10 18:18:10 +0000882 // For more accurate line information if this is the only instruction
883 // in the block then emit it, otherwise we have the unconditional
884 // fall-through case, which needs no instructions.
Dan Gohmand98d6202008-10-02 22:15:21 +0000885 } else {
886 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000887 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
888 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000889 }
Dan Gohman84023e02010-07-10 09:00:22 +0000890 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000891}
892
Dan Gohman3d45a852009-09-03 22:53:57 +0000893/// SelectFNeg - Emit an FNeg operation.
894///
895bool
Dan Gohman46510a72010-04-15 01:51:59 +0000896FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000897 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
898 if (OpReg == 0) return false;
899
Dan Gohmana6cb6412010-05-11 23:54:07 +0000900 bool OpRegIsKill = hasTrivialKill(I);
901
Dan Gohman4a215a12009-09-11 00:36:43 +0000902 // If the target has ISD::FNEG, use it.
903 EVT VT = TLI.getValueType(I->getType());
904 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000905 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000906 if (ResultReg != 0) {
907 UpdateValueMap(I, ResultReg);
908 return true;
909 }
910
Dan Gohman5e5abb72009-09-11 00:34:46 +0000911 // Bitcast the value to integer, twiddle the sign bit with xor,
912 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000913 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000914 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
915 if (!TLI.isTypeLegal(IntVT))
916 return false;
917
918 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000919 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000920 if (IntReg == 0)
921 return false;
922
Dan Gohmana6cb6412010-05-11 23:54:07 +0000923 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
924 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000925 UINT64_C(1) << (VT.getSizeInBits()-1),
926 IntVT.getSimpleVT());
927 if (IntResultReg == 0)
928 return false;
929
930 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000931 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000932 if (ResultReg == 0)
933 return false;
934
935 UpdateValueMap(I, ResultReg);
936 return true;
937}
938
Dan Gohman40b189e2008-09-05 18:18:20 +0000939bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000940FastISel::SelectExtractValue(const User *U) {
941 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000942 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000943 return false;
944
Eli Friedman482feb32011-05-16 21:06:17 +0000945 // Make sure we only try to handle extracts with a legal result. But also
946 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000947 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
948 if (!RealVT.isSimple())
949 return false;
950 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000951 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000952 return false;
953
954 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000955 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000956
957 // Get the base result register.
958 unsigned ResultReg;
959 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
960 if (I != FuncInfo.ValueMap.end())
961 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000962 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000963 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000964 else
965 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000966
967 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000968 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000969
970 SmallVector<EVT, 4> AggValueVTs;
971 ComputeValueVTs(TLI, AggTy, AggValueVTs);
972
973 for (unsigned i = 0; i < VTIndex; i++)
974 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
975
976 UpdateValueMap(EVI, ResultReg);
977 return true;
978}
979
980bool
Dan Gohman46510a72010-04-15 01:51:59 +0000981FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000982 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000983 case Instruction::Add:
984 return SelectBinaryOp(I, ISD::ADD);
985 case Instruction::FAdd:
986 return SelectBinaryOp(I, ISD::FADD);
987 case Instruction::Sub:
988 return SelectBinaryOp(I, ISD::SUB);
989 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000990 // FNeg is currently represented in LLVM IR as a special case of FSub.
991 if (BinaryOperator::isFNeg(I))
992 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000993 return SelectBinaryOp(I, ISD::FSUB);
994 case Instruction::Mul:
995 return SelectBinaryOp(I, ISD::MUL);
996 case Instruction::FMul:
997 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000998 case Instruction::SDiv:
999 return SelectBinaryOp(I, ISD::SDIV);
1000 case Instruction::UDiv:
1001 return SelectBinaryOp(I, ISD::UDIV);
1002 case Instruction::FDiv:
1003 return SelectBinaryOp(I, ISD::FDIV);
1004 case Instruction::SRem:
1005 return SelectBinaryOp(I, ISD::SREM);
1006 case Instruction::URem:
1007 return SelectBinaryOp(I, ISD::UREM);
1008 case Instruction::FRem:
1009 return SelectBinaryOp(I, ISD::FREM);
1010 case Instruction::Shl:
1011 return SelectBinaryOp(I, ISD::SHL);
1012 case Instruction::LShr:
1013 return SelectBinaryOp(I, ISD::SRL);
1014 case Instruction::AShr:
1015 return SelectBinaryOp(I, ISD::SRA);
1016 case Instruction::And:
1017 return SelectBinaryOp(I, ISD::AND);
1018 case Instruction::Or:
1019 return SelectBinaryOp(I, ISD::OR);
1020 case Instruction::Xor:
1021 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001022
Dan Gohman3df24e62008-09-03 23:12:08 +00001023 case Instruction::GetElementPtr:
1024 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001025
Dan Gohman3df24e62008-09-03 23:12:08 +00001026 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +00001027 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001028
Dan Gohman3df24e62008-09-03 23:12:08 +00001029 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001030 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +00001031 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +00001032 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +00001033 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +00001034 }
Dan Gohman3df24e62008-09-03 23:12:08 +00001035
1036 // Conditional branches are not handed yet.
1037 // Halt "fast" selection and bail.
1038 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001039 }
1040
Dan Gohman087c8502008-09-05 01:08:41 +00001041 case Instruction::Unreachable:
1042 // Nothing to emit.
1043 return true;
1044
Dan Gohman0586d912008-09-10 20:11:02 +00001045 case Instruction::Alloca:
1046 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001047 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001048 return true;
1049
1050 // Dynamic-sized alloca is not handled yet.
1051 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001052
Dan Gohman33134c42008-09-25 17:05:24 +00001053 case Instruction::Call:
1054 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001055
Dan Gohman3df24e62008-09-03 23:12:08 +00001056 case Instruction::BitCast:
1057 return SelectBitCast(I);
1058
1059 case Instruction::FPToSI:
1060 return SelectCast(I, ISD::FP_TO_SINT);
1061 case Instruction::ZExt:
1062 return SelectCast(I, ISD::ZERO_EXTEND);
1063 case Instruction::SExt:
1064 return SelectCast(I, ISD::SIGN_EXTEND);
1065 case Instruction::Trunc:
1066 return SelectCast(I, ISD::TRUNCATE);
1067 case Instruction::SIToFP:
1068 return SelectCast(I, ISD::SINT_TO_FP);
1069
1070 case Instruction::IntToPtr: // Deliberate fall-through.
1071 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001072 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1073 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001074 if (DstVT.bitsGT(SrcVT))
1075 return SelectCast(I, ISD::ZERO_EXTEND);
1076 if (DstVT.bitsLT(SrcVT))
1077 return SelectCast(I, ISD::TRUNCATE);
1078 unsigned Reg = getRegForValue(I->getOperand(0));
1079 if (Reg == 0) return false;
1080 UpdateValueMap(I, Reg);
1081 return true;
1082 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001083
Eli Friedman2586b8f2011-05-16 20:27:46 +00001084 case Instruction::ExtractValue:
1085 return SelectExtractValue(I);
1086
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001087 case Instruction::PHI:
1088 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1089
Dan Gohman3df24e62008-09-03 23:12:08 +00001090 default:
1091 // Unhandled instruction. Halt "fast" selection and bail.
1092 return false;
1093 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001094}
1095
Bob Wilsond49edb72012-08-03 04:06:28 +00001096FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1097 const TargetLibraryInfo *libInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001098 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001099 MRI(FuncInfo.MF->getRegInfo()),
1100 MFI(*FuncInfo.MF->getFrameInfo()),
1101 MCP(*FuncInfo.MF->getConstantPool()),
1102 TM(FuncInfo.MF->getTarget()),
Micah Villmow3574eca2012-10-08 16:38:25 +00001103 TD(*TM.getDataLayout()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001104 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001105 TLI(*TM.getTargetLowering()),
Bob Wilsond49edb72012-08-03 04:06:28 +00001106 TRI(*TM.getRegisterInfo()),
1107 LibInfo(libInfo) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001108}
1109
Dan Gohmane285a742008-08-14 21:51:29 +00001110FastISel::~FastISel() {}
1111
Evan Cheng092e5e72013-02-11 01:27:15 +00001112bool FastISel::FastLowerArguments() {
1113 return false;
1114}
1115
Owen Anderson825b72b2009-08-11 20:47:22 +00001116unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001117 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001118 return 0;
1119}
1120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001122 unsigned,
1123 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001124 return 0;
1125}
1126
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001127unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001128 unsigned,
1129 unsigned /*Op0*/, bool /*Op0IsKill*/,
1130 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001131 return 0;
1132}
1133
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001134unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001135 return 0;
1136}
1137
Owen Anderson825b72b2009-08-11 20:47:22 +00001138unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001139 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001140 return 0;
1141}
1142
Owen Anderson825b72b2009-08-11 20:47:22 +00001143unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001144 unsigned,
1145 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001146 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001147 return 0;
1148}
1149
Owen Anderson825b72b2009-08-11 20:47:22 +00001150unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001151 unsigned,
1152 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001153 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001154 return 0;
1155}
1156
Owen Anderson825b72b2009-08-11 20:47:22 +00001157unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001158 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001159 unsigned /*Op0*/, bool /*Op0IsKill*/,
1160 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001161 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001162 return 0;
1163}
1164
1165/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1166/// to emit an instruction with an immediate operand using FastEmit_ri.
1167/// If that fails, it materializes the immediate into a register and try
1168/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001169unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001170 unsigned Op0, bool Op0IsKill,
1171 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001172 // If this is a multiply by a power of two, emit this as a shift left.
1173 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1174 Opcode = ISD::SHL;
1175 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001176 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1177 // div x, 8 -> srl x, 3
1178 Opcode = ISD::SRL;
1179 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001180 }
Owen Andersond74ea772011-04-22 23:38:06 +00001181
Chris Lattner602fc062011-04-17 20:23:29 +00001182 // Horrible hack (to be removed), check to make sure shift amounts are
1183 // in-range.
1184 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1185 Imm >= VT.getSizeInBits())
1186 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001187
Evan Cheng83785c82008-08-20 22:45:34 +00001188 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001189 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001190 if (ResultReg != 0)
1191 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001192 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001193 if (MaterialReg == 0) {
1194 // This is a bit ugly/slow, but failing here means falling out of
1195 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001196 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001197 VT.getSizeInBits());
1198 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosier7ae3bb82013-03-28 23:04:47 +00001199 assert (MaterialReg != 0 && "Unable to materialize imm.");
1200 if (MaterialReg == 0) return 0;
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001201 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001202 return FastEmit_rr(VT, VT, Opcode,
1203 Op0, Op0IsKill,
1204 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001205}
1206
1207unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1208 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001209}
1210
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001211unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001212 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001213 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001214 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001215
Dan Gohman84023e02010-07-10 09:00:22 +00001216 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001217 return ResultReg;
1218}
1219
1220unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1221 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001222 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001223 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001224 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001225
Evan Cheng5960e4e2008-09-08 08:38:20 +00001226 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1228 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001229 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1231 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1233 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001234 }
1235
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001236 return ResultReg;
1237}
1238
1239unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1240 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001241 unsigned Op0, bool Op0IsKill,
1242 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001243 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001244 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001245
Evan Cheng5960e4e2008-09-08 08:38:20 +00001246 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001247 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001248 .addReg(Op0, Op0IsKill * RegState::Kill)
1249 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001250 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001252 .addReg(Op0, Op0IsKill * RegState::Kill)
1253 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1255 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001256 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001257 return ResultReg;
1258}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001259
Owen Andersond71867a2011-05-05 17:59:04 +00001260unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1261 const TargetRegisterClass *RC,
1262 unsigned Op0, bool Op0IsKill,
1263 unsigned Op1, bool Op1IsKill,
1264 unsigned Op2, bool Op2IsKill) {
1265 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001266 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001267
1268 if (II.getNumDefs() >= 1)
1269 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1270 .addReg(Op0, Op0IsKill * RegState::Kill)
1271 .addReg(Op1, Op1IsKill * RegState::Kill)
1272 .addReg(Op2, Op2IsKill * RegState::Kill);
1273 else {
1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1275 .addReg(Op0, Op0IsKill * RegState::Kill)
1276 .addReg(Op1, Op1IsKill * RegState::Kill)
1277 .addReg(Op2, Op2IsKill * RegState::Kill);
1278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1279 ResultReg).addReg(II.ImplicitDefs[0]);
1280 }
1281 return ResultReg;
1282}
1283
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001284unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1285 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001286 unsigned Op0, bool Op0IsKill,
1287 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001288 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001289 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +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 .addImm(Imm);
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 .addImm(Imm);
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 Gohmand5fe57d2008-08-21 01:41:07 +00001302 return ResultReg;
1303}
1304
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001305unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1306 const TargetRegisterClass *RC,
1307 unsigned Op0, bool Op0IsKill,
1308 uint64_t Imm1, uint64_t Imm2) {
1309 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001310 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001311
1312 if (II.getNumDefs() >= 1)
1313 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1314 .addReg(Op0, Op0IsKill * RegState::Kill)
1315 .addImm(Imm1)
1316 .addImm(Imm2);
1317 else {
1318 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1319 .addReg(Op0, Op0IsKill * RegState::Kill)
1320 .addImm(Imm1)
1321 .addImm(Imm2);
1322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1323 ResultReg).addReg(II.ImplicitDefs[0]);
1324 }
1325 return ResultReg;
1326}
1327
Dan Gohman10df0fa2008-08-27 01:09:54 +00001328unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1329 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001330 unsigned Op0, bool Op0IsKill,
1331 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001332 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001333 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +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)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001337 .addReg(Op0, Op0IsKill * RegState::Kill)
1338 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001339 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001340 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001341 .addReg(Op0, Op0IsKill * RegState::Kill)
1342 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001343 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1344 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001345 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001346 return ResultReg;
1347}
1348
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001349unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1350 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001351 unsigned Op0, bool Op0IsKill,
1352 unsigned Op1, bool Op1IsKill,
1353 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001354 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001355 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001356
Evan Cheng5960e4e2008-09-08 08:38:20 +00001357 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001358 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001359 .addReg(Op0, Op0IsKill * RegState::Kill)
1360 .addReg(Op1, Op1IsKill * RegState::Kill)
1361 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001362 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001363 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001364 .addReg(Op0, Op0IsKill * RegState::Kill)
1365 .addReg(Op1, Op1IsKill * RegState::Kill)
1366 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1368 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001369 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001370 return ResultReg;
1371}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001372
Manman Ren68f25572012-06-01 19:33:18 +00001373unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1374 const TargetRegisterClass *RC,
1375 unsigned Op0, bool Op0IsKill,
1376 unsigned Op1, bool Op1IsKill,
1377 uint64_t Imm1, uint64_t Imm2) {
1378 unsigned ResultReg = createResultReg(RC);
1379 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1380
1381 if (II.getNumDefs() >= 1)
1382 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1383 .addReg(Op0, Op0IsKill * RegState::Kill)
1384 .addReg(Op1, Op1IsKill * RegState::Kill)
1385 .addImm(Imm1).addImm(Imm2);
1386 else {
1387 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1388 .addReg(Op0, Op0IsKill * RegState::Kill)
1389 .addReg(Op1, Op1IsKill * RegState::Kill)
1390 .addImm(Imm1).addImm(Imm2);
1391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1392 ResultReg).addReg(II.ImplicitDefs[0]);
1393 }
1394 return ResultReg;
1395}
1396
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001397unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1398 const TargetRegisterClass *RC,
1399 uint64_t Imm) {
1400 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001401 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001402
Evan Cheng5960e4e2008-09-08 08:38:20 +00001403 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001404 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001405 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001407 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1408 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001409 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001410 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001411}
Owen Anderson8970f002008-08-27 22:30:02 +00001412
Owen Andersond74ea772011-04-22 23:38:06 +00001413unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1414 const TargetRegisterClass *RC,
1415 uint64_t Imm1, uint64_t Imm2) {
1416 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001417 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001418
1419 if (II.getNumDefs() >= 1)
1420 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1421 .addImm(Imm1).addImm(Imm2);
1422 else {
1423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1424 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1425 ResultReg).addReg(II.ImplicitDefs[0]);
1426 }
1427 return ResultReg;
1428}
1429
Owen Anderson825b72b2009-08-11 20:47:22 +00001430unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001431 unsigned Op0, bool Op0IsKill,
1432 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001433 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001434 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1435 "Cannot yet extract from physregs");
Jakob Stoklund Olesenee0d5d42012-05-20 06:38:37 +00001436 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1437 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohman84023e02010-07-10 09:00:22 +00001438 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1439 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001440 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001441 return ResultReg;
1442}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001443
1444/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1445/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001446unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1447 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001448}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001449
1450/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1451/// Emit code to ensure constants are copied into registers when needed.
1452/// Remember the virtual registers that need to be added to the Machine PHI
1453/// nodes as input. We cannot just directly add them, because expansion
1454/// might result in multiple MBB's for one BB. As such, the start of the
1455/// BB might correspond to a different MBB than the end.
1456bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1457 const TerminatorInst *TI = LLVMBB->getTerminator();
1458
1459 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001460 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001461
1462 // Check successor nodes' PHI nodes that expect a constant to be available
1463 // from this block.
1464 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1465 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1466 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001467 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001468
1469 // If this terminator has multiple identical successors (common for
1470 // switches), only handle each succ once.
1471 if (!SuccsHandled.insert(SuccMBB)) continue;
1472
1473 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1474
1475 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1476 // nodes and Machine PHI nodes, but the incoming operands have not been
1477 // emitted yet.
1478 for (BasicBlock::const_iterator I = SuccBB->begin();
1479 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001480
Dan Gohmanf81eca02010-04-22 20:46:50 +00001481 // Ignore dead phi's.
1482 if (PN->use_empty()) continue;
1483
1484 // Only handle legal types. Two interesting things to note here. First,
1485 // by bailing out early, we may leave behind some dead instructions,
1486 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001487 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001488 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001489 // exactly one register for each non-void instruction.
1490 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1491 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier2f2d1d72012-02-04 00:39:19 +00001492 // Handle integer promotions, though, because they're common and easy.
1493 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanf81eca02010-04-22 20:46:50 +00001494 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1495 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001496 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001497 return false;
1498 }
1499 }
1500
1501 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1502
Dan Gohmanfb95f892010-05-07 01:10:20 +00001503 // Set the DebugLoc for the copy. Prefer the location of the operand
1504 // if there is one; use the location of the PHI otherwise.
1505 DL = PN->getDebugLoc();
1506 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1507 DL = Inst->getDebugLoc();
1508
Dan Gohmanf81eca02010-04-22 20:46:50 +00001509 unsigned Reg = getRegForValue(PHIOp);
1510 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001511 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001512 return false;
1513 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001514 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001515 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001516 }
1517 }
1518
1519 return true;
1520}
Eli Bendersky75299e32013-04-19 22:29:18 +00001521
1522bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Bendersky462123f2013-04-19 23:26:18 +00001523 assert(LI->hasOneUse() &&
1524 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky75299e32013-04-19 22:29:18 +00001525 // We know that the load has a single use, but don't know what it is. If it
1526 // isn't one of the folded instructions, then we can't succeed here. Handle
1527 // this by scanning the single-use users of the load until we get to FoldInst.
1528 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1529
1530 const Instruction *TheUser = LI->use_back();
1531 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1532 // Stay in the right block.
1533 TheUser->getParent() == FoldInst->getParent() &&
1534 --MaxUsers) { // Don't scan too far.
1535 // If there are multiple or no uses of this instruction, then bail out.
1536 if (!TheUser->hasOneUse())
1537 return false;
1538
1539 TheUser = TheUser->use_back();
1540 }
1541
1542 // If we didn't find the fold instruction, then we failed to collapse the
1543 // sequence.
1544 if (TheUser != FoldInst)
1545 return false;
1546
1547 // Don't try to fold volatile loads. Target has to deal with alignment
1548 // constraints.
Eli Bendersky462123f2013-04-19 23:26:18 +00001549 if (LI->isVolatile())
1550 return false;
Eli Bendersky75299e32013-04-19 22:29:18 +00001551
1552 // Figure out which vreg this is going into. If there is no assigned vreg yet
1553 // then there actually was no reference to it. Perhaps the load is referenced
1554 // by a dead instruction.
1555 unsigned LoadReg = getRegForValue(LI);
1556 if (LoadReg == 0)
1557 return false;
1558
Eli Bendersky462123f2013-04-19 23:26:18 +00001559 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1560 // may mean that the instruction got lowered to multiple MIs, or the use of
1561 // the loaded value ended up being multiple operands of the result.
1562 if (!MRI.hasOneUse(LoadReg))
1563 return false;
1564
Eli Bendersky75299e32013-04-19 22:29:18 +00001565 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Eli Bendersky75299e32013-04-19 22:29:18 +00001566 MachineInstr *User = &*RI;
1567
1568 // Set the insertion point properly. Folding the load can cause generation of
Eli Bendersky462123f2013-04-19 23:26:18 +00001569 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky75299e32013-04-19 22:29:18 +00001570 // sure they get inserted in a logical place before the new instruction.
1571 FuncInfo.InsertPt = User;
1572 FuncInfo.MBB = User->getParent();
1573
1574 // Ask the target to try folding the load.
1575 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1576}
1577
1578