blob: 17fb9ea2bca1647cfa8b536f1a161c5f76fa2b3e [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
Juergen Ributzka8179e9e2014-07-11 22:01:42 +000042#include "llvm/CodeGen/Analysis.h"
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"
Juergen Ributzka454d3742014-06-13 00:45:11 +000046#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Analysis/Loads.h"
48#include "llvm/CodeGen/Analysis.h"
49#include "llvm/CodeGen/FunctionLoweringInfo.h"
Juergen Ributzka04558dc2014-06-12 03:29:26 +000050#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000051#include "llvm/CodeGen/MachineInstrBuilder.h"
52#include "llvm/CodeGen/MachineModuleInfo.h"
53#include "llvm/CodeGen/MachineRegisterInfo.h"
Juergen Ributzka04558dc2014-06-12 03:29:26 +000054#include "llvm/CodeGen/StackMaps.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000055#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000056#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000057#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalVariable.h"
59#include "llvm/IR/Instructions.h"
60#include "llvm/IR/IntrinsicInst.h"
61#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000062#include "llvm/Support/Debug.h"
63#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000064#include "llvm/Target/TargetInstrInfo.h"
Bob Wilson3e6fa462012-08-03 04:06:28 +000065#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng864fcc12008-08-20 22:45:34 +000066#include "llvm/Target/TargetLowering.h"
Dan Gohman02c84b82008-08-20 21:05:57 +000067#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000068#include "llvm/Target/TargetSubtargetInfo.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000069using namespace llvm;
70
Chandler Carruth1b9dde02014-04-22 02:02:50 +000071#define DEBUG_TYPE "isel"
72
Chad Rosier61e8d102011-11-28 19:59:09 +000073STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
Juergen Ributzka7a76c242014-09-03 18:46:45 +000074 "target-independent selector");
Chad Rosier61e8d102011-11-28 19:59:09 +000075STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
Juergen Ributzka7a76c242014-09-03 18:46:45 +000076 "target-specific selector");
Chad Rosier46addb92011-11-29 19:40:47 +000077STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosierff40b1e2011-11-16 21:05:28 +000078
Juergen Ributzka8179e9e2014-07-11 22:01:42 +000079void FastISel::ArgListEntry::setAttributes(ImmutableCallSite *CS,
80 unsigned AttrIdx) {
Juergen Ributzka7a76c242014-09-03 18:46:45 +000081 IsSExt = CS->paramHasAttr(AttrIdx, Attribute::SExt);
82 IsZExt = CS->paramHasAttr(AttrIdx, Attribute::ZExt);
83 IsInReg = CS->paramHasAttr(AttrIdx, Attribute::InReg);
84 IsSRet = CS->paramHasAttr(AttrIdx, Attribute::StructRet);
85 IsNest = CS->paramHasAttr(AttrIdx, Attribute::Nest);
86 IsByVal = CS->paramHasAttr(AttrIdx, Attribute::ByVal);
87 IsInAlloca = CS->paramHasAttr(AttrIdx, Attribute::InAlloca);
88 IsReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned);
89 Alignment = CS->getParamAlignment(AttrIdx);
Juergen Ributzka8179e9e2014-07-11 22:01:42 +000090}
91
Juergen Ributzka7a76c242014-09-03 18:46:45 +000092/// Set the current block to which generated machine instructions will be
93/// appended, and clear the local CSE map.
Dan Gohmand7b5ce32010-07-10 09:00:22 +000094void FastISel::startNewBlock() {
95 LocalValueMap.clear();
96
Jakob Stoklund Olesen6a7d6832013-07-04 04:53:49 +000097 // Instructions are appended to FuncInfo.MBB. If the basic block already
Jakob Stoklund Olesen3d8560c2013-07-04 04:32:39 +000098 // contains labels or copies, use the last instruction as the last local
99 // value.
Craig Topperc0196b12014-04-14 00:51:57 +0000100 EmitStartPt = nullptr;
Jakob Stoklund Olesen3d8560c2013-07-04 04:32:39 +0000101 if (!FuncInfo.MBB->empty())
102 EmitStartPt = &FuncInfo.MBB->back();
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000103 LastLocalValue = EmitStartPt;
104}
105
Evan Cheng615620c2013-02-11 01:27:15 +0000106bool FastISel::LowerArguments() {
107 if (!FuncInfo.CanLowerReturn)
108 // Fallback to SDISel argument lowering code to deal with sret pointer
109 // parameter.
110 return false;
Stephen Lincfe7f352013-07-08 00:37:03 +0000111
Evan Cheng615620c2013-02-11 01:27:15 +0000112 if (!FastLowerArguments())
113 return false;
114
David Blaikie97c6c5b2013-06-21 22:56:30 +0000115 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng615620c2013-02-11 01:27:15 +0000116 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000117 E = FuncInfo.Fn->arg_end();
118 I != E; ++I) {
David Blaikie97c6c5b2013-06-21 22:56:30 +0000119 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
120 assert(VI != LocalValueMap.end() && "Missed an argument?");
121 FuncInfo.ValueMap[I] = VI->second;
Evan Cheng615620c2013-02-11 01:27:15 +0000122 }
123 return true;
124}
125
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000126void FastISel::flushLocalValueMap() {
127 LocalValueMap.clear();
128 LastLocalValue = EmitStartPt;
129 recomputeInsertPt();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000130}
131
Juergen Ributzka4f1a54a2014-08-28 00:09:46 +0000132bool FastISel::hasTrivialKill(const Value *V) {
Dan Gohman88fb2532010-05-14 22:53:18 +0000133 // Don't consider constants or arguments to have trivial kills.
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000134 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman88fb2532010-05-14 22:53:18 +0000135 if (!I)
136 return false;
137
138 // No-op casts are trivially coalesced by fast-isel.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000139 if (const auto *Cast = dyn_cast<CastInst>(I))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000140 if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
Chandler Carruth7ec50852012-11-01 08:07:29 +0000141 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman88fb2532010-05-14 22:53:18 +0000142 return false;
143
Juergen Ributzka4f1a54a2014-08-28 00:09:46 +0000144 // Even the value might have only one use in the LLVM IR, it is possible that
145 // FastISel might fold the use into another instruction and now there is more
146 // than one use at the Machine Instruction level.
147 unsigned Reg = lookUpRegForValue(V);
148 if (Reg && !MRI.use_empty(Reg))
149 return false;
150
Chad Rosier291ce472011-11-15 23:34:05 +0000151 // GEPs with all zero indices are trivially coalesced by fast-isel.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000152 if (const auto *GEP = dyn_cast<GetElementPtrInst>(I))
Chad Rosier291ce472011-11-15 23:34:05 +0000153 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
154 return false;
155
Dan Gohman88fb2532010-05-14 22:53:18 +0000156 // Only instructions with a single use in the same basic block are considered
157 // to have trivial kills.
158 return I->hasOneUse() &&
159 !(I->getOpcode() == Instruction::BitCast ||
160 I->getOpcode() == Instruction::PtrToInt ||
161 I->getOpcode() == Instruction::IntToPtr) &&
Chandler Carruthcdf47882014-03-09 03:16:01 +0000162 cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000163}
164
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000165unsigned FastISel::getRegForValue(const Value *V) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000166 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohmanca93aab2009-04-07 20:40:11 +0000167 // Don't handle non-simple values in FastISel.
168 if (!RealVT.isSimple())
169 return 0;
Dan Gohman4c315242008-12-08 07:57:47 +0000170
171 // Ignore illegal types. We must do this before looking up the value
172 // in ValueMap because Arguments are given virtual registers regardless
173 // of whether FastISel can handle them.
Owen Anderson9f944592009-08-11 20:47:22 +0000174 MVT VT = RealVT.getSimpleVT();
Dan Gohman4c315242008-12-08 07:57:47 +0000175 if (!TLI.isTypeLegal(VT)) {
Eli Friedmanc7035512011-05-25 23:49:02 +0000176 // Handle integer promotions, though, because they're common and easy.
177 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson117c9e82009-08-12 00:36:31 +0000178 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohman4c315242008-12-08 07:57:47 +0000179 else
180 return 0;
181 }
182
Eric Christopher1a06cc92012-03-20 01:07:47 +0000183 // Look up the value to see if we already have a register for it.
184 unsigned Reg = lookUpRegForValue(V);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000185 if (Reg)
Dan Gohmane039d552008-09-03 23:32:19 +0000186 return Reg;
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000187
Dan Gohmana7c717d82010-05-06 00:02:14 +0000188 // In bottom-up mode, just create the virtual register which will be used
189 // to hold the value. It will be materialized later.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000190 if (isa<Instruction>(V) &&
191 (!isa<AllocaInst>(V) ||
192 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
193 return FuncInfo.InitializeRegForValue(V);
Dan Gohmana7c717d82010-05-06 00:02:14 +0000194
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000195 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000196
197 // Materialize the value in a register. Emit any instructions in the
198 // local value area.
199 Reg = materializeRegForValue(V, VT);
200
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000201 leaveLocalValueArea(SaveInsertPt);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000202
203 return Reg;
Dan Gohman626b5d82010-05-03 23:36:34 +0000204}
205
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000206unsigned FastISel::materializeConstant(const Value *V, MVT VT) {
Dan Gohman626b5d82010-05-03 23:36:34 +0000207 unsigned Reg = 0;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000208 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman9801ba42008-09-19 22:16:54 +0000209 if (CI->getValue().getActiveBits() <= 64)
210 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000211 } else if (isa<AllocaInst>(V))
Dan Gohman9801ba42008-09-19 22:16:54 +0000212 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000213 else if (isa<ConstantPointerNull>(V))
Dan Gohmanc1d47c52008-10-07 22:03:27 +0000214 // Translate this as an integer zero so that it can be
215 // local-CSE'd with actual integer zeros.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000216 Reg = getRegForValue(
217 Constant::getNullValue(DL.getIntPtrType(V->getContext())));
218 else if (const auto *CF = dyn_cast<ConstantFP>(V)) {
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000219 if (CF->isNullValue())
Eli Friedman406c4712011-04-27 22:41:55 +0000220 Reg = TargetMaterializeFloatZero(CF);
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000221 else
Eli Friedman406c4712011-04-27 22:41:55 +0000222 // Try to emit the constant directly.
223 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000224
225 if (!Reg) {
Dan Gohman8a2dae52010-04-13 17:07:06 +0000226 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000227 const APFloat &Flt = CF->getValueAPF();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000228 EVT IntVT = TLI.getPointerTy();
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000229
230 uint64_t x[2];
231 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000232 bool isExact;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000233 (void)Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
234 APFloat::rmTowardZero, &isExact);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000235 if (isExact) {
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000236 APInt IntVal(IntBitWidth, x);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000237
Owen Anderson47db9412009-07-22 00:24:57 +0000238 unsigned IntegerReg =
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000239 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman9801ba42008-09-19 22:16:54 +0000240 if (IntegerReg != 0)
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000241 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg,
242 /*Kill=*/false);
Dan Gohman9801ba42008-09-19 22:16:54 +0000243 }
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000244 }
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000245 } else if (const auto *Op = dyn_cast<Operator>(V)) {
246 if (!selectOperator(Op, Op->getOpcode()))
Dan Gohman722f5fc2010-07-01 02:58:57 +0000247 if (!isa<Instruction>(Op) ||
248 !TargetSelectInstruction(cast<Instruction>(Op)))
249 return 0;
Dan Gohman7c58cf72010-06-21 14:17:46 +0000250 Reg = lookUpRegForValue(Op);
Dan Gohmanc45733f2008-08-28 21:19:07 +0000251 } else if (isa<UndefValue>(V)) {
Dan Gohmane039d552008-09-03 23:32:19 +0000252 Reg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000253 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000254 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000255 }
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000256 return Reg;
257}
Wesley Peck527da1b2010-11-23 03:31:01 +0000258
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000259/// Helper for getRegForValue. This function is called when the value isn't
260/// already available in a register and must be materialized with new
261/// instructions.
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000262unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
263 unsigned Reg = 0;
264 // Give the target-specific code a try first.
265 if (isa<Constant>(V))
Dan Gohman9801ba42008-09-19 22:16:54 +0000266 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peck527da1b2010-11-23 03:31:01 +0000267
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000268 // If target-specific code couldn't or didn't want to handle the value, then
269 // give target-independent code a try.
270 if (!Reg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000271 Reg = materializeConstant(V, VT);
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000272
Dan Gohman9801ba42008-09-19 22:16:54 +0000273 // Don't cache constant materializations in the general ValueMap.
274 // To do so would require tracking what uses they dominate.
Juergen Ributzka4bf6c012014-08-19 19:05:24 +0000275 if (Reg) {
Dan Gohman3663f152008-09-25 01:28:51 +0000276 LocalValueMap[V] = Reg;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000277 LastLocalValue = MRI.getVRegDef(Reg);
278 }
Dan Gohmane039d552008-09-03 23:32:19 +0000279 return Reg;
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000280}
281
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000282unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng1e979012008-09-09 01:26:59 +0000283 // Look up the value to see if we already have a register for it. We
284 // cache values defined by Instructions across blocks, and other values
285 // only locally. This is because Instructions already have the SSA
Dan Gohman626b5d82010-05-03 23:36:34 +0000286 // def-dominates-use requirement enforced.
Dan Gohman87fb4e82010-07-07 16:29:44 +0000287 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
288 if (I != FuncInfo.ValueMap.end())
Dan Gohmanf91aff52010-06-21 14:21:47 +0000289 return I->second;
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000290 return LocalValueMap[V];
Evan Cheng1e979012008-09-09 01:26:59 +0000291}
292
Eli Friedmana4d4a012011-05-16 21:06:17 +0000293void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohmanfcf54562008-09-05 18:18:20 +0000294 if (!isa<Instruction>(I)) {
295 LocalValueMap[I] = Reg;
Eli Friedmana4d4a012011-05-16 21:06:17 +0000296 return;
Dan Gohmanfcf54562008-09-05 18:18:20 +0000297 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000298
Dan Gohman87fb4e82010-07-07 16:29:44 +0000299 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000300 if (AssignedReg == 0)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000301 // Use the new register.
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000302 AssignedReg = Reg;
Chris Lattnera101f6f2009-04-12 07:46:30 +0000303 else if (Reg != AssignedReg) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000304 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedmana4d4a012011-05-16 21:06:17 +0000305 for (unsigned i = 0; i < NumRegs; i++)
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000306 FuncInfo.RegFixups[AssignedReg + i] = Reg + i;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000307
308 AssignedReg = Reg;
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000309 }
Owen Anderson6f0c51d2008-08-30 00:38:46 +0000310}
311
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000312std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohman4c315242008-12-08 07:57:47 +0000313 unsigned IdxN = getRegForValue(Idx);
314 if (IdxN == 0)
315 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000316 return std::pair<unsigned, bool>(0, false);
317
318 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohman4c315242008-12-08 07:57:47 +0000319
320 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Andersonc6daf8f2009-08-11 21:59:30 +0000321 MVT PtrVT = TLI.getPointerTy();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000322 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000323 if (IdxVT.bitsLT(PtrVT)) {
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000324 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN,
325 IdxNIsKill);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000326 IdxNIsKill = true;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000327 } else if (IdxVT.bitsGT(PtrVT)) {
328 IdxN =
329 FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN, IdxNIsKill);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000330 IdxNIsKill = true;
331 }
332 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohman4c315242008-12-08 07:57:47 +0000333}
334
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000335void FastISel::recomputeInsertPt() {
336 if (getLastLocalValue()) {
337 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanb5e918d2010-07-19 22:48:56 +0000338 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000339 ++FuncInfo.InsertPt;
340 } else
341 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
342
343 // Now skip past any EH_LABELs, which must remain at the beginning.
344 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
345 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
346 ++FuncInfo.InsertPt;
347}
348
Chad Rosier46addb92011-11-29 19:40:47 +0000349void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
350 MachineBasicBlock::iterator E) {
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000351 assert(I && E && std::distance(I, E) > 0 && "Invalid iterator!");
Chad Rosier46addb92011-11-29 19:40:47 +0000352 while (I != E) {
353 MachineInstr *Dead = &*I;
354 ++I;
355 Dead->eraseFromParent();
Jan Wen Voung7857a642013-03-08 22:56:31 +0000356 ++NumFastIselDead;
Chad Rosier46addb92011-11-29 19:40:47 +0000357 }
358 recomputeInsertPt();
359}
360
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000361FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000362 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000363 DebugLoc OldDL = DbgLoc;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000364 recomputeInsertPt();
Rafael Espindolaea09c592014-02-18 22:05:46 +0000365 DbgLoc = DebugLoc();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000366 SavePoint SP = {OldInsertPt, OldDL};
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000367 return SP;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000368}
369
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000370void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000371 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000372 LastLocalValue = std::prev(FuncInfo.InsertPt);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000373
374 // Restore the previous insert position.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000375 FuncInfo.InsertPt = OldInsertPt.InsertPt;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000376 DbgLoc = OldInsertPt.DL;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000377}
378
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000379bool FastISel::selectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000380 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson9f944592009-08-11 20:47:22 +0000381 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000382 // Unhandled type. Halt "fast" selection and bail.
383 return false;
Dan Gohmanfd634592008-09-05 18:44:22 +0000384
Dan Gohman3bcbbec2008-08-26 20:52:40 +0000385 // We only handle legal types. For example, on x86-32 the instruction
386 // selector contains all of the 64-bit instructions from x86-64,
387 // under the assumption that i64 won't be used if the target doesn't
388 // support it.
Dan Gohmanfd634592008-09-05 18:44:22 +0000389 if (!TLI.isTypeLegal(VT)) {
Owen Anderson9f944592009-08-11 20:47:22 +0000390 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohmanfd634592008-09-05 18:44:22 +0000391 // don't require additional zeroing, which makes them easy.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000392 if (VT == MVT::i1 && (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
393 ISDOpcode == ISD::XOR))
Owen Anderson117c9e82009-08-12 00:36:31 +0000394 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohmanfd634592008-09-05 18:44:22 +0000395 else
396 return false;
397 }
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000398
Chris Lattnerfba7ca62011-04-17 01:16:47 +0000399 // Check if the first operand is a constant, and handle it as "ri". At -O0,
400 // we don't have anything that canonicalizes operand order.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000401 if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
Chris Lattnerfba7ca62011-04-17 01:16:47 +0000402 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
403 unsigned Op1 = getRegForValue(I->getOperand(1));
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000404 if (!Op1)
405 return false;
Chris Lattnerfba7ca62011-04-17 01:16:47 +0000406 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersondd450b82011-04-22 23:38:06 +0000407
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000408 unsigned ResultReg =
409 FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, Op1IsKill,
410 CI->getZExtValue(), VT.getSimpleVT());
411 if (!ResultReg)
412 return false;
Owen Andersondd450b82011-04-22 23:38:06 +0000413
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000414 // We successfully emitted code for the given LLVM Instruction.
415 UpdateValueMap(I, ResultReg);
416 return true;
Chris Lattnerfba7ca62011-04-17 01:16:47 +0000417 }
Owen Andersondd450b82011-04-22 23:38:06 +0000418
Dan Gohman7bda51f2008-09-03 23:12:08 +0000419 unsigned Op0 = getRegForValue(I->getOperand(0));
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000420 if (!Op0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmanfe905652008-08-21 01:41:07 +0000421 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000422 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
423
Dan Gohmanfe905652008-08-21 01:41:07 +0000424 // Check if the second operand is a constant and handle it appropriately.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000425 if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000426 uint64_t Imm = CI->getZExtValue();
Owen Andersondd450b82011-04-22 23:38:06 +0000427
Chris Lattner48f75ad2011-04-18 07:00:40 +0000428 // Transform "sdiv exact X, 8" -> "sra X, 3".
429 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000430 cast<BinaryOperator>(I)->isExact() && isPowerOf2_64(Imm)) {
Chris Lattner48f75ad2011-04-18 07:00:40 +0000431 Imm = Log2_64(Imm);
432 ISDOpcode = ISD::SRA;
433 }
Owen Andersondd450b82011-04-22 23:38:06 +0000434
Chad Rosier6a63a742012-03-22 00:21:17 +0000435 // Transform "urem x, pow2" -> "and x, pow2-1".
436 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
437 isPowerOf2_64(Imm)) {
438 --Imm;
439 ISDOpcode = ISD::AND;
440 }
441
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000442 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
443 Op0IsKill, Imm, VT.getSimpleVT());
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000444 if (!ResultReg)
445 return false;
Owen Andersondd450b82011-04-22 23:38:06 +0000446
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000447 // We successfully emitted code for the given LLVM Instruction.
448 UpdateValueMap(I, ResultReg);
449 return true;
Dan Gohmanfe905652008-08-21 01:41:07 +0000450 }
451
Dan Gohman5ca269e2008-08-27 01:09:54 +0000452 // Check if the second operand is a constant float.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000453 if (const auto *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000454 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000455 ISDOpcode, Op0, Op0IsKill, CF);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000456 if (ResultReg) {
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000457 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000458 UpdateValueMap(I, ResultReg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000459 return true;
460 }
Dan Gohman5ca269e2008-08-27 01:09:54 +0000461 }
462
Dan Gohman7bda51f2008-09-03 23:12:08 +0000463 unsigned Op1 = getRegForValue(I->getOperand(1));
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000464 if (!Op1) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmanfe905652008-08-21 01:41:07 +0000465 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000466 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
467
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000468 // Now we have both operands in registers. Emit the instruction.
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000469 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000470 ISDOpcode, Op0, Op0IsKill, Op1, Op1IsKill);
471 if (!ResultReg)
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000472 // Target-specific code wasn't able to find a machine opcode for
473 // the given ISD opcode and type. Halt "fast" selection and bail.
474 return false;
475
Dan Gohmanb16a7782008-08-20 00:23:20 +0000476 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000477 UpdateValueMap(I, ResultReg);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000478 return true;
479}
480
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000481bool FastISel::selectGetElementPtr(const User *I) {
Dan Gohman7bda51f2008-09-03 23:12:08 +0000482 unsigned N = getRegForValue(I->getOperand(0));
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000483 if (!N) // Unhandled operand. Halt "fast" selection and bail.
Evan Cheng864fcc12008-08-20 22:45:34 +0000484 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000485 bool NIsKill = hasTrivialKill(I->getOperand(0));
486
Chad Rosierf83ab702011-11-17 07:15:58 +0000487 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
488 // into a single N = N + TotalOffset.
489 uint64_t TotalOffs = 0;
490 // FIXME: What's a good SWAG number for MaxOffs?
491 uint64_t MaxOffs = 2048;
Chris Lattner229907c2011-07-18 04:54:35 +0000492 Type *Ty = I->getOperand(0)->getType();
Owen Anderson9f944592009-08-11 20:47:22 +0000493 MVT VT = TLI.getPointerTy();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000494 for (GetElementPtrInst::const_op_iterator OI = I->op_begin() + 1,
495 E = I->op_end();
496 OI != E; ++OI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000497 const Value *Idx = *OI;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000498 if (auto *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng864fcc12008-08-20 22:45:34 +0000499 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
500 if (Field) {
501 // N = N + Offset
Rafael Espindolaea09c592014-02-18 22:05:46 +0000502 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
Chad Rosierf83ab702011-11-17 07:15:58 +0000503 if (TotalOffs >= MaxOffs) {
504 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000505 if (!N) // Unhandled operand. Halt "fast" selection and bail.
Chad Rosierf83ab702011-11-17 07:15:58 +0000506 return false;
507 NIsKill = true;
508 TotalOffs = 0;
509 }
Evan Cheng864fcc12008-08-20 22:45:34 +0000510 }
511 Ty = StTy->getElementType(Field);
512 } else {
513 Ty = cast<SequentialType>(Ty)->getElementType();
514
515 // If this is a constant subscript, handle it quickly.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000516 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
517 if (CI->isZero())
518 continue;
Chad Rosierf83ab702011-11-17 07:15:58 +0000519 // N = N + Offset
Chad Rosier879c34f2012-07-06 17:44:22 +0000520 TotalOffs +=
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000521 DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
Chad Rosierf83ab702011-11-17 07:15:58 +0000522 if (TotalOffs >= MaxOffs) {
523 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000524 if (!N) // Unhandled operand. Halt "fast" selection and bail.
Chad Rosierf83ab702011-11-17 07:15:58 +0000525 return false;
526 NIsKill = true;
527 TotalOffs = 0;
528 }
529 continue;
530 }
531 if (TotalOffs) {
532 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000533 if (!N) // Unhandled operand. Halt "fast" selection and bail.
Evan Cheng864fcc12008-08-20 22:45:34 +0000534 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000535 NIsKill = true;
Chad Rosierf83ab702011-11-17 07:15:58 +0000536 TotalOffs = 0;
Evan Cheng864fcc12008-08-20 22:45:34 +0000537 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000538
Evan Cheng864fcc12008-08-20 22:45:34 +0000539 // N = N + Idx * ElementSize;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000540 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000541 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
542 unsigned IdxN = Pair.first;
543 bool IdxNIsKill = Pair.second;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000544 if (!IdxN) // Unhandled operand. Halt "fast" selection and bail.
Evan Cheng864fcc12008-08-20 22:45:34 +0000545 return false;
546
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000547 if (ElementSize != 1) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000548 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000549 if (!IdxN) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000550 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000551 IdxNIsKill = true;
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000552 }
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000553 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000554 if (!N) // Unhandled operand. Halt "fast" selection and bail.
Evan Cheng864fcc12008-08-20 22:45:34 +0000555 return false;
556 }
557 }
Chad Rosierf83ab702011-11-17 07:15:58 +0000558 if (TotalOffs) {
559 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000560 if (!N) // Unhandled operand. Halt "fast" selection and bail.
Chad Rosierf83ab702011-11-17 07:15:58 +0000561 return false;
562 }
Evan Cheng864fcc12008-08-20 22:45:34 +0000563
564 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000565 UpdateValueMap(I, N);
Evan Cheng864fcc12008-08-20 22:45:34 +0000566 return true;
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000567}
568
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000569bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops,
570 const CallInst *CI, unsigned StartIdx) {
571 for (unsigned i = StartIdx, e = CI->getNumArgOperands(); i != e; ++i) {
572 Value *Val = CI->getArgOperand(i);
Juergen Ributzka190305b2014-07-01 22:25:49 +0000573 // Check for constants and encode them with a StackMaps::ConstantOp prefix.
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000574 if (const auto *C = dyn_cast<ConstantInt>(Val)) {
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000575 Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
576 Ops.push_back(MachineOperand::CreateImm(C->getSExtValue()));
577 } else if (isa<ConstantPointerNull>(Val)) {
578 Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
579 Ops.push_back(MachineOperand::CreateImm(0));
580 } else if (auto *AI = dyn_cast<AllocaInst>(Val)) {
Juergen Ributzka190305b2014-07-01 22:25:49 +0000581 // Values coming from a stack location also require a sepcial encoding,
582 // but that is added later on by the target specific frame index
583 // elimination implementation.
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000584 auto SI = FuncInfo.StaticAllocaMap.find(AI);
585 if (SI != FuncInfo.StaticAllocaMap.end())
586 Ops.push_back(MachineOperand::CreateFI(SI->second));
587 else
588 return false;
589 } else {
590 unsigned Reg = getRegForValue(Val);
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000591 if (!Reg)
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000592 return false;
593 Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
594 }
595 }
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000596 return true;
597}
598
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000599bool FastISel::selectStackmap(const CallInst *I) {
Juergen Ributzka190305b2014-07-01 22:25:49 +0000600 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
601 // [live variables...])
602 assert(I->getCalledFunction()->getReturnType()->isVoidTy() &&
603 "Stackmap cannot return a value.");
604
605 // The stackmap intrinsic only records the live variables (the arguments
606 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
607 // intrinsic, this won't be lowered to a function call. This means we don't
608 // have to worry about calling conventions and target-specific lowering code.
609 // Instead we perform the call lowering right here.
610 //
611 // CALLSEQ_START(0)
612 // STACKMAP(id, nbytes, ...)
613 // CALLSEQ_END(0, 0)
614 //
615 SmallVector<MachineOperand, 32> Ops;
616
617 // Add the <id> and <numBytes> constants.
618 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
619 "Expected a constant integer.");
620 const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
621 Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
622
623 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
624 "Expected a constant integer.");
625 const auto *NumBytes =
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000626 cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
Juergen Ributzka190305b2014-07-01 22:25:49 +0000627 Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
628
629 // Push live variables for the stack map (skipping the first two arguments
630 // <id> and <numBytes>).
631 if (!addStackMapLiveVars(Ops, I, 2))
632 return false;
633
634 // We are not adding any register mask info here, because the stackmap doesn't
635 // clobber anything.
636
637 // Add scratch registers as implicit def and early clobber.
638 CallingConv::ID CC = I->getCallingConv();
639 const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
640 for (unsigned i = 0; ScratchRegs[i]; ++i)
641 Ops.push_back(MachineOperand::CreateReg(
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000642 ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
643 /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
Juergen Ributzka190305b2014-07-01 22:25:49 +0000644
645 // Issue CALLSEQ_START
646 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
647 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000648 .addImm(0);
Juergen Ributzka190305b2014-07-01 22:25:49 +0000649
650 // Issue STACKMAP.
651 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
652 TII.get(TargetOpcode::STACKMAP));
653 for (auto const &MO : Ops)
654 MIB.addOperand(MO);
655
656 // Issue CALLSEQ_END
657 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
658 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000659 .addImm(0)
660 .addImm(0);
Juergen Ributzka190305b2014-07-01 22:25:49 +0000661
662 // Inform the Frame Information that we have a stackmap in this function.
663 FuncInfo.MF->getFrameInfo()->setHasStackMap();
664
665 return true;
666}
667
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000668/// \brief Lower an argument list according to the target calling convention.
669///
670/// This is a helper for lowering intrinsics that follow a target calling
671/// convention or require stack pointer adjustment. Only a subset of the
672/// intrinsic's operands need to participate in the calling convention.
673bool FastISel::lowerCallOperands(const CallInst *CI, unsigned ArgIdx,
674 unsigned NumArgs, const Value *Callee,
675 bool ForceRetVoidTy, CallLoweringInfo &CLI) {
676 ArgListTy Args;
677 Args.reserve(NumArgs);
678
679 // Populate the argument list.
680 // Attributes for args start at offset 1, after the return attribute.
681 ImmutableCallSite CS(CI);
682 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs, AttrI = ArgIdx + 1;
683 ArgI != ArgE; ++ArgI) {
684 Value *V = CI->getOperand(ArgI);
685
686 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
687
688 ArgListEntry Entry;
689 Entry.Val = V;
690 Entry.Ty = V->getType();
691 Entry.setAttributes(&CS, AttrI);
692 Args.push_back(Entry);
693 }
694
695 Type *RetTy = ForceRetVoidTy ? Type::getVoidTy(CI->getType()->getContext())
696 : CI->getType();
697 CLI.setCallee(CI->getCallingConv(), RetTy, Callee, std::move(Args), NumArgs);
698
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000699 return lowerCallTo(CLI);
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000700}
701
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000702bool FastISel::selectPatchpoint(const CallInst *I) {
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000703 // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>,
704 // i32 <numBytes>,
705 // i8* <target>,
706 // i32 <numArgs>,
707 // [Args...],
708 // [live variables...])
709 CallingConv::ID CC = I->getCallingConv();
710 bool IsAnyRegCC = CC == CallingConv::AnyReg;
711 bool HasDef = !I->getType()->isVoidTy();
712 Value *Callee = I->getOperand(PatchPointOpers::TargetPos);
713
714 // Get the real number of arguments participating in the call <numArgs>
715 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)) &&
716 "Expected a constant integer.");
717 const auto *NumArgsVal =
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000718 cast<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos));
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000719 unsigned NumArgs = NumArgsVal->getZExtValue();
720
721 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
722 // This includes all meta-operands up to but not including CC.
723 unsigned NumMetaOpers = PatchPointOpers::CCPos;
724 assert(I->getNumArgOperands() >= NumMetaOpers + NumArgs &&
725 "Not enough arguments provided to the patchpoint intrinsic");
726
727 // For AnyRegCC the arguments are lowered later on manually.
728 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
729 CallLoweringInfo CLI;
730 if (!lowerCallOperands(I, NumMetaOpers, NumCallArgs, Callee, IsAnyRegCC, CLI))
731 return false;
732
733 assert(CLI.Call && "No call instruction specified.");
734
735 SmallVector<MachineOperand, 32> Ops;
736
737 // Add an explicit result reg if we use the anyreg calling convention.
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000738 if (IsAnyRegCC && HasDef) {
Juergen Ributzkaa4159432014-07-15 02:22:43 +0000739 assert(CLI.NumResultRegs == 0 && "Unexpected result register.");
740 CLI.ResultReg = createResultReg(TLI.getRegClassFor(MVT::i64));
741 CLI.NumResultRegs = 1;
742 Ops.push_back(MachineOperand::CreateReg(CLI.ResultReg, /*IsDef=*/true));
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000743 }
744
745 // Add the <id> and <numBytes> constants.
746 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
747 "Expected a constant integer.");
748 const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
749 Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
750
751 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
752 "Expected a constant integer.");
753 const auto *NumBytes =
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000754 cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000755 Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
756
757 // Assume that the callee is a constant address or null pointer.
758 // FIXME: handle function symbols in the future.
Juergen Ributzkae8514fc2014-07-31 00:11:16 +0000759 uint64_t CalleeAddr;
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000760 if (const auto *C = dyn_cast<IntToPtrInst>(Callee))
761 CalleeAddr = cast<ConstantInt>(C->getOperand(0))->getZExtValue();
762 else if (const auto *C = dyn_cast<ConstantExpr>(Callee)) {
763 if (C->getOpcode() == Instruction::IntToPtr)
764 CalleeAddr = cast<ConstantInt>(C->getOperand(0))->getZExtValue();
765 else
766 llvm_unreachable("Unsupported ConstantExpr.");
767 } else if (isa<ConstantPointerNull>(Callee))
768 CalleeAddr = 0;
769 else
770 llvm_unreachable("Unsupported callee address.");
771
772 Ops.push_back(MachineOperand::CreateImm(CalleeAddr));
773
774 // Adjust <numArgs> to account for any arguments that have been passed on
775 // the stack instead.
776 unsigned NumCallRegArgs = IsAnyRegCC ? NumArgs : CLI.OutRegs.size();
777 Ops.push_back(MachineOperand::CreateImm(NumCallRegArgs));
778
779 // Add the calling convention
780 Ops.push_back(MachineOperand::CreateImm((unsigned)CC));
781
782 // Add the arguments we omitted previously. The register allocator should
783 // place these in any free register.
784 if (IsAnyRegCC) {
785 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i) {
786 unsigned Reg = getRegForValue(I->getArgOperand(i));
787 if (!Reg)
788 return false;
789 Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
790 }
791 }
792
793 // Push the arguments from the call instruction.
794 for (auto Reg : CLI.OutRegs)
795 Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
796
797 // Push live variables for the stack map.
798 if (!addStackMapLiveVars(Ops, I, NumMetaOpers + NumArgs))
799 return false;
800
801 // Push the register mask info.
802 Ops.push_back(MachineOperand::CreateRegMask(TRI.getCallPreservedMask(CC)));
803
804 // Add scratch registers as implicit def and early clobber.
805 const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
806 for (unsigned i = 0; ScratchRegs[i]; ++i)
807 Ops.push_back(MachineOperand::CreateReg(
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000808 ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
809 /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000810
811 // Add implicit defs (return values).
812 for (auto Reg : CLI.InRegs)
813 Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/true,
814 /*IsImpl=*/true));
815
Juergen Ributzka718bb712014-07-15 02:22:46 +0000816 // Insert the patchpoint instruction before the call generated by the target.
817 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, CLI.Call, DbgLoc,
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000818 TII.get(TargetOpcode::PATCHPOINT));
819
820 for (auto &MO : Ops)
821 MIB.addOperand(MO);
822
823 MIB->setPhysRegsDeadExcept(CLI.InRegs, TRI);
824
825 // Delete the original call instruction.
826 CLI.Call->eraseFromParent();
827
828 // Inform the Frame Information that we have a patchpoint in this function.
829 FuncInfo.MF->getFrameInfo()->setHasPatchPoint();
830
Juergen Ributzkaa4159432014-07-15 02:22:43 +0000831 if (CLI.NumResultRegs)
832 UpdateValueMap(I, CLI.ResultReg, CLI.NumResultRegs);
Juergen Ributzka3d9e6752014-07-11 22:19:02 +0000833 return true;
834}
835
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000836/// Returns an AttributeSet representing the attributes applied to the return
837/// value of the given call.
838static AttributeSet getReturnAttrs(FastISel::CallLoweringInfo &CLI) {
839 SmallVector<Attribute::AttrKind, 2> Attrs;
840 if (CLI.RetSExt)
841 Attrs.push_back(Attribute::SExt);
842 if (CLI.RetZExt)
843 Attrs.push_back(Attribute::ZExt);
844 if (CLI.IsInReg)
845 Attrs.push_back(Attribute::InReg);
846
847 return AttributeSet::get(CLI.RetTy->getContext(), AttributeSet::ReturnIndex,
848 Attrs);
849}
850
851bool FastISel::LowerCallTo(const CallInst *CI, const char *SymName,
852 unsigned NumArgs) {
853 ImmutableCallSite CS(CI);
854
855 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
856 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
857 Type *RetTy = FTy->getReturnType();
858
859 ArgListTy Args;
860 Args.reserve(NumArgs);
861
862 // Populate the argument list.
863 // Attributes for args start at offset 1, after the return attribute.
864 for (unsigned ArgI = 0; ArgI != NumArgs; ++ArgI) {
865 Value *V = CI->getOperand(ArgI);
866
867 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
868
869 ArgListEntry Entry;
870 Entry.Val = V;
871 Entry.Ty = V->getType();
872 Entry.setAttributes(&CS, ArgI + 1);
873 Args.push_back(Entry);
874 }
875
876 CallLoweringInfo CLI;
877 CLI.setCallee(RetTy, FTy, SymName, std::move(Args), CS, NumArgs);
878
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000879 return lowerCallTo(CLI);
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000880}
881
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000882bool FastISel::lowerCallTo(CallLoweringInfo &CLI) {
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000883 // Handle the incoming return values from the call.
884 CLI.clearIns();
885 SmallVector<EVT, 4> RetTys;
886 ComputeValueVTs(TLI, CLI.RetTy, RetTys);
887
888 SmallVector<ISD::OutputArg, 4> Outs;
889 GetReturnInfo(CLI.RetTy, getReturnAttrs(CLI), Outs, TLI);
890
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000891 bool CanLowerReturn = TLI.CanLowerReturn(
892 CLI.CallConv, *FuncInfo.MF, CLI.IsVarArg, Outs, CLI.RetTy->getContext());
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000893
894 // FIXME: sret demotion isn't supported yet - bail out.
895 if (!CanLowerReturn)
896 return false;
897
898 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
899 EVT VT = RetTys[I];
900 MVT RegisterVT = TLI.getRegisterType(CLI.RetTy->getContext(), VT);
901 unsigned NumRegs = TLI.getNumRegisters(CLI.RetTy->getContext(), VT);
902 for (unsigned i = 0; i != NumRegs; ++i) {
903 ISD::InputArg MyFlags;
904 MyFlags.VT = RegisterVT;
905 MyFlags.ArgVT = VT;
906 MyFlags.Used = CLI.IsReturnValueUsed;
907 if (CLI.RetSExt)
908 MyFlags.Flags.setSExt();
909 if (CLI.RetZExt)
910 MyFlags.Flags.setZExt();
911 if (CLI.IsInReg)
912 MyFlags.Flags.setInReg();
913 CLI.Ins.push_back(MyFlags);
914 }
915 }
916
917 // Handle all of the outgoing arguments.
918 CLI.clearOuts();
919 for (auto &Arg : CLI.getArgs()) {
920 Type *FinalType = Arg.Ty;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000921 if (Arg.IsByVal)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000922 FinalType = cast<PointerType>(Arg.Ty)->getElementType();
923 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000924 FinalType, CLI.CallConv, CLI.IsVarArg);
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000925
926 ISD::ArgFlagsTy Flags;
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000927 if (Arg.IsZExt)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000928 Flags.setZExt();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000929 if (Arg.IsSExt)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000930 Flags.setSExt();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000931 if (Arg.IsInReg)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000932 Flags.setInReg();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000933 if (Arg.IsSRet)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000934 Flags.setSRet();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000935 if (Arg.IsByVal)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000936 Flags.setByVal();
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000937 if (Arg.IsInAlloca) {
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000938 Flags.setInAlloca();
939 // Set the byval flag for CCAssignFn callbacks that don't know about
940 // inalloca. This way we can know how many bytes we should've allocated
941 // and how many bytes a callee cleanup function will pop. If we port
942 // inalloca to more targets, we'll have to add custom inalloca handling in
943 // the various CC lowering callbacks.
944 Flags.setByVal();
945 }
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000946 if (Arg.IsByVal || Arg.IsInAlloca) {
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000947 PointerType *Ty = cast<PointerType>(Arg.Ty);
948 Type *ElementTy = Ty->getElementType();
949 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
950 // For ByVal, alignment should come from FE. BE will guess if this info is
951 // not there, but there are cases it cannot get right.
952 unsigned FrameAlign = Arg.Alignment;
953 if (!FrameAlign)
954 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
955 Flags.setByValSize(FrameSize);
956 Flags.setByValAlign(FrameAlign);
957 }
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000958 if (Arg.IsNest)
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000959 Flags.setNest();
960 if (NeedsRegBlock)
961 Flags.setInConsecutiveRegs();
962 unsigned OriginalAlignment = DL.getABITypeAlignment(Arg.Ty);
963 Flags.setOrigAlign(OriginalAlignment);
964
965 CLI.OutVals.push_back(Arg.Val);
966 CLI.OutFlags.push_back(Flags);
967 }
968
969 if (!FastLowerCall(CLI))
970 return false;
971
972 // Set all unused physreg defs as dead.
973 assert(CLI.Call && "No call instruction specified.");
974 CLI.Call->setPhysRegsDeadExcept(CLI.InRegs, TRI);
975
976 if (CLI.NumResultRegs && CLI.CS)
977 UpdateValueMap(CLI.CS->getInstruction(), CLI.ResultReg, CLI.NumResultRegs);
978
979 return true;
980}
981
Juergen Ributzka7a76c242014-09-03 18:46:45 +0000982bool FastISel::lowerCall(const CallInst *CI) {
Juergen Ributzka8179e9e2014-07-11 22:01:42 +0000983 ImmutableCallSite CS(CI);
984
985 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
986 FunctionType *FuncTy = cast<FunctionType>(PT->getElementType());
987 Type *RetTy = FuncTy->getReturnType();
988
989 ArgListTy Args;
990 ArgListEntry Entry;
991 Args.reserve(CS.arg_size());
992
993 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
994 i != e; ++i) {
995 Value *V = *i;
996
997 // Skip empty types
998 if (V->getType()->isEmptyTy())
999 continue;
1000
1001 Entry.Val = V;
1002 Entry.Ty = V->getType();
1003
1004 // Skip the first return-type Attribute to get to params.
1005 Entry.setAttributes(&CS, i - CS.arg_begin() + 1);
1006 Args.push_back(Entry);
1007 }
1008
1009 // Check if target-independent constraints permit a tail call here.
1010 // Target-dependent constraints are checked within FastLowerCall.
1011 bool IsTailCall = CI->isTailCall();
Juergen Ributzka480872b2014-07-16 00:01:22 +00001012 if (IsTailCall && !isInTailCallPosition(CS, TM))
Juergen Ributzka8179e9e2014-07-11 22:01:42 +00001013 IsTailCall = false;
1014
1015 CallLoweringInfo CLI;
1016 CLI.setCallee(RetTy, FuncTy, CI->getCalledValue(), std::move(Args), CS)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001017 .setTailCall(IsTailCall);
Juergen Ributzka8179e9e2014-07-11 22:01:42 +00001018
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001019 return lowerCallTo(CLI);
Juergen Ributzka8179e9e2014-07-11 22:01:42 +00001020}
1021
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001022bool FastISel::selectCall(const User *I) {
Dan Gohman7da91ae2011-04-26 17:18:34 +00001023 const CallInst *Call = cast<CallInst>(I);
1024
1025 // Handle simple inline asms.
Dan Gohmande239d22011-10-12 15:56:56 +00001026 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Juergen Ributzka618ce3e2014-07-16 22:20:51 +00001027 // If the inline asm has side effects, then make sure that no local value
1028 // lives across by flushing the local value map.
1029 if (IA->hasSideEffects())
1030 flushLocalValueMap();
1031
Dan Gohman7da91ae2011-04-26 17:18:34 +00001032 // Don't attempt to handle constraints.
1033 if (!IA->getConstraintString().empty())
1034 return false;
1035
1036 unsigned ExtraInfo = 0;
1037 if (IA->hasSideEffects())
1038 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
1039 if (IA->isAlignStack())
1040 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
1041
Rafael Espindolaea09c592014-02-18 22:05:46 +00001042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohman7da91ae2011-04-26 17:18:34 +00001043 TII.get(TargetOpcode::INLINEASM))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001044 .addExternalSymbol(IA->getAsmString().c_str())
1045 .addImm(ExtraInfo);
Dan Gohman7da91ae2011-04-26 17:18:34 +00001046 return true;
1047 }
1048
Michael J. Spencer8b98bf22012-02-22 19:06:13 +00001049 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
1050 ComputeUsesVAFloatArgument(*Call, &MMI);
1051
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001052 // Handle intrinsic function calls.
1053 if (const auto *II = dyn_cast<IntrinsicInst>(Call))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001054 return selectIntrinsicCall(II);
Dan Gohman32a733e2008-09-25 17:05:24 +00001055
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001056 // Usually, it does not make sense to initialize a value,
1057 // make an unrelated function call and use the value, because
1058 // it tends to be spilled on the stack. So, we move the pointer
1059 // to the last local value to the beginning of the block, so that
1060 // all the values which have already been materialized,
1061 // appear after the call. It also makes sense to skip intrinsics
1062 // since they tend to be inlined.
1063 flushLocalValueMap();
1064
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001065 return lowerCall(Call);
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001066}
1067
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001068bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001069 switch (II->getIntrinsicID()) {
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001070 default:
1071 break;
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001072 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher81e2bf22012-02-17 23:03:39 +00001073 case Intrinsic::lifetime_start:
1074 case Intrinsic::lifetime_end:
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001075 // The donothing intrinsic does, well, nothing.
Chad Rosier88d53ea2012-07-06 17:33:39 +00001076 case Intrinsic::donothing:
Eric Christopher81e2bf22012-02-17 23:03:39 +00001077 return true;
Bill Wendling65c0fd42009-02-13 02:16:35 +00001078 case Intrinsic::dbg_declare: {
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001079 const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
Manman Ren983a16c2013-06-28 05:43:10 +00001080 DIVariable DIVar(DI->getVariable());
Stephen Lincfe7f352013-07-08 00:37:03 +00001081 assert((!DIVar || DIVar.isVariable()) &&
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001082 "Variable in DbgDeclareInst should be either null or a DIVariable.");
1083 if (!DIVar || !FuncInfo.MF->getMMI().hasDebugInfo()) {
Eric Christopher142820b2012-03-15 21:33:44 +00001084 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel87127712009-07-02 22:43:26 +00001085 return true;
Eric Christopher142820b2012-03-15 21:33:44 +00001086 }
Devang Patel87127712009-07-02 22:43:26 +00001087
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001088 const Value *Address = DI->getAddress();
Eric Christopher3390a6e2012-03-15 21:33:47 +00001089 if (!Address || isa<UndefValue>(Address)) {
Eric Christopher142820b2012-03-15 21:33:44 +00001090 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendb2eb472010-02-06 02:26:02 +00001091 return true;
Eric Christopher142820b2012-03-15 21:33:44 +00001092 }
Devang Patele4682fa2010-09-14 20:29:31 +00001093
Adrian Prantl418d1d12013-07-09 20:28:37 +00001094 unsigned Offset = 0;
David Blaikie0252265b2013-06-16 20:34:15 +00001095 Optional<MachineOperand> Op;
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001096 if (const auto *Arg = dyn_cast<Argument>(Address))
Devang Patel9d904e12011-09-08 22:59:09 +00001097 // Some arguments' frame index is recorded during argument lowering.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001098 Offset = FuncInfo.getArgumentFrameIndex(Arg);
1099 if (Offset)
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001100 Op = MachineOperand::CreateFI(Offset);
David Blaikie0252265b2013-06-16 20:34:15 +00001101 if (!Op)
1102 if (unsigned Reg = lookUpRegForValue(Address))
1103 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher60e01c52012-03-20 01:07:58 +00001104
Bill Wendling9f829f12012-03-30 00:02:55 +00001105 // If we have a VLA that has a "use" in a metadata node that's then used
1106 // here but it has no other uses, then we have a problem. E.g.,
1107 //
1108 // int foo (const int *x) {
1109 // char a[*x];
1110 // return 0;
1111 // }
1112 //
1113 // If we assign 'a' a vreg and fast isel later on has to use the selection
1114 // DAG isel, it will want to copy the value to the vreg. However, there are
1115 // no uses, which goes counter to what selection DAG isel expects.
David Blaikie0252265b2013-06-16 20:34:15 +00001116 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher60e01c52012-03-20 01:07:58 +00001117 (!isa<AllocaInst>(Address) ||
1118 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie0252265b2013-06-16 20:34:15 +00001119 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
Adrian Prantl262bcf42013-09-18 22:08:59 +00001120 false);
Wesley Peck527da1b2010-11-23 03:31:01 +00001121
Adrian Prantl262bcf42013-09-18 22:08:59 +00001122 if (Op) {
Adrian Prantl418d1d12013-07-09 20:28:37 +00001123 if (Op->isReg()) {
1124 Op->setIsDebug(true);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001125 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie6004dbc2013-10-14 20:15:04 +00001126 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
1127 DI->getVariable());
1128 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001129 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie6004dbc2013-10-14 20:15:04 +00001130 TII.get(TargetOpcode::DBG_VALUE))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001131 .addOperand(*Op)
1132 .addImm(0)
1133 .addMetadata(DI->getVariable());
Adrian Prantl262bcf42013-09-18 22:08:59 +00001134 } else {
Eric Christophere5e54c82012-03-20 01:07:53 +00001135 // We can't yet handle anything else here because it would require
1136 // generating code, thus altering codegen because of debug info.
Adrian Prantl0d1e5592013-05-22 18:02:19 +00001137 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Adrian Prantl262bcf42013-09-18 22:08:59 +00001138 }
Dan Gohman32a733e2008-09-25 17:05:24 +00001139 return true;
Bill Wendling65c0fd42009-02-13 02:16:35 +00001140 }
Dale Johannesendd331042010-02-26 20:01:55 +00001141 case Intrinsic::dbg_value: {
Dale Johannesen5d7f0a02010-04-07 01:15:14 +00001142 // This form of DBG_VALUE is target-independent.
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001143 const DbgValueInst *DI = cast<DbgValueInst>(II);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001144 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001145 const Value *V = DI->getValue();
Dale Johannesendd331042010-02-26 20:01:55 +00001146 if (!V) {
1147 // Currently the optimizer can produce this; insert an undef to
1148 // help debugging. Probably the optimizer should not do this.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001150 .addReg(0U)
1151 .addImm(DI->getOffset())
1152 .addMetadata(DI->getVariable());
1153 } else if (const auto *CI = dyn_cast<ConstantInt>(V)) {
Devang Patelf071d722011-06-24 20:46:11 +00001154 if (CI->getBitWidth() > 64)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001155 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001156 .addCImm(CI)
1157 .addImm(DI->getOffset())
1158 .addMetadata(DI->getVariable());
Chad Rosier879c34f2012-07-06 17:44:22 +00001159 else
Rafael Espindolaea09c592014-02-18 22:05:46 +00001160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001161 .addImm(CI->getZExtValue())
1162 .addImm(DI->getOffset())
1163 .addMetadata(DI->getVariable());
1164 } else if (const auto *CF = dyn_cast<ConstantFP>(V)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001166 .addFPImm(CF)
1167 .addImm(DI->getOffset())
1168 .addMetadata(DI->getVariable());
Dale Johannesendd331042010-02-26 20:01:55 +00001169 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantldb3e26d2013-09-16 23:29:03 +00001170 // FIXME: This does not handle register-indirect values at offset 0.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001171 bool IsIndirect = DI->getOffset() != 0;
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect, Reg,
1173 DI->getOffset(), DI->getVariable());
Dale Johannesendd331042010-02-26 20:01:55 +00001174 } else {
1175 // We can't yet handle anything else here because it would require
1176 // generating code, thus altering codegen because of debug info.
Adrian Prantl0d1e5592013-05-22 18:02:19 +00001177 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peck527da1b2010-11-23 03:31:01 +00001178 }
Dale Johannesendd331042010-02-26 20:01:55 +00001179 return true;
1180 }
Eli Friedman8f1e11c2011-05-14 00:47:51 +00001181 case Intrinsic::objectsize: {
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001182 ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1));
Eli Friedman8f1e11c2011-05-14 00:47:51 +00001183 unsigned long long Res = CI->isZero() ? -1ULL : 0;
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001184 Constant *ResCI = ConstantInt::get(II->getType(), Res);
Eli Friedman8f1e11c2011-05-14 00:47:51 +00001185 unsigned ResultReg = getRegForValue(ResCI);
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001186 if (!ResultReg)
Eli Friedman8f1e11c2011-05-14 00:47:51 +00001187 return false;
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001188 UpdateValueMap(II, ResultReg);
Eli Friedman8f1e11c2011-05-14 00:47:51 +00001189 return true;
1190 }
Chad Rosier9c1796f2013-03-07 20:42:17 +00001191 case Intrinsic::expect: {
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001192 unsigned ResultReg = getRegForValue(II->getArgOperand(0));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001193 if (!ResultReg)
Nick Lewycky48beb212013-03-11 21:44:37 +00001194 return false;
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001195 UpdateValueMap(II, ResultReg);
Chad Rosier3a200e12013-03-07 21:38:33 +00001196 return true;
Chad Rosier9c1796f2013-03-07 20:42:17 +00001197 }
Juergen Ributzka190305b2014-07-01 22:25:49 +00001198 case Intrinsic::experimental_stackmap:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001199 return selectStackmap(II);
Juergen Ributzka3d9e6752014-07-11 22:19:02 +00001200 case Intrinsic::experimental_patchpoint_void:
1201 case Intrinsic::experimental_patchpoint_i64:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001202 return selectPatchpoint(II);
Dan Gohman32a733e2008-09-25 17:05:24 +00001203 }
Dan Gohman8a2dae52010-04-13 17:07:06 +00001204
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001205 return FastLowerIntrinsicCall(II);
Dan Gohman32a733e2008-09-25 17:05:24 +00001206}
1207
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001208bool FastISel::selectCast(const User *I, unsigned Opcode) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001209 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1210 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001211
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001212 if (SrcVT == MVT::Other || !SrcVT.isSimple() || DstVT == MVT::Other ||
1213 !DstVT.isSimple())
Owen Andersonca1711a2008-08-26 23:46:32 +00001214 // Unhandled type. Halt "fast" selection and bail.
1215 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001216
Eli Friedmanc7035512011-05-25 23:49:02 +00001217 // Check if the destination type is legal.
Dan Gohmana62e4ab2009-03-13 23:53:06 +00001218 if (!TLI.isTypeLegal(DstVT))
Eli Friedmanc7035512011-05-25 23:49:02 +00001219 return false;
Dan Gohmana62e4ab2009-03-13 23:53:06 +00001220
Eli Friedmanc7035512011-05-25 23:49:02 +00001221 // Check if the source operand is legal.
Dan Gohmana62e4ab2009-03-13 23:53:06 +00001222 if (!TLI.isTypeLegal(SrcVT))
Eli Friedmanc7035512011-05-25 23:49:02 +00001223 return false;
Dan Gohmana62e4ab2009-03-13 23:53:06 +00001224
Dan Gohman7bda51f2008-09-03 23:12:08 +00001225 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersonca1711a2008-08-26 23:46:32 +00001226 if (!InputReg)
1227 // Unhandled operand. Halt "fast" selection and bail.
1228 return false;
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001229
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001230 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
1231
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001232 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
1233 Opcode, InputReg, InputRegIsKill);
Owen Andersonca1711a2008-08-26 23:46:32 +00001234 if (!ResultReg)
1235 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001236
Dan Gohman7bda51f2008-09-03 23:12:08 +00001237 UpdateValueMap(I, ResultReg);
Owen Andersonca1711a2008-08-26 23:46:32 +00001238 return true;
1239}
1240
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001241bool FastISel::selectBitCast(const User *I) {
Dan Gohmanb0b5a272008-08-27 18:10:19 +00001242 // If the bitcast doesn't change the type, just use the operand value.
1243 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman7bda51f2008-09-03 23:12:08 +00001244 unsigned Reg = getRegForValue(I->getOperand(0));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001245 if (!Reg)
Dan Gohman61cfa302008-08-27 20:41:38 +00001246 return false;
Dan Gohman7bda51f2008-09-03 23:12:08 +00001247 UpdateValueMap(I, Reg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +00001248 return true;
1249 }
1250
Wesley Peck527da1b2010-11-23 03:31:01 +00001251 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglundc494d242012-12-17 14:30:06 +00001252 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
1253 EVT DstEVT = TLI.getValueType(I->getType());
1254 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
1255 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersonca1711a2008-08-26 23:46:32 +00001256 // Unhandled type. Halt "fast" selection and bail.
1257 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001258
Patrik Hagglundc494d242012-12-17 14:30:06 +00001259 MVT SrcVT = SrcEVT.getSimpleVT();
1260 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman7bda51f2008-09-03 23:12:08 +00001261 unsigned Op0 = getRegForValue(I->getOperand(0));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001262 if (!Op0) // Unhandled operand. Halt "fast" selection and bail.
Owen Andersonca1711a2008-08-26 23:46:32 +00001263 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001264 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00001265
Dan Gohmanb0b5a272008-08-27 18:10:19 +00001266 // First, try to perform the bitcast by inserting a reg-reg copy.
1267 unsigned ResultReg = 0;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001268 if (SrcVT == DstVT) {
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001269 const TargetRegisterClass *SrcClass = TLI.getRegClassFor(SrcVT);
1270 const TargetRegisterClass *DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesen51642ae2010-07-11 05:16:54 +00001271 // Don't attempt a cross-class copy. It will likely fail.
1272 if (SrcClass == DstClass) {
1273 ResultReg = createResultReg(DstClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1275 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
Jakob Stoklund Olesen51642ae2010-07-11 05:16:54 +00001276 }
Dan Gohmanb0b5a272008-08-27 18:10:19 +00001277 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001278
1279 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanb0b5a272008-08-27 18:10:19 +00001280 if (!ResultReg)
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001281 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peck527da1b2010-11-23 03:31:01 +00001282
Dan Gohmanb0b5a272008-08-27 18:10:19 +00001283 if (!ResultReg)
Owen Andersonca1711a2008-08-26 23:46:32 +00001284 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001285
Dan Gohman7bda51f2008-09-03 23:12:08 +00001286 UpdateValueMap(I, ResultReg);
Owen Andersonca1711a2008-08-26 23:46:32 +00001287 return true;
1288}
1289
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001290bool FastISel::SelectInstruction(const Instruction *I) {
Dan Gohman6e9a8fc2010-04-23 15:29:50 +00001291 // Just before the terminator instruction, insert instructions to
1292 // feed PHI nodes in successor blocks.
1293 if (isa<TerminatorInst>(I))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001294 if (!handlePHINodesInSuccessorBlocks(I->getParent()))
Dan Gohman6e9a8fc2010-04-23 15:29:50 +00001295 return false;
1296
Rafael Espindolaea09c592014-02-18 22:05:46 +00001297 DbgLoc = I->getDebugLoc();
Dan Gohmane450d742010-04-20 00:48:35 +00001298
Chad Rosier46addb92011-11-29 19:40:47 +00001299 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
1300
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001301 if (const auto *Call = dyn_cast<CallInst>(I)) {
Bob Wilson3e6fa462012-08-03 04:06:28 +00001302 const Function *F = Call->getCalledFunction();
1303 LibFunc::Func Func;
Akira Hatanaka3d90f992014-04-15 21:30:06 +00001304
1305 // As a special case, don't handle calls to builtin library functions that
1306 // may be translated directly to target instructions.
Bob Wilson3e6fa462012-08-03 04:06:28 +00001307 if (F && !F->hasLocalLinkage() && F->hasName() &&
1308 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson871701c2012-08-03 21:26:24 +00001309 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilson3e6fa462012-08-03 04:06:28 +00001310 return false;
Akira Hatanaka3d90f992014-04-15 21:30:06 +00001311
1312 // Don't handle Intrinsic::trap if a trap funciton is specified.
1313 if (F && F->getIntrinsicID() == Intrinsic::trap &&
1314 !TM.Options.getTrapFunctionName().empty())
1315 return false;
Bob Wilson3e6fa462012-08-03 04:06:28 +00001316 }
1317
Dan Gohman18f94462009-12-05 01:27:58 +00001318 // First, try doing target-independent selection.
Juergen Ributzka7e998fb2014-09-02 21:07:44 +00001319 if (!SkipTargetIndependentISel) {
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001320 if (selectOperator(I, I->getOpcode())) {
Juergen Ributzka7e998fb2014-09-02 21:07:44 +00001321 ++NumFastIselSuccessIndependent;
1322 DbgLoc = DebugLoc();
1323 return true;
1324 }
1325 // Remove dead code. However, ignore call instructions since we've flushed
1326 // the local value map and recomputed the insert point.
1327 if (!isa<CallInst>(I)) {
1328 recomputeInsertPt();
1329 if (SavedInsertPt != FuncInfo.InsertPt)
1330 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
1331 }
1332 SavedInsertPt = FuncInfo.InsertPt;
1333 }
1334 // Next, try calling the target to attempt to handle the instruction.
1335 if (TargetSelectInstruction(I)) {
1336 ++NumFastIselSuccessTarget;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001337 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +00001338 return true;
Dan Gohmane450d742010-04-20 00:48:35 +00001339 }
Chad Rosier879c34f2012-07-06 17:44:22 +00001340 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosier46addb92011-11-29 19:40:47 +00001341 // the local value map and recomputed the insert point.
1342 if (!isa<CallInst>(I)) {
1343 recomputeInsertPt();
1344 if (SavedInsertPt != FuncInfo.InsertPt)
1345 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
1346 }
Dan Gohman18f94462009-12-05 01:27:58 +00001347
Rafael Espindolaea09c592014-02-18 22:05:46 +00001348 DbgLoc = DebugLoc();
Juergen Ributzka31328162014-08-28 02:06:55 +00001349 // Undo phi node updates, because they will be added again by SelectionDAG.
1350 if (isa<TerminatorInst>(I))
1351 FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
Dan Gohman18f94462009-12-05 01:27:58 +00001352 return false;
Dan Gohmanfcf54562008-09-05 18:18:20 +00001353}
1354
Dan Gohman1ab1d312008-10-02 22:15:21 +00001355/// FastEmitBranch - Emit an unconditional branch to the given block,
1356/// unless it is the immediate (fall-through) successor, and update
1357/// the CFG.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001358void FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
Evan Cheng615620c2013-02-11 01:27:15 +00001359 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
1360 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christophere9abba72012-04-10 18:18:10 +00001361 // For more accurate line information if this is the only instruction
1362 // in the block then emit it, otherwise we have the unconditional
1363 // fall-through case, which needs no instructions.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001364 } else {
1365 // The unconditional branch case.
Craig Topperc0196b12014-04-14 00:51:57 +00001366 TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
Rafael Espindolaea09c592014-02-18 22:05:46 +00001367 SmallVector<MachineOperand, 0>(), DbgLoc);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001368 }
Juergen Ributzka454d3742014-06-13 00:45:11 +00001369 uint32_t BranchWeight = 0;
1370 if (FuncInfo.BPI)
1371 BranchWeight = FuncInfo.BPI->getEdgeWeight(FuncInfo.MBB->getBasicBlock(),
1372 MSucc->getBasicBlock());
1373 FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001374}
1375
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001376/// SelectFNeg - Emit an FNeg operation.
1377///
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001378bool FastISel::selectFNeg(const User *I) {
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001379 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001380 if (!OpReg)
1381 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001382 bool OpRegIsKill = hasTrivialKill(I);
1383
Dan Gohman9cbef322009-09-11 00:36:43 +00001384 // If the target has ISD::FNEG, use it.
1385 EVT VT = TLI.getValueType(I->getType());
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001386 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), ISD::FNEG,
1387 OpReg, OpRegIsKill);
1388 if (ResultReg) {
Dan Gohman9cbef322009-09-11 00:36:43 +00001389 UpdateValueMap(I, ResultReg);
1390 return true;
1391 }
1392
Dan Gohman89b090e2009-09-11 00:34:46 +00001393 // Bitcast the value to integer, twiddle the sign bit with xor,
1394 // and then bitcast it back to floating-point.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001395 if (VT.getSizeInBits() > 64)
1396 return false;
Dan Gohman89b090e2009-09-11 00:34:46 +00001397 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
1398 if (!TLI.isTypeLegal(IntVT))
1399 return false;
1400
1401 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00001402 ISD::BITCAST, OpReg, OpRegIsKill);
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001403 if (!IntReg)
Dan Gohman89b090e2009-09-11 00:34:46 +00001404 return false;
1405
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001406 unsigned IntResultReg = FastEmit_ri_(
1407 IntVT.getSimpleVT(), ISD::XOR, IntReg, /*IsKill=*/true,
1408 UINT64_C(1) << (VT.getSizeInBits() - 1), IntVT.getSimpleVT());
1409 if (!IntResultReg)
Dan Gohman89b090e2009-09-11 00:34:46 +00001410 return false;
1411
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001412 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), ISD::BITCAST,
1413 IntResultReg, /*IsKill=*/true);
1414 if (!ResultReg)
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001415 return false;
1416
1417 UpdateValueMap(I, ResultReg);
1418 return true;
1419}
1420
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001421bool FastISel::selectExtractValue(const User *U) {
Eli Friedman9ac94472011-05-16 20:27:46 +00001422 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedman4c08bb42011-05-16 20:34:53 +00001423 if (!EVI)
Eli Friedman9ac94472011-05-16 20:27:46 +00001424 return false;
1425
Eli Friedmana4d4a012011-05-16 21:06:17 +00001426 // Make sure we only try to handle extracts with a legal result. But also
1427 // allow i1 because it's easy.
Eli Friedman9ac94472011-05-16 20:27:46 +00001428 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
1429 if (!RealVT.isSimple())
1430 return false;
1431 MVT VT = RealVT.getSimpleVT();
Eli Friedmana4d4a012011-05-16 21:06:17 +00001432 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman9ac94472011-05-16 20:27:46 +00001433 return false;
1434
1435 const Value *Op0 = EVI->getOperand(0);
Chris Lattner229907c2011-07-18 04:54:35 +00001436 Type *AggTy = Op0->getType();
Eli Friedman9ac94472011-05-16 20:27:46 +00001437
1438 // Get the base result register.
1439 unsigned ResultReg;
1440 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
1441 if (I != FuncInfo.ValueMap.end())
1442 ResultReg = I->second;
Eli Friedmanbd375f12011-06-06 05:46:34 +00001443 else if (isa<Instruction>(Op0))
Eli Friedman9ac94472011-05-16 20:27:46 +00001444 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedmanbd375f12011-06-06 05:46:34 +00001445 else
1446 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman9ac94472011-05-16 20:27:46 +00001447
1448 // Get the actual result register, which is an offset from the base register.
Jay Foad57aa6362011-07-13 10:26:04 +00001449 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman9ac94472011-05-16 20:27:46 +00001450
1451 SmallVector<EVT, 4> AggValueVTs;
1452 ComputeValueVTs(TLI, AggTy, AggValueVTs);
1453
1454 for (unsigned i = 0; i < VTIndex; i++)
1455 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
1456
1457 UpdateValueMap(EVI, ResultReg);
1458 return true;
1459}
1460
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001461bool FastISel::selectOperator(const User *I, unsigned Opcode) {
Dan Gohmanfcf54562008-09-05 18:18:20 +00001462 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001463 case Instruction::Add:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001464 return selectBinaryOp(I, ISD::ADD);
Dan Gohmana5b96452009-06-04 22:49:04 +00001465 case Instruction::FAdd:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001466 return selectBinaryOp(I, ISD::FADD);
Dan Gohmana5b96452009-06-04 22:49:04 +00001467 case Instruction::Sub:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001468 return selectBinaryOp(I, ISD::SUB);
Dan Gohmana5b96452009-06-04 22:49:04 +00001469 case Instruction::FSub:
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001470 // FNeg is currently represented in LLVM IR as a special case of FSub.
1471 if (BinaryOperator::isFNeg(I))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001472 return selectFNeg(I);
1473 return selectBinaryOp(I, ISD::FSUB);
Dan Gohmana5b96452009-06-04 22:49:04 +00001474 case Instruction::Mul:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001475 return selectBinaryOp(I, ISD::MUL);
Dan Gohmana5b96452009-06-04 22:49:04 +00001476 case Instruction::FMul:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001477 return selectBinaryOp(I, ISD::FMUL);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001478 case Instruction::SDiv:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001479 return selectBinaryOp(I, ISD::SDIV);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001480 case Instruction::UDiv:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001481 return selectBinaryOp(I, ISD::UDIV);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001482 case Instruction::FDiv:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001483 return selectBinaryOp(I, ISD::FDIV);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001484 case Instruction::SRem:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001485 return selectBinaryOp(I, ISD::SREM);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001486 case Instruction::URem:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001487 return selectBinaryOp(I, ISD::UREM);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001488 case Instruction::FRem:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001489 return selectBinaryOp(I, ISD::FREM);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001490 case Instruction::Shl:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001491 return selectBinaryOp(I, ISD::SHL);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001492 case Instruction::LShr:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001493 return selectBinaryOp(I, ISD::SRL);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001494 case Instruction::AShr:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001495 return selectBinaryOp(I, ISD::SRA);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001496 case Instruction::And:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001497 return selectBinaryOp(I, ISD::AND);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001498 case Instruction::Or:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001499 return selectBinaryOp(I, ISD::OR);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001500 case Instruction::Xor:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001501 return selectBinaryOp(I, ISD::XOR);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001502
Dan Gohman7bda51f2008-09-03 23:12:08 +00001503 case Instruction::GetElementPtr:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001504 return selectGetElementPtr(I);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +00001505
Dan Gohman7bda51f2008-09-03 23:12:08 +00001506 case Instruction::Br: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001507 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +00001508
Dan Gohman7bda51f2008-09-03 23:12:08 +00001509 if (BI->isUnconditional()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001510 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001511 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings0125b642010-06-17 22:43:56 +00001512 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman7bda51f2008-09-03 23:12:08 +00001513 return true;
Owen Anderson14054922008-08-27 00:31:01 +00001514 }
Dan Gohman7bda51f2008-09-03 23:12:08 +00001515
1516 // Conditional branches are not handed yet.
1517 // Halt "fast" selection and bail.
1518 return false;
Dan Gohmanb2226e22008-08-13 20:19:35 +00001519 }
1520
Dan Gohmanea56bdd2008-09-05 01:08:41 +00001521 case Instruction::Unreachable:
Yaron Kerend7ba46b2014-04-19 13:47:43 +00001522 if (TM.Options.TrapUnreachable)
1523 return FastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
1524 else
1525 return true;
Dan Gohmanea56bdd2008-09-05 01:08:41 +00001526
Dan Gohman39d82f92008-09-10 20:11:02 +00001527 case Instruction::Alloca:
1528 // FunctionLowering has the static-sized case covered.
Dan Gohman87fb4e82010-07-07 16:29:44 +00001529 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman39d82f92008-09-10 20:11:02 +00001530 return true;
1531
1532 // Dynamic-sized alloca is not handled yet.
1533 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001534
Dan Gohman32a733e2008-09-25 17:05:24 +00001535 case Instruction::Call:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001536 return selectCall(I);
Wesley Peck527da1b2010-11-23 03:31:01 +00001537
Dan Gohman7bda51f2008-09-03 23:12:08 +00001538 case Instruction::BitCast:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001539 return selectBitCast(I);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001540
1541 case Instruction::FPToSI:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001542 return selectCast(I, ISD::FP_TO_SINT);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001543 case Instruction::ZExt:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001544 return selectCast(I, ISD::ZERO_EXTEND);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001545 case Instruction::SExt:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001546 return selectCast(I, ISD::SIGN_EXTEND);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001547 case Instruction::Trunc:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001548 return selectCast(I, ISD::TRUNCATE);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001549 case Instruction::SIToFP:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001550 return selectCast(I, ISD::SINT_TO_FP);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001551
1552 case Instruction::IntToPtr: // Deliberate fall-through.
1553 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001554 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1555 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman7bda51f2008-09-03 23:12:08 +00001556 if (DstVT.bitsGT(SrcVT))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001557 return selectCast(I, ISD::ZERO_EXTEND);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001558 if (DstVT.bitsLT(SrcVT))
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001559 return selectCast(I, ISD::TRUNCATE);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001560 unsigned Reg = getRegForValue(I->getOperand(0));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001561 if (!Reg)
1562 return false;
Dan Gohman7bda51f2008-09-03 23:12:08 +00001563 UpdateValueMap(I, Reg);
1564 return true;
1565 }
Dan Gohman918fe082008-09-23 21:53:34 +00001566
Eli Friedman9ac94472011-05-16 20:27:46 +00001567 case Instruction::ExtractValue:
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001568 return selectExtractValue(I);
Eli Friedman9ac94472011-05-16 20:27:46 +00001569
Dan Gohmanf41ad472010-04-20 15:00:41 +00001570 case Instruction::PHI:
1571 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1572
Dan Gohman7bda51f2008-09-03 23:12:08 +00001573 default:
1574 // Unhandled instruction. Halt "fast" selection and bail.
1575 return false;
1576 }
Dan Gohmanb2226e22008-08-13 20:19:35 +00001577}
1578
Juergen Ributzka7e998fb2014-09-02 21:07:44 +00001579FastISel::FastISel(FunctionLoweringInfo &FuncInfo,
1580 const TargetLibraryInfo *LibInfo,
1581 bool SkipTargetIndependentISel)
1582 : FuncInfo(FuncInfo), MF(FuncInfo.MF), MRI(FuncInfo.MF->getRegInfo()),
Eric Christopherd9134482014-08-04 21:25:23 +00001583 MFI(*FuncInfo.MF->getFrameInfo()), MCP(*FuncInfo.MF->getConstantPool()),
1584 TM(FuncInfo.MF->getTarget()), DL(*TM.getSubtargetImpl()->getDataLayout()),
1585 TII(*TM.getSubtargetImpl()->getInstrInfo()),
1586 TLI(*TM.getSubtargetImpl()->getTargetLowering()),
Juergen Ributzka7e998fb2014-09-02 21:07:44 +00001587 TRI(*TM.getSubtargetImpl()->getRegisterInfo()), LibInfo(LibInfo),
1588 SkipTargetIndependentISel(SkipTargetIndependentISel) {}
Dan Gohman02c84b82008-08-20 21:05:57 +00001589
Dan Gohmanc4442382008-08-14 21:51:29 +00001590FastISel::~FastISel() {}
1591
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001592bool FastISel::FastLowerArguments() { return false; }
Evan Cheng615620c2013-02-11 01:27:15 +00001593
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001594bool FastISel::FastLowerCall(CallLoweringInfo & /*CLI*/) { return false; }
Juergen Ributzka8179e9e2014-07-11 22:01:42 +00001595
Reid Klecknerfb951982014-07-12 00:06:46 +00001596bool FastISel::FastLowerIntrinsicCall(const IntrinsicInst * /*II*/) {
Juergen Ributzka5dd32132014-07-11 20:42:12 +00001597 return false;
1598}
1599
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001600unsigned FastISel::FastEmit_(MVT, MVT, unsigned) { return 0; }
1601
1602unsigned FastISel::FastEmit_r(MVT, MVT, unsigned, unsigned /*Op0*/,
1603 bool /*Op0IsKill*/) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001604 return 0;
1605}
1606
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001607unsigned FastISel::FastEmit_rr(MVT, MVT, unsigned, unsigned /*Op0*/,
1608 bool /*Op0IsKill*/, unsigned /*Op1*/,
1609 bool /*Op1IsKill*/) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001610 return 0;
1611}
1612
Dan Gohman404a9842010-01-05 22:26:32 +00001613unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng864fcc12008-08-20 22:45:34 +00001614 return 0;
1615}
1616
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001617unsigned FastISel::FastEmit_f(MVT, MVT, unsigned,
1618 const ConstantFP * /*FPImm*/) {
Dan Gohman5ca269e2008-08-27 01:09:54 +00001619 return 0;
1620}
1621
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001622unsigned FastISel::FastEmit_ri(MVT, MVT, unsigned, unsigned /*Op0*/,
1623 bool /*Op0IsKill*/, uint64_t /*Imm*/) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001624 return 0;
1625}
1626
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001627unsigned FastISel::FastEmit_rf(MVT, MVT, unsigned, unsigned /*Op0*/,
1628 bool /*Op0IsKill*/,
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001629 const ConstantFP * /*FPImm*/) {
Dan Gohman5ca269e2008-08-27 01:09:54 +00001630 return 0;
1631}
1632
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001633unsigned FastISel::FastEmit_rri(MVT, MVT, unsigned, unsigned /*Op0*/,
1634 bool /*Op0IsKill*/, unsigned /*Op1*/,
1635 bool /*Op1IsKill*/, uint64_t /*Imm*/) {
Evan Cheng864fcc12008-08-20 22:45:34 +00001636 return 0;
1637}
1638
1639/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1640/// to emit an instruction with an immediate operand using FastEmit_ri.
1641/// If that fails, it materializes the immediate into a register and try
1642/// FastEmit_rr instead.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001643unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0,
1644 bool Op0IsKill, uint64_t Imm, MVT ImmType) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001645 // If this is a multiply by a power of two, emit this as a shift left.
1646 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1647 Opcode = ISD::SHL;
1648 Imm = Log2_64(Imm);
Chris Lattner562d6e82011-04-18 06:55:51 +00001649 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1650 // div x, 8 -> srl x, 3
1651 Opcode = ISD::SRL;
1652 Imm = Log2_64(Imm);
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001653 }
Owen Andersondd450b82011-04-22 23:38:06 +00001654
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001655 // Horrible hack (to be removed), check to make sure shift amounts are
1656 // in-range.
1657 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1658 Imm >= VT.getSizeInBits())
1659 return 0;
Owen Andersondd450b82011-04-22 23:38:06 +00001660
Evan Cheng864fcc12008-08-20 22:45:34 +00001661 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001662 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001663 if (ResultReg)
Evan Cheng864fcc12008-08-20 22:45:34 +00001664 return ResultReg;
Owen Anderson8dd01cc2008-08-25 23:58:18 +00001665 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001666 if (!MaterialReg) {
Eli Friedman4105ed12011-04-29 23:34:52 +00001667 // This is a bit ugly/slow, but failing here means falling out of
1668 // fast-isel, which would be very slow.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001669 IntegerType *ITy =
1670 IntegerType::get(FuncInfo.Fn->getContext(), VT.getSizeInBits());
Eli Friedman4105ed12011-04-29 23:34:52 +00001671 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001672 if (!MaterialReg)
1673 return 0;
Eli Friedman4105ed12011-04-29 23:34:52 +00001674 }
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001675 return FastEmit_rr(VT, VT, Opcode, Op0, Op0IsKill, MaterialReg,
1676 /*IsKill=*/true);
Dan Gohmanfe905652008-08-21 01:41:07 +00001677}
1678
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001679unsigned FastISel::createResultReg(const TargetRegisterClass *RC) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001680 return MRI.createVirtualRegister(RC);
Evan Cheng864fcc12008-08-20 22:45:34 +00001681}
1682
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001683unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II, unsigned Op,
1684 unsigned OpNum) {
Tim Northover2f553f32014-04-15 13:59:49 +00001685 if (TargetRegisterInfo::isVirtualRegister(Op)) {
1686 const TargetRegisterClass *RegClass =
1687 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
1688 if (!MRI.constrainRegClass(Op, RegClass)) {
1689 // If it's not legal to COPY between the register classes, something
1690 // has gone very wrong before we got here.
1691 unsigned NewOp = createResultReg(RegClass);
1692 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1693 TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
1694 return NewOp;
1695 }
1696 }
1697 return Op;
1698}
1699
Dan Gohmanb2226e22008-08-13 20:19:35 +00001700unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001701 const TargetRegisterClass *RC) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001702 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001703 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001704
Rafael Espindolaea09c592014-02-18 22:05:46 +00001705 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001706 return ResultReg;
1707}
1708
1709unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001710 const TargetRegisterClass *RC, unsigned Op0,
1711 bool Op0IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001712 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001713
Tim Northover2f553f32014-04-15 13:59:49 +00001714 unsigned ResultReg = createResultReg(RC);
1715 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1716
Evan Chenge775d352008-09-08 08:38:20 +00001717 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001719 .addReg(Op0, getKillRegState(Op0IsKill));
Evan Chenge775d352008-09-08 08:38:20 +00001720 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001721 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001722 .addReg(Op0, getKillRegState(Op0IsKill));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1724 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001725 }
1726
Dan Gohmanb2226e22008-08-13 20:19:35 +00001727 return ResultReg;
1728}
1729
1730unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001731 const TargetRegisterClass *RC, unsigned Op0,
1732 bool Op0IsKill, unsigned Op1,
1733 bool Op1IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001734 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001735
Tim Northover2f553f32014-04-15 13:59:49 +00001736 unsigned ResultReg = createResultReg(RC);
1737 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1738 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1739
Evan Chenge775d352008-09-08 08:38:20 +00001740 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001741 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001742 .addReg(Op0, getKillRegState(Op0IsKill))
1743 .addReg(Op1, getKillRegState(Op1IsKill));
Evan Chenge775d352008-09-08 08:38:20 +00001744 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001745 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001746 .addReg(Op0, getKillRegState(Op0IsKill))
1747 .addReg(Op1, getKillRegState(Op1IsKill));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001748 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1749 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001750 }
Dan Gohmanb2226e22008-08-13 20:19:35 +00001751 return ResultReg;
1752}
Dan Gohmanfe905652008-08-21 01:41:07 +00001753
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001754unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001755 const TargetRegisterClass *RC, unsigned Op0,
1756 bool Op0IsKill, unsigned Op1,
1757 bool Op1IsKill, unsigned Op2,
1758 bool Op2IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001759 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001760
Tim Northover2f553f32014-04-15 13:59:49 +00001761 unsigned ResultReg = createResultReg(RC);
1762 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1763 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1764 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
1765
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001766 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001767 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001768 .addReg(Op0, getKillRegState(Op0IsKill))
1769 .addReg(Op1, getKillRegState(Op1IsKill))
1770 .addReg(Op2, getKillRegState(Op2IsKill));
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001771 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001772 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001773 .addReg(Op0, getKillRegState(Op0IsKill))
1774 .addReg(Op1, getKillRegState(Op1IsKill))
1775 .addReg(Op2, getKillRegState(Op2IsKill));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001776 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1777 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001778 }
1779 return ResultReg;
1780}
1781
Dan Gohmanfe905652008-08-21 01:41:07 +00001782unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001783 const TargetRegisterClass *RC, unsigned Op0,
1784 bool Op0IsKill, uint64_t Imm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001785 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanfe905652008-08-21 01:41:07 +00001786
Tim Northover2f553f32014-04-15 13:59:49 +00001787 unsigned ResultReg = createResultReg(RC);
Juergen Ributzka833bc682014-08-27 20:47:33 +00001788 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
Tim Northover2f553f32014-04-15 13:59:49 +00001789
Evan Chenge775d352008-09-08 08:38:20 +00001790 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001791 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001792 .addReg(Op0, getKillRegState(Op0IsKill))
1793 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001794 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001795 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001796 .addReg(Op0, getKillRegState(Op0IsKill))
1797 .addImm(Imm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001798 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1799 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001800 }
Dan Gohmanfe905652008-08-21 01:41:07 +00001801 return ResultReg;
1802}
1803
Owen Anderson66443c02011-03-11 21:33:55 +00001804unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001805 const TargetRegisterClass *RC, unsigned Op0,
1806 bool Op0IsKill, uint64_t Imm1,
1807 uint64_t Imm2) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001808 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson66443c02011-03-11 21:33:55 +00001809
Tim Northover2f553f32014-04-15 13:59:49 +00001810 unsigned ResultReg = createResultReg(RC);
1811 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1812
Owen Anderson66443c02011-03-11 21:33:55 +00001813 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001814 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001815 .addReg(Op0, getKillRegState(Op0IsKill))
1816 .addImm(Imm1)
1817 .addImm(Imm2);
Owen Anderson66443c02011-03-11 21:33:55 +00001818 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001819 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001820 .addReg(Op0, getKillRegState(Op0IsKill))
1821 .addImm(Imm1)
1822 .addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001823 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1824 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson66443c02011-03-11 21:33:55 +00001825 }
1826 return ResultReg;
1827}
1828
Dan Gohman5ca269e2008-08-27 01:09:54 +00001829unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001830 const TargetRegisterClass *RC, unsigned Op0,
1831 bool Op0IsKill, const ConstantFP *FPImm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001832 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman5ca269e2008-08-27 01:09:54 +00001833
Tim Northover2f553f32014-04-15 13:59:49 +00001834 unsigned ResultReg = createResultReg(RC);
1835 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1836
Evan Chenge775d352008-09-08 08:38:20 +00001837 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001838 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001839 .addReg(Op0, getKillRegState(Op0IsKill))
1840 .addFPImm(FPImm);
Evan Chenge775d352008-09-08 08:38:20 +00001841 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001843 .addReg(Op0, getKillRegState(Op0IsKill))
1844 .addFPImm(FPImm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001845 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1846 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001847 }
Dan Gohman5ca269e2008-08-27 01:09:54 +00001848 return ResultReg;
1849}
1850
Dan Gohmanfe905652008-08-21 01:41:07 +00001851unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001852 const TargetRegisterClass *RC, unsigned Op0,
1853 bool Op0IsKill, unsigned Op1,
1854 bool Op1IsKill, uint64_t Imm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001855 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanfe905652008-08-21 01:41:07 +00001856
Tim Northover2f553f32014-04-15 13:59:49 +00001857 unsigned ResultReg = createResultReg(RC);
1858 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1859 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1860
Evan Chenge775d352008-09-08 08:38:20 +00001861 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001862 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001863 .addReg(Op0, getKillRegState(Op0IsKill))
1864 .addReg(Op1, getKillRegState(Op1IsKill))
1865 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001866 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001868 .addReg(Op0, getKillRegState(Op0IsKill))
1869 .addReg(Op1, getKillRegState(Op1IsKill))
1870 .addImm(Imm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1872 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001873 }
Dan Gohmanfe905652008-08-21 01:41:07 +00001874 return ResultReg;
1875}
Owen Anderson32635db2008-08-25 20:20:32 +00001876
Manman Rene8735522012-06-01 19:33:18 +00001877unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1878 const TargetRegisterClass *RC,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001879 unsigned Op0, bool Op0IsKill, unsigned Op1,
1880 bool Op1IsKill, uint64_t Imm1,
1881 uint64_t Imm2) {
Manman Rene8735522012-06-01 19:33:18 +00001882 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1883
Tim Northover2f553f32014-04-15 13:59:49 +00001884 unsigned ResultReg = createResultReg(RC);
1885 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1886 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1887
Manman Rene8735522012-06-01 19:33:18 +00001888 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001890 .addReg(Op0, getKillRegState(Op0IsKill))
1891 .addReg(Op1, getKillRegState(Op1IsKill))
1892 .addImm(Imm1)
1893 .addImm(Imm2);
Manman Rene8735522012-06-01 19:33:18 +00001894 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001896 .addReg(Op0, getKillRegState(Op0IsKill))
1897 .addReg(Op1, getKillRegState(Op1IsKill))
1898 .addImm(Imm1)
1899 .addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001900 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1901 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Manman Rene8735522012-06-01 19:33:18 +00001902 }
1903 return ResultReg;
1904}
1905
Owen Anderson32635db2008-08-25 20:20:32 +00001906unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001907 const TargetRegisterClass *RC, uint64_t Imm) {
Owen Anderson32635db2008-08-25 20:20:32 +00001908 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001909 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peck527da1b2010-11-23 03:31:01 +00001910
Evan Chenge775d352008-09-08 08:38:20 +00001911 if (II.getNumDefs() >= 1)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001912 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1913 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001914 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001915 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
1916 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1917 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001918 }
Owen Anderson32635db2008-08-25 20:20:32 +00001919 return ResultReg;
Evan Cheng2c067322008-08-25 22:20:39 +00001920}
Owen Anderson5f57bc22008-08-27 22:30:02 +00001921
Owen Andersondd450b82011-04-22 23:38:06 +00001922unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001923 const TargetRegisterClass *RC, uint64_t Imm1,
1924 uint64_t Imm2) {
Owen Andersondd450b82011-04-22 23:38:06 +00001925 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001926 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersondd450b82011-04-22 23:38:06 +00001927
1928 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001929 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001930 .addImm(Imm1)
1931 .addImm(Imm2);
Owen Andersondd450b82011-04-22 23:38:06 +00001932 else {
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001933 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1)
1934 .addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001935 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1936 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersondd450b82011-04-22 23:38:06 +00001937 }
1938 return ResultReg;
1939}
1940
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001941unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT, unsigned Op0,
1942 bool Op0IsKill, uint32_t Idx) {
Evan Cheng4a0bf662009-01-22 09:10:11 +00001943 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001944 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1945 "Cannot yet extract from physregs");
Jakob Stoklund Olesen1f1c6ad2012-05-20 06:38:37 +00001946 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1947 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001948 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
1949 ResultReg).addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson5f57bc22008-08-27 22:30:02 +00001950 return ResultReg;
1951}
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001952
1953/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1954/// with all but the least significant bit set to zero.
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001955unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1956 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001957}
Dan Gohmanc594eab2010-04-22 20:46:50 +00001958
1959/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1960/// Emit code to ensure constants are copied into registers when needed.
1961/// Remember the virtual registers that need to be added to the Machine PHI
1962/// nodes as input. We cannot just directly add them, because expansion
1963/// might result in multiple MBB's for one BB. As such, the start of the
1964/// BB might correspond to a different MBB than the end.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001965bool FastISel::handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohmanc594eab2010-04-22 20:46:50 +00001966 const TerminatorInst *TI = LLVMBB->getTerminator();
1967
1968 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Juergen Ributzka31328162014-08-28 02:06:55 +00001969 FuncInfo.OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanc594eab2010-04-22 20:46:50 +00001970
1971 // Check successor nodes' PHI nodes that expect a constant to be available
1972 // from this block.
1973 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1974 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001975 if (!isa<PHINode>(SuccBB->begin()))
1976 continue;
Dan Gohman87fb4e82010-07-07 16:29:44 +00001977 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanc594eab2010-04-22 20:46:50 +00001978
1979 // If this terminator has multiple identical successors (common for
1980 // switches), only handle each succ once.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001981 if (!SuccsHandled.insert(SuccMBB))
1982 continue;
Dan Gohmanc594eab2010-04-22 20:46:50 +00001983
1984 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1985
1986 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1987 // nodes and Machine PHI nodes, but the incoming operands have not been
1988 // emitted yet.
1989 for (BasicBlock::const_iterator I = SuccBB->begin();
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001990 const auto *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmane6d40162010-05-07 01:10:20 +00001991
Dan Gohmanc594eab2010-04-22 20:46:50 +00001992 // Ignore dead phi's.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00001993 if (PN->use_empty())
1994 continue;
Dan Gohmanc594eab2010-04-22 20:46:50 +00001995
1996 // Only handle legal types. Two interesting things to note here. First,
1997 // by bailing out early, we may leave behind some dead instructions,
1998 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001999 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman93f59202010-07-02 00:10:16 +00002000 // use CreateRegs to create registers, so it always creates
Dan Gohmanc594eab2010-04-22 20:46:50 +00002001 // exactly one register for each non-void instruction.
2002 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
2003 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier6d68c7c2012-02-04 00:39:19 +00002004 // Handle integer promotions, though, because they're common and easy.
2005 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanc594eab2010-04-22 20:46:50 +00002006 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
2007 else {
Juergen Ributzka31328162014-08-28 02:06:55 +00002008 FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
Dan Gohmanc594eab2010-04-22 20:46:50 +00002009 return false;
2010 }
2011 }
2012
2013 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
2014
Dan Gohmane6d40162010-05-07 01:10:20 +00002015 // Set the DebugLoc for the copy. Prefer the location of the operand
2016 // if there is one; use the location of the PHI otherwise.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002017 DbgLoc = PN->getDebugLoc();
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002018 if (const auto *Inst = dyn_cast<Instruction>(PHIOp))
Rafael Espindolaea09c592014-02-18 22:05:46 +00002019 DbgLoc = Inst->getDebugLoc();
Dan Gohmane6d40162010-05-07 01:10:20 +00002020
Dan Gohmanc594eab2010-04-22 20:46:50 +00002021 unsigned Reg = getRegForValue(PHIOp);
Juergen Ributzka31328162014-08-28 02:06:55 +00002022 if (!Reg) {
2023 FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
Dan Gohmanc594eab2010-04-22 20:46:50 +00002024 return false;
2025 }
Dan Gohman87fb4e82010-07-07 16:29:44 +00002026 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Rafael Espindolaea09c592014-02-18 22:05:46 +00002027 DbgLoc = DebugLoc();
Dan Gohmanc594eab2010-04-22 20:46:50 +00002028 }
2029 }
2030
2031 return true;
2032}
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002033
2034bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Benderskye80691d2013-04-19 23:26:18 +00002035 assert(LI->hasOneUse() &&
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002036 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002037 // We know that the load has a single use, but don't know what it is. If it
2038 // isn't one of the folded instructions, then we can't succeed here. Handle
2039 // this by scanning the single-use users of the load until we get to FoldInst.
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002040 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002041
Chandler Carruthcdf47882014-03-09 03:16:01 +00002042 const Instruction *TheUser = LI->user_back();
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002043 while (TheUser != FoldInst && // Scan up until we find FoldInst.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002044 // Stay in the right block.
2045 TheUser->getParent() == FoldInst->getParent() &&
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002046 --MaxUsers) { // Don't scan too far.
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002047 // If there are multiple or no uses of this instruction, then bail out.
2048 if (!TheUser->hasOneUse())
2049 return false;
2050
Chandler Carruthcdf47882014-03-09 03:16:01 +00002051 TheUser = TheUser->user_back();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002052 }
2053
2054 // If we didn't find the fold instruction, then we failed to collapse the
2055 // sequence.
2056 if (TheUser != FoldInst)
2057 return false;
2058
2059 // Don't try to fold volatile loads. Target has to deal with alignment
2060 // constraints.
Eli Benderskye80691d2013-04-19 23:26:18 +00002061 if (LI->isVolatile())
2062 return false;
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002063
2064 // Figure out which vreg this is going into. If there is no assigned vreg yet
2065 // then there actually was no reference to it. Perhaps the load is referenced
2066 // by a dead instruction.
2067 unsigned LoadReg = getRegForValue(LI);
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002068 if (!LoadReg)
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002069 return false;
2070
Eli Benderskye80691d2013-04-19 23:26:18 +00002071 // We can't fold if this vreg has no uses or more than one use. Multiple uses
2072 // may mean that the instruction got lowered to multiple MIs, or the use of
2073 // the loaded value ended up being multiple operands of the result.
2074 if (!MRI.hasOneUse(LoadReg))
2075 return false;
2076
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002077 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Owen Anderson16c6bf42014-03-13 23:12:04 +00002078 MachineInstr *User = RI->getParent();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002079
2080 // Set the insertion point properly. Folding the load can cause generation of
Eli Benderskye80691d2013-04-19 23:26:18 +00002081 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002082 // sure they get inserted in a logical place before the new instruction.
2083 FuncInfo.InsertPt = User;
2084 FuncInfo.MBB = User->getParent();
2085
2086 // Ask the target to try folding the load.
2087 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
2088}
2089
Bob Wilson9f3e6b22013-11-15 19:09:27 +00002090bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
2091 // Must be an add.
2092 if (!isa<AddOperator>(Add))
2093 return false;
2094 // Type size needs to match.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002095 if (DL.getTypeSizeInBits(GEP->getType()) !=
2096 DL.getTypeSizeInBits(Add->getType()))
Bob Wilson9f3e6b22013-11-15 19:09:27 +00002097 return false;
2098 // Must be in the same basic block.
2099 if (isa<Instruction>(Add) &&
2100 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
2101 return false;
2102 // Must have a constant operand.
2103 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
2104}
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002105
Juergen Ributzka349777d2014-06-12 23:27:57 +00002106MachineMemOperand *
2107FastISel::createMachineMemOperandFor(const Instruction *I) const {
2108 const Value *Ptr;
2109 Type *ValTy;
2110 unsigned Alignment;
2111 unsigned Flags;
2112 bool IsVolatile;
2113
2114 if (const auto *LI = dyn_cast<LoadInst>(I)) {
2115 Alignment = LI->getAlignment();
2116 IsVolatile = LI->isVolatile();
2117 Flags = MachineMemOperand::MOLoad;
2118 Ptr = LI->getPointerOperand();
2119 ValTy = LI->getType();
2120 } else if (const auto *SI = dyn_cast<StoreInst>(I)) {
2121 Alignment = SI->getAlignment();
2122 IsVolatile = SI->isVolatile();
2123 Flags = MachineMemOperand::MOStore;
2124 Ptr = SI->getPointerOperand();
2125 ValTy = SI->getValueOperand()->getType();
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002126 } else
Juergen Ributzka349777d2014-06-12 23:27:57 +00002127 return nullptr;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002128
2129 bool IsNonTemporal = I->getMetadata("nontemporal") != nullptr;
2130 bool IsInvariant = I->getMetadata("invariant.load") != nullptr;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002131 const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range);
2132
Hal Finkelcc39b672014-07-24 12:16:19 +00002133 AAMDNodes AAInfo;
2134 I->getAAMetadata(AAInfo);
2135
Juergen Ributzka7a76c242014-09-03 18:46:45 +00002136 if (Alignment == 0) // Ensure that codegen never sees alignment 0.
Juergen Ributzka349777d2014-06-12 23:27:57 +00002137 Alignment = DL.getABITypeAlignment(ValTy);
2138
Eric Christopherd9134482014-08-04 21:25:23 +00002139 unsigned Size =
2140 TM.getSubtargetImpl()->getDataLayout()->getTypeStoreSize(ValTy);
Juergen Ributzka349777d2014-06-12 23:27:57 +00002141
2142 if (IsVolatile)
2143 Flags |= MachineMemOperand::MOVolatile;
2144 if (IsNonTemporal)
2145 Flags |= MachineMemOperand::MONonTemporal;
2146 if (IsInvariant)
2147 Flags |= MachineMemOperand::MOInvariant;
2148
2149 return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size,
Hal Finkelcc39b672014-07-24 12:16:19 +00002150 Alignment, AAInfo, Ranges);
Juergen Ributzka349777d2014-06-12 23:27:57 +00002151}