blob: c58d730454f00c1c365e72949ce16a8762322ce9 [file] [log] [blame]
Dan Gohman3b172f12010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000012// "Fast" instruction selection is designed to emit very poor code quickly.
13// Also, it is not designed to be able to do much lowering, so most illegal
Chris Lattner44d2a982008-10-13 01:59:13 +000014// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
15// also not intended to be able to do much optimization, except in a few cases
16// where doing optimizations reduces overall compile time. For example, folding
17// constants into immediate fields is often done, because it's cheap and it
18// reduces the number of instructions later phases have to examine.
Dan Gohman5ec9efd2008-09-30 20:48:29 +000019//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
Chris Lattner44d2a982008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohman5ec9efd2008-09-30 20:48:29 +000023// the complicated lowering logic that SelectionDAG currently has.
24//
25// The intended use for "fast" instruction selection is "-O0" mode
26// compilation, where the quality of the generated code is irrelevant when
Chris Lattner44d2a982008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohman5ec9efd2008-09-30 20:48:29 +000028// at -O0, the LLVM optimizers are not running, and this makes the
29// compile time of codegen a much higher portion of the overall compile
Chris Lattner44d2a982008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohman5ec9efd2008-09-30 20:48:29 +000031// handle enough code on its own to provide noticeable overall speedups
32// in -O0 compiles.
33//
34// Basic operations are supported in a target-independent way, by reading
35// the same instruction descriptions that the SelectionDAG selector reads,
36// and identifying simple arithmetic operations that can be directly selected
Chris Lattner44d2a982008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohman5ec9efd2008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Dan Gohman33134c42008-09-25 17:05:24 +000042#include "llvm/Function.h"
43#include "llvm/GlobalVariable.h"
Dan Gohman6f2766d2008-08-19 22:31:46 +000044#include "llvm/Instructions.h"
Dan Gohman33134c42008-09-25 17:05:24 +000045#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000046#include "llvm/Operator.h"
Eli Friedman2586b8f2011-05-16 20:27:46 +000047#include "llvm/CodeGen/Analysis.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000048#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000049#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000050#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000051#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000052#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000053#include "llvm/Analysis/DebugInfo.h"
Dan Gohman7fbcc982010-07-01 03:49:38 +000054#include "llvm/Analysis/Loads.h"
Evan Cheng83785c82008-08-20 22:45:34 +000055#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000056#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000057#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000058#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000059#include "llvm/Support/ErrorHandling.h"
Devang Patelafeaae72010-12-06 22:39:26 +000060#include "llvm/Support/Debug.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000061using namespace llvm;
62
Dan Gohman84023e02010-07-10 09:00:22 +000063/// startNewBlock - Set the current block to which generated machine
64/// instructions will be appended, and clear the local CSE map.
65///
66void FastISel::startNewBlock() {
67 LocalValueMap.clear();
68
Ivan Krasin74af88a2011-08-18 22:06:10 +000069 EmitStartPt = 0;
Dan Gohman84023e02010-07-10 09:00:22 +000070
Ivan Krasin74af88a2011-08-18 22:06:10 +000071 // Advance the emit start point past any EH_LABEL instructions.
Dan Gohman84023e02010-07-10 09:00:22 +000072 MachineBasicBlock::iterator
73 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
74 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
Ivan Krasin74af88a2011-08-18 22:06:10 +000075 EmitStartPt = I;
Dan Gohman84023e02010-07-10 09:00:22 +000076 ++I;
77 }
Ivan Krasin74af88a2011-08-18 22:06:10 +000078 LastLocalValue = EmitStartPt;
79}
80
81void FastISel::flushLocalValueMap() {
82 LocalValueMap.clear();
83 LastLocalValue = EmitStartPt;
84 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +000085}
86
Dan Gohmana6cb6412010-05-11 23:54:07 +000087bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000088 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000089 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000090 if (!I)
91 return false;
92
93 // No-op casts are trivially coalesced by fast-isel.
94 if (const CastInst *Cast = dyn_cast<CastInst>(I))
95 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
96 !hasTrivialKill(Cast->getOperand(0)))
97 return false;
98
Chad Rosier22b34cc2011-11-15 23:34:05 +000099 // GEPs with all zero indices are trivially coalesced by fast-isel.
100 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
101 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
102 return false;
103
Dan Gohman7f0d6952010-05-14 22:53:18 +0000104 // Only instructions with a single use in the same basic block are considered
105 // to have trivial kills.
106 return I->hasOneUse() &&
107 !(I->getOpcode() == Instruction::BitCast ||
108 I->getOpcode() == Instruction::PtrToInt ||
109 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000110 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000111}
112
Dan Gohman46510a72010-04-15 01:51:59 +0000113unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000114 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000115 // Don't handle non-simple values in FastISel.
116 if (!RealVT.isSimple())
117 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000118
119 // Ignore illegal types. We must do this before looking up the value
120 // in ValueMap because Arguments are given virtual registers regardless
121 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000123 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000124 // Handle integer promotions, though, because they're common and easy.
125 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000126 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000127 else
128 return 0;
129 }
130
Dan Gohman104e4ce2008-09-03 23:32:19 +0000131 // Look up the value to see if we already have a register for it. We
132 // cache values defined by Instructions across blocks, and other values
133 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000134 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000135 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
Chris Lattnerfff65b32011-04-17 01:16:47 +0000136 if (I != FuncInfo.ValueMap.end())
137 return I->second;
138
Dan Gohman104e4ce2008-09-03 23:32:19 +0000139 unsigned Reg = LocalValueMap[V];
140 if (Reg != 0)
141 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000142
Dan Gohman97c94b82010-05-06 00:02:14 +0000143 // In bottom-up mode, just create the virtual register which will be used
144 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000145 if (isa<Instruction>(V) &&
146 (!isa<AllocaInst>(V) ||
147 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
148 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000149
Dan Gohmana10b8492010-07-14 01:07:44 +0000150 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000151
152 // Materialize the value in a register. Emit any instructions in the
153 // local value area.
154 Reg = materializeRegForValue(V, VT);
155
156 leaveLocalValueArea(SaveInsertPt);
157
158 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000159}
160
Eric Christopher44a2c342010-08-17 01:30:33 +0000161/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000162/// called when the value isn't already available in a register and must
163/// be materialized with new instructions.
164unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
165 unsigned Reg = 0;
166
Dan Gohman46510a72010-04-15 01:51:59 +0000167 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000168 if (CI->getValue().getActiveBits() <= 64)
169 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000170 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000171 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000172 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000173 // Translate this as an integer zero so that it can be
174 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000175 Reg =
176 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000177 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000178 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000179 Reg = TargetMaterializeFloatZero(CF);
180 } else {
181 // Try to emit the constant directly.
182 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
183 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000184
185 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000186 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000187 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000188 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000189
190 uint64_t x[2];
191 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000192 bool isExact;
193 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
194 APFloat::rmTowardZero, &isExact);
195 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000196 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000197
Owen Andersone922c022009-07-22 00:24:57 +0000198 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000199 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000200 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000201 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
202 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000203 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000204 }
Dan Gohman46510a72010-04-15 01:51:59 +0000205 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000206 if (!SelectOperator(Op, Op->getOpcode()))
207 if (!isa<Instruction>(Op) ||
208 !TargetSelectInstruction(cast<Instruction>(Op)))
209 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000210 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000211 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000212 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000213 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
214 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000215 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000216
Dan Gohmandceffe62008-09-25 01:28:51 +0000217 // If target-independent code couldn't handle the value, give target-specific
218 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000219 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000220 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000221
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000222 // Don't cache constant materializations in the general ValueMap.
223 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000224 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000225 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000226 LastLocalValue = MRI.getVRegDef(Reg);
227 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000228 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000229}
230
Dan Gohman46510a72010-04-15 01:51:59 +0000231unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000232 // Look up the value to see if we already have a register for it. We
233 // cache values defined by Instructions across blocks, and other values
234 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000235 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000236 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
237 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000238 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000239 return LocalValueMap[V];
240}
241
Owen Andersoncc54e762008-08-30 00:38:46 +0000242/// UpdateValueMap - Update the value map to include the new mapping for this
243/// instruction, or insert an extra copy to get the result in a previous
244/// determined register.
245/// NOTE: This is only necessary because we might select a block that uses
246/// a value before we select the block that defines the value. It might be
247/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000248void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000249 if (!isa<Instruction>(I)) {
250 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000251 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000252 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000253
Dan Gohmana4160c32010-07-07 16:29:44 +0000254 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000255 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000256 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000257 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000258 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000259 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000260 for (unsigned i = 0; i < NumRegs; i++)
261 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000262
263 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000264 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000265}
266
Dan Gohmana6cb6412010-05-11 23:54:07 +0000267std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000268 unsigned IdxN = getRegForValue(Idx);
269 if (IdxN == 0)
270 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000271 return std::pair<unsigned, bool>(0, false);
272
273 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000274
275 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000276 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000277 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000278 if (IdxVT.bitsLT(PtrVT)) {
279 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
280 IdxN, IdxNIsKill);
281 IdxNIsKill = true;
282 }
283 else if (IdxVT.bitsGT(PtrVT)) {
284 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
285 IdxN, IdxNIsKill);
286 IdxNIsKill = true;
287 }
288 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000289}
290
Dan Gohman84023e02010-07-10 09:00:22 +0000291void FastISel::recomputeInsertPt() {
292 if (getLastLocalValue()) {
293 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000294 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000295 ++FuncInfo.InsertPt;
296 } else
297 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
298
299 // Now skip past any EH_LABELs, which must remain at the beginning.
300 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
301 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
302 ++FuncInfo.InsertPt;
303}
304
Dan Gohmana10b8492010-07-14 01:07:44 +0000305FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000306 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Dan Gohman163f78e2010-07-14 22:01:31 +0000307 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000308 recomputeInsertPt();
Dan Gohmana10b8492010-07-14 01:07:44 +0000309 DL = DebugLoc();
Dan Gohman163f78e2010-07-14 22:01:31 +0000310 SavePoint SP = { OldInsertPt, OldDL };
Dan Gohmana10b8492010-07-14 01:07:44 +0000311 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000312}
313
Dan Gohmana10b8492010-07-14 01:07:44 +0000314void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000315 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
316 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
317
318 // Restore the previous insert position.
Dan Gohmana10b8492010-07-14 01:07:44 +0000319 FuncInfo.InsertPt = OldInsertPt.InsertPt;
320 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000321}
322
Dan Gohmanbdedd442008-08-20 00:11:48 +0000323/// SelectBinaryOp - Select and emit code for a binary operator instruction,
324/// which has an opcode which directly corresponds to the given ISD opcode.
325///
Dan Gohman46510a72010-04-15 01:51:59 +0000326bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000327 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000329 // Unhandled type. Halt "fast" selection and bail.
330 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000331
Dan Gohmanb71fea22008-08-26 20:52:40 +0000332 // We only handle legal types. For example, on x86-32 the instruction
333 // selector contains all of the 64-bit instructions from x86-64,
334 // under the assumption that i64 won't be used if the target doesn't
335 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000336 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000338 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000340 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
341 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000342 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000343 else
344 return false;
345 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000346
Chris Lattnerfff65b32011-04-17 01:16:47 +0000347 // Check if the first operand is a constant, and handle it as "ri". At -O0,
348 // we don't have anything that canonicalizes operand order.
349 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
350 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
351 unsigned Op1 = getRegForValue(I->getOperand(1));
352 if (Op1 == 0) return false;
353
354 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000355
Chris Lattner602fc062011-04-17 20:23:29 +0000356 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
357 Op1IsKill, CI->getZExtValue(),
358 VT.getSimpleVT());
359 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000360
Chris Lattner602fc062011-04-17 20:23:29 +0000361 // We successfully emitted code for the given LLVM Instruction.
362 UpdateValueMap(I, ResultReg);
363 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000364 }
Owen Andersond74ea772011-04-22 23:38:06 +0000365
366
Dan Gohman3df24e62008-09-03 23:12:08 +0000367 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000368 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000369 return false;
370
Dan Gohmana6cb6412010-05-11 23:54:07 +0000371 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
372
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000373 // Check if the second operand is a constant and handle it appropriately.
374 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000375 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000376
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000377 // Transform "sdiv exact X, 8" -> "sra X, 3".
378 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
379 cast<BinaryOperator>(I)->isExact() &&
380 isPowerOf2_64(Imm)) {
381 Imm = Log2_64(Imm);
382 ISDOpcode = ISD::SRA;
383 }
Owen Andersond74ea772011-04-22 23:38:06 +0000384
Chris Lattner602fc062011-04-17 20:23:29 +0000385 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
386 Op0IsKill, Imm, VT.getSimpleVT());
387 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000388
Chris Lattner602fc062011-04-17 20:23:29 +0000389 // We successfully emitted code for the given LLVM Instruction.
390 UpdateValueMap(I, ResultReg);
391 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000392 }
393
Dan Gohman10df0fa2008-08-27 01:09:54 +0000394 // Check if the second operand is a constant float.
395 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000396 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000397 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000398 if (ResultReg != 0) {
399 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000400 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000401 return true;
402 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000403 }
404
Dan Gohman3df24e62008-09-03 23:12:08 +0000405 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000406 if (Op1 == 0)
407 // Unhandled operand. Halt "fast" selection and bail.
408 return false;
409
Dan Gohmana6cb6412010-05-11 23:54:07 +0000410 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
411
Dan Gohmanad368ac2008-08-27 18:10:19 +0000412 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000413 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000414 ISDOpcode,
415 Op0, Op0IsKill,
416 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000417 if (ResultReg == 0)
418 // Target-specific code wasn't able to find a machine opcode for
419 // the given ISD opcode and type. Halt "fast" selection and bail.
420 return false;
421
Dan Gohman8014e862008-08-20 00:23:20 +0000422 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000423 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000424 return true;
425}
426
Dan Gohman46510a72010-04-15 01:51:59 +0000427bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000428 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000429 if (N == 0)
430 // Unhandled operand. Halt "fast" selection and bail.
431 return false;
432
Dan Gohmana6cb6412010-05-11 23:54:07 +0000433 bool NIsKill = hasTrivialKill(I->getOperand(0));
434
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000435 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000437 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
438 E = I->op_end(); OI != E; ++OI) {
439 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000440 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000441 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
442 if (Field) {
443 // N = N + Offset
444 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
445 // FIXME: This can be optimized by combining the add with a
446 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000447 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000448 if (N == 0)
449 // Unhandled operand. Halt "fast" selection and bail.
450 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000451 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000452 }
453 Ty = StTy->getElementType(Field);
454 } else {
455 Ty = cast<SequentialType>(Ty)->getElementType();
456
457 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000458 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000459 if (CI->isZero()) continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000460 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000461 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000462 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000463 if (N == 0)
464 // Unhandled operand. Halt "fast" selection and bail.
465 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000466 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000467 continue;
468 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000469
Evan Cheng83785c82008-08-20 22:45:34 +0000470 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000471 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000472 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
473 unsigned IdxN = Pair.first;
474 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000475 if (IdxN == 0)
476 // Unhandled operand. Halt "fast" selection and bail.
477 return false;
478
Dan Gohman80bc6e22008-08-26 20:57:08 +0000479 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000480 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000481 if (IdxN == 0)
482 // Unhandled operand. Halt "fast" selection and bail.
483 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000484 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000485 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000486 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000487 if (N == 0)
488 // Unhandled operand. Halt "fast" selection and bail.
489 return false;
490 }
491 }
492
493 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000494 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000495 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000496}
497
Dan Gohman46510a72010-04-15 01:51:59 +0000498bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000499 const CallInst *Call = cast<CallInst>(I);
500
501 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000502 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000503 // Don't attempt to handle constraints.
504 if (!IA->getConstraintString().empty())
505 return false;
506
507 unsigned ExtraInfo = 0;
508 if (IA->hasSideEffects())
509 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
510 if (IA->isAlignStack())
511 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
512
513 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
514 TII.get(TargetOpcode::INLINEASM))
515 .addExternalSymbol(IA->getAsmString().c_str())
516 .addImm(ExtraInfo);
517 return true;
518 }
519
520 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000521 if (!F) return false;
522
Dan Gohman4183e312010-04-13 17:07:06 +0000523 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000524 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000525 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000526 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000527 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000528 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000529 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000530 return true;
531
Dan Gohman46510a72010-04-15 01:51:59 +0000532 const Value *Address = DI->getAddress();
Devang Patel6fe75aa2010-09-14 20:29:31 +0000533 if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
Dale Johannesendc918562010-02-06 02:26:02 +0000534 return true;
Devang Patel6fe75aa2010-09-14 20:29:31 +0000535
536 unsigned Reg = 0;
537 unsigned Offset = 0;
538 if (const Argument *Arg = dyn_cast<Argument>(Address)) {
Devang Patel9aee3352011-09-08 22:59:09 +0000539 // Some arguments' frame index is recorded during argument lowering.
540 Offset = FuncInfo.getArgumentFrameIndex(Arg);
541 if (Offset)
542 Reg = TRI.getFrameRegister(*FuncInfo.MF);
Devang Patel4bafda92010-09-10 20:32:09 +0000543 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000544 if (!Reg)
545 Reg = getRegForValue(Address);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000546
Devang Patel6fe75aa2010-09-14 20:29:31 +0000547 if (Reg)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000548 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Devang Patel6fe75aa2010-09-14 20:29:31 +0000549 TII.get(TargetOpcode::DBG_VALUE))
550 .addReg(Reg, RegState::Debug).addImm(Offset)
551 .addMetadata(DI->getVariable());
Dan Gohman33134c42008-09-25 17:05:24 +0000552 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000553 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000554 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000555 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000556 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000557 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000558 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000559 if (!V) {
560 // Currently the optimizer can produce this; insert an undef to
561 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000562 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
563 .addReg(0U).addImm(DI->getOffset())
564 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000565 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000566 if (CI->getBitWidth() > 64)
567 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
568 .addCImm(CI).addImm(DI->getOffset())
569 .addMetadata(DI->getVariable());
570 else
571 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
572 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
573 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000574 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000575 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
576 .addFPImm(CF).addImm(DI->getOffset())
577 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000578 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000579 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
580 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
581 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000582 } else {
583 // We can't yet handle anything else here because it would require
584 // generating code, thus altering codegen because of debug info.
Devang Patelafeaae72010-12-06 22:39:26 +0000585 DEBUG(dbgs() << "Dropping debug info for " << DI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000586 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000587 return true;
588 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000589 case Intrinsic::eh_exception: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000590 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000591 if (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)!=TargetLowering::Expand)
592 break;
Owen Andersond74ea772011-04-22 23:38:06 +0000593
Chris Lattner832e4942011-04-19 05:52:03 +0000594 assert(FuncInfo.MBB->isLandingPad() &&
595 "Call to eh.exception not in landing pad!");
596 unsigned Reg = TLI.getExceptionAddressRegister();
597 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
598 unsigned ResultReg = createResultReg(RC);
599 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
600 ResultReg).addReg(Reg);
Dan Gohmana61e73b2011-04-26 17:18:34 +0000601 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000602 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000603 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000604 case Intrinsic::eh_selector: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000605 EVT VT = TLI.getValueType(Call->getType());
Chris Lattner832e4942011-04-19 05:52:03 +0000606 if (TLI.getOperationAction(ISD::EHSELECTION, VT) != TargetLowering::Expand)
607 break;
608 if (FuncInfo.MBB->isLandingPad())
Dan Gohmana61e73b2011-04-26 17:18:34 +0000609 AddCatchInfo(*Call, &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattner832e4942011-04-19 05:52:03 +0000610 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000611#ifndef NDEBUG
Dan Gohmana61e73b2011-04-26 17:18:34 +0000612 FuncInfo.CatchInfoLost.insert(Call);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000613#endif
Chris Lattner832e4942011-04-19 05:52:03 +0000614 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Chris Lattnered3a8062010-04-05 06:05:26 +0000615 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattner832e4942011-04-19 05:52:03 +0000616 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000617 }
Chris Lattner832e4942011-04-19 05:52:03 +0000618
619 unsigned Reg = TLI.getExceptionSelectorRegister();
620 EVT SrcVT = TLI.getPointerTy();
621 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
622 unsigned ResultReg = createResultReg(RC);
623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
624 ResultReg).addReg(Reg);
625
Dan Gohmana61e73b2011-04-26 17:18:34 +0000626 bool ResultRegIsKill = hasTrivialKill(Call);
Chris Lattner832e4942011-04-19 05:52:03 +0000627
628 // Cast the register to the type of the selector.
629 if (SrcVT.bitsGT(MVT::i32))
630 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
631 ResultReg, ResultRegIsKill);
632 else if (SrcVT.bitsLT(MVT::i32))
633 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
634 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
635 if (ResultReg == 0)
636 // Unhandled operand. Halt "fast" selection and bail.
637 return false;
638
Dan Gohmana61e73b2011-04-26 17:18:34 +0000639 UpdateValueMap(Call, ResultReg);
Chris Lattner832e4942011-04-19 05:52:03 +0000640
641 return true;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000642 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000643 case Intrinsic::objectsize: {
644 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
645 unsigned long long Res = CI->isZero() ? -1ULL : 0;
646 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
647 unsigned ResultReg = getRegForValue(ResCI);
648 if (ResultReg == 0)
649 return false;
650 UpdateValueMap(Call, ResultReg);
651 return true;
652 }
Dan Gohman33134c42008-09-25 17:05:24 +0000653 }
Dan Gohman4183e312010-04-13 17:07:06 +0000654
Ivan Krasin74af88a2011-08-18 22:06:10 +0000655 // Usually, it does not make sense to initialize a value,
656 // make an unrelated function call and use the value, because
657 // it tends to be spilled on the stack. So, we move the pointer
658 // to the last local value to the beginning of the block, so that
659 // all the values which have already been materialized,
660 // appear after the call. It also makes sense to skip intrinsics
661 // since they tend to be inlined.
662 if (!isa<IntrinsicInst>(F))
663 flushLocalValueMap();
664
Dan Gohman4183e312010-04-13 17:07:06 +0000665 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000666 return false;
667}
668
Dan Gohman46510a72010-04-15 01:51:59 +0000669bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000670 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
671 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
674 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000675 // Unhandled type. Halt "fast" selection and bail.
676 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000677
Eli Friedman76927d732011-05-25 23:49:02 +0000678 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000679 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000680 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000681
Eli Friedman76927d732011-05-25 23:49:02 +0000682 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000683 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000684 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000685
Dan Gohman3df24e62008-09-03 23:12:08 +0000686 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000687 if (!InputReg)
688 // Unhandled operand. Halt "fast" selection and bail.
689 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000690
Dan Gohmana6cb6412010-05-11 23:54:07 +0000691 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
692
Owen Andersond0533c92008-08-26 23:46:32 +0000693 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
694 DstVT.getSimpleVT(),
695 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000696 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000697 if (!ResultReg)
698 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699
Dan Gohman3df24e62008-09-03 23:12:08 +0000700 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000701 return true;
702}
703
Dan Gohman46510a72010-04-15 01:51:59 +0000704bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000705 // If the bitcast doesn't change the type, just use the operand value.
706 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000707 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000708 if (Reg == 0)
709 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000710 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000711 return true;
712 }
713
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000714 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000715 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
716 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000717
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
719 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000720 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
721 // Unhandled type. Halt "fast" selection and bail.
722 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000723
Dan Gohman3df24e62008-09-03 23:12:08 +0000724 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000725 if (Op0 == 0)
726 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000727 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000728
729 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730
Dan Gohmanad368ac2008-08-27 18:10:19 +0000731 // First, try to perform the bitcast by inserting a reg-reg copy.
732 unsigned ResultReg = 0;
733 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
734 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
735 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000736 // Don't attempt a cross-class copy. It will likely fail.
737 if (SrcClass == DstClass) {
738 ResultReg = createResultReg(DstClass);
739 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
740 ResultReg).addReg(Op0);
741 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000742 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000743
744 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000745 if (!ResultReg)
746 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000747 ISD::BITCAST, Op0, Op0IsKill);
748
Dan Gohmanad368ac2008-08-27 18:10:19 +0000749 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000750 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000751
Dan Gohman3df24e62008-09-03 23:12:08 +0000752 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000753 return true;
754}
755
Dan Gohman3df24e62008-09-03 23:12:08 +0000756bool
Dan Gohman46510a72010-04-15 01:51:59 +0000757FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000758 // Just before the terminator instruction, insert instructions to
759 // feed PHI nodes in successor blocks.
760 if (isa<TerminatorInst>(I))
761 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
762 return false;
763
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000764 DL = I->getDebugLoc();
765
Dan Gohman6e3ff372009-12-05 01:27:58 +0000766 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000767 if (SelectOperator(I, I->getOpcode())) {
768 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000769 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000770 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000771
772 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000773 if (TargetSelectInstruction(I)) {
774 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000775 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000776 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000777
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000778 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000779 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000780}
781
Dan Gohmand98d6202008-10-02 22:15:21 +0000782/// FastEmitBranch - Emit an unconditional branch to the given block,
783/// unless it is the immediate (fall-through) successor, and update
784/// the CFG.
785void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000786FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohman84023e02010-07-10 09:00:22 +0000787 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000788 // The unconditional fall-through case, which needs no instructions.
789 } else {
790 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000791 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
792 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000793 }
Dan Gohman84023e02010-07-10 09:00:22 +0000794 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000795}
796
Dan Gohman3d45a852009-09-03 22:53:57 +0000797/// SelectFNeg - Emit an FNeg operation.
798///
799bool
Dan Gohman46510a72010-04-15 01:51:59 +0000800FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000801 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
802 if (OpReg == 0) return false;
803
Dan Gohmana6cb6412010-05-11 23:54:07 +0000804 bool OpRegIsKill = hasTrivialKill(I);
805
Dan Gohman4a215a12009-09-11 00:36:43 +0000806 // If the target has ISD::FNEG, use it.
807 EVT VT = TLI.getValueType(I->getType());
808 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000809 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000810 if (ResultReg != 0) {
811 UpdateValueMap(I, ResultReg);
812 return true;
813 }
814
Dan Gohman5e5abb72009-09-11 00:34:46 +0000815 // Bitcast the value to integer, twiddle the sign bit with xor,
816 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000817 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000818 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
819 if (!TLI.isTypeLegal(IntVT))
820 return false;
821
822 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000823 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000824 if (IntReg == 0)
825 return false;
826
Dan Gohmana6cb6412010-05-11 23:54:07 +0000827 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
828 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000829 UINT64_C(1) << (VT.getSizeInBits()-1),
830 IntVT.getSimpleVT());
831 if (IntResultReg == 0)
832 return false;
833
834 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000836 if (ResultReg == 0)
837 return false;
838
839 UpdateValueMap(I, ResultReg);
840 return true;
841}
842
Dan Gohman40b189e2008-09-05 18:18:20 +0000843bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000844FastISel::SelectExtractValue(const User *U) {
845 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000846 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000847 return false;
848
Eli Friedman482feb32011-05-16 21:06:17 +0000849 // Make sure we only try to handle extracts with a legal result. But also
850 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000851 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
852 if (!RealVT.isSimple())
853 return false;
854 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000855 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000856 return false;
857
858 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000859 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000860
861 // Get the base result register.
862 unsigned ResultReg;
863 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
864 if (I != FuncInfo.ValueMap.end())
865 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000866 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000867 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000868 else
869 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000870
871 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000872 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000873
874 SmallVector<EVT, 4> AggValueVTs;
875 ComputeValueVTs(TLI, AggTy, AggValueVTs);
876
877 for (unsigned i = 0; i < VTIndex; i++)
878 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
879
880 UpdateValueMap(EVI, ResultReg);
881 return true;
882}
883
884bool
Dan Gohman46510a72010-04-15 01:51:59 +0000885FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000886 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000887 case Instruction::Add:
888 return SelectBinaryOp(I, ISD::ADD);
889 case Instruction::FAdd:
890 return SelectBinaryOp(I, ISD::FADD);
891 case Instruction::Sub:
892 return SelectBinaryOp(I, ISD::SUB);
893 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000894 // FNeg is currently represented in LLVM IR as a special case of FSub.
895 if (BinaryOperator::isFNeg(I))
896 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000897 return SelectBinaryOp(I, ISD::FSUB);
898 case Instruction::Mul:
899 return SelectBinaryOp(I, ISD::MUL);
900 case Instruction::FMul:
901 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000902 case Instruction::SDiv:
903 return SelectBinaryOp(I, ISD::SDIV);
904 case Instruction::UDiv:
905 return SelectBinaryOp(I, ISD::UDIV);
906 case Instruction::FDiv:
907 return SelectBinaryOp(I, ISD::FDIV);
908 case Instruction::SRem:
909 return SelectBinaryOp(I, ISD::SREM);
910 case Instruction::URem:
911 return SelectBinaryOp(I, ISD::UREM);
912 case Instruction::FRem:
913 return SelectBinaryOp(I, ISD::FREM);
914 case Instruction::Shl:
915 return SelectBinaryOp(I, ISD::SHL);
916 case Instruction::LShr:
917 return SelectBinaryOp(I, ISD::SRL);
918 case Instruction::AShr:
919 return SelectBinaryOp(I, ISD::SRA);
920 case Instruction::And:
921 return SelectBinaryOp(I, ISD::AND);
922 case Instruction::Or:
923 return SelectBinaryOp(I, ISD::OR);
924 case Instruction::Xor:
925 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000926
Dan Gohman3df24e62008-09-03 23:12:08 +0000927 case Instruction::GetElementPtr:
928 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000929
Dan Gohman3df24e62008-09-03 23:12:08 +0000930 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000931 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000932
Dan Gohman3df24e62008-09-03 23:12:08 +0000933 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000934 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000935 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000936 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000937 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000938 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000939
940 // Conditional branches are not handed yet.
941 // Halt "fast" selection and bail.
942 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000943 }
944
Dan Gohman087c8502008-09-05 01:08:41 +0000945 case Instruction::Unreachable:
946 // Nothing to emit.
947 return true;
948
Dan Gohman0586d912008-09-10 20:11:02 +0000949 case Instruction::Alloca:
950 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000951 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000952 return true;
953
954 // Dynamic-sized alloca is not handled yet.
955 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000956
Dan Gohman33134c42008-09-25 17:05:24 +0000957 case Instruction::Call:
958 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000959
Dan Gohman3df24e62008-09-03 23:12:08 +0000960 case Instruction::BitCast:
961 return SelectBitCast(I);
962
963 case Instruction::FPToSI:
964 return SelectCast(I, ISD::FP_TO_SINT);
965 case Instruction::ZExt:
966 return SelectCast(I, ISD::ZERO_EXTEND);
967 case Instruction::SExt:
968 return SelectCast(I, ISD::SIGN_EXTEND);
969 case Instruction::Trunc:
970 return SelectCast(I, ISD::TRUNCATE);
971 case Instruction::SIToFP:
972 return SelectCast(I, ISD::SINT_TO_FP);
973
974 case Instruction::IntToPtr: // Deliberate fall-through.
975 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000976 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
977 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000978 if (DstVT.bitsGT(SrcVT))
979 return SelectCast(I, ISD::ZERO_EXTEND);
980 if (DstVT.bitsLT(SrcVT))
981 return SelectCast(I, ISD::TRUNCATE);
982 unsigned Reg = getRegForValue(I->getOperand(0));
983 if (Reg == 0) return false;
984 UpdateValueMap(I, Reg);
985 return true;
986 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000987
Eli Friedman2586b8f2011-05-16 20:27:46 +0000988 case Instruction::ExtractValue:
989 return SelectExtractValue(I);
990
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000991 case Instruction::PHI:
992 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
993
Dan Gohman3df24e62008-09-03 23:12:08 +0000994 default:
995 // Unhandled instruction. Halt "fast" selection and bail.
996 return false;
997 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000998}
999
Dan Gohmana4160c32010-07-07 16:29:44 +00001000FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001001 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001002 MRI(FuncInfo.MF->getRegInfo()),
1003 MFI(*FuncInfo.MF->getFrameInfo()),
1004 MCP(*FuncInfo.MF->getConstantPool()),
1005 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001006 TD(*TM.getTargetData()),
1007 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001008 TLI(*TM.getTargetLowering()),
Dan Gohman84023e02010-07-10 09:00:22 +00001009 TRI(*TM.getRegisterInfo()) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001010}
1011
Dan Gohmane285a742008-08-14 21:51:29 +00001012FastISel::~FastISel() {}
1013
Owen Anderson825b72b2009-08-11 20:47:22 +00001014unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001015 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001016 return 0;
1017}
1018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001020 unsigned,
1021 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001022 return 0;
1023}
1024
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001025unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001026 unsigned,
1027 unsigned /*Op0*/, bool /*Op0IsKill*/,
1028 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001029 return 0;
1030}
1031
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001032unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001033 return 0;
1034}
1035
Owen Anderson825b72b2009-08-11 20:47:22 +00001036unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001037 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001038 return 0;
1039}
1040
Owen Anderson825b72b2009-08-11 20:47:22 +00001041unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001042 unsigned,
1043 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001044 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001045 return 0;
1046}
1047
Owen Anderson825b72b2009-08-11 20:47:22 +00001048unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001049 unsigned,
1050 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001051 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001052 return 0;
1053}
1054
Owen Anderson825b72b2009-08-11 20:47:22 +00001055unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001056 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001057 unsigned /*Op0*/, bool /*Op0IsKill*/,
1058 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001059 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001060 return 0;
1061}
1062
1063/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1064/// to emit an instruction with an immediate operand using FastEmit_ri.
1065/// If that fails, it materializes the immediate into a register and try
1066/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001067unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001068 unsigned Op0, bool Op0IsKill,
1069 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001070 // If this is a multiply by a power of two, emit this as a shift left.
1071 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1072 Opcode = ISD::SHL;
1073 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001074 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1075 // div x, 8 -> srl x, 3
1076 Opcode = ISD::SRL;
1077 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001078 }
Owen Andersond74ea772011-04-22 23:38:06 +00001079
Chris Lattner602fc062011-04-17 20:23:29 +00001080 // Horrible hack (to be removed), check to make sure shift amounts are
1081 // in-range.
1082 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1083 Imm >= VT.getSizeInBits())
1084 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001085
Evan Cheng83785c82008-08-20 22:45:34 +00001086 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001087 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001088 if (ResultReg != 0)
1089 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001090 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001091 if (MaterialReg == 0) {
1092 // This is a bit ugly/slow, but failing here means falling out of
1093 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001094 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001095 VT.getSizeInBits());
1096 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
1097 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001098 return FastEmit_rr(VT, VT, Opcode,
1099 Op0, Op0IsKill,
1100 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001101}
1102
1103unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1104 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001105}
1106
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001107unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001108 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001109 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001110 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001111
Dan Gohman84023e02010-07-10 09:00:22 +00001112 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001113 return ResultReg;
1114}
1115
1116unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1117 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001118 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001119 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001120 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001121
Evan Cheng5960e4e2008-09-08 08:38:20 +00001122 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001123 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1124 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001125 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001126 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1127 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001128 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1129 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001130 }
1131
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001132 return ResultReg;
1133}
1134
1135unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1136 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001137 unsigned Op0, bool Op0IsKill,
1138 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001139 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001140 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001141
Evan Cheng5960e4e2008-09-08 08:38:20 +00001142 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001143 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001144 .addReg(Op0, Op0IsKill * RegState::Kill)
1145 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001146 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001147 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001148 .addReg(Op0, Op0IsKill * RegState::Kill)
1149 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1151 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001152 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001153 return ResultReg;
1154}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001155
Owen Andersond71867a2011-05-05 17:59:04 +00001156unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1157 const TargetRegisterClass *RC,
1158 unsigned Op0, bool Op0IsKill,
1159 unsigned Op1, bool Op1IsKill,
1160 unsigned Op2, bool Op2IsKill) {
1161 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001162 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001163
1164 if (II.getNumDefs() >= 1)
1165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1166 .addReg(Op0, Op0IsKill * RegState::Kill)
1167 .addReg(Op1, Op1IsKill * RegState::Kill)
1168 .addReg(Op2, Op2IsKill * RegState::Kill);
1169 else {
1170 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1171 .addReg(Op0, Op0IsKill * RegState::Kill)
1172 .addReg(Op1, Op1IsKill * RegState::Kill)
1173 .addReg(Op2, Op2IsKill * RegState::Kill);
1174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1175 ResultReg).addReg(II.ImplicitDefs[0]);
1176 }
1177 return ResultReg;
1178}
1179
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001180unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1181 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001182 unsigned Op0, bool Op0IsKill,
1183 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001184 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001185 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001186
Evan Cheng5960e4e2008-09-08 08:38:20 +00001187 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001188 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001189 .addReg(Op0, Op0IsKill * RegState::Kill)
1190 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001191 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001192 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001193 .addReg(Op0, Op0IsKill * RegState::Kill)
1194 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001195 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1196 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001197 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001198 return ResultReg;
1199}
1200
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001201unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1202 const TargetRegisterClass *RC,
1203 unsigned Op0, bool Op0IsKill,
1204 uint64_t Imm1, uint64_t Imm2) {
1205 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001206 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001207
1208 if (II.getNumDefs() >= 1)
1209 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1210 .addReg(Op0, Op0IsKill * RegState::Kill)
1211 .addImm(Imm1)
1212 .addImm(Imm2);
1213 else {
1214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1215 .addReg(Op0, Op0IsKill * RegState::Kill)
1216 .addImm(Imm1)
1217 .addImm(Imm2);
1218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1219 ResultReg).addReg(II.ImplicitDefs[0]);
1220 }
1221 return ResultReg;
1222}
1223
Dan Gohman10df0fa2008-08-27 01:09:54 +00001224unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1225 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001226 unsigned Op0, bool Op0IsKill,
1227 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001228 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001229 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001230
Evan Cheng5960e4e2008-09-08 08:38:20 +00001231 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001233 .addReg(Op0, Op0IsKill * RegState::Kill)
1234 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001235 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001237 .addReg(Op0, Op0IsKill * RegState::Kill)
1238 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001239 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1240 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001241 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001242 return ResultReg;
1243}
1244
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001245unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1246 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001247 unsigned Op0, bool Op0IsKill,
1248 unsigned Op1, bool Op1IsKill,
1249 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001250 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001251 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001252
Evan Cheng5960e4e2008-09-08 08:38:20 +00001253 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001255 .addReg(Op0, Op0IsKill * RegState::Kill)
1256 .addReg(Op1, Op1IsKill * RegState::Kill)
1257 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001258 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001260 .addReg(Op0, Op0IsKill * RegState::Kill)
1261 .addReg(Op1, Op1IsKill * RegState::Kill)
1262 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1264 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001265 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001266 return ResultReg;
1267}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001268
1269unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1270 const TargetRegisterClass *RC,
1271 uint64_t Imm) {
1272 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001273 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274
Evan Cheng5960e4e2008-09-08 08:38:20 +00001275 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001276 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001277 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1280 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001281 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001282 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001283}
Owen Anderson8970f002008-08-27 22:30:02 +00001284
Owen Andersond74ea772011-04-22 23:38:06 +00001285unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1286 const TargetRegisterClass *RC,
1287 uint64_t Imm1, uint64_t Imm2) {
1288 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001289 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001290
1291 if (II.getNumDefs() >= 1)
1292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1293 .addImm(Imm1).addImm(Imm2);
1294 else {
1295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1296 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1297 ResultReg).addReg(II.ImplicitDefs[0]);
1298 }
1299 return ResultReg;
1300}
1301
Owen Anderson825b72b2009-08-11 20:47:22 +00001302unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001303 unsigned Op0, bool Op0IsKill,
1304 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001305 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001306 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1307 "Cannot yet extract from physregs");
Dan Gohman84023e02010-07-10 09:00:22 +00001308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1309 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001310 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001311 return ResultReg;
1312}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001313
1314/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1315/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001316unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1317 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001318}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001319
1320/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1321/// Emit code to ensure constants are copied into registers when needed.
1322/// Remember the virtual registers that need to be added to the Machine PHI
1323/// nodes as input. We cannot just directly add them, because expansion
1324/// might result in multiple MBB's for one BB. As such, the start of the
1325/// BB might correspond to a different MBB than the end.
1326bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1327 const TerminatorInst *TI = LLVMBB->getTerminator();
1328
1329 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001330 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001331
1332 // Check successor nodes' PHI nodes that expect a constant to be available
1333 // from this block.
1334 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1335 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1336 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001337 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001338
1339 // If this terminator has multiple identical successors (common for
1340 // switches), only handle each succ once.
1341 if (!SuccsHandled.insert(SuccMBB)) continue;
1342
1343 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1344
1345 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1346 // nodes and Machine PHI nodes, but the incoming operands have not been
1347 // emitted yet.
1348 for (BasicBlock::const_iterator I = SuccBB->begin();
1349 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001350
Dan Gohmanf81eca02010-04-22 20:46:50 +00001351 // Ignore dead phi's.
1352 if (PN->use_empty()) continue;
1353
1354 // Only handle legal types. Two interesting things to note here. First,
1355 // by bailing out early, we may leave behind some dead instructions,
1356 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001357 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001358 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001359 // exactly one register for each non-void instruction.
1360 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1361 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1362 // Promote MVT::i1.
1363 if (VT == MVT::i1)
1364 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1365 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001366 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001367 return false;
1368 }
1369 }
1370
1371 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1372
Dan Gohmanfb95f892010-05-07 01:10:20 +00001373 // Set the DebugLoc for the copy. Prefer the location of the operand
1374 // if there is one; use the location of the PHI otherwise.
1375 DL = PN->getDebugLoc();
1376 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1377 DL = Inst->getDebugLoc();
1378
Dan Gohmanf81eca02010-04-22 20:46:50 +00001379 unsigned Reg = getRegForValue(PHIOp);
1380 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001381 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001382 return false;
1383 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001384 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001385 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001386 }
1387 }
1388
1389 return true;
1390}