blob: 9e182efbbbf20818c4619b7202ffdd89ed5cc1d9 [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001///===-- FastISel.cpp - Implementation of the FastISel class --------------===//
2//
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"
Devang Patel53bb5c92009-11-10 23:06:00 +000046#include "llvm/LLVMContext.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000047#include "llvm/CodeGen/FastISel.h"
48#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000049#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000050#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000051#include "llvm/CodeGen/DwarfWriter.h"
52#include "llvm/Analysis/DebugInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000053#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000054#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000055#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000056#include "llvm/Target/TargetMachine.h"
Dan Gohman2048b852009-11-23 18:04:58 +000057#include "SelectionDAGBuilder.h"
Dan Gohman66336ed2009-11-23 17:42:46 +000058#include "FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000059using namespace llvm;
60
Dan Gohman3df24e62008-09-03 23:12:08 +000061unsigned FastISel::getRegForValue(Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +000062 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +000063 // Don't handle non-simple values in FastISel.
64 if (!RealVT.isSimple())
65 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000066
67 // Ignore illegal types. We must do this before looking up the value
68 // in ValueMap because Arguments are given virtual registers regardless
69 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +000070 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000071 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +000072 // Promote MVT::i1 to a legal type though, because it's common and easy.
73 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +000074 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000075 else
76 return 0;
77 }
78
Dan Gohman104e4ce2008-09-03 23:32:19 +000079 // Look up the value to see if we already have a register for it. We
80 // cache values defined by Instructions across blocks, and other values
81 // only locally. This is because Instructions already have the SSA
82 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000083 if (ValueMap.count(V))
84 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000085 unsigned Reg = LocalValueMap[V];
86 if (Reg != 0)
87 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000088
Dan Gohmanad368ac2008-08-27 18:10:19 +000089 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000090 if (CI->getValue().getActiveBits() <= 64)
91 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000092 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000093 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000094 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +000095 // Translate this as an integer zero so that it can be
96 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +000097 Reg =
98 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohmanad368ac2008-08-27 18:10:19 +000099 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000100 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000101
102 if (!Reg) {
103 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000104 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000105
106 uint64_t x[2];
107 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000108 bool isExact;
109 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
110 APFloat::rmTowardZero, &isExact);
111 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000112 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000113
Owen Andersone922c022009-07-22 00:24:57 +0000114 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000115 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000116 if (IntegerReg != 0)
117 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
118 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000119 }
Dan Gohman40b189e2008-09-05 18:18:20 +0000120 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
121 if (!SelectOperator(CE, CE->getOpcode())) return 0;
122 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +0000123 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000124 Reg = createResultReg(TLI.getRegClassFor(VT));
Bill Wendling9bc96a52009-02-03 00:55:04 +0000125 BuildMI(MBB, DL, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000126 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000127
Dan Gohmandceffe62008-09-25 01:28:51 +0000128 // If target-independent code couldn't handle the value, give target-specific
129 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000130 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000131 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000132
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000133 // Don't cache constant materializations in the general ValueMap.
134 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000135 if (Reg != 0)
136 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000137 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000138}
139
Evan Cheng59fbc802008-09-09 01:26:59 +0000140unsigned FastISel::lookUpRegForValue(Value *V) {
141 // Look up the value to see if we already have a register for it. We
142 // cache values defined by Instructions across blocks, and other values
143 // only locally. This is because Instructions already have the SSA
144 // def-dominatess-use requirement enforced.
145 if (ValueMap.count(V))
146 return ValueMap[V];
147 return LocalValueMap[V];
148}
149
Owen Andersoncc54e762008-08-30 00:38:46 +0000150/// UpdateValueMap - Update the value map to include the new mapping for this
151/// instruction, or insert an extra copy to get the result in a previous
152/// determined register.
153/// NOTE: This is only necessary because we might select a block that uses
154/// a value before we select the block that defines the value. It might be
155/// possible to fix this by selecting blocks in reverse postorder.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000156unsigned FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000157 if (!isa<Instruction>(I)) {
158 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000159 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000160 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000161
162 unsigned &AssignedReg = ValueMap[I];
163 if (AssignedReg == 0)
164 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000165 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000166 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
167 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
168 Reg, RegClass, RegClass);
169 }
170 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000171}
172
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000173unsigned FastISel::getRegForGEPIndex(Value *Idx) {
174 unsigned IdxN = getRegForValue(Idx);
175 if (IdxN == 0)
176 // Unhandled operand. Halt "fast" selection and bail.
177 return 0;
178
179 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000180 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000181 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000182 if (IdxVT.bitsLT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000183 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000184 else if (IdxVT.bitsGT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000185 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000186 return IdxN;
187}
188
Dan Gohmanbdedd442008-08-20 00:11:48 +0000189/// SelectBinaryOp - Select and emit code for a binary operator instruction,
190/// which has an opcode which directly corresponds to the given ISD opcode.
191///
Dan Gohman40b189e2008-09-05 18:18:20 +0000192bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000193 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000195 // Unhandled type. Halt "fast" selection and bail.
196 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000197
Dan Gohmanb71fea22008-08-26 20:52:40 +0000198 // We only handle legal types. For example, on x86-32 the instruction
199 // selector contains all of the 64-bit instructions from x86-64,
200 // under the assumption that i64 won't be used if the target doesn't
201 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000202 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000204 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000206 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
207 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000208 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000209 else
210 return false;
211 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000212
Dan Gohman3df24e62008-09-03 23:12:08 +0000213 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000214 if (Op0 == 0)
215 // Unhandled operand. Halt "fast" selection and bail.
216 return false;
217
218 // Check if the second operand is a constant and handle it appropriately.
219 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000220 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
221 ISDOpcode, Op0, CI->getZExtValue());
222 if (ResultReg != 0) {
223 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000224 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000225 return true;
226 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000227 }
228
Dan Gohman10df0fa2008-08-27 01:09:54 +0000229 // Check if the second operand is a constant float.
230 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000231 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
232 ISDOpcode, Op0, CF);
233 if (ResultReg != 0) {
234 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000235 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000236 return true;
237 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000238 }
239
Dan Gohman3df24e62008-09-03 23:12:08 +0000240 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000241 if (Op1 == 0)
242 // Unhandled operand. Halt "fast" selection and bail.
243 return false;
244
Dan Gohmanad368ac2008-08-27 18:10:19 +0000245 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000246 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
247 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000248 if (ResultReg == 0)
249 // Target-specific code wasn't able to find a machine opcode for
250 // the given ISD opcode and type. Halt "fast" selection and bail.
251 return false;
252
Dan Gohman8014e862008-08-20 00:23:20 +0000253 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000254 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000255 return true;
256}
257
Dan Gohman40b189e2008-09-05 18:18:20 +0000258bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000259 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000260 if (N == 0)
261 // Unhandled operand. Halt "fast" selection and bail.
262 return false;
263
264 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 MVT VT = TLI.getPointerTy();
Evan Cheng83785c82008-08-20 22:45:34 +0000266 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
267 OI != E; ++OI) {
268 Value *Idx = *OI;
269 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
270 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
271 if (Field) {
272 // N = N + Offset
273 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
274 // FIXME: This can be optimized by combining the add with a
275 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000276 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000277 if (N == 0)
278 // Unhandled operand. Halt "fast" selection and bail.
279 return false;
280 }
281 Ty = StTy->getElementType(Field);
282 } else {
283 Ty = cast<SequentialType>(Ty)->getElementType();
284
285 // If this is a constant subscript, handle it quickly.
286 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
287 if (CI->getZExtValue() == 0) continue;
288 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000289 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000290 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000291 if (N == 0)
292 // Unhandled operand. Halt "fast" selection and bail.
293 return false;
294 continue;
295 }
296
297 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000298 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000299 unsigned IdxN = getRegForGEPIndex(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000300 if (IdxN == 0)
301 // Unhandled operand. Halt "fast" selection and bail.
302 return false;
303
Dan Gohman80bc6e22008-08-26 20:57:08 +0000304 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000305 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000306 if (IdxN == 0)
307 // Unhandled operand. Halt "fast" selection and bail.
308 return false;
309 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000310 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000311 if (N == 0)
312 // Unhandled operand. Halt "fast" selection and bail.
313 return false;
314 }
315 }
316
317 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000318 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000319 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000320}
321
Dan Gohman33134c42008-09-25 17:05:24 +0000322bool FastISel::SelectCall(User *I) {
323 Function *F = cast<CallInst>(I)->getCalledFunction();
324 if (!F) return false;
325
326 unsigned IID = F->getIntrinsicID();
327 switch (IID) {
328 default: break;
Devang Patel70d75ca2009-11-12 19:02:56 +0000329 case Intrinsic::dbg_stoppoint:
330 case Intrinsic::dbg_region_start:
331 case Intrinsic::dbg_region_end:
332 case Intrinsic::dbg_func_start:
333 // FIXME - Remove this instructions once the dust settles.
Dan Gohman33134c42008-09-25 17:05:24 +0000334 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000335 case Intrinsic::dbg_declare: {
336 DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000337 if (!isValidDebugInfoIntrinsic(*DI, CodeGenOpt::None) || !DW
338 || !DW->ShouldEmitDwarfDebug())
339 return true;
340
Devang Patel7e1e31f2009-07-02 22:43:26 +0000341 Value *Address = DI->getAddress();
342 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
343 Address = BCI->getOperand(0);
344 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
345 // Don't handle byval struct arguments or VLAs, for example.
346 if (!AI) break;
347 DenseMap<const AllocaInst*, int>::iterator SI =
348 StaticAllocaMap.find(AI);
349 if (SI == StaticAllocaMap.end()) break; // VLAs.
350 int FI = SI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +0000351 if (MMI) {
352 MetadataContext &TheMetadata =
353 DI->getParent()->getContext().getMetadata();
354 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
355 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, DI);
356 MMI->setVariableDbgInfo(DI->getVariable(), FI, Dbg);
357 }
Dan Gohman33134c42008-09-25 17:05:24 +0000358 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000359 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000360 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000361 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000362 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
363 default: break;
364 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000365 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000366 unsigned Reg = TLI.getExceptionAddressRegister();
367 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
368 unsigned ResultReg = createResultReg(RC);
369 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
370 Reg, RC, RC);
371 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000372 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000373 UpdateValueMap(I, ResultReg);
374 return true;
375 }
376 }
377 break;
378 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000379 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000380 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000381 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
382 default: break;
383 case TargetLowering::Expand: {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000384 if (MMI) {
385 if (MBB->isLandingPad())
386 AddCatchInfo(*cast<CallInst>(I), MMI, MBB);
387 else {
388#ifndef NDEBUG
389 CatchInfoLost.insert(cast<CallInst>(I));
390#endif
391 // FIXME: Mark exception selector register as live in. Hack for PR1508.
392 unsigned Reg = TLI.getExceptionSelectorRegister();
393 if (Reg) MBB->addLiveIn(Reg);
394 }
395
396 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000397 EVT SrcVT = TLI.getPointerTy();
398 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000399 unsigned ResultReg = createResultReg(RC);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000400 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
401 RC, RC);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000402 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000403 InsertedCopy = InsertedCopy;
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000404
405 // Cast the register to the type of the selector.
406 if (SrcVT.bitsGT(MVT::i32))
407 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
408 ResultReg);
409 else if (SrcVT.bitsLT(MVT::i32))
410 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
411 ISD::SIGN_EXTEND, ResultReg);
412 if (ResultReg == 0)
413 // Unhandled operand. Halt "fast" selection and bail.
414 return false;
415
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000416 UpdateValueMap(I, ResultReg);
417 } else {
418 unsigned ResultReg =
Owen Andersona7235ea2009-07-31 20:28:14 +0000419 getRegForValue(Constant::getNullValue(I->getType()));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000420 UpdateValueMap(I, ResultReg);
421 }
422 return true;
423 }
424 }
425 break;
426 }
Dan Gohman33134c42008-09-25 17:05:24 +0000427 }
428 return false;
429}
430
Dan Gohman40b189e2008-09-05 18:18:20 +0000431bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000432 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
433 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000434
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
436 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000437 // Unhandled type. Halt "fast" selection and bail.
438 return false;
439
Dan Gohman474d3b32009-03-13 23:53:06 +0000440 // Check if the destination type is legal. Or as a special case,
441 // it may be i1 if we're doing a truncate because that's
442 // easy and somewhat common.
443 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000445 // Unhandled type. Halt "fast" selection and bail.
446 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000447
448 // Check if the source operand is legal. Or as a special case,
449 // it may be i1 if we're doing zero-extension because that's
450 // easy and somewhat common.
451 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000453 // Unhandled type. Halt "fast" selection and bail.
454 return false;
455
Dan Gohman3df24e62008-09-03 23:12:08 +0000456 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000457 if (!InputReg)
458 // Unhandled operand. Halt "fast" selection and bail.
459 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000460
461 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000463 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000464 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg);
465 if (!InputReg)
466 return false;
467 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000468 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000470 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000471
Owen Andersond0533c92008-08-26 23:46:32 +0000472 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
473 DstVT.getSimpleVT(),
474 Opcode,
475 InputReg);
476 if (!ResultReg)
477 return false;
478
Dan Gohman3df24e62008-09-03 23:12:08 +0000479 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000480 return true;
481}
482
Dan Gohman40b189e2008-09-05 18:18:20 +0000483bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000484 // If the bitcast doesn't change the type, just use the operand value.
485 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000486 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000487 if (Reg == 0)
488 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000489 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000490 return true;
491 }
492
493 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000494 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
495 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000496
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
498 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000499 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
500 // Unhandled type. Halt "fast" selection and bail.
501 return false;
502
Dan Gohman3df24e62008-09-03 23:12:08 +0000503 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000504 if (Op0 == 0)
505 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000506 return false;
507
Dan Gohmanad368ac2008-08-27 18:10:19 +0000508 // First, try to perform the bitcast by inserting a reg-reg copy.
509 unsigned ResultReg = 0;
510 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
511 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
512 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
513 ResultReg = createResultReg(DstClass);
514
515 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
516 Op0, DstClass, SrcClass);
517 if (!InsertedCopy)
518 ResultReg = 0;
519 }
520
521 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
522 if (!ResultReg)
523 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
524 ISD::BIT_CONVERT, Op0);
525
526 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000527 return false;
528
Dan Gohman3df24e62008-09-03 23:12:08 +0000529 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000530 return true;
531}
532
Dan Gohman3df24e62008-09-03 23:12:08 +0000533bool
534FastISel::SelectInstruction(Instruction *I) {
Dan Gohman6e3ff372009-12-05 01:27:58 +0000535 // First, try doing target-independent selection.
536 if (SelectOperator(I, I->getOpcode()))
537 return true;
538
539 // Next, try calling the target to attempt to handle the instruction.
540 if (TargetSelectInstruction(I))
541 return true;
542
543 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000544}
545
Dan Gohmand98d6202008-10-02 22:15:21 +0000546/// FastEmitBranch - Emit an unconditional branch to the given block,
547/// unless it is the immediate (fall-through) successor, and update
548/// the CFG.
549void
550FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000551 if (MBB->isLayoutSuccessor(MSucc)) {
552 // The unconditional fall-through case, which needs no instructions.
553 } else {
554 // The unconditional branch case.
555 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
556 }
557 MBB->addSuccessor(MSucc);
558}
559
Dan Gohman3d45a852009-09-03 22:53:57 +0000560/// SelectFNeg - Emit an FNeg operation.
561///
562bool
563FastISel::SelectFNeg(User *I) {
564 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
565 if (OpReg == 0) return false;
566
Dan Gohman4a215a12009-09-11 00:36:43 +0000567 // If the target has ISD::FNEG, use it.
568 EVT VT = TLI.getValueType(I->getType());
569 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
570 ISD::FNEG, OpReg);
571 if (ResultReg != 0) {
572 UpdateValueMap(I, ResultReg);
573 return true;
574 }
575
Dan Gohman5e5abb72009-09-11 00:34:46 +0000576 // Bitcast the value to integer, twiddle the sign bit with xor,
577 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000578 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000579 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
580 if (!TLI.isTypeLegal(IntVT))
581 return false;
582
583 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
584 ISD::BIT_CONVERT, OpReg);
585 if (IntReg == 0)
586 return false;
587
588 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, IntReg,
589 UINT64_C(1) << (VT.getSizeInBits()-1),
590 IntVT.getSimpleVT());
591 if (IntResultReg == 0)
592 return false;
593
594 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
595 ISD::BIT_CONVERT, IntResultReg);
Dan Gohman3d45a852009-09-03 22:53:57 +0000596 if (ResultReg == 0)
597 return false;
598
599 UpdateValueMap(I, ResultReg);
600 return true;
601}
602
Dan Gohman40b189e2008-09-05 18:18:20 +0000603bool
604FastISel::SelectOperator(User *I, unsigned Opcode) {
605 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000606 case Instruction::Add:
607 return SelectBinaryOp(I, ISD::ADD);
608 case Instruction::FAdd:
609 return SelectBinaryOp(I, ISD::FADD);
610 case Instruction::Sub:
611 return SelectBinaryOp(I, ISD::SUB);
612 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000613 // FNeg is currently represented in LLVM IR as a special case of FSub.
614 if (BinaryOperator::isFNeg(I))
615 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000616 return SelectBinaryOp(I, ISD::FSUB);
617 case Instruction::Mul:
618 return SelectBinaryOp(I, ISD::MUL);
619 case Instruction::FMul:
620 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000621 case Instruction::SDiv:
622 return SelectBinaryOp(I, ISD::SDIV);
623 case Instruction::UDiv:
624 return SelectBinaryOp(I, ISD::UDIV);
625 case Instruction::FDiv:
626 return SelectBinaryOp(I, ISD::FDIV);
627 case Instruction::SRem:
628 return SelectBinaryOp(I, ISD::SREM);
629 case Instruction::URem:
630 return SelectBinaryOp(I, ISD::UREM);
631 case Instruction::FRem:
632 return SelectBinaryOp(I, ISD::FREM);
633 case Instruction::Shl:
634 return SelectBinaryOp(I, ISD::SHL);
635 case Instruction::LShr:
636 return SelectBinaryOp(I, ISD::SRL);
637 case Instruction::AShr:
638 return SelectBinaryOp(I, ISD::SRA);
639 case Instruction::And:
640 return SelectBinaryOp(I, ISD::AND);
641 case Instruction::Or:
642 return SelectBinaryOp(I, ISD::OR);
643 case Instruction::Xor:
644 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000645
Dan Gohman3df24e62008-09-03 23:12:08 +0000646 case Instruction::GetElementPtr:
647 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000648
Dan Gohman3df24e62008-09-03 23:12:08 +0000649 case Instruction::Br: {
650 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000651
Dan Gohman3df24e62008-09-03 23:12:08 +0000652 if (BI->isUnconditional()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000653 BasicBlock *LLVMSucc = BI->getSuccessor(0);
654 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000655 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000656 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000657 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000658
659 // Conditional branches are not handed yet.
660 // Halt "fast" selection and bail.
661 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000662 }
663
Dan Gohman087c8502008-09-05 01:08:41 +0000664 case Instruction::Unreachable:
665 // Nothing to emit.
666 return true;
667
Dan Gohman3df24e62008-09-03 23:12:08 +0000668 case Instruction::PHI:
669 // PHI nodes are already emitted.
670 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000671
672 case Instruction::Alloca:
673 // FunctionLowering has the static-sized case covered.
674 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
675 return true;
676
677 // Dynamic-sized alloca is not handled yet.
678 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000679
Dan Gohman33134c42008-09-25 17:05:24 +0000680 case Instruction::Call:
681 return SelectCall(I);
682
Dan Gohman3df24e62008-09-03 23:12:08 +0000683 case Instruction::BitCast:
684 return SelectBitCast(I);
685
686 case Instruction::FPToSI:
687 return SelectCast(I, ISD::FP_TO_SINT);
688 case Instruction::ZExt:
689 return SelectCast(I, ISD::ZERO_EXTEND);
690 case Instruction::SExt:
691 return SelectCast(I, ISD::SIGN_EXTEND);
692 case Instruction::Trunc:
693 return SelectCast(I, ISD::TRUNCATE);
694 case Instruction::SIToFP:
695 return SelectCast(I, ISD::SINT_TO_FP);
696
697 case Instruction::IntToPtr: // Deliberate fall-through.
698 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000699 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
700 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000701 if (DstVT.bitsGT(SrcVT))
702 return SelectCast(I, ISD::ZERO_EXTEND);
703 if (DstVT.bitsLT(SrcVT))
704 return SelectCast(I, ISD::TRUNCATE);
705 unsigned Reg = getRegForValue(I->getOperand(0));
706 if (Reg == 0) return false;
707 UpdateValueMap(I, Reg);
708 return true;
709 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000710
Dan Gohman3df24e62008-09-03 23:12:08 +0000711 default:
712 // Unhandled instruction. Halt "fast" selection and bail.
713 return false;
714 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000715}
716
Dan Gohman3df24e62008-09-03 23:12:08 +0000717FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000718 MachineModuleInfo *mmi,
Devang Patel83489bb2009-01-13 00:35:13 +0000719 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +0000720 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000721 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000722 DenseMap<const AllocaInst *, int> &am
723#ifndef NDEBUG
724 , SmallSet<Instruction*, 8> &cil
725#endif
726 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000727 : MBB(0),
728 ValueMap(vm),
729 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000730 StaticAllocaMap(am),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000731#ifndef NDEBUG
732 CatchInfoLost(cil),
733#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000734 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000735 MMI(mmi),
Devang Patel83489bb2009-01-13 00:35:13 +0000736 DW(dw),
Dan Gohman3df24e62008-09-03 23:12:08 +0000737 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000738 MFI(*MF.getFrameInfo()),
739 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000740 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000741 TD(*TM.getTargetData()),
742 TII(*TM.getInstrInfo()),
Owen Andersone922c022009-07-22 00:24:57 +0000743 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000744}
745
Dan Gohmane285a742008-08-14 21:51:29 +0000746FastISel::~FastISel() {}
747
Owen Anderson825b72b2009-08-11 20:47:22 +0000748unsigned FastISel::FastEmit_(MVT, MVT,
Evan Cheng36fd9412008-09-02 21:59:13 +0000749 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000750 return 0;
751}
752
Owen Anderson825b72b2009-08-11 20:47:22 +0000753unsigned FastISel::FastEmit_r(MVT, MVT,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000754 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000755 return 0;
756}
757
Owen Anderson825b72b2009-08-11 20:47:22 +0000758unsigned FastISel::FastEmit_rr(MVT, MVT,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000759 ISD::NodeType, unsigned /*Op0*/,
760 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000761 return 0;
762}
763
Owen Anderson825b72b2009-08-11 20:47:22 +0000764unsigned FastISel::FastEmit_i(MVT, MVT, ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000765 return 0;
766}
767
Owen Anderson825b72b2009-08-11 20:47:22 +0000768unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman10df0fa2008-08-27 01:09:54 +0000769 ISD::NodeType, ConstantFP * /*FPImm*/) {
770 return 0;
771}
772
Owen Anderson825b72b2009-08-11 20:47:22 +0000773unsigned FastISel::FastEmit_ri(MVT, MVT,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000774 ISD::NodeType, unsigned /*Op0*/,
775 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000776 return 0;
777}
778
Owen Anderson825b72b2009-08-11 20:47:22 +0000779unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman10df0fa2008-08-27 01:09:54 +0000780 ISD::NodeType, unsigned /*Op0*/,
781 ConstantFP * /*FPImm*/) {
782 return 0;
783}
784
Owen Anderson825b72b2009-08-11 20:47:22 +0000785unsigned FastISel::FastEmit_rri(MVT, MVT,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000786 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000787 unsigned /*Op0*/, unsigned /*Op1*/,
788 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000789 return 0;
790}
791
792/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
793/// to emit an instruction with an immediate operand using FastEmit_ri.
794/// If that fails, it materializes the immediate into a register and try
795/// FastEmit_rr instead.
Owen Anderson825b72b2009-08-11 20:47:22 +0000796unsigned FastISel::FastEmit_ri_(MVT VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000797 unsigned Op0, uint64_t Imm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000799 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000800 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000801 if (ResultReg != 0)
802 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000803 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000804 if (MaterialReg == 0)
805 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000806 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000807}
808
Dan Gohman10df0fa2008-08-27 01:09:54 +0000809/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
810/// to emit an instruction with a floating-point immediate operand using
811/// FastEmit_rf. If that fails, it materializes the immediate into a register
812/// and try FastEmit_rr instead.
Owen Anderson825b72b2009-08-11 20:47:22 +0000813unsigned FastISel::FastEmit_rf_(MVT VT, ISD::NodeType Opcode,
Dan Gohman10df0fa2008-08-27 01:09:54 +0000814 unsigned Op0, ConstantFP *FPImm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000816 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000817 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000818 if (ResultReg != 0)
819 return ResultReg;
820
821 // Materialize the constant in a register.
822 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
823 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000824 // If the target doesn't have a way to directly enter a floating-point
825 // value into a register, use an alternate approach.
826 // TODO: The current approach only supports floating-point constants
827 // that can be constructed by conversion from integer values. This should
828 // be replaced by code that creates a load from a constant-pool entry,
829 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000830 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000831 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000832
833 uint64_t x[2];
834 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000835 bool isExact;
836 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
837 APFloat::rmTowardZero, &isExact);
838 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000839 return 0;
840 APInt IntVal(IntBitWidth, 2, x);
841
842 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
843 ISD::Constant, IntVal.getZExtValue());
844 if (IntegerReg == 0)
845 return 0;
846 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
847 ISD::SINT_TO_FP, IntegerReg);
848 if (MaterialReg == 0)
849 return 0;
850 }
851 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
852}
853
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000854unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
855 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000856}
857
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000858unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000859 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000860 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000861 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000862
Bill Wendling9bc96a52009-02-03 00:55:04 +0000863 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000864 return ResultReg;
865}
866
867unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
868 const TargetRegisterClass *RC,
869 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000870 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000871 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000872
Evan Cheng5960e4e2008-09-08 08:38:20 +0000873 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000874 BuildMI(MBB, DL, II, ResultReg).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000875 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000876 BuildMI(MBB, DL, II).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000877 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
878 II.ImplicitDefs[0], RC, RC);
879 if (!InsertedCopy)
880 ResultReg = 0;
881 }
882
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000883 return ResultReg;
884}
885
886unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
887 const TargetRegisterClass *RC,
888 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000889 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000890 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000891
Evan Cheng5960e4e2008-09-08 08:38:20 +0000892 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000893 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000894 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000895 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000896 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
897 II.ImplicitDefs[0], RC, RC);
898 if (!InsertedCopy)
899 ResultReg = 0;
900 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000901 return ResultReg;
902}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000903
904unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
905 const TargetRegisterClass *RC,
906 unsigned Op0, uint64_t Imm) {
907 unsigned ResultReg = createResultReg(RC);
908 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
909
Evan Cheng5960e4e2008-09-08 08:38:20 +0000910 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000911 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000912 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000913 BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000914 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
915 II.ImplicitDefs[0], RC, RC);
916 if (!InsertedCopy)
917 ResultReg = 0;
918 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000919 return ResultReg;
920}
921
Dan Gohman10df0fa2008-08-27 01:09:54 +0000922unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
923 const TargetRegisterClass *RC,
924 unsigned Op0, ConstantFP *FPImm) {
925 unsigned ResultReg = createResultReg(RC);
926 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
927
Evan Cheng5960e4e2008-09-08 08:38:20 +0000928 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000929 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000930 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000931 BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000932 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
933 II.ImplicitDefs[0], RC, RC);
934 if (!InsertedCopy)
935 ResultReg = 0;
936 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000937 return ResultReg;
938}
939
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000940unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
941 const TargetRegisterClass *RC,
942 unsigned Op0, unsigned Op1, uint64_t Imm) {
943 unsigned ResultReg = createResultReg(RC);
944 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
945
Evan Cheng5960e4e2008-09-08 08:38:20 +0000946 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000947 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000948 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000949 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000950 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
951 II.ImplicitDefs[0], RC, RC);
952 if (!InsertedCopy)
953 ResultReg = 0;
954 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000955 return ResultReg;
956}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000957
958unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
959 const TargetRegisterClass *RC,
960 uint64_t Imm) {
961 unsigned ResultReg = createResultReg(RC);
962 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
963
Evan Cheng5960e4e2008-09-08 08:38:20 +0000964 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000965 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000966 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000967 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000968 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
969 II.ImplicitDefs[0], RC, RC);
970 if (!InsertedCopy)
971 ResultReg = 0;
972 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000973 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000974}
Owen Anderson8970f002008-08-27 22:30:02 +0000975
Owen Anderson825b72b2009-08-11 20:47:22 +0000976unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Evan Cheng536ab132009-01-22 09:10:11 +0000977 unsigned Op0, uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +0000978 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000979
Evan Cheng536ab132009-01-22 09:10:11 +0000980 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Owen Anderson8970f002008-08-27 22:30:02 +0000981 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
982
Evan Cheng5960e4e2008-09-08 08:38:20 +0000983 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000984 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000985 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000986 BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000987 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
988 II.ImplicitDefs[0], RC, RC);
989 if (!InsertedCopy)
990 ResultReg = 0;
991 }
Owen Anderson8970f002008-08-27 22:30:02 +0000992 return ResultReg;
993}
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000994
995/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
996/// with all but the least significant bit set to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000997unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000998 return FastEmit_ri(VT, VT, ISD::AND, Op, 1);
999}