blob: 27db21e7025fb0ba97deddf6efb5dedcf28e35a7 [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,
39 } BaseType;
40
41 struct { // This is really a union, discriminated by BaseType!
42 SDOperand Reg;
43 int FrameIndex;
44 } Base;
45
46 unsigned Scale;
47 SDOperand IndexReg;
48 unsigned Disp;
49 GlobalValue *GV;
50
51 X86ISelAddressMode()
Evan Chengbd3d25c2005-11-30 02:51:20 +000052 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 }
54 };
55}
56
57namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000058 Statistic<>
59 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
60
61 //===--------------------------------------------------------------------===//
62 /// ISel - X86 specific code to select X86 machine instructions for
63 /// SelectionDAG operations.
64 ///
65 class X86DAGToDAGISel : public SelectionDAGISel {
66 /// ContainsFPCode - Every instruction we select that uses or defines a FP
67 /// register should set this to true.
68 bool ContainsFPCode;
69
70 /// X86Lowering - This object fully describes how to lower LLVM code to an
71 /// X86-specific SelectionDAG.
72 X86TargetLowering X86Lowering;
73
74 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
75 /// make the right decision when generating code for different targets.
76 const X86Subtarget *Subtarget;
77 public:
78 X86DAGToDAGISel(TargetMachine &TM)
79 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
80 Subtarget = &TM.getSubtarget<X86Subtarget>();
81 }
82
83 virtual const char *getPassName() const {
84 return "X86 DAG->DAG Instruction Selection";
85 }
86
87 /// InstructionSelectBasicBlock - This callback is invoked by
88 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
89 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
90
91// Include the pieces autogenerated from the target description.
92#include "X86GenDAGISel.inc"
93
94 private:
95 SDOperand Select(SDOperand N);
96
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000097 void SelectAddress(SDOperand N, X86ISelAddressMode &AM);
98 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
99
100 /// getI8Imm - Return a target constant with the specified value, of type
101 /// i8.
102 inline SDOperand getI8Imm(unsigned Imm) {
103 return CurDAG->getTargetConstant(Imm, MVT::i8);
104 }
105
Chris Lattnerc961eea2005-11-16 01:54:32 +0000106 /// getI16Imm - Return a target constant with the specified value, of type
107 /// i16.
108 inline SDOperand getI16Imm(unsigned Imm) {
109 return CurDAG->getTargetConstant(Imm, MVT::i16);
110 }
111
112 /// getI32Imm - Return a target constant with the specified value, of type
113 /// i32.
114 inline SDOperand getI32Imm(unsigned Imm) {
115 return CurDAG->getTargetConstant(Imm, MVT::i32);
116 }
117 };
118}
119
120/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
121/// when it has created a SelectionDAG for us to codegen.
122void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
123 DEBUG(BB->dump());
124
125 // Codegen the basic block.
126 DAG.setRoot(Select(DAG.getRoot()));
127 DAG.RemoveDeadNodes();
128
129 // Emit machine code to BB.
130 ScheduleAndEmitDAG(DAG);
131}
132
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000133/// SelectAddress - Pattern match the maximal addressing mode for this node.
134void X86DAGToDAGISel::SelectAddress(SDOperand N, X86ISelAddressMode &AM) {
135 MatchAddress(N, AM);
136
137 if (AM.BaseType == X86ISelAddressMode::RegBase && !AM.Base.Reg.Val) {
138 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
139 } else {
140 AM.Base.Reg = Select(AM.Base.Reg);
141 }
142 if (!AM.IndexReg.Val) {
143 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
144 } else {
145 AM.IndexReg = Select(AM.IndexReg);
146 }
147}
148
149/// FIXME: copied from X86ISelPattern.cpp
150/// MatchAddress - Add the specified node to the specified addressing mode,
151/// returning true if it cannot be done. This just pattern matches for the
152/// addressing mode
153bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
154 switch (N.getOpcode()) {
155 default: break;
156 case ISD::FrameIndex:
157 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
158 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
159 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
160 return false;
161 }
162 break;
163 case ISD::GlobalAddress:
164 if (AM.GV == 0) {
165 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
166 // For Darwin, external and weak symbols are indirect, so we want to load
167 // the value at address GV, not the value of GV itself. This means that
168 // the GlobalAddress must be in the base or index register of the address,
169 // not the GV offset field.
170 if (Subtarget->getIndirectExternAndWeakGlobals() &&
171 (GV->hasWeakLinkage() || GV->isExternal())) {
172 break;
173 } else {
174 AM.GV = GV;
175 return false;
176 }
177 }
178 break;
179 case ISD::Constant:
180 AM.Disp += cast<ConstantSDNode>(N)->getValue();
181 return false;
182 case ISD::SHL:
183 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
184 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
185 unsigned Val = CN->getValue();
186 if (Val == 1 || Val == 2 || Val == 3) {
187 AM.Scale = 1 << Val;
188 SDOperand ShVal = N.Val->getOperand(0);
189
190 // Okay, we know that we have a scale by now. However, if the scaled
191 // value is an add of something and a constant, we can fold the
192 // constant into the disp field here.
193 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
194 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
195 AM.IndexReg = ShVal.Val->getOperand(0);
196 ConstantSDNode *AddVal =
197 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
198 AM.Disp += AddVal->getValue() << Val;
199 } else {
200 AM.IndexReg = ShVal;
201 }
202 return false;
203 }
204 }
205 break;
206 case ISD::MUL:
207 // X*[3,5,9] -> X+X*[2,4,8]
208 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
209 AM.Base.Reg.Val == 0)
210 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
211 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
212 AM.Scale = unsigned(CN->getValue())-1;
213
214 SDOperand MulVal = N.Val->getOperand(0);
215 SDOperand Reg;
216
217 // Okay, we know that we have a scale by now. However, if the scaled
218 // value is an add of something and a constant, we can fold the
219 // constant into the disp field here.
220 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
221 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
222 Reg = MulVal.Val->getOperand(0);
223 ConstantSDNode *AddVal =
224 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
225 AM.Disp += AddVal->getValue() * CN->getValue();
226 } else {
227 Reg = N.Val->getOperand(0);
228 }
229
230 AM.IndexReg = AM.Base.Reg = Reg;
231 return false;
232 }
233 break;
234
235 case ISD::ADD: {
236 X86ISelAddressMode Backup = AM;
237 if (!MatchAddress(N.Val->getOperand(0), AM) &&
238 !MatchAddress(N.Val->getOperand(1), AM))
239 return false;
240 AM = Backup;
241 if (!MatchAddress(N.Val->getOperand(1), AM) &&
242 !MatchAddress(N.Val->getOperand(0), AM))
243 return false;
244 AM = Backup;
245 break;
246 }
247 }
248
249 // Is the base register already occupied?
250 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
251 // If so, check to see if the scale index register is set.
252 if (AM.IndexReg.Val == 0) {
253 AM.IndexReg = N;
254 AM.Scale = 1;
255 return false;
256 }
257
258 // Otherwise, we cannot select it.
259 return true;
260 }
261
262 // Default, generate it as a register.
263 AM.BaseType = X86ISelAddressMode::RegBase;
264 AM.Base.Reg = N;
265 return false;
266}
267
Chris Lattnerc961eea2005-11-16 01:54:32 +0000268SDOperand X86DAGToDAGISel::Select(SDOperand Op) {
269 SDNode *N = Op.Val;
Evan Chengbd3d25c2005-11-30 02:51:20 +0000270 MVT::ValueType OpVT = N->getValueType(0);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000271 unsigned Opc;
272
273 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
274 return Op; // Already selected.
275
276 switch (N->getOpcode()) {
277 default: break;
Evan Chengbd3d25c2005-11-30 02:51:20 +0000278
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000279 case ISD::SHL:
Evan Chengbd3d25c2005-11-30 02:51:20 +0000280 case ISD::SRL:
281 case ISD::SRA:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000282 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
Evan Chengbd3d25c2005-11-30 02:51:20 +0000283 if (N->getOpcode() == ISD::SHL && CN->getValue() == 1) {
284 // X = SHL Y, 1 -> X = ADD Y, Y
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000285 switch (OpVT) {
286 default: assert(0 && "Cannot shift this type!");
287 case MVT::i8: Opc = X86::ADD8rr; break;
288 case MVT::i16: Opc = X86::ADD16rr; break;
289 case MVT::i32: Opc = X86::ADD32rr; break;
290 }
291 SDOperand Tmp0 = Select(N->getOperand(0));
Chris Lattner350d22e2005-11-30 22:59:19 +0000292 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Tmp0, Tmp0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000293 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000294 } else {
295 static const unsigned SHLTab[] = {
296 X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL
297 };
298 static const unsigned SRLTab[] = {
299 X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL
300 };
301 static const unsigned SRATab[] = {
302 X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL
303 };
304
305 switch (OpVT) {
306 default: assert(0 && "Cannot shift this type!");
307 case MVT::i1:
308 case MVT::i8: Opc = 0; break;
309 case MVT::i16: Opc = 1; break;
310 case MVT::i32: Opc = 2; break;
311 }
312
313 switch (N->getOpcode()) {
314 default: assert(0 && "Unreachable!");
315 case ISD::SHL: Opc = SHLTab[Opc]; break;
316 case ISD::SRL: Opc = SRLTab[Opc]; break;
317 case ISD::SRA: Opc = SRATab[Opc]; break;
318 }
319
320 SDOperand Tmp0 = Select(N->getOperand(0));
Chris Lattner350d22e2005-11-30 22:59:19 +0000321 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Tmp0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000322 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000323 break;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000324
Chris Lattnerc961eea2005-11-16 01:54:32 +0000325 case ISD::RET: {
326 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
327 switch (N->getNumOperands()) {
328 default:
329 assert(0 && "Unknown return instruction!");
330 case 3:
331 assert(0 && "Not yet handled return instruction!");
332 break;
333 case 2: {
334 SDOperand Val = Select(N->getOperand(1));
335 switch (N->getOperand(1).getValueType()) {
336 default:
337 assert(0 && "All other types should have been promoted!!");
338 case MVT::i32:
339 Chain = CurDAG->getCopyToReg(Chain, X86::EAX, Val);
340 break;
341 case MVT::f32:
342 case MVT::f64:
343 assert(0 && "Not yet handled return instruction!");
344 break;
345 }
346 }
347 case 1:
348 break;
349 }
350 if (X86Lowering.getBytesToPopOnReturn() == 0)
Chris Lattner350d22e2005-11-30 22:59:19 +0000351 return CurDAG->SelectNodeTo(N, X86::RET, MVT::Other, Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000352 else
Chris Lattner350d22e2005-11-30 22:59:19 +0000353 return CurDAG->SelectNodeTo(N, X86::RET, MVT::Other,
354 getI16Imm(X86Lowering.getBytesToPopOnReturn()),
355 Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000356 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000357
358 case ISD::LOAD: {
Evan Chengbd3d25c2005-11-30 02:51:20 +0000359 switch (OpVT) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000360 default: assert(0 && "Cannot load this type!");
361 case MVT::i1:
362 case MVT::i8: Opc = X86::MOV8rm; break;
363 case MVT::i16: Opc = X86::MOV16rm; break;
364 case MVT::i32: Opc = X86::MOV32rm; break;
365 case MVT::f32: Opc = X86::MOVSSrm; break;
366 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
367 }
368
369 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N->getOperand(1))){
370 unsigned CPIdx = BB->getParent()->getConstantPool()->
371 getConstantPoolIndex(CP->get());
372 // ???
373 assert(0 && "Can't handle load from constant pool!");
374 } else {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000375 X86ISelAddressMode AM;
Evan Chengbd3d25c2005-11-30 02:51:20 +0000376 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
377
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000378 SelectAddress(N->getOperand(1), AM);
379 SDOperand Scale = getI8Imm (AM.Scale);
Evan Chengbd3d25c2005-11-30 02:51:20 +0000380 SDOperand Disp = AM.GV
381 ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000382 : getI32Imm(AM.Disp);
383 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Chris Lattner350d22e2005-11-30 22:59:19 +0000384 return CurDAG->SelectNodeTo(N, Opc, OpVT, MVT::Other,
385 AM.Base.Reg, Scale, AM.IndexReg, Disp,
386 Chain)
387 .getValue(Op.ResNo);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000388 } else {
389 SDOperand Base = CurDAG->getFrameIndex(AM.Base.FrameIndex, MVT::i32);
Chris Lattner350d22e2005-11-30 22:59:19 +0000390 return CurDAG->SelectNodeTo(N, Opc, OpVT, MVT::Other,
391 Base, Scale, AM.IndexReg, Disp, Chain)
392 .getValue(Op.ResNo);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000393 }
394 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000395 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000396
397 case ISD::STORE: {
398 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
399 SDOperand Tmp1 = Select(N->getOperand(1));
400 X86ISelAddressMode AM;
401 SelectAddress(N->getOperand(2), AM);
402
403 Opc = 0;
404 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
405 switch (CN->getValueType(0)) {
406 default: assert(0 && "Invalid type for operation!");
407 case MVT::i1:
408 case MVT::i8: Opc = X86::MOV8mi; break;
409 case MVT::i16: Opc = X86::MOV16mi; break;
410 case MVT::i32: Opc = X86::MOV32mi; break;
411 }
412 }
413
414 if (!Opc) {
415 switch (N->getOperand(1).getValueType()) {
416 default: assert(0 && "Cannot store this type!");
417 case MVT::i1:
418 case MVT::i8: Opc = X86::MOV8mr; break;
419 case MVT::i16: Opc = X86::MOV16mr; break;
420 case MVT::i32: Opc = X86::MOV32mr; break;
421 case MVT::f32: Opc = X86::MOVSSmr; break;
422 case MVT::f64: Opc = X86::FST64m; break;
423 }
424 }
425
426 SDOperand Scale = getI8Imm (AM.Scale);
427 SDOperand Disp = AM.GV
428 ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
429 : getI32Imm(AM.Disp);
430 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Chris Lattner350d22e2005-11-30 22:59:19 +0000431 return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
432 AM.Base.Reg, Scale, AM.IndexReg, Disp, Tmp1,
433 Chain);
Evan Chengbd3d25c2005-11-30 02:51:20 +0000434 } else {
435 SDOperand Base = CurDAG->getFrameIndex(AM.Base.FrameIndex, MVT::i32);
Chris Lattner350d22e2005-11-30 22:59:19 +0000436 return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
437 Base, Scale, AM.IndexReg, Disp, Tmp1, Chain);
Evan Chengbd3d25c2005-11-30 02:51:20 +0000438 }
439 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000440 }
441
442 return SelectCode(Op);
443}
444
445/// createX86ISelDag - This pass converts a legalized DAG into a
446/// X86-specific DAG, ready for instruction scheduling.
447///
448FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
449 return new X86DAGToDAGISel(TM);
450}