blob: db87384c9310f4bcaf6a4f87b301c7e508ddf0b0 [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;
109 Scale = getI8Imm (AM.Scale);
110 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())) {
182 break;
183 } else {
184 AM.GV = GV;
185 return false;
186 }
187 }
188 break;
Evan Chengec693f72005-12-08 02:01:35 +0000189
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000190 case ISD::Constant:
191 AM.Disp += cast<ConstantSDNode>(N)->getValue();
192 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000193
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000194 case ISD::SHL:
195 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
196 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
197 unsigned Val = CN->getValue();
198 if (Val == 1 || Val == 2 || Val == 3) {
199 AM.Scale = 1 << Val;
200 SDOperand ShVal = N.Val->getOperand(0);
201
202 // Okay, we know that we have a scale by now. However, if the scaled
203 // value is an add of something and a constant, we can fold the
204 // constant into the disp field here.
205 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
206 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
207 AM.IndexReg = ShVal.Val->getOperand(0);
208 ConstantSDNode *AddVal =
209 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
210 AM.Disp += AddVal->getValue() << Val;
211 } else {
212 AM.IndexReg = ShVal;
213 }
214 return false;
215 }
216 }
217 break;
Evan Chengec693f72005-12-08 02:01:35 +0000218
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000219 case ISD::MUL:
220 // X*[3,5,9] -> X+X*[2,4,8]
221 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
222 AM.Base.Reg.Val == 0)
223 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
224 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
225 AM.Scale = unsigned(CN->getValue())-1;
226
227 SDOperand MulVal = N.Val->getOperand(0);
228 SDOperand Reg;
229
230 // Okay, we know that we have a scale by now. However, if the scaled
231 // value is an add of something and a constant, we can fold the
232 // constant into the disp field here.
233 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
234 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
235 Reg = MulVal.Val->getOperand(0);
236 ConstantSDNode *AddVal =
237 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
238 AM.Disp += AddVal->getValue() * CN->getValue();
239 } else {
240 Reg = N.Val->getOperand(0);
241 }
242
243 AM.IndexReg = AM.Base.Reg = Reg;
244 return false;
245 }
246 break;
247
248 case ISD::ADD: {
249 X86ISelAddressMode Backup = AM;
250 if (!MatchAddress(N.Val->getOperand(0), AM) &&
251 !MatchAddress(N.Val->getOperand(1), AM))
252 return false;
253 AM = Backup;
254 if (!MatchAddress(N.Val->getOperand(1), AM) &&
255 !MatchAddress(N.Val->getOperand(0), AM))
256 return false;
257 AM = Backup;
258 break;
259 }
260 }
261
262 // Is the base register already occupied?
263 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
264 // If so, check to see if the scale index register is set.
265 if (AM.IndexReg.Val == 0) {
266 AM.IndexReg = N;
267 AM.Scale = 1;
268 return false;
269 }
270
271 // Otherwise, we cannot select it.
272 return true;
273 }
274
275 // Default, generate it as a register.
276 AM.BaseType = X86ISelAddressMode::RegBase;
277 AM.Base.Reg = N;
278 return false;
279}
280
Evan Chengec693f72005-12-08 02:01:35 +0000281/// SelectAddr - returns true if it is able pattern match an addressing mode.
282/// It returns the operands which make up the maximal addressing mode it can
283/// match by reference.
284bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
285 SDOperand &Index, SDOperand &Disp) {
286 X86ISelAddressMode AM;
287 if (!MatchAddress(N, AM)) {
288 if (AM.BaseType == X86ISelAddressMode::RegBase) {
289 if (AM.Base.Reg.Val)
290 AM.Base.Reg = Select(AM.Base.Reg);
291 else
292 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
293 }
294 if (AM.IndexReg.Val)
295 AM.IndexReg = Select(AM.IndexReg);
296 else
297 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
298
Evan Chenge5280532005-12-12 21:49:40 +0000299 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000300 return true;
301 }
302 return false;
303}
304
305static bool isRegister0(SDOperand Op)
306{
307 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
308 return (R->getReg() == 0);
309 return false;
310}
311
312/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
313/// mode it matches can be cost effectively emitted as an LEA instruction.
314/// For X86, it always is unless it's just a (Reg + const).
315bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
316 SDOperand &Index, SDOperand &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000317 X86ISelAddressMode AM;
318 if (!MatchAddress(N, AM)) {
319 bool SelectBase = false;
320 bool SelectIndex = false;
321 bool Check = false;
322 if (AM.BaseType == X86ISelAddressMode::RegBase) {
323 if (AM.Base.Reg.Val) {
324 Check = true;
325 SelectBase = true;
Evan Chengec693f72005-12-08 02:01:35 +0000326 } else {
Evan Chenge5280532005-12-12 21:49:40 +0000327 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000328 }
Evan Chengec693f72005-12-08 02:01:35 +0000329 }
Evan Chenge5280532005-12-12 21:49:40 +0000330
331 if (AM.IndexReg.Val) {
332 SelectIndex = true;
333 } else {
334 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
335 }
336
337 if (Check) {
338 unsigned Complexity = 0;
339 if (AM.Scale > 1)
340 Complexity++;
341 if (SelectIndex)
342 Complexity++;
343 if (AM.GV)
344 Complexity++;
345 else if (AM.Disp > 1)
346 Complexity++;
347 if (Complexity <= 1)
348 return false;
349 }
350
351 if (SelectBase)
352 AM.Base.Reg = Select(AM.Base.Reg);
353 if (SelectIndex)
354 AM.IndexReg = Select(AM.IndexReg);
355
356 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000357 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000358 }
Evan Chenge5280532005-12-12 21:49:40 +0000359 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000360}
361
Evan Chengdef941b2005-12-15 01:02:48 +0000362SDOperand X86DAGToDAGISel::Select(SDOperand N) {
363 SDNode *Node = N.Val;
364 MVT::ValueType NVT = Node->getValueType(0);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000365 unsigned Opc;
366
Evan Chengaed7c722005-12-17 01:24:02 +0000367 if (Node->getOpcode() >= ISD::BUILTIN_OP_END &&
368 Node->getOpcode() < X86ISD::FIRST_NUMBER)
Evan Chengdef941b2005-12-15 01:02:48 +0000369 return N; // Already selected.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000370
Evan Chengdef941b2005-12-15 01:02:48 +0000371 switch (Node->getOpcode()) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000372 default: break;
Evan Chengbd3d25c2005-11-30 02:51:20 +0000373
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000374 case ISD::SHL:
Evan Chengdef941b2005-12-15 01:02:48 +0000375 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Evan Cheng640f2992005-12-01 00:43:55 +0000376 if (CN->getValue() == 1) {
Evan Chengbd3d25c2005-11-30 02:51:20 +0000377 // X = SHL Y, 1 -> X = ADD Y, Y
Evan Chengdef941b2005-12-15 01:02:48 +0000378 switch (NVT) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000379 default: assert(0 && "Cannot shift this type!");
380 case MVT::i8: Opc = X86::ADD8rr; break;
381 case MVT::i16: Opc = X86::ADD16rr; break;
382 case MVT::i32: Opc = X86::ADD32rr; break;
383 }
Evan Chengdef941b2005-12-15 01:02:48 +0000384 SDOperand Tmp0 = Select(Node->getOperand(0));
385 if (Node->hasOneUse())
386 return CurDAG->SelectNodeTo(Node, Opc, NVT, Tmp0, Tmp0);
387 else
388 return CodeGenMap[N] =
389 CurDAG->getTargetNode(Opc, NVT, Tmp0, Tmp0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000390 }
391 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000392 break;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000393
Evan Cheng45f37bc2005-12-17 02:02:50 +0000394 case ISD::TRUNCATE: {
395 unsigned Reg;
396 MVT::ValueType VT;
397 switch (Node->getOperand(0).getValueType()) {
398 default: assert(0 && "Unknown truncate!");
399 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
400 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
401 }
402 SDOperand Tmp0 = Select(Node->getOperand(0));
403 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
404 SDOperand InFlag = SDOperand(0,0);
405 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
406 Reg, Tmp1, InFlag).getValue(1);
407 SDOperand Chain = Result.getValue(0);
408 InFlag = Result.getValue(1);
409
410 switch (NVT) {
411 default: assert(0 && "Unknown truncate!");
412 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
413 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
414 }
415
416 Result = CurDAG->getCopyFromReg(Chain,
417 Reg, VT, InFlag);
418 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
419 break;
420 }
421
Chris Lattnerc961eea2005-11-16 01:54:32 +0000422 case ISD::RET: {
Evan Chengdef941b2005-12-15 01:02:48 +0000423 SDOperand Chain = Node->getOperand(0); // Token chain.
424 unsigned NumOps = Node->getNumOperands();
Evan Chengcbd6ed42005-12-12 20:32:18 +0000425
426 // Note: A bit of a hack / optimization... Try to delay chain selection
427 // as much as possible. So it's more likely it has already been selected
428 // for a real use.
429 switch (NumOps) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000430 default:
431 assert(0 && "Unknown return instruction!");
432 case 3:
Evan Chengcbd6ed42005-12-12 20:32:18 +0000433 Chain = Select(Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000434 assert(0 && "Not yet handled return instruction!");
435 break;
436 case 2: {
Evan Chengdef941b2005-12-15 01:02:48 +0000437 SDOperand Val = Select(Node->getOperand(1));
Evan Chengcbd6ed42005-12-12 20:32:18 +0000438 Chain = Select(Chain);
Evan Chengdef941b2005-12-15 01:02:48 +0000439 switch (Node->getOperand(1).getValueType()) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000440 default:
441 assert(0 && "All other types should have been promoted!!");
442 case MVT::i32:
443 Chain = CurDAG->getCopyToReg(Chain, X86::EAX, Val);
444 break;
445 case MVT::f32:
446 case MVT::f64:
447 assert(0 && "Not yet handled return instruction!");
448 break;
449 }
450 }
451 case 1:
Evan Chengcbd6ed42005-12-12 20:32:18 +0000452 Chain = Select(Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000453 break;
454 }
455 if (X86Lowering.getBytesToPopOnReturn() == 0)
Evan Chengdef941b2005-12-15 01:02:48 +0000456 return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other, Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000457 else
Evan Chengdef941b2005-12-15 01:02:48 +0000458 return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other,
Chris Lattner350d22e2005-11-30 22:59:19 +0000459 getI16Imm(X86Lowering.getBytesToPopOnReturn()),
460 Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000461 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000462 }
463
Evan Chengdef941b2005-12-15 01:02:48 +0000464 return SelectCode(N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000465}
466
467/// createX86ISelDag - This pass converts a legalized DAG into a
468/// X86-specific DAG, ready for instruction scheduling.
469///
470FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
471 return new X86DAGToDAGISel(TM);
472}