blob: 4fad8dff77c986cce1cffdc425e6789553cc4654 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86Subtarget.h"
17#include "X86ISelLowering.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000018#include "llvm/GlobalValue.h"
19#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SelectionDAGISel.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/ADT/Statistic.h"
25using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28// Pattern Matcher Implementation
29//===----------------------------------------------------------------------===//
30
31namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000032 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
33 /// SDOperand's instead of register numbers for the leaves of the matched
34 /// tree.
35 struct X86ISelAddressMode {
36 enum {
37 RegBase,
38 FrameIndexBase,
Evan Chengec693f72005-12-08 02:01:35 +000039 ConstantPoolBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000040 } BaseType;
41
42 struct { // This is really a union, discriminated by BaseType!
43 SDOperand Reg;
44 int FrameIndex;
45 } Base;
46
47 unsigned Scale;
48 SDOperand IndexReg;
49 unsigned Disp;
50 GlobalValue *GV;
51
52 X86ISelAddressMode()
Evan Chengbd3d25c2005-11-30 02:51:20 +000053 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 }
55 };
56}
57
58namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000059 Statistic<>
60 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
61
62 //===--------------------------------------------------------------------===//
63 /// ISel - X86 specific code to select X86 machine instructions for
64 /// SelectionDAG operations.
65 ///
66 class X86DAGToDAGISel : public SelectionDAGISel {
67 /// ContainsFPCode - Every instruction we select that uses or defines a FP
68 /// register should set this to true.
69 bool ContainsFPCode;
70
71 /// X86Lowering - This object fully describes how to lower LLVM code to an
72 /// X86-specific SelectionDAG.
73 X86TargetLowering X86Lowering;
74
75 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
76 /// make the right decision when generating code for different targets.
77 const X86Subtarget *Subtarget;
78 public:
79 X86DAGToDAGISel(TargetMachine &TM)
80 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
81 Subtarget = &TM.getSubtarget<X86Subtarget>();
82 }
83
84 virtual const char *getPassName() const {
85 return "X86 DAG->DAG Instruction Selection";
86 }
87
88 /// InstructionSelectBasicBlock - This callback is invoked by
89 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
90 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
91
92// Include the pieces autogenerated from the target description.
93#include "X86GenDAGISel.inc"
94
95 private:
96 SDOperand Select(SDOperand N);
97
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000098 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
Evan Chengec693f72005-12-08 02:01:35 +000099 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
100 SDOperand &Index, SDOperand &Disp);
101 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
102 SDOperand &Index, SDOperand &Disp);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000103
Evan Chenge5280532005-12-12 21:49:40 +0000104 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
105 SDOperand &Scale, SDOperand &Index,
106 SDOperand &Disp) {
107 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
108 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000109 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000110 Index = AM.IndexReg;
111 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
112 : getI32Imm(AM.Disp);
113 }
114
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000115 /// getI8Imm - Return a target constant with the specified value, of type
116 /// i8.
117 inline SDOperand getI8Imm(unsigned Imm) {
118 return CurDAG->getTargetConstant(Imm, MVT::i8);
119 }
120
Chris Lattnerc961eea2005-11-16 01:54:32 +0000121 /// getI16Imm - Return a target constant with the specified value, of type
122 /// i16.
123 inline SDOperand getI16Imm(unsigned Imm) {
124 return CurDAG->getTargetConstant(Imm, MVT::i16);
125 }
126
127 /// getI32Imm - Return a target constant with the specified value, of type
128 /// i32.
129 inline SDOperand getI32Imm(unsigned Imm) {
130 return CurDAG->getTargetConstant(Imm, MVT::i32);
131 }
132 };
133}
134
135/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
136/// when it has created a SelectionDAG for us to codegen.
137void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
138 DEBUG(BB->dump());
139
140 // Codegen the basic block.
141 DAG.setRoot(Select(DAG.getRoot()));
142 DAG.RemoveDeadNodes();
143
144 // Emit machine code to BB.
145 ScheduleAndEmitDAG(DAG);
146}
147
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000148/// FIXME: copied from X86ISelPattern.cpp
149/// MatchAddress - Add the specified node to the specified addressing mode,
150/// returning true if it cannot be done. This just pattern matches for the
151/// addressing mode
152bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
153 switch (N.getOpcode()) {
154 default: break;
155 case ISD::FrameIndex:
156 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
157 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
158 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
159 return false;
160 }
161 break;
Evan Chengec693f72005-12-08 02:01:35 +0000162
163 case ISD::ConstantPool:
164 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
165 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) {
166 AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
167 AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32);
168 return false;
169 }
170 }
171 break;
172
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000173 case ISD::GlobalAddress:
174 if (AM.GV == 0) {
175 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
176 // For Darwin, external and weak symbols are indirect, so we want to load
177 // the value at address GV, not the value of GV itself. This means that
178 // the GlobalAddress must be in the base or index register of the address,
179 // not the GV offset field.
180 if (Subtarget->getIndirectExternAndWeakGlobals() &&
181 (GV->hasWeakLinkage() || GV->isExternal())) {
Evan Chengbdce7b42005-12-17 09:13:43 +0000182 AM.Base.Reg =
183 CurDAG->getTargetNode(X86::MOV32rm, MVT::i32, MVT::Other,
184 CurDAG->getRegister(0, MVT::i32),
185 getI8Imm(1), CurDAG->getRegister(0, MVT::i32),
186 CurDAG->getTargetGlobalAddress(GV, MVT::i32),
187 CurDAG->getEntryNode());
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000188 } else {
189 AM.GV = GV;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000190 }
Evan Chengbdce7b42005-12-17 09:13:43 +0000191 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000192 }
193 break;
Evan Chengec693f72005-12-08 02:01:35 +0000194
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000195 case ISD::Constant:
196 AM.Disp += cast<ConstantSDNode>(N)->getValue();
197 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000198
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000199 case ISD::SHL:
200 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
201 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
202 unsigned Val = CN->getValue();
203 if (Val == 1 || Val == 2 || Val == 3) {
204 AM.Scale = 1 << Val;
205 SDOperand ShVal = N.Val->getOperand(0);
206
207 // Okay, we know that we have a scale by now. However, if the scaled
208 // value is an add of something and a constant, we can fold the
209 // constant into the disp field here.
210 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
211 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
212 AM.IndexReg = ShVal.Val->getOperand(0);
213 ConstantSDNode *AddVal =
214 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
215 AM.Disp += AddVal->getValue() << Val;
216 } else {
217 AM.IndexReg = ShVal;
218 }
219 return false;
220 }
221 }
222 break;
Evan Chengec693f72005-12-08 02:01:35 +0000223
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000224 case ISD::MUL:
225 // X*[3,5,9] -> X+X*[2,4,8]
226 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
227 AM.Base.Reg.Val == 0)
228 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
229 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
230 AM.Scale = unsigned(CN->getValue())-1;
231
232 SDOperand MulVal = N.Val->getOperand(0);
233 SDOperand Reg;
234
235 // Okay, we know that we have a scale by now. However, if the scaled
236 // value is an add of something and a constant, we can fold the
237 // constant into the disp field here.
238 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
239 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
240 Reg = MulVal.Val->getOperand(0);
241 ConstantSDNode *AddVal =
242 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
243 AM.Disp += AddVal->getValue() * CN->getValue();
244 } else {
245 Reg = N.Val->getOperand(0);
246 }
247
248 AM.IndexReg = AM.Base.Reg = Reg;
249 return false;
250 }
251 break;
252
253 case ISD::ADD: {
254 X86ISelAddressMode Backup = AM;
255 if (!MatchAddress(N.Val->getOperand(0), AM) &&
256 !MatchAddress(N.Val->getOperand(1), AM))
257 return false;
258 AM = Backup;
259 if (!MatchAddress(N.Val->getOperand(1), AM) &&
260 !MatchAddress(N.Val->getOperand(0), AM))
261 return false;
262 AM = Backup;
263 break;
264 }
265 }
266
267 // Is the base register already occupied?
268 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
269 // If so, check to see if the scale index register is set.
270 if (AM.IndexReg.Val == 0) {
271 AM.IndexReg = N;
272 AM.Scale = 1;
273 return false;
274 }
275
276 // Otherwise, we cannot select it.
277 return true;
278 }
279
280 // Default, generate it as a register.
281 AM.BaseType = X86ISelAddressMode::RegBase;
282 AM.Base.Reg = N;
283 return false;
284}
285
Evan Chengec693f72005-12-08 02:01:35 +0000286/// SelectAddr - returns true if it is able pattern match an addressing mode.
287/// It returns the operands which make up the maximal addressing mode it can
288/// match by reference.
289bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
290 SDOperand &Index, SDOperand &Disp) {
291 X86ISelAddressMode AM;
292 if (!MatchAddress(N, AM)) {
293 if (AM.BaseType == X86ISelAddressMode::RegBase) {
294 if (AM.Base.Reg.Val)
295 AM.Base.Reg = Select(AM.Base.Reg);
296 else
297 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
298 }
299 if (AM.IndexReg.Val)
300 AM.IndexReg = Select(AM.IndexReg);
301 else
302 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
303
Evan Chenge5280532005-12-12 21:49:40 +0000304 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000305 return true;
306 }
307 return false;
308}
309
310static bool isRegister0(SDOperand Op)
311{
312 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
313 return (R->getReg() == 0);
314 return false;
315}
316
317/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
318/// mode it matches can be cost effectively emitted as an LEA instruction.
319/// For X86, it always is unless it's just a (Reg + const).
320bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
321 SDOperand &Index, SDOperand &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000322 X86ISelAddressMode AM;
323 if (!MatchAddress(N, AM)) {
324 bool SelectBase = false;
325 bool SelectIndex = false;
326 bool Check = false;
327 if (AM.BaseType == X86ISelAddressMode::RegBase) {
328 if (AM.Base.Reg.Val) {
329 Check = true;
330 SelectBase = true;
Evan Chengec693f72005-12-08 02:01:35 +0000331 } else {
Evan Chenge5280532005-12-12 21:49:40 +0000332 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000333 }
Evan Chengec693f72005-12-08 02:01:35 +0000334 }
Evan Chenge5280532005-12-12 21:49:40 +0000335
336 if (AM.IndexReg.Val) {
337 SelectIndex = true;
338 } else {
339 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
340 }
341
342 if (Check) {
343 unsigned Complexity = 0;
344 if (AM.Scale > 1)
345 Complexity++;
346 if (SelectIndex)
347 Complexity++;
348 if (AM.GV)
349 Complexity++;
350 else if (AM.Disp > 1)
351 Complexity++;
352 if (Complexity <= 1)
353 return false;
354 }
355
356 if (SelectBase)
357 AM.Base.Reg = Select(AM.Base.Reg);
358 if (SelectIndex)
359 AM.IndexReg = Select(AM.IndexReg);
360
361 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000362 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000363 }
Evan Chenge5280532005-12-12 21:49:40 +0000364 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000365}
366
Evan Chengdef941b2005-12-15 01:02:48 +0000367SDOperand X86DAGToDAGISel::Select(SDOperand N) {
368 SDNode *Node = N.Val;
369 MVT::ValueType NVT = Node->getValueType(0);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000370 unsigned Opc;
371
Evan Chengaed7c722005-12-17 01:24:02 +0000372 if (Node->getOpcode() >= ISD::BUILTIN_OP_END &&
373 Node->getOpcode() < X86ISD::FIRST_NUMBER)
Evan Chengdef941b2005-12-15 01:02:48 +0000374 return N; // Already selected.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000375
Evan Chengdef941b2005-12-15 01:02:48 +0000376 switch (Node->getOpcode()) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000377 default: break;
Evan Chengbd3d25c2005-11-30 02:51:20 +0000378
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000379 case ISD::SHL:
Evan Chengdef941b2005-12-15 01:02:48 +0000380 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Evan Cheng640f2992005-12-01 00:43:55 +0000381 if (CN->getValue() == 1) {
Evan Chengbd3d25c2005-11-30 02:51:20 +0000382 // X = SHL Y, 1 -> X = ADD Y, Y
Evan Chengdef941b2005-12-15 01:02:48 +0000383 switch (NVT) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000384 default: assert(0 && "Cannot shift this type!");
385 case MVT::i8: Opc = X86::ADD8rr; break;
386 case MVT::i16: Opc = X86::ADD16rr; break;
387 case MVT::i32: Opc = X86::ADD32rr; break;
388 }
Evan Chengdef941b2005-12-15 01:02:48 +0000389 SDOperand Tmp0 = Select(Node->getOperand(0));
390 if (Node->hasOneUse())
391 return CurDAG->SelectNodeTo(Node, Opc, NVT, Tmp0, Tmp0);
392 else
393 return CodeGenMap[N] =
394 CurDAG->getTargetNode(Opc, NVT, Tmp0, Tmp0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000395 }
396 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000397 break;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000398
Evan Cheng45f37bc2005-12-17 02:02:50 +0000399 case ISD::TRUNCATE: {
400 unsigned Reg;
401 MVT::ValueType VT;
402 switch (Node->getOperand(0).getValueType()) {
403 default: assert(0 && "Unknown truncate!");
404 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
405 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
406 }
407 SDOperand Tmp0 = Select(Node->getOperand(0));
408 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
409 SDOperand InFlag = SDOperand(0,0);
410 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
411 Reg, Tmp1, InFlag).getValue(1);
412 SDOperand Chain = Result.getValue(0);
413 InFlag = Result.getValue(1);
414
415 switch (NVT) {
416 default: assert(0 && "Unknown truncate!");
417 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
418 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
419 }
420
421 Result = CurDAG->getCopyFromReg(Chain,
422 Reg, VT, InFlag);
423 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
424 break;
425 }
426
Chris Lattnerc961eea2005-11-16 01:54:32 +0000427 case ISD::RET: {
Evan Chengdef941b2005-12-15 01:02:48 +0000428 SDOperand Chain = Node->getOperand(0); // Token chain.
429 unsigned NumOps = Node->getNumOperands();
Evan Chengcbd6ed42005-12-12 20:32:18 +0000430
431 // Note: A bit of a hack / optimization... Try to delay chain selection
432 // as much as possible. So it's more likely it has already been selected
433 // for a real use.
434 switch (NumOps) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000435 default:
436 assert(0 && "Unknown return instruction!");
437 case 3:
Evan Chengcbd6ed42005-12-12 20:32:18 +0000438 Chain = Select(Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000439 assert(0 && "Not yet handled return instruction!");
440 break;
441 case 2: {
Evan Chengdef941b2005-12-15 01:02:48 +0000442 SDOperand Val = Select(Node->getOperand(1));
Evan Chengcbd6ed42005-12-12 20:32:18 +0000443 Chain = Select(Chain);
Evan Chengdef941b2005-12-15 01:02:48 +0000444 switch (Node->getOperand(1).getValueType()) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000445 default:
446 assert(0 && "All other types should have been promoted!!");
447 case MVT::i32:
448 Chain = CurDAG->getCopyToReg(Chain, X86::EAX, Val);
449 break;
450 case MVT::f32:
451 case MVT::f64:
452 assert(0 && "Not yet handled return instruction!");
453 break;
454 }
455 }
456 case 1:
Evan Chengcbd6ed42005-12-12 20:32:18 +0000457 Chain = Select(Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000458 break;
459 }
460 if (X86Lowering.getBytesToPopOnReturn() == 0)
Evan Chengdef941b2005-12-15 01:02:48 +0000461 return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other, Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000462 else
Evan Chengdef941b2005-12-15 01:02:48 +0000463 return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other,
Chris Lattner350d22e2005-11-30 22:59:19 +0000464 getI16Imm(X86Lowering.getBytesToPopOnReturn()),
465 Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000466 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000467 }
468
Evan Chengdef941b2005-12-15 01:02:48 +0000469 return SelectCode(N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000470}
471
472/// createX86ISelDag - This pass converts a legalized DAG into a
473/// X86-specific DAG, ready for instruction scheduling.
474///
475FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
476 return new X86DAGToDAGISel(TM);
477}