blob: a5b72748e2f425fcf216d32c3431f8ca38869e82 [file] [log] [blame]
Dan Gohmane149e982010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb2226e22008-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 Gohmanb4863502008-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 Lattnerc52af452008-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 Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohmanb4863502008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb2226e22008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Chad Rosierff40b1e2011-11-16 21:05:28 +000042#define DEBUG_TYPE "isel"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/CodeGen/FastISel.h"
David Blaikie0252265b2013-06-16 20:34:15 +000044#include "llvm/ADT/Optional.h"
Chandler Carruthed0881b2012-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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000052#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000053#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000054#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 Carruthed0881b2012-12-03 16:50:05 +000059#include "llvm/Support/Debug.h"
60#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000061#include "llvm/Target/TargetInstrInfo.h"
Bob Wilson3e6fa462012-08-03 04:06:28 +000062#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng864fcc12008-08-20 22:45:34 +000063#include "llvm/Target/TargetLowering.h"
Dan Gohman02c84b82008-08-20 21:05:57 +000064#include "llvm/Target/TargetMachine.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000065using namespace llvm;
66
Chad Rosier61e8d102011-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 Rosier46addb92011-11-29 19:40:47 +000071STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosierff40b1e2011-11-16 21:05:28 +000072
Dan Gohmand7b5ce32010-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 Olesen6a7d6832013-07-04 04:53:49 +000079 // Instructions are appended to FuncInfo.MBB. If the basic block already
Jakob Stoklund Olesen3d8560c2013-07-04 04:32:39 +000080 // contains labels or copies, use the last instruction as the last local
81 // value.
Craig Topperc0196b12014-04-14 00:51:57 +000082 EmitStartPt = nullptr;
Jakob Stoklund Olesen3d8560c2013-07-04 04:32:39 +000083 if (!FuncInfo.MBB->empty())
84 EmitStartPt = &FuncInfo.MBB->back();
Ivan Krasind7cbd4c2011-08-18 22:06:10 +000085 LastLocalValue = EmitStartPt;
86}
87
Evan Cheng615620c2013-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 Lincfe7f352013-07-08 00:37:03 +000093
Evan Cheng615620c2013-02-11 01:27:15 +000094 if (!FastLowerArguments())
95 return false;
96
David Blaikie97c6c5b2013-06-21 22:56:30 +000097 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng615620c2013-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 Blaikie97c6c5b2013-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 Cheng615620c2013-02-11 01:27:15 +0000103 }
104 return true;
105}
106
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000107void FastISel::flushLocalValueMap() {
108 LocalValueMap.clear();
109 LastLocalValue = EmitStartPt;
110 recomputeInsertPt();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000111}
112
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000113bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman88fb2532010-05-14 22:53:18 +0000114 // Don't consider constants or arguments to have trivial kills.
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000115 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman88fb2532010-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))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000121 if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
Chandler Carruth7ec50852012-11-01 08:07:29 +0000122 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman88fb2532010-05-14 22:53:18 +0000123 return false;
124
Chad Rosier291ce472011-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 Gohman88fb2532010-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) &&
Chandler Carruthcdf47882014-03-09 03:16:01 +0000136 cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000137}
138
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000139unsigned FastISel::getRegForValue(const Value *V) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000140 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohmanca93aab2009-04-07 20:40:11 +0000141 // Don't handle non-simple values in FastISel.
142 if (!RealVT.isSimple())
143 return 0;
Dan Gohman4c315242008-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 Anderson9f944592009-08-11 20:47:22 +0000148 MVT VT = RealVT.getSimpleVT();
Dan Gohman4c315242008-12-08 07:57:47 +0000149 if (!TLI.isTypeLegal(VT)) {
Eli Friedmanc7035512011-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 Anderson117c9e82009-08-12 00:36:31 +0000152 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohman4c315242008-12-08 07:57:47 +0000153 else
154 return 0;
155 }
156
Eric Christopher1a06cc92012-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 Gohmane039d552008-09-03 23:32:19 +0000159 if (Reg != 0)
160 return Reg;
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000161
Dan Gohmana7c717d82010-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 Gohmand7b5ce32010-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 Gohmana7c717d82010-05-06 00:02:14 +0000168
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000169 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohmand7b5ce32010-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 Christopherf4fba5c2012-10-03 08:10:01 +0000175 leaveLocalValueArea(SaveInsertPt);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000176
177 return Reg;
Dan Gohman626b5d82010-05-03 23:36:34 +0000178}
179
Eric Christopher541f8012010-08-17 01:30:33 +0000180/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman626b5d82010-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 Gohmanbcaf6812010-04-15 01:51:59 +0000186 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman9801ba42008-09-19 22:16:54 +0000187 if (CI->getValue().getActiveBits() <= 64)
188 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman39d82f92008-09-10 20:11:02 +0000189 } else if (isa<AllocaInst>(V)) {
Dan Gohman9801ba42008-09-19 22:16:54 +0000190 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohmanc45733f2008-08-28 21:19:07 +0000191 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohmanc1d47c52008-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 Anderson55f1c092009-08-13 21:58:54 +0000194 Reg =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000195 getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext())));
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000196 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedman33c13392011-04-28 00:42:03 +0000197 if (CF->isNullValue()) {
Eli Friedman406c4712011-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 Gohmanb0b5a272008-08-27 18:10:19 +0000203
204 if (!Reg) {
Dan Gohman8a2dae52010-04-13 17:07:06 +0000205 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000206 const APFloat &Flt = CF->getValueAPF();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000207 EVT IntVT = TLI.getPointerTy();
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000208
209 uint64_t x[2];
210 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000211 bool isExact;
212 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopher997aaa92012-03-20 01:07:56 +0000213 APFloat::rmTowardZero, &isExact);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000214 if (isExact) {
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000215 APInt IntVal(IntBitWidth, x);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000216
Owen Anderson47db9412009-07-22 00:24:57 +0000217 unsigned IntegerReg =
Owen Andersonedb4a702009-07-24 23:12:02 +0000218 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman9801ba42008-09-19 22:16:54 +0000219 if (IntegerReg != 0)
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000220 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
221 IntegerReg, /*Kill=*/false);
Dan Gohman9801ba42008-09-19 22:16:54 +0000222 }
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000223 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000224 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman722f5fc2010-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 Gohman7c58cf72010-06-21 14:17:46 +0000229 Reg = lookUpRegForValue(Op);
Dan Gohmanc45733f2008-08-28 21:19:07 +0000230 } else if (isa<UndefValue>(V)) {
Dan Gohmane039d552008-09-03 23:32:19 +0000231 Reg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000233 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000234 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000235
Dan Gohman3663f152008-09-25 01:28:51 +0000236 // If target-independent code couldn't handle the value, give target-specific
237 // code a try.
Owen Anderson1dd2e402008-09-05 23:36:01 +0000238 if (!Reg && isa<Constant>(V))
Dan Gohman9801ba42008-09-19 22:16:54 +0000239 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peck527da1b2010-11-23 03:31:01 +0000240
Dan Gohman9801ba42008-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 Gohmand7b5ce32010-07-10 09:00:22 +0000243 if (Reg != 0) {
Dan Gohman3663f152008-09-25 01:28:51 +0000244 LocalValueMap[V] = Reg;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000245 LastLocalValue = MRI.getVRegDef(Reg);
246 }
Dan Gohmane039d552008-09-03 23:32:19 +0000247 return Reg;
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000248}
249
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000250unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng1e979012008-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 Gohman626b5d82010-05-03 23:36:34 +0000254 // def-dominates-use requirement enforced.
Dan Gohman87fb4e82010-07-07 16:29:44 +0000255 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
256 if (I != FuncInfo.ValueMap.end())
Dan Gohmanf91aff52010-06-21 14:21:47 +0000257 return I->second;
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000258 return LocalValueMap[V];
Evan Cheng1e979012008-09-09 01:26:59 +0000259}
260
Owen Anderson6f0c51d2008-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 Friedmana4d4a012011-05-16 21:06:17 +0000267void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohmanfcf54562008-09-05 18:18:20 +0000268 if (!isa<Instruction>(I)) {
269 LocalValueMap[I] = Reg;
Eli Friedmana4d4a012011-05-16 21:06:17 +0000270 return;
Dan Gohmanfcf54562008-09-05 18:18:20 +0000271 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000272
Dan Gohman87fb4e82010-07-07 16:29:44 +0000273 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000274 if (AssignedReg == 0)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000275 // Use the new register.
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000276 AssignedReg = Reg;
Chris Lattnera101f6f2009-04-12 07:46:30 +0000277 else if (Reg != AssignedReg) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000278 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedmana4d4a012011-05-16 21:06:17 +0000279 for (unsigned i = 0; i < NumRegs; i++)
280 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000281
282 AssignedReg = Reg;
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000283 }
Owen Anderson6f0c51d2008-08-30 00:38:46 +0000284}
285
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000286std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohman4c315242008-12-08 07:57:47 +0000287 unsigned IdxN = getRegForValue(Idx);
288 if (IdxN == 0)
289 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000290 return std::pair<unsigned, bool>(0, false);
291
292 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohman4c315242008-12-08 07:57:47 +0000293
294 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Andersonc6daf8f2009-08-11 21:59:30 +0000295 MVT PtrVT = TLI.getPointerTy();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000296 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohman1a1b51f2010-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 Gohman4c315242008-12-08 07:57:47 +0000308}
309
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000310void FastISel::recomputeInsertPt() {
311 if (getLastLocalValue()) {
312 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanb5e918d2010-07-19 22:48:56 +0000313 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohmand7b5ce32010-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 Rosier46addb92011-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 Voung7857a642013-03-08 22:56:31 +0000331 ++NumFastIselDead;
Chad Rosier46addb92011-11-29 19:40:47 +0000332 }
333 recomputeInsertPt();
334}
335
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000336FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000337 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000338 DebugLoc OldDL = DbgLoc;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000339 recomputeInsertPt();
Rafael Espindolaea09c592014-02-18 22:05:46 +0000340 DbgLoc = DebugLoc();
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000341 SavePoint SP = { OldInsertPt, OldDL };
342 return SP;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000343}
344
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000345void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000346 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000347 LastLocalValue = std::prev(FuncInfo.InsertPt);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000348
349 // Restore the previous insert position.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000350 FuncInfo.InsertPt = OldInsertPt.InsertPt;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000351 DbgLoc = OldInsertPt.DL;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000352}
353
Dan Gohmana3e4d5a2008-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 Gohmanbcaf6812010-04-15 01:51:59 +0000357bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000358 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson9f944592009-08-11 20:47:22 +0000359 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000360 // Unhandled type. Halt "fast" selection and bail.
361 return false;
Dan Gohmanfd634592008-09-05 18:44:22 +0000362
Dan Gohman3bcbbec2008-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 Gohmanfd634592008-09-05 18:44:22 +0000367 if (!TLI.isTypeLegal(VT)) {
Owen Anderson9f944592009-08-11 20:47:22 +0000368 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohmanfd634592008-09-05 18:44:22 +0000369 // don't require additional zeroing, which makes them easy.
Owen Anderson9f944592009-08-11 20:47:22 +0000370 if (VT == MVT::i1 &&
Dan Gohman5e490a72008-09-25 17:22:52 +0000371 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
372 ISDOpcode == ISD::XOR))
Owen Anderson117c9e82009-08-12 00:36:31 +0000373 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohmanfd634592008-09-05 18:44:22 +0000374 else
375 return false;
376 }
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000377
Chris Lattnerfba7ca62011-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 Andersondd450b82011-04-22 23:38:06 +0000386
Chris Lattnerb53ccb82011-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 Andersondd450b82011-04-22 23:38:06 +0000391
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000392 // We successfully emitted code for the given LLVM Instruction.
393 UpdateValueMap(I, ResultReg);
394 return true;
Chris Lattnerfba7ca62011-04-17 01:16:47 +0000395 }
Owen Andersondd450b82011-04-22 23:38:06 +0000396
397
Dan Gohman7bda51f2008-09-03 23:12:08 +0000398 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000399 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmanfe905652008-08-21 01:41:07 +0000400 return false;
401
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000402 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
403
Dan Gohmanfe905652008-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 Lattnerb53ccb82011-04-17 20:23:29 +0000406 uint64_t Imm = CI->getZExtValue();
Owen Andersondd450b82011-04-22 23:38:06 +0000407
Chris Lattner48f75ad2011-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 Andersondd450b82011-04-22 23:38:06 +0000415
Chad Rosier6a63a742012-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 Lattnerb53ccb82011-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 Andersondd450b82011-04-22 23:38:06 +0000426
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000427 // We successfully emitted code for the given LLVM Instruction.
428 UpdateValueMap(I, ResultReg);
429 return true;
Dan Gohmanfe905652008-08-21 01:41:07 +0000430 }
431
Dan Gohman5ca269e2008-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 Gohmanb0b5a272008-08-27 18:10:19 +0000434 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000435 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000436 if (ResultReg != 0) {
437 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000438 UpdateValueMap(I, ResultReg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000439 return true;
440 }
Dan Gohman5ca269e2008-08-27 01:09:54 +0000441 }
442
Dan Gohman7bda51f2008-09-03 23:12:08 +0000443 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmanfe905652008-08-21 01:41:07 +0000444 if (Op1 == 0)
445 // Unhandled operand. Halt "fast" selection and bail.
446 return false;
447
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000448 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
449
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000450 // Now we have both operands in registers. Emit the instruction.
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000451 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000452 ISDOpcode,
453 Op0, Op0IsKill,
454 Op1, Op1IsKill);
Dan Gohmana3e4d5a2008-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 Gohmanb16a7782008-08-20 00:23:20 +0000460 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000461 UpdateValueMap(I, ResultReg);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000462 return true;
463}
464
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000465bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman7bda51f2008-09-03 23:12:08 +0000466 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng864fcc12008-08-20 22:45:34 +0000467 if (N == 0)
468 // Unhandled operand. Halt "fast" selection and bail.
469 return false;
470
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000471 bool NIsKill = hasTrivialKill(I->getOperand(0));
472
Chad Rosierf83ab702011-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 Lattner229907c2011-07-18 04:54:35 +0000478 Type *Ty = I->getOperand(0)->getType();
Owen Anderson9f944592009-08-11 20:47:22 +0000479 MVT VT = TLI.getPointerTy();
Dan Gohmanbcaf6812010-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 Lattner229907c2011-07-18 04:54:35 +0000483 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng864fcc12008-08-20 22:45:34 +0000484 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
485 if (Field) {
486 // N = N + Offset
Rafael Espindolaea09c592014-02-18 22:05:46 +0000487 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
Chad Rosierf83ab702011-11-17 07:15:58 +0000488 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 Cheng864fcc12008-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 Gohmanbcaf6812010-04-15 01:51:59 +0000502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf1d83042010-06-18 14:22:04 +0000503 if (CI->isZero()) continue;
Chad Rosierf83ab702011-11-17 07:15:58 +0000504 // N = N + Offset
Chad Rosier879c34f2012-07-06 17:44:22 +0000505 TotalOffs +=
Rafael Espindolaea09c592014-02-18 22:05:46 +0000506 DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosierf83ab702011-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 Cheng864fcc12008-08-20 22:45:34 +0000519 if (N == 0)
520 // Unhandled operand. Halt "fast" selection and bail.
521 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000522 NIsKill = true;
Chad Rosierf83ab702011-11-17 07:15:58 +0000523 TotalOffs = 0;
Evan Cheng864fcc12008-08-20 22:45:34 +0000524 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000525
Evan Cheng864fcc12008-08-20 22:45:34 +0000526 // N = N + Idx * ElementSize;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000527 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000528 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
529 unsigned IdxN = Pair.first;
530 bool IdxNIsKill = Pair.second;
Evan Cheng864fcc12008-08-20 22:45:34 +0000531 if (IdxN == 0)
532 // Unhandled operand. Halt "fast" selection and bail.
533 return false;
534
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000535 if (ElementSize != 1) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000536 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000537 if (IdxN == 0)
538 // Unhandled operand. Halt "fast" selection and bail.
539 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000540 IdxNIsKill = true;
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000541 }
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000542 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng864fcc12008-08-20 22:45:34 +0000543 if (N == 0)
544 // Unhandled operand. Halt "fast" selection and bail.
545 return false;
546 }
547 }
Chad Rosierf83ab702011-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 Cheng864fcc12008-08-20 22:45:34 +0000554
555 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000556 UpdateValueMap(I, N);
Evan Cheng864fcc12008-08-20 22:45:34 +0000557 return true;
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000558}
559
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000560bool FastISel::SelectCall(const User *I) {
Dan Gohman7da91ae2011-04-26 17:18:34 +0000561 const CallInst *Call = cast<CallInst>(I);
562
563 // Handle simple inline asms.
Dan Gohmande239d22011-10-12 15:56:56 +0000564 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohman7da91ae2011-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
Rafael Espindolaea09c592014-02-18 22:05:46 +0000575 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohman7da91ae2011-04-26 17:18:34 +0000576 TII.get(TargetOpcode::INLINEASM))
577 .addExternalSymbol(IA->getAsmString().c_str())
578 .addImm(ExtraInfo);
579 return true;
580 }
581
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000582 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
583 ComputeUsesVAFloatArgument(*Call, &MMI);
584
Dan Gohman7da91ae2011-04-26 17:18:34 +0000585 const Function *F = Call->getCalledFunction();
Dan Gohman32a733e2008-09-25 17:05:24 +0000586 if (!F) return false;
587
Dan Gohman8a2dae52010-04-13 17:07:06 +0000588 // Handle selected intrinsic function calls.
Chris Lattner91328b32011-04-19 05:52:03 +0000589 switch (F->getIntrinsicID()) {
Dan Gohman32a733e2008-09-25 17:05:24 +0000590 default: break;
Chad Rosiera33015d2012-05-11 23:21:01 +0000591 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher81e2bf22012-02-17 23:03:39 +0000592 case Intrinsic::lifetime_start:
593 case Intrinsic::lifetime_end:
Chad Rosier88d53ea2012-07-06 17:33:39 +0000594 // The donothing intrinsic does, well, nothing.
595 case Intrinsic::donothing:
Eric Christopher81e2bf22012-02-17 23:03:39 +0000596 return true;
Chad Rosier88d53ea2012-07-06 17:33:39 +0000597
Bill Wendling65c0fd42009-02-13 02:16:35 +0000598 case Intrinsic::dbg_declare: {
Dan Gohman7da91ae2011-04-26 17:18:34 +0000599 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Manman Ren983a16c2013-06-28 05:43:10 +0000600 DIVariable DIVar(DI->getVariable());
Stephen Lincfe7f352013-07-08 00:37:03 +0000601 assert((!DIVar || DIVar.isVariable()) &&
Manman Ren983a16c2013-06-28 05:43:10 +0000602 "Variable in DbgDeclareInst should be either null or a DIVariable.");
603 if (!DIVar ||
Eric Christopher142820b2012-03-15 21:33:44 +0000604 !FuncInfo.MF->getMMI().hasDebugInfo()) {
605 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel87127712009-07-02 22:43:26 +0000606 return true;
Eric Christopher142820b2012-03-15 21:33:44 +0000607 }
Devang Patel87127712009-07-02 22:43:26 +0000608
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000609 const Value *Address = DI->getAddress();
Eric Christopher3390a6e2012-03-15 21:33:47 +0000610 if (!Address || isa<UndefValue>(Address)) {
Eric Christopher142820b2012-03-15 21:33:44 +0000611 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendb2eb472010-02-06 02:26:02 +0000612 return true;
Eric Christopher142820b2012-03-15 21:33:44 +0000613 }
Devang Patele4682fa2010-09-14 20:29:31 +0000614
Adrian Prantl418d1d12013-07-09 20:28:37 +0000615 unsigned Offset = 0;
David Blaikie0252265b2013-06-16 20:34:15 +0000616 Optional<MachineOperand> Op;
617 if (const Argument *Arg = dyn_cast<Argument>(Address))
Devang Patel9d904e12011-09-08 22:59:09 +0000618 // Some arguments' frame index is recorded during argument lowering.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000619 Offset = FuncInfo.getArgumentFrameIndex(Arg);
620 if (Offset)
621 Op = MachineOperand::CreateFI(Offset);
David Blaikie0252265b2013-06-16 20:34:15 +0000622 if (!Op)
623 if (unsigned Reg = lookUpRegForValue(Address))
624 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher60e01c52012-03-20 01:07:58 +0000625
Bill Wendling9f829f12012-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 Blaikie0252265b2013-06-16 20:34:15 +0000637 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher60e01c52012-03-20 01:07:58 +0000638 (!isa<AllocaInst>(Address) ||
639 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie0252265b2013-06-16 20:34:15 +0000640 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
Adrian Prantl262bcf42013-09-18 22:08:59 +0000641 false);
Wesley Peck527da1b2010-11-23 03:31:01 +0000642
Adrian Prantl262bcf42013-09-18 22:08:59 +0000643 if (Op) {
Adrian Prantl418d1d12013-07-09 20:28:37 +0000644 if (Op->isReg()) {
645 Op->setIsDebug(true);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000646 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie6004dbc2013-10-14 20:15:04 +0000647 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
648 DI->getVariable());
649 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000650 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie6004dbc2013-10-14 20:15:04 +0000651 TII.get(TargetOpcode::DBG_VALUE))
652 .addOperand(*Op)
653 .addImm(0)
654 .addMetadata(DI->getVariable());
Adrian Prantl262bcf42013-09-18 22:08:59 +0000655 } else {
Eric Christophere5e54c82012-03-20 01:07:53 +0000656 // We can't yet handle anything else here because it would require
657 // generating code, thus altering codegen because of debug info.
Adrian Prantl0d1e5592013-05-22 18:02:19 +0000658 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Adrian Prantl262bcf42013-09-18 22:08:59 +0000659 }
Dan Gohman32a733e2008-09-25 17:05:24 +0000660 return true;
Bill Wendling65c0fd42009-02-13 02:16:35 +0000661 }
Dale Johannesendd331042010-02-26 20:01:55 +0000662 case Intrinsic::dbg_value: {
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000663 // This form of DBG_VALUE is target-independent.
Dan Gohman7da91ae2011-04-26 17:18:34 +0000664 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000665 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000666 const Value *V = DI->getValue();
Dale Johannesendd331042010-02-26 20:01:55 +0000667 if (!V) {
668 // Currently the optimizer can produce this; insert an undef to
669 // help debugging. Probably the optimizer should not do this.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000671 .addReg(0U).addImm(DI->getOffset())
672 .addMetadata(DI->getVariable());
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000673 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patelf071d722011-06-24 20:46:11 +0000674 if (CI->getBitWidth() > 64)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000675 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patelf071d722011-06-24 20:46:11 +0000676 .addCImm(CI).addImm(DI->getOffset())
677 .addMetadata(DI->getVariable());
Chad Rosier879c34f2012-07-06 17:44:22 +0000678 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patelf071d722011-06-24 20:46:11 +0000680 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
681 .addMetadata(DI->getVariable());
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000682 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000684 .addFPImm(CF).addImm(DI->getOffset())
685 .addMetadata(DI->getVariable());
Dale Johannesendd331042010-02-26 20:01:55 +0000686 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000687 // FIXME: This does not handle register-indirect values at offset 0.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000688 bool IsIndirect = DI->getOffset() != 0;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000689 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect,
Adrian Prantl418d1d12013-07-09 20:28:37 +0000690 Reg, DI->getOffset(), DI->getVariable());
Dale Johannesendd331042010-02-26 20:01:55 +0000691 } else {
692 // We can't yet handle anything else here because it would require
693 // generating code, thus altering codegen because of debug info.
Adrian Prantl0d1e5592013-05-22 18:02:19 +0000694 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peck527da1b2010-11-23 03:31:01 +0000695 }
Dale Johannesendd331042010-02-26 20:01:55 +0000696 return true;
697 }
Eli Friedman8f1e11c2011-05-14 00:47:51 +0000698 case Intrinsic::objectsize: {
699 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
700 unsigned long long Res = CI->isZero() ? -1ULL : 0;
701 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
702 unsigned ResultReg = getRegForValue(ResCI);
703 if (ResultReg == 0)
704 return false;
705 UpdateValueMap(Call, ResultReg);
706 return true;
707 }
Chad Rosier9c1796f2013-03-07 20:42:17 +0000708 case Intrinsic::expect: {
Chad Rosier3a200e12013-03-07 21:38:33 +0000709 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky48beb212013-03-11 21:44:37 +0000710 if (ResultReg == 0)
711 return false;
Chad Rosier3a200e12013-03-07 21:38:33 +0000712 UpdateValueMap(Call, ResultReg);
713 return true;
Chad Rosier9c1796f2013-03-07 20:42:17 +0000714 }
Dan Gohman32a733e2008-09-25 17:05:24 +0000715 }
Dan Gohman8a2dae52010-04-13 17:07:06 +0000716
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000717 // Usually, it does not make sense to initialize a value,
718 // make an unrelated function call and use the value, because
719 // it tends to be spilled on the stack. So, we move the pointer
720 // to the last local value to the beginning of the block, so that
721 // all the values which have already been materialized,
722 // appear after the call. It also makes sense to skip intrinsics
723 // since they tend to be inlined.
Pete Cooper047f81a2013-02-22 01:50:38 +0000724 if (!isa<IntrinsicInst>(Call))
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000725 flushLocalValueMap();
726
Dan Gohman8a2dae52010-04-13 17:07:06 +0000727 // An arbitrary call. Bail.
Dan Gohman32a733e2008-09-25 17:05:24 +0000728 return false;
729}
730
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000731bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000732 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
733 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +0000734
Owen Anderson9f944592009-08-11 20:47:22 +0000735 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
736 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersonca1711a2008-08-26 23:46:32 +0000737 // Unhandled type. Halt "fast" selection and bail.
738 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000739
Eli Friedmanc7035512011-05-25 23:49:02 +0000740 // Check if the destination type is legal.
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000741 if (!TLI.isTypeLegal(DstVT))
Eli Friedmanc7035512011-05-25 23:49:02 +0000742 return false;
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000743
Eli Friedmanc7035512011-05-25 23:49:02 +0000744 // Check if the source operand is legal.
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000745 if (!TLI.isTypeLegal(SrcVT))
Eli Friedmanc7035512011-05-25 23:49:02 +0000746 return false;
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000747
Dan Gohman7bda51f2008-09-03 23:12:08 +0000748 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersonca1711a2008-08-26 23:46:32 +0000749 if (!InputReg)
750 // Unhandled operand. Halt "fast" selection and bail.
751 return false;
Dan Gohmanc0bb9592009-03-13 20:42:20 +0000752
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000753 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
754
Owen Andersonca1711a2008-08-26 23:46:32 +0000755 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
756 DstVT.getSimpleVT(),
757 Opcode,
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000758 InputReg, InputRegIsKill);
Owen Andersonca1711a2008-08-26 23:46:32 +0000759 if (!ResultReg)
760 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000761
Dan Gohman7bda51f2008-09-03 23:12:08 +0000762 UpdateValueMap(I, ResultReg);
Owen Andersonca1711a2008-08-26 23:46:32 +0000763 return true;
764}
765
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000766bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000767 // If the bitcast doesn't change the type, just use the operand value.
768 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman7bda51f2008-09-03 23:12:08 +0000769 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohman61cfa302008-08-27 20:41:38 +0000770 if (Reg == 0)
771 return false;
Dan Gohman7bda51f2008-09-03 23:12:08 +0000772 UpdateValueMap(I, Reg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000773 return true;
774 }
775
Wesley Peck527da1b2010-11-23 03:31:01 +0000776 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglundc494d242012-12-17 14:30:06 +0000777 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
778 EVT DstEVT = TLI.getValueType(I->getType());
779 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
780 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersonca1711a2008-08-26 23:46:32 +0000781 // Unhandled type. Halt "fast" selection and bail.
782 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000783
Patrik Hagglundc494d242012-12-17 14:30:06 +0000784 MVT SrcVT = SrcEVT.getSimpleVT();
785 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman7bda51f2008-09-03 23:12:08 +0000786 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000787 if (Op0 == 0)
788 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersonca1711a2008-08-26 23:46:32 +0000789 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000790
791 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +0000792
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000793 // First, try to perform the bitcast by inserting a reg-reg copy.
794 unsigned ResultReg = 0;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000795 if (SrcVT == DstVT) {
Craig Topper760b1342012-02-22 05:59:10 +0000796 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
797 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesen51642ae2010-07-11 05:16:54 +0000798 // Don't attempt a cross-class copy. It will likely fail.
799 if (SrcClass == DstClass) {
800 ResultReg = createResultReg(DstClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
802 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
Jakob Stoklund Olesen51642ae2010-07-11 05:16:54 +0000803 }
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000804 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000805
806 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000807 if (!ResultReg)
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000808 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peck527da1b2010-11-23 03:31:01 +0000809
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000810 if (!ResultReg)
Owen Andersonca1711a2008-08-26 23:46:32 +0000811 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000812
Dan Gohman7bda51f2008-09-03 23:12:08 +0000813 UpdateValueMap(I, ResultReg);
Owen Andersonca1711a2008-08-26 23:46:32 +0000814 return true;
815}
816
Dan Gohman7bda51f2008-09-03 23:12:08 +0000817bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000818FastISel::SelectInstruction(const Instruction *I) {
Dan Gohman6e9a8fc2010-04-23 15:29:50 +0000819 // Just before the terminator instruction, insert instructions to
820 // feed PHI nodes in successor blocks.
821 if (isa<TerminatorInst>(I))
822 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
823 return false;
824
Rafael Espindolaea09c592014-02-18 22:05:46 +0000825 DbgLoc = I->getDebugLoc();
Dan Gohmane450d742010-04-20 00:48:35 +0000826
Chad Rosier46addb92011-11-29 19:40:47 +0000827 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
828
Bob Wilson3e6fa462012-08-03 04:06:28 +0000829 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
830 const Function *F = Call->getCalledFunction();
831 LibFunc::Func Func;
Akira Hatanaka3d90f992014-04-15 21:30:06 +0000832
833 // As a special case, don't handle calls to builtin library functions that
834 // may be translated directly to target instructions.
Bob Wilson3e6fa462012-08-03 04:06:28 +0000835 if (F && !F->hasLocalLinkage() && F->hasName() &&
836 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson871701c2012-08-03 21:26:24 +0000837 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilson3e6fa462012-08-03 04:06:28 +0000838 return false;
Akira Hatanaka3d90f992014-04-15 21:30:06 +0000839
840 // Don't handle Intrinsic::trap if a trap funciton is specified.
841 if (F && F->getIntrinsicID() == Intrinsic::trap &&
842 !TM.Options.getTrapFunctionName().empty())
843 return false;
Bob Wilson3e6fa462012-08-03 04:06:28 +0000844 }
845
Dan Gohman18f94462009-12-05 01:27:58 +0000846 // First, try doing target-independent selection.
Michael Ilsemanba8446c2013-02-27 19:54:00 +0000847 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000848 ++NumFastIselSuccessIndependent;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000849 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +0000850 return true;
Dan Gohmane450d742010-04-20 00:48:35 +0000851 }
Chad Rosier879c34f2012-07-06 17:44:22 +0000852 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosier46addb92011-11-29 19:40:47 +0000853 // the local value map and recomputed the insert point.
854 if (!isa<CallInst>(I)) {
855 recomputeInsertPt();
856 if (SavedInsertPt != FuncInfo.InsertPt)
857 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
858 }
Dan Gohman18f94462009-12-05 01:27:58 +0000859
860 // Next, try calling the target to attempt to handle the instruction.
Chad Rosier46addb92011-11-29 19:40:47 +0000861 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohmane450d742010-04-20 00:48:35 +0000862 if (TargetSelectInstruction(I)) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000863 ++NumFastIselSuccessTarget;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000864 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +0000865 return true;
Dan Gohmane450d742010-04-20 00:48:35 +0000866 }
Chad Rosier46addb92011-11-29 19:40:47 +0000867 // Check for dead code and remove as necessary.
868 recomputeInsertPt();
869 if (SavedInsertPt != FuncInfo.InsertPt)
870 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman18f94462009-12-05 01:27:58 +0000871
Rafael Espindolaea09c592014-02-18 22:05:46 +0000872 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +0000873 return false;
Dan Gohmanfcf54562008-09-05 18:18:20 +0000874}
875
Dan Gohman1ab1d312008-10-02 22:15:21 +0000876/// FastEmitBranch - Emit an unconditional branch to the given block,
877/// unless it is the immediate (fall-through) successor, and update
878/// the CFG.
879void
Rafael Espindolaea09c592014-02-18 22:05:46 +0000880FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
Eric Christophere9abba72012-04-10 18:18:10 +0000881
Evan Cheng615620c2013-02-11 01:27:15 +0000882 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
883 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christophere9abba72012-04-10 18:18:10 +0000884 // For more accurate line information if this is the only instruction
885 // in the block then emit it, otherwise we have the unconditional
886 // fall-through case, which needs no instructions.
Dan Gohman1ab1d312008-10-02 22:15:21 +0000887 } else {
888 // The unconditional branch case.
Craig Topperc0196b12014-04-14 00:51:57 +0000889 TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000890 SmallVector<MachineOperand, 0>(), DbgLoc);
Dan Gohman1ab1d312008-10-02 22:15:21 +0000891 }
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000892 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohman1ab1d312008-10-02 22:15:21 +0000893}
894
Dan Gohmanaa92dc12009-09-03 22:53:57 +0000895/// SelectFNeg - Emit an FNeg operation.
896///
897bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000898FastISel::SelectFNeg(const User *I) {
Dan Gohmanaa92dc12009-09-03 22:53:57 +0000899 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
900 if (OpReg == 0) return false;
901
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000902 bool OpRegIsKill = hasTrivialKill(I);
903
Dan Gohman9cbef322009-09-11 00:36:43 +0000904 // If the target has ISD::FNEG, use it.
905 EVT VT = TLI.getValueType(I->getType());
906 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000907 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman9cbef322009-09-11 00:36:43 +0000908 if (ResultReg != 0) {
909 UpdateValueMap(I, ResultReg);
910 return true;
911 }
912
Dan Gohman89b090e2009-09-11 00:34:46 +0000913 // Bitcast the value to integer, twiddle the sign bit with xor,
914 // and then bitcast it back to floating-point.
Dan Gohmanaa92dc12009-09-03 22:53:57 +0000915 if (VT.getSizeInBits() > 64) return false;
Dan Gohman89b090e2009-09-11 00:34:46 +0000916 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
917 if (!TLI.isTypeLegal(IntVT))
918 return false;
919
920 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +0000921 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman89b090e2009-09-11 00:34:46 +0000922 if (IntReg == 0)
923 return false;
924
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000925 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
926 IntReg, /*Kill=*/true,
Dan Gohman89b090e2009-09-11 00:34:46 +0000927 UINT64_C(1) << (VT.getSizeInBits()-1),
928 IntVT.getSimpleVT());
929 if (IntResultReg == 0)
930 return false;
931
932 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +0000933 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohmanaa92dc12009-09-03 22:53:57 +0000934 if (ResultReg == 0)
935 return false;
936
937 UpdateValueMap(I, ResultReg);
938 return true;
939}
940
Dan Gohmanfcf54562008-09-05 18:18:20 +0000941bool
Eli Friedman9ac94472011-05-16 20:27:46 +0000942FastISel::SelectExtractValue(const User *U) {
943 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedman4c08bb42011-05-16 20:34:53 +0000944 if (!EVI)
Eli Friedman9ac94472011-05-16 20:27:46 +0000945 return false;
946
Eli Friedmana4d4a012011-05-16 21:06:17 +0000947 // Make sure we only try to handle extracts with a legal result. But also
948 // allow i1 because it's easy.
Eli Friedman9ac94472011-05-16 20:27:46 +0000949 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
950 if (!RealVT.isSimple())
951 return false;
952 MVT VT = RealVT.getSimpleVT();
Eli Friedmana4d4a012011-05-16 21:06:17 +0000953 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman9ac94472011-05-16 20:27:46 +0000954 return false;
955
956 const Value *Op0 = EVI->getOperand(0);
Chris Lattner229907c2011-07-18 04:54:35 +0000957 Type *AggTy = Op0->getType();
Eli Friedman9ac94472011-05-16 20:27:46 +0000958
959 // Get the base result register.
960 unsigned ResultReg;
961 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
962 if (I != FuncInfo.ValueMap.end())
963 ResultReg = I->second;
Eli Friedmanbd375f12011-06-06 05:46:34 +0000964 else if (isa<Instruction>(Op0))
Eli Friedman9ac94472011-05-16 20:27:46 +0000965 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedmanbd375f12011-06-06 05:46:34 +0000966 else
967 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman9ac94472011-05-16 20:27:46 +0000968
969 // Get the actual result register, which is an offset from the base register.
Jay Foad57aa6362011-07-13 10:26:04 +0000970 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman9ac94472011-05-16 20:27:46 +0000971
972 SmallVector<EVT, 4> AggValueVTs;
973 ComputeValueVTs(TLI, AggTy, AggValueVTs);
974
975 for (unsigned i = 0; i < VTIndex; i++)
976 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
977
978 UpdateValueMap(EVI, ResultReg);
979 return true;
980}
981
982bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000983FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohmanfcf54562008-09-05 18:18:20 +0000984 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +0000985 case Instruction::Add:
986 return SelectBinaryOp(I, ISD::ADD);
987 case Instruction::FAdd:
988 return SelectBinaryOp(I, ISD::FADD);
989 case Instruction::Sub:
990 return SelectBinaryOp(I, ISD::SUB);
991 case Instruction::FSub:
Dan Gohmanaa92dc12009-09-03 22:53:57 +0000992 // FNeg is currently represented in LLVM IR as a special case of FSub.
993 if (BinaryOperator::isFNeg(I))
994 return SelectFNeg(I);
Dan Gohmana5b96452009-06-04 22:49:04 +0000995 return SelectBinaryOp(I, ISD::FSUB);
996 case Instruction::Mul:
997 return SelectBinaryOp(I, ISD::MUL);
998 case Instruction::FMul:
999 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001000 case Instruction::SDiv:
1001 return SelectBinaryOp(I, ISD::SDIV);
1002 case Instruction::UDiv:
1003 return SelectBinaryOp(I, ISD::UDIV);
1004 case Instruction::FDiv:
1005 return SelectBinaryOp(I, ISD::FDIV);
1006 case Instruction::SRem:
1007 return SelectBinaryOp(I, ISD::SREM);
1008 case Instruction::URem:
1009 return SelectBinaryOp(I, ISD::UREM);
1010 case Instruction::FRem:
1011 return SelectBinaryOp(I, ISD::FREM);
1012 case Instruction::Shl:
1013 return SelectBinaryOp(I, ISD::SHL);
1014 case Instruction::LShr:
1015 return SelectBinaryOp(I, ISD::SRL);
1016 case Instruction::AShr:
1017 return SelectBinaryOp(I, ISD::SRA);
1018 case Instruction::And:
1019 return SelectBinaryOp(I, ISD::AND);
1020 case Instruction::Or:
1021 return SelectBinaryOp(I, ISD::OR);
1022 case Instruction::Xor:
1023 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001024
Dan Gohman7bda51f2008-09-03 23:12:08 +00001025 case Instruction::GetElementPtr:
1026 return SelectGetElementPtr(I);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +00001027
Dan Gohman7bda51f2008-09-03 23:12:08 +00001028 case Instruction::Br: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001029 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +00001030
Dan Gohman7bda51f2008-09-03 23:12:08 +00001031 if (BI->isUnconditional()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001032 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001033 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings0125b642010-06-17 22:43:56 +00001034 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman7bda51f2008-09-03 23:12:08 +00001035 return true;
Owen Anderson14054922008-08-27 00:31:01 +00001036 }
Dan Gohman7bda51f2008-09-03 23:12:08 +00001037
1038 // Conditional branches are not handed yet.
1039 // Halt "fast" selection and bail.
1040 return false;
Dan Gohmanb2226e22008-08-13 20:19:35 +00001041 }
1042
Dan Gohmanea56bdd2008-09-05 01:08:41 +00001043 case Instruction::Unreachable:
Yaron Kerend7ba46b2014-04-19 13:47:43 +00001044 if (TM.Options.TrapUnreachable)
1045 return FastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
1046 else
1047 return true;
Dan Gohmanea56bdd2008-09-05 01:08:41 +00001048
Dan Gohman39d82f92008-09-10 20:11:02 +00001049 case Instruction::Alloca:
1050 // FunctionLowering has the static-sized case covered.
Dan Gohman87fb4e82010-07-07 16:29:44 +00001051 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman39d82f92008-09-10 20:11:02 +00001052 return true;
1053
1054 // Dynamic-sized alloca is not handled yet.
1055 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001056
Dan Gohman32a733e2008-09-25 17:05:24 +00001057 case Instruction::Call:
1058 return SelectCall(I);
Wesley Peck527da1b2010-11-23 03:31:01 +00001059
Dan Gohman7bda51f2008-09-03 23:12:08 +00001060 case Instruction::BitCast:
1061 return SelectBitCast(I);
1062
1063 case Instruction::FPToSI:
1064 return SelectCast(I, ISD::FP_TO_SINT);
1065 case Instruction::ZExt:
1066 return SelectCast(I, ISD::ZERO_EXTEND);
1067 case Instruction::SExt:
1068 return SelectCast(I, ISD::SIGN_EXTEND);
1069 case Instruction::Trunc:
1070 return SelectCast(I, ISD::TRUNCATE);
1071 case Instruction::SIToFP:
1072 return SelectCast(I, ISD::SINT_TO_FP);
1073
1074 case Instruction::IntToPtr: // Deliberate fall-through.
1075 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001076 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1077 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman7bda51f2008-09-03 23:12:08 +00001078 if (DstVT.bitsGT(SrcVT))
1079 return SelectCast(I, ISD::ZERO_EXTEND);
1080 if (DstVT.bitsLT(SrcVT))
1081 return SelectCast(I, ISD::TRUNCATE);
1082 unsigned Reg = getRegForValue(I->getOperand(0));
1083 if (Reg == 0) return false;
1084 UpdateValueMap(I, Reg);
1085 return true;
1086 }
Dan Gohman918fe082008-09-23 21:53:34 +00001087
Eli Friedman9ac94472011-05-16 20:27:46 +00001088 case Instruction::ExtractValue:
1089 return SelectExtractValue(I);
1090
Dan Gohmanf41ad472010-04-20 15:00:41 +00001091 case Instruction::PHI:
1092 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1093
Dan Gohman7bda51f2008-09-03 23:12:08 +00001094 default:
1095 // Unhandled instruction. Halt "fast" selection and bail.
1096 return false;
1097 }
Dan Gohmanb2226e22008-08-13 20:19:35 +00001098}
1099
Bob Wilson3e6fa462012-08-03 04:06:28 +00001100FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1101 const TargetLibraryInfo *libInfo)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001102 : FuncInfo(funcInfo),
Dan Gohman87fb4e82010-07-07 16:29:44 +00001103 MRI(FuncInfo.MF->getRegInfo()),
1104 MFI(*FuncInfo.MF->getFrameInfo()),
1105 MCP(*FuncInfo.MF->getConstantPool()),
1106 TM(FuncInfo.MF->getTarget()),
Rafael Espindolaea09c592014-02-18 22:05:46 +00001107 DL(*TM.getDataLayout()),
Dan Gohman49e19e92008-08-22 00:20:26 +00001108 TII(*TM.getInstrInfo()),
Dan Gohmanffcb5902010-05-05 23:58:35 +00001109 TLI(*TM.getTargetLowering()),
Bob Wilson3e6fa462012-08-03 04:06:28 +00001110 TRI(*TM.getRegisterInfo()),
1111 LibInfo(libInfo) {
Dan Gohman02c84b82008-08-20 21:05:57 +00001112}
1113
Dan Gohmanc4442382008-08-14 21:51:29 +00001114FastISel::~FastISel() {}
1115
Evan Cheng615620c2013-02-11 01:27:15 +00001116bool FastISel::FastLowerArguments() {
1117 return false;
1118}
1119
Owen Anderson9f944592009-08-11 20:47:22 +00001120unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman404a9842010-01-05 22:26:32 +00001121 unsigned) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001122 return 0;
1123}
1124
Owen Anderson9f944592009-08-11 20:47:22 +00001125unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001126 unsigned,
1127 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001128 return 0;
1129}
1130
Wesley Peck527da1b2010-11-23 03:31:01 +00001131unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001132 unsigned,
1133 unsigned /*Op0*/, bool /*Op0IsKill*/,
1134 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001135 return 0;
1136}
1137
Dan Gohman404a9842010-01-05 22:26:32 +00001138unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng864fcc12008-08-20 22:45:34 +00001139 return 0;
1140}
1141
Owen Anderson9f944592009-08-11 20:47:22 +00001142unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001143 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman5ca269e2008-08-27 01:09:54 +00001144 return 0;
1145}
1146
Owen Anderson9f944592009-08-11 20:47:22 +00001147unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001148 unsigned,
1149 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson8dd01cc2008-08-25 23:58:18 +00001150 uint64_t /*Imm*/) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001151 return 0;
1152}
1153
Owen Anderson9f944592009-08-11 20:47:22 +00001154unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001155 unsigned,
1156 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001157 const ConstantFP * /*FPImm*/) {
Dan Gohman5ca269e2008-08-27 01:09:54 +00001158 return 0;
1159}
1160
Owen Anderson9f944592009-08-11 20:47:22 +00001161unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman404a9842010-01-05 22:26:32 +00001162 unsigned,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001163 unsigned /*Op0*/, bool /*Op0IsKill*/,
1164 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmanfe905652008-08-21 01:41:07 +00001165 uint64_t /*Imm*/) {
Evan Cheng864fcc12008-08-20 22:45:34 +00001166 return 0;
1167}
1168
1169/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1170/// to emit an instruction with an immediate operand using FastEmit_ri.
1171/// If that fails, it materializes the immediate into a register and try
1172/// FastEmit_rr instead.
Dan Gohman404a9842010-01-05 22:26:32 +00001173unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001174 unsigned Op0, bool Op0IsKill,
1175 uint64_t Imm, MVT ImmType) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001176 // If this is a multiply by a power of two, emit this as a shift left.
1177 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1178 Opcode = ISD::SHL;
1179 Imm = Log2_64(Imm);
Chris Lattner562d6e82011-04-18 06:55:51 +00001180 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1181 // div x, 8 -> srl x, 3
1182 Opcode = ISD::SRL;
1183 Imm = Log2_64(Imm);
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001184 }
Owen Andersondd450b82011-04-22 23:38:06 +00001185
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001186 // Horrible hack (to be removed), check to make sure shift amounts are
1187 // in-range.
1188 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1189 Imm >= VT.getSizeInBits())
1190 return 0;
Owen Andersondd450b82011-04-22 23:38:06 +00001191
Evan Cheng864fcc12008-08-20 22:45:34 +00001192 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001193 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng864fcc12008-08-20 22:45:34 +00001194 if (ResultReg != 0)
1195 return ResultReg;
Owen Anderson8dd01cc2008-08-25 23:58:18 +00001196 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedman4105ed12011-04-29 23:34:52 +00001197 if (MaterialReg == 0) {
1198 // This is a bit ugly/slow, but failing here means falling out of
1199 // fast-isel, which would be very slow.
Chris Lattner229907c2011-07-18 04:54:35 +00001200 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedman4105ed12011-04-29 23:34:52 +00001201 VT.getSizeInBits());
1202 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosierdbac0252013-03-28 23:04:47 +00001203 assert (MaterialReg != 0 && "Unable to materialize imm.");
1204 if (MaterialReg == 0) return 0;
Eli Friedman4105ed12011-04-29 23:34:52 +00001205 }
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001206 return FastEmit_rr(VT, VT, Opcode,
1207 Op0, Op0IsKill,
1208 MaterialReg, /*Kill=*/true);
Dan Gohmanfe905652008-08-21 01:41:07 +00001209}
1210
1211unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1212 return MRI.createVirtualRegister(RC);
Evan Cheng864fcc12008-08-20 22:45:34 +00001213}
1214
Tim Northover2f553f32014-04-15 13:59:49 +00001215unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II,
1216 unsigned Op, unsigned OpNum) {
1217 if (TargetRegisterInfo::isVirtualRegister(Op)) {
1218 const TargetRegisterClass *RegClass =
1219 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
1220 if (!MRI.constrainRegClass(Op, RegClass)) {
1221 // If it's not legal to COPY between the register classes, something
1222 // has gone very wrong before we got here.
1223 unsigned NewOp = createResultReg(RegClass);
1224 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1225 TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
1226 return NewOp;
1227 }
1228 }
1229 return Op;
1230}
1231
Dan Gohmanb2226e22008-08-13 20:19:35 +00001232unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman2471f6c2008-08-20 18:09:38 +00001233 const TargetRegisterClass* RC) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001234 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001235 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001236
Rafael Espindolaea09c592014-02-18 22:05:46 +00001237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001238 return ResultReg;
1239}
1240
1241unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1242 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001243 unsigned Op0, bool Op0IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001244 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001245
Tim Northover2f553f32014-04-15 13:59:49 +00001246 unsigned ResultReg = createResultReg(RC);
1247 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1248
Evan Chenge775d352008-09-08 08:38:20 +00001249 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001251 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Chenge775d352008-09-08 08:38:20 +00001252 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001254 .addReg(Op0, Op0IsKill * RegState::Kill);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001255 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1256 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001257 }
1258
Dan Gohmanb2226e22008-08-13 20:19:35 +00001259 return ResultReg;
1260}
1261
1262unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1263 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001264 unsigned Op0, bool Op0IsKill,
1265 unsigned Op1, bool Op1IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001266 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001267
Tim Northover2f553f32014-04-15 13:59:49 +00001268 unsigned ResultReg = createResultReg(RC);
1269 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1270 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1271
Evan Chenge775d352008-09-08 08:38:20 +00001272 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001274 .addReg(Op0, Op0IsKill * RegState::Kill)
1275 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Chenge775d352008-09-08 08:38:20 +00001276 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001277 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001278 .addReg(Op0, Op0IsKill * RegState::Kill)
1279 .addReg(Op1, Op1IsKill * RegState::Kill);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001280 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1281 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001282 }
Dan Gohmanb2226e22008-08-13 20:19:35 +00001283 return ResultReg;
1284}
Dan Gohmanfe905652008-08-21 01:41:07 +00001285
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001286unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1287 const TargetRegisterClass *RC,
1288 unsigned Op0, bool Op0IsKill,
1289 unsigned Op1, bool Op1IsKill,
1290 unsigned Op2, bool Op2IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001291 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001292
Tim Northover2f553f32014-04-15 13:59:49 +00001293 unsigned ResultReg = createResultReg(RC);
1294 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1295 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1296 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
1297
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001298 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001300 .addReg(Op0, Op0IsKill * RegState::Kill)
1301 .addReg(Op1, Op1IsKill * RegState::Kill)
1302 .addReg(Op2, Op2IsKill * RegState::Kill);
1303 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001304 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001305 .addReg(Op0, Op0IsKill * RegState::Kill)
1306 .addReg(Op1, Op1IsKill * RegState::Kill)
1307 .addReg(Op2, Op2IsKill * RegState::Kill);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1309 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001310 }
1311 return ResultReg;
1312}
1313
Dan Gohmanfe905652008-08-21 01:41:07 +00001314unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1315 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001316 unsigned Op0, bool Op0IsKill,
1317 uint64_t Imm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001318 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanfe905652008-08-21 01:41:07 +00001319
Tim Northover2f553f32014-04-15 13:59:49 +00001320 unsigned ResultReg = createResultReg(RC);
1321 RC = TII.getRegClass(II, II.getNumDefs(), &TRI, *FuncInfo.MF);
1322 MRI.constrainRegClass(Op0, RC);
1323
Evan Chenge775d352008-09-08 08:38:20 +00001324 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001325 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001326 .addReg(Op0, Op0IsKill * RegState::Kill)
1327 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001328 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001329 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001330 .addReg(Op0, Op0IsKill * RegState::Kill)
1331 .addImm(Imm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001332 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1333 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001334 }
Dan Gohmanfe905652008-08-21 01:41:07 +00001335 return ResultReg;
1336}
1337
Owen Anderson66443c02011-03-11 21:33:55 +00001338unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1339 const TargetRegisterClass *RC,
1340 unsigned Op0, bool Op0IsKill,
1341 uint64_t Imm1, uint64_t Imm2) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001342 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson66443c02011-03-11 21:33:55 +00001343
Tim Northover2f553f32014-04-15 13:59:49 +00001344 unsigned ResultReg = createResultReg(RC);
1345 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1346
Owen Anderson66443c02011-03-11 21:33:55 +00001347 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Anderson66443c02011-03-11 21:33:55 +00001349 .addReg(Op0, Op0IsKill * RegState::Kill)
1350 .addImm(Imm1)
1351 .addImm(Imm2);
1352 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001353 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Anderson66443c02011-03-11 21:33:55 +00001354 .addReg(Op0, Op0IsKill * RegState::Kill)
1355 .addImm(Imm1)
1356 .addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1358 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson66443c02011-03-11 21:33:55 +00001359 }
1360 return ResultReg;
1361}
1362
Dan Gohman5ca269e2008-08-27 01:09:54 +00001363unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1364 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001365 unsigned Op0, bool Op0IsKill,
1366 const ConstantFP *FPImm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001367 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman5ca269e2008-08-27 01:09:54 +00001368
Tim Northover2f553f32014-04-15 13:59:49 +00001369 unsigned ResultReg = createResultReg(RC);
1370 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1371
Evan Chenge775d352008-09-08 08:38:20 +00001372 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001373 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001374 .addReg(Op0, Op0IsKill * RegState::Kill)
1375 .addFPImm(FPImm);
Evan Chenge775d352008-09-08 08:38:20 +00001376 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001378 .addReg(Op0, Op0IsKill * RegState::Kill)
1379 .addFPImm(FPImm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1381 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001382 }
Dan Gohman5ca269e2008-08-27 01:09:54 +00001383 return ResultReg;
1384}
1385
Dan Gohmanfe905652008-08-21 01:41:07 +00001386unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1387 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001388 unsigned Op0, bool Op0IsKill,
1389 unsigned Op1, bool Op1IsKill,
1390 uint64_t Imm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001391 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanfe905652008-08-21 01:41:07 +00001392
Tim Northover2f553f32014-04-15 13:59:49 +00001393 unsigned ResultReg = createResultReg(RC);
1394 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1395 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1396
Evan Chenge775d352008-09-08 08:38:20 +00001397 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001399 .addReg(Op0, Op0IsKill * RegState::Kill)
1400 .addReg(Op1, Op1IsKill * RegState::Kill)
1401 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001402 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001404 .addReg(Op0, Op0IsKill * RegState::Kill)
1405 .addReg(Op1, Op1IsKill * RegState::Kill)
1406 .addImm(Imm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001407 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1408 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001409 }
Dan Gohmanfe905652008-08-21 01:41:07 +00001410 return ResultReg;
1411}
Owen Anderson32635db2008-08-25 20:20:32 +00001412
Manman Rene8735522012-06-01 19:33:18 +00001413unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1414 const TargetRegisterClass *RC,
1415 unsigned Op0, bool Op0IsKill,
1416 unsigned Op1, bool Op1IsKill,
1417 uint64_t Imm1, uint64_t Imm2) {
Manman Rene8735522012-06-01 19:33:18 +00001418 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1419
Tim Northover2f553f32014-04-15 13:59:49 +00001420 unsigned ResultReg = createResultReg(RC);
1421 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1422 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1423
Manman Rene8735522012-06-01 19:33:18 +00001424 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Manman Rene8735522012-06-01 19:33:18 +00001426 .addReg(Op0, Op0IsKill * RegState::Kill)
1427 .addReg(Op1, Op1IsKill * RegState::Kill)
1428 .addImm(Imm1).addImm(Imm2);
1429 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Manman Rene8735522012-06-01 19:33:18 +00001431 .addReg(Op0, Op0IsKill * RegState::Kill)
1432 .addReg(Op1, Op1IsKill * RegState::Kill)
1433 .addImm(Imm1).addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001434 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1435 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Manman Rene8735522012-06-01 19:33:18 +00001436 }
1437 return ResultReg;
1438}
1439
Owen Anderson32635db2008-08-25 20:20:32 +00001440unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1441 const TargetRegisterClass *RC,
1442 uint64_t Imm) {
1443 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001444 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peck527da1b2010-11-23 03:31:01 +00001445
Evan Chenge775d352008-09-08 08:38:20 +00001446 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001448 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
1450 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1451 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001452 }
Owen Anderson32635db2008-08-25 20:20:32 +00001453 return ResultReg;
Evan Cheng2c067322008-08-25 22:20:39 +00001454}
Owen Anderson5f57bc22008-08-27 22:30:02 +00001455
Owen Andersondd450b82011-04-22 23:38:06 +00001456unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1457 const TargetRegisterClass *RC,
1458 uint64_t Imm1, uint64_t Imm2) {
1459 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001460 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersondd450b82011-04-22 23:38:06 +00001461
1462 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001463 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Andersondd450b82011-04-22 23:38:06 +00001464 .addImm(Imm1).addImm(Imm2);
1465 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001466 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2);
1467 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1468 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersondd450b82011-04-22 23:38:06 +00001469 }
1470 return ResultReg;
1471}
1472
Owen Anderson9f944592009-08-11 20:47:22 +00001473unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001474 unsigned Op0, bool Op0IsKill,
1475 uint32_t Idx) {
Evan Cheng4a0bf662009-01-22 09:10:11 +00001476 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001477 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1478 "Cannot yet extract from physregs");
Jakob Stoklund Olesen1f1c6ad2012-05-20 06:38:37 +00001479 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1480 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001481 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00001482 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001483 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson5f57bc22008-08-27 22:30:02 +00001484 return ResultReg;
1485}
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001486
1487/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1488/// with all but the least significant bit set to zero.
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001489unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1490 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001491}
Dan Gohmanc594eab2010-04-22 20:46:50 +00001492
1493/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1494/// Emit code to ensure constants are copied into registers when needed.
1495/// Remember the virtual registers that need to be added to the Machine PHI
1496/// nodes as input. We cannot just directly add them, because expansion
1497/// might result in multiple MBB's for one BB. As such, the start of the
1498/// BB might correspond to a different MBB than the end.
1499bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1500 const TerminatorInst *TI = LLVMBB->getTerminator();
1501
1502 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman87fb4e82010-07-07 16:29:44 +00001503 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanc594eab2010-04-22 20:46:50 +00001504
1505 // Check successor nodes' PHI nodes that expect a constant to be available
1506 // from this block.
1507 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1508 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1509 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohman87fb4e82010-07-07 16:29:44 +00001510 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanc594eab2010-04-22 20:46:50 +00001511
1512 // If this terminator has multiple identical successors (common for
1513 // switches), only handle each succ once.
1514 if (!SuccsHandled.insert(SuccMBB)) continue;
1515
1516 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1517
1518 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1519 // nodes and Machine PHI nodes, but the incoming operands have not been
1520 // emitted yet.
1521 for (BasicBlock::const_iterator I = SuccBB->begin();
1522 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmane6d40162010-05-07 01:10:20 +00001523
Dan Gohmanc594eab2010-04-22 20:46:50 +00001524 // Ignore dead phi's.
1525 if (PN->use_empty()) continue;
1526
1527 // Only handle legal types. Two interesting things to note here. First,
1528 // by bailing out early, we may leave behind some dead instructions,
1529 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001530 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman93f59202010-07-02 00:10:16 +00001531 // use CreateRegs to create registers, so it always creates
Dan Gohmanc594eab2010-04-22 20:46:50 +00001532 // exactly one register for each non-void instruction.
1533 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1534 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier6d68c7c2012-02-04 00:39:19 +00001535 // Handle integer promotions, though, because they're common and easy.
1536 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanc594eab2010-04-22 20:46:50 +00001537 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1538 else {
Dan Gohman87fb4e82010-07-07 16:29:44 +00001539 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanc594eab2010-04-22 20:46:50 +00001540 return false;
1541 }
1542 }
1543
1544 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1545
Dan Gohmane6d40162010-05-07 01:10:20 +00001546 // Set the DebugLoc for the copy. Prefer the location of the operand
1547 // if there is one; use the location of the PHI otherwise.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001548 DbgLoc = PN->getDebugLoc();
Dan Gohmane6d40162010-05-07 01:10:20 +00001549 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001550 DbgLoc = Inst->getDebugLoc();
Dan Gohmane6d40162010-05-07 01:10:20 +00001551
Dan Gohmanc594eab2010-04-22 20:46:50 +00001552 unsigned Reg = getRegForValue(PHIOp);
1553 if (Reg == 0) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00001554 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanc594eab2010-04-22 20:46:50 +00001555 return false;
1556 }
Dan Gohman87fb4e82010-07-07 16:29:44 +00001557 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001558 DbgLoc = DebugLoc();
Dan Gohmanc594eab2010-04-22 20:46:50 +00001559 }
1560 }
1561
1562 return true;
1563}
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001564
1565bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Benderskye80691d2013-04-19 23:26:18 +00001566 assert(LI->hasOneUse() &&
1567 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001568 // We know that the load has a single use, but don't know what it is. If it
1569 // isn't one of the folded instructions, then we can't succeed here. Handle
1570 // this by scanning the single-use users of the load until we get to FoldInst.
1571 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1572
Chandler Carruthcdf47882014-03-09 03:16:01 +00001573 const Instruction *TheUser = LI->user_back();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001574 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1575 // Stay in the right block.
1576 TheUser->getParent() == FoldInst->getParent() &&
1577 --MaxUsers) { // Don't scan too far.
1578 // If there are multiple or no uses of this instruction, then bail out.
1579 if (!TheUser->hasOneUse())
1580 return false;
1581
Chandler Carruthcdf47882014-03-09 03:16:01 +00001582 TheUser = TheUser->user_back();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001583 }
1584
1585 // If we didn't find the fold instruction, then we failed to collapse the
1586 // sequence.
1587 if (TheUser != FoldInst)
1588 return false;
1589
1590 // Don't try to fold volatile loads. Target has to deal with alignment
1591 // constraints.
Eli Benderskye80691d2013-04-19 23:26:18 +00001592 if (LI->isVolatile())
1593 return false;
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001594
1595 // Figure out which vreg this is going into. If there is no assigned vreg yet
1596 // then there actually was no reference to it. Perhaps the load is referenced
1597 // by a dead instruction.
1598 unsigned LoadReg = getRegForValue(LI);
1599 if (LoadReg == 0)
1600 return false;
1601
Eli Benderskye80691d2013-04-19 23:26:18 +00001602 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1603 // may mean that the instruction got lowered to multiple MIs, or the use of
1604 // the loaded value ended up being multiple operands of the result.
1605 if (!MRI.hasOneUse(LoadReg))
1606 return false;
1607
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001608 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Owen Anderson16c6bf42014-03-13 23:12:04 +00001609 MachineInstr *User = RI->getParent();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001610
1611 // Set the insertion point properly. Folding the load can cause generation of
Eli Benderskye80691d2013-04-19 23:26:18 +00001612 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001613 // sure they get inserted in a logical place before the new instruction.
1614 FuncInfo.InsertPt = User;
1615 FuncInfo.MBB = User->getParent();
1616
1617 // Ask the target to try folding the load.
1618 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1619}
1620
Bob Wilson9f3e6b22013-11-15 19:09:27 +00001621bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
1622 // Must be an add.
1623 if (!isa<AddOperator>(Add))
1624 return false;
1625 // Type size needs to match.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001626 if (DL.getTypeSizeInBits(GEP->getType()) !=
1627 DL.getTypeSizeInBits(Add->getType()))
Bob Wilson9f3e6b22013-11-15 19:09:27 +00001628 return false;
1629 // Must be in the same basic block.
1630 if (isa<Instruction>(Add) &&
1631 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
1632 return false;
1633 // Must have a constant operand.
1634 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
1635}
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001636