blob: ba0bf566a39e27a55a78e63e0c65209f88afc5be [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()
52 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
53 }
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;
270 MVT::ValueType OpVT = Op.getValueType();
271 unsigned Opc;
272
273 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
274 return Op; // Already selected.
275
276 switch (N->getOpcode()) {
277 default: break;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000278 case ISD::SHL:
279 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
280 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
281 switch (OpVT) {
282 default: assert(0 && "Cannot shift this type!");
283 case MVT::i8: Opc = X86::ADD8rr; break;
284 case MVT::i16: Opc = X86::ADD16rr; break;
285 case MVT::i32: Opc = X86::ADD32rr; break;
286 }
287 SDOperand Tmp0 = Select(N->getOperand(0));
288 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Tmp0, Tmp0);
289 return SDOperand(N, 0);
290 }
291 }
292
Chris Lattnerc961eea2005-11-16 01:54:32 +0000293 case ISD::RET: {
294 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
295 switch (N->getNumOperands()) {
296 default:
297 assert(0 && "Unknown return instruction!");
298 case 3:
299 assert(0 && "Not yet handled return instruction!");
300 break;
301 case 2: {
302 SDOperand Val = Select(N->getOperand(1));
303 switch (N->getOperand(1).getValueType()) {
304 default:
305 assert(0 && "All other types should have been promoted!!");
306 case MVT::i32:
307 Chain = CurDAG->getCopyToReg(Chain, X86::EAX, Val);
308 break;
309 case MVT::f32:
310 case MVT::f64:
311 assert(0 && "Not yet handled return instruction!");
312 break;
313 }
314 }
315 case 1:
316 break;
317 }
318 if (X86Lowering.getBytesToPopOnReturn() == 0)
319 CurDAG->SelectNodeTo(N, X86::RET, MVT::Other, Chain);
320 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000321 CurDAG->SelectNodeTo(N, X86::RET, MVT::Other,
322 getI16Imm(X86Lowering.getBytesToPopOnReturn()),
323 Chain);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000324
325 return SDOperand(N, 0);
326 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000327
328 case ISD::LOAD: {
329 switch (N->getValueType(0)) {
330 default: assert(0 && "Cannot load this type!");
331 case MVT::i1:
332 case MVT::i8: Opc = X86::MOV8rm; break;
333 case MVT::i16: Opc = X86::MOV16rm; break;
334 case MVT::i32: Opc = X86::MOV32rm; break;
335 case MVT::f32: Opc = X86::MOVSSrm; break;
336 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
337 }
338
339 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N->getOperand(1))){
340 unsigned CPIdx = BB->getParent()->getConstantPool()->
341 getConstantPoolIndex(CP->get());
342 // ???
343 assert(0 && "Can't handle load from constant pool!");
344 } else {
345 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
346 X86ISelAddressMode AM;
347 SelectAddress(N->getOperand(1), AM);
348 SDOperand Scale = getI8Imm (AM.Scale);
349 SDOperand Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32)
350 : getI32Imm(AM.Disp);
351 if (AM.BaseType == X86ISelAddressMode::RegBase) {
352 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
353 AM.Base.Reg, Scale, AM.IndexReg, Disp, Chain);
354 } else {
355 SDOperand Base = CurDAG->getFrameIndex(AM.Base.FrameIndex, MVT::i32);
356 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
357 Base, Scale, AM.IndexReg, Disp, Chain);
358 }
359 }
360 return SDOperand(N, Op.ResNo);
361 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000362 }
363
364 return SelectCode(Op);
365}
366
367/// createX86ISelDag - This pass converts a legalized DAG into a
368/// X86-specific DAG, ready for instruction scheduling.
369///
370FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
371 return new X86DAGToDAGISel(TM);
372}