blob: c51beab5bde8a01a63175a7ad3b160b04e5797e7 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group 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 pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/SelectionDAG.h"
21#include "llvm/CodeGen/SelectionDAGISel.h"
22#include "llvm/CodeGen/SSARegMap.h"
23#include "llvm/Target/TargetData.h"
24#include "llvm/Target/TargetLowering.h"
25#include "llvm/Support/MathExtras.h"
26#include "llvm/ADT/Statistic.h"
27#include <set>
28using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// X86TargetLowering - X86 Implementation of the TargetLowering interface
32namespace {
33 class X86TargetLowering : public TargetLowering {
34 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
35 public:
36 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
37 // Set up the TargetLowering object.
38 addRegisterClass(MVT::i8, X86::R8RegisterClass);
39 addRegisterClass(MVT::i16, X86::R16RegisterClass);
40 addRegisterClass(MVT::i32, X86::R32RegisterClass);
41 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
42
43 // FIXME: Eliminate these two classes when legalize can handle promotions
44 // well.
45 addRegisterClass(MVT::i1, X86::R8RegisterClass);
46 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
47
48 computeRegisterProperties();
49
50 setOperationUnsupported(ISD::MUL, MVT::i8);
51 setOperationUnsupported(ISD::SELECT, MVT::i1);
52 setOperationUnsupported(ISD::SELECT, MVT::i8);
53
54 addLegalFPImmediate(+0.0); // FLD0
55 addLegalFPImmediate(+1.0); // FLD1
56 addLegalFPImmediate(-0.0); // FLD0/FCHS
57 addLegalFPImmediate(-1.0); // FLD1/FCHS
58 }
59
60 /// LowerArguments - This hook must be implemented to indicate how we should
61 /// lower the arguments for the specified function, into the specified DAG.
62 virtual std::vector<SDOperand>
63 LowerArguments(Function &F, SelectionDAG &DAG);
64
65 /// LowerCallTo - This hook lowers an abstract call to a function into an
66 /// actual call.
67 virtual SDNode *LowerCallTo(const Type *RetTy, SDOperand Callee,
68 ArgListTy &Args, SelectionDAG &DAG);
69 };
70}
71
72
73std::vector<SDOperand>
74X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
75 std::vector<SDOperand> ArgValues;
76
77 // Add DAG nodes to load the arguments... On entry to a function on the X86,
78 // the stack frame looks like this:
79 //
80 // [ESP] -- return address
81 // [ESP + 4] -- first argument (leftmost lexically)
82 // [ESP + 8] -- second argument, if first argument is four bytes in size
83 // ...
84 //
85 MachineFunction &MF = DAG.getMachineFunction();
86 MachineFrameInfo *MFI = MF.getFrameInfo();
87
88 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
89 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
90 MVT::ValueType ObjectVT = getValueType(I->getType());
91 unsigned ArgIncrement = 4;
92 unsigned ObjSize;
93 switch (ObjectVT) {
94 default: assert(0 && "Unhandled argument type!");
95 case MVT::i1:
96 case MVT::i8: ObjSize = 1; break;
97 case MVT::i16: ObjSize = 2; break;
98 case MVT::i32: ObjSize = 4; break;
99 case MVT::i64: ObjSize = ArgIncrement = 8; break;
100 case MVT::f32: ObjSize = 4; break;
101 case MVT::f64: ObjSize = ArgIncrement = 8; break;
102 }
103 // Create the frame index object for this incoming parameter...
104 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
105
106 // Create the SelectionDAG nodes corresponding to a load from this parameter
107 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
108
109 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
110 // dead loads.
111 SDOperand ArgValue;
112 if (!I->use_empty())
113 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
114 else {
115 if (MVT::isInteger(ObjectVT))
116 ArgValue = DAG.getConstant(0, ObjectVT);
117 else
118 ArgValue = DAG.getConstantFP(0, ObjectVT);
119 }
120 ArgValues.push_back(ArgValue);
121
122 ArgOffset += ArgIncrement; // Move on to the next argument...
123 }
124
125 // If the function takes variable number of arguments, make a frame index for
126 // the start of the first vararg value... for expansion of llvm.va_start.
127 if (F.isVarArg())
128 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
129
130 return ArgValues;
131}
132
133SDNode *X86TargetLowering::LowerCallTo(const Type *RetTy, SDOperand Callee,
134 ArgListTy &Args, SelectionDAG &DAG) {
135 // Count how many bytes are to be pushed on the stack.
136 unsigned NumBytes = 0;
137
138 if (Args.empty()) {
139 // Save zero bytes.
140 DAG.setRoot(DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, DAG.getRoot(),
141 DAG.getConstant(0, getPointerTy())));
142 } else {
143 for (unsigned i = 0, e = Args.size(); i != e; ++i)
144 switch (getValueType(Args[i].second)) {
145 default: assert(0 && "Unknown value type!");
146 case MVT::i1:
147 case MVT::i8:
148 case MVT::i16:
149 case MVT::i32:
150 case MVT::f32:
151 NumBytes += 4;
152 break;
153 case MVT::i64:
154 case MVT::f64:
155 NumBytes += 8;
156 break;
157 }
158
159 DAG.setRoot(DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, DAG.getRoot(),
160 DAG.getConstant(NumBytes, getPointerTy())));
161
162 // Arguments go on the stack in reverse order, as specified by the ABI.
163 unsigned ArgOffset = 0;
164 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
165 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
166 unsigned ArgReg;
167 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
168 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
169
170 switch (getValueType(Args[i].second)) {
171 default: assert(0 && "Unexpected ValueType for argument!");
172 case MVT::i1:
173 case MVT::i8:
174 case MVT::i16:
175 // Promote the integer to 32 bits. If the input type is signed use a
176 // sign extend, otherwise use a zero extend.
177 if (Args[i].second->isSigned())
178 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
179 else
180 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
181
182 // FALL THROUGH
183 case MVT::i32:
184 case MVT::f32:
185 // FIXME: Note that all of these stores are independent of each other.
186 DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
187 Args[i].first, PtrOff));
188 ArgOffset += 4;
189 break;
190 case MVT::i64:
191 case MVT::f64:
192 // FIXME: Note that all of these stores are independent of each other.
193 DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
194 Args[i].first, PtrOff));
195 ArgOffset += 8;
196 break;
197 }
198 }
199 }
200
201 std::vector<MVT::ValueType> RetVals;
202 MVT::ValueType RetTyVT = getValueType(RetTy);
203 if (RetTyVT != MVT::isVoid)
204 RetVals.push_back(RetTyVT);
205 RetVals.push_back(MVT::Other);
206
207 SDNode *TheCall = DAG.getCall(RetVals, DAG.getRoot(), Callee);
208 DAG.setRoot(SDOperand(TheCall, TheCall->getNumValues()-1));
209 DAG.setRoot(DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, DAG.getRoot(),
210 DAG.getConstant(NumBytes, getPointerTy())));
211 return TheCall;
212}
213
214
215
216
217
218
219namespace {
220 Statistic<>
221 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
222
223 //===--------------------------------------------------------------------===//
224 /// ISel - X86 specific code to select X86 machine instructions for
225 /// SelectionDAG operations.
226 ///
227 class ISel : public SelectionDAGISel {
228 /// ContainsFPCode - Every instruction we select that uses or defines a FP
229 /// register should set this to true.
230 bool ContainsFPCode;
231
232 /// X86Lowering - This object fully describes how to lower LLVM code to an
233 /// X86-specific SelectionDAG.
234 X86TargetLowering X86Lowering;
235
236
237 /// ExprMap - As shared expressions are codegen'd, we keep track of which
238 /// vreg the value is produced in, so we only emit one copy of each compiled
239 /// tree.
240 std::map<SDOperand, unsigned> ExprMap;
241 std::set<SDOperand> LoweredTokens;
242
243 public:
244 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
245 }
246
247 /// InstructionSelectBasicBlock - This callback is invoked by
248 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
249 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
250 // While we're doing this, keep track of whether we see any FP code for
251 // FP_REG_KILL insertion.
252 ContainsFPCode = false;
253
254 // Codegen the basic block.
255 Select(DAG.getRoot());
256
257 // Insert FP_REG_KILL instructions into basic blocks that need them. This
258 // only occurs due to the floating point stackifier not being aggressive
259 // enough to handle arbitrary global stackification.
260 //
261 // Currently we insert an FP_REG_KILL instruction into each block that
262 // uses or defines a floating point virtual register.
263 //
264 // When the global register allocators (like linear scan) finally update
265 // live variable analysis, we can keep floating point values in registers
266 // across basic blocks. This will be a huge win, but we are waiting on
267 // the global allocators before we can do this.
268 //
269 if (ContainsFPCode && BB->succ_size()) {
270 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
271 ++NumFPKill;
272 }
273
274 // Clear state used for selection.
275 ExprMap.clear();
276 LoweredTokens.clear();
277 }
278
279 void EmitCMP(SDOperand LHS, SDOperand RHS);
280 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Cond);
281 unsigned SelectExpr(SDOperand N);
282 bool SelectAddress(SDOperand N, X86AddressMode &AM);
283 void Select(SDOperand N);
284 };
285}
286
287/// SelectAddress - Add the specified node to the specified addressing mode,
288/// returning true if it cannot be done.
289bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
290 switch (N.getOpcode()) {
291 default: break;
292 case ISD::FrameIndex:
293 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
294 AM.BaseType = X86AddressMode::FrameIndexBase;
295 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
296 return false;
297 }
298 break;
299 case ISD::GlobalAddress:
300 if (AM.GV == 0) {
301 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
302 return false;
303 }
304 break;
305 case ISD::Constant:
306 AM.Disp += cast<ConstantSDNode>(N)->getValue();
307 return false;
308 case ISD::SHL:
309 if (AM.IndexReg == 0 || AM.Scale == 1)
310 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
311 unsigned Val = CN->getValue();
312 if (Val == 1 || Val == 2 || Val == 3) {
313 AM.Scale = 1 << Val;
314 AM.IndexReg = SelectExpr(N.Val->getOperand(0));
315 return false;
316 }
317 }
318 break;
319
320 case ISD::ADD: {
321 X86AddressMode Backup = AM;
322 if (!SelectAddress(N.Val->getOperand(0), AM) &&
323 !SelectAddress(N.Val->getOperand(1), AM))
324 return false;
325 AM = Backup;
326 break;
327 }
328 }
329
330 if (AM.BaseType != X86AddressMode::RegBase ||
331 AM.Base.Reg)
332 return true;
333
334 // Default, generate it as a register.
335 AM.BaseType = X86AddressMode::RegBase;
336 AM.Base.Reg = SelectExpr(N);
337 return false;
338}
339
340/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
341/// assuming that the temporary registers are in the 8-bit register class.
342///
343/// Tmp1 = setcc1
344/// Tmp2 = setcc2
345/// DestReg = logicalop Tmp1, Tmp2
346///
347static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
348 unsigned SetCC2, unsigned LogicalOp,
349 unsigned DestReg) {
350 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
351 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
352 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
353 BuildMI(BB, SetCC1, 0, Tmp1);
354 BuildMI(BB, SetCC2, 0, Tmp2);
355 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
356}
357
358/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
359/// condition codes match the specified SetCCOpcode. Note that some conditions
360/// require multiple instructions to generate the correct value.
361static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
362 ISD::CondCode SetCCOpcode, bool isFP) {
363 unsigned Opc;
364 if (!isFP) {
365 switch (SetCCOpcode) {
366 default: assert(0 && "Illegal integer SetCC!");
367 case ISD::SETEQ: Opc = X86::SETEr; break;
368 case ISD::SETGT: Opc = X86::SETGr; break;
369 case ISD::SETGE: Opc = X86::SETGEr; break;
370 case ISD::SETLT: Opc = X86::SETLr; break;
371 case ISD::SETLE: Opc = X86::SETLEr; break;
372 case ISD::SETNE: Opc = X86::SETNEr; break;
373 case ISD::SETULT: Opc = X86::SETBr; break;
374 case ISD::SETUGT: Opc = X86::SETAr; break;
375 case ISD::SETULE: Opc = X86::SETBEr; break;
376 case ISD::SETUGE: Opc = X86::SETAEr; break;
377 }
378 } else {
379 // On a floating point condition, the flags are set as follows:
380 // ZF PF CF op
381 // 0 | 0 | 0 | X > Y
382 // 0 | 0 | 1 | X < Y
383 // 1 | 0 | 0 | X == Y
384 // 1 | 1 | 1 | unordered
385 //
386 switch (SetCCOpcode) {
387 default: assert(0 && "Invalid FP setcc!");
388 case ISD::SETUEQ:
389 case ISD::SETEQ:
390 Opc = X86::SETEr; // True if ZF = 1
391 break;
392 case ISD::SETOGT:
393 case ISD::SETGT:
394 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
395 break;
396 case ISD::SETOGE:
397 case ISD::SETGE:
398 Opc = X86::SETAEr; // True if CF = 0
399 break;
400 case ISD::SETULT:
401 case ISD::SETLT:
402 Opc = X86::SETBr; // True if CF = 1
403 break;
404 case ISD::SETULE:
405 case ISD::SETLE:
406 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
407 break;
408 case ISD::SETONE:
409 case ISD::SETNE:
410 Opc = X86::SETNEr; // True if ZF = 0
411 break;
412 case ISD::SETUO:
413 Opc = X86::SETPr; // True if PF = 1
414 break;
415 case ISD::SETO:
416 Opc = X86::SETNPr; // True if PF = 0
417 break;
418 case ISD::SETOEQ: // !PF & ZF
419 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
420 return;
421 case ISD::SETOLT: // !PF & CF
422 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
423 return;
424 case ISD::SETOLE: // !PF & (CF || ZF)
425 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
426 return;
427 case ISD::SETUGT: // PF | (!ZF & !CF)
428 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
429 return;
430 case ISD::SETUGE: // PF | !CF
431 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
432 return;
433 case ISD::SETUNE: // PF | !ZF
434 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
435 return;
436 }
437 }
438 BuildMI(BB, Opc, 0, DestReg);
439}
440
441
442/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
443/// the Dest block if the Cond condition is true. If we cannot fold this
444/// condition into the branch, return true.
445///
446bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Cond) {
447 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
448 // B) using two conditional branches instead of one condbr, two setcc's, and
449 // an or.
450 if ((Cond.getOpcode() == ISD::OR ||
451 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
452 // And and or set the flags for us, so there is no need to emit a TST of the
453 // result. It is only safe to do this if there is only a single use of the
454 // AND/OR though, otherwise we don't know it will be emitted here.
455 SelectExpr(Cond);
456 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
457 return false;
458 }
459
460 // Codegen br not C -> JE.
461 if (Cond.getOpcode() == ISD::XOR)
462 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
463 if (NC->isAllOnesValue()) {
464 unsigned CondR = SelectExpr(Cond.Val->getOperand(0));
465 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
466 BuildMI(BB, X86::JE, 1).addMBB(Dest);
467 return false;
468 }
469
470 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
471 if (SetCC == 0)
472 return true; // Can only handle simple setcc's so far.
473
474 unsigned Opc;
475
476 // Handle integer conditions first.
477 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
478 switch (SetCC->getCondition()) {
479 default: assert(0 && "Illegal integer SetCC!");
480 case ISD::SETEQ: Opc = X86::JE; break;
481 case ISD::SETGT: Opc = X86::JG; break;
482 case ISD::SETGE: Opc = X86::JGE; break;
483 case ISD::SETLT: Opc = X86::JL; break;
484 case ISD::SETLE: Opc = X86::JLE; break;
485 case ISD::SETNE: Opc = X86::JNE; break;
486 case ISD::SETULT: Opc = X86::JB; break;
487 case ISD::SETUGT: Opc = X86::JA; break;
488 case ISD::SETULE: Opc = X86::JBE; break;
489 case ISD::SETUGE: Opc = X86::JAE; break;
490 }
491 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
492 BuildMI(BB, Opc, 1).addMBB(Dest);
493 return false;
494 }
495
496 ContainsFPCode = true;
497 unsigned Opc2 = 0; // Second branch if needed.
498
499 // On a floating point condition, the flags are set as follows:
500 // ZF PF CF op
501 // 0 | 0 | 0 | X > Y
502 // 0 | 0 | 1 | X < Y
503 // 1 | 0 | 0 | X == Y
504 // 1 | 1 | 1 | unordered
505 //
506 switch (SetCC->getCondition()) {
507 default: assert(0 && "Invalid FP setcc!");
508 case ISD::SETUEQ:
509 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
510 case ISD::SETOGT:
511 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
512 case ISD::SETOGE:
513 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
514 case ISD::SETULT:
515 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
516 case ISD::SETULE:
517 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
518 case ISD::SETONE:
519 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
520 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
521 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
522 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
523 Opc = X86::JA; // ZF = 0 & CF = 0
524 Opc2 = X86::JP; // PF = 1
525 break;
526 case ISD::SETUGE: // PF = 1 | CF = 0
527 Opc = X86::JAE; // CF = 0
528 Opc2 = X86::JP; // PF = 1
529 break;
530 case ISD::SETUNE: // PF = 1 | ZF = 0
531 Opc = X86::JNE; // ZF = 0
532 Opc2 = X86::JP; // PF = 1
533 break;
534 case ISD::SETOEQ: // PF = 0 & ZF = 1
535 //X86::JNP, X86::JE
536 //X86::AND8rr
537 return true; // FIXME: Emit more efficient code for this branch.
538 case ISD::SETOLT: // PF = 0 & CF = 1
539 //X86::JNP, X86::JB
540 //X86::AND8rr
541 return true; // FIXME: Emit more efficient code for this branch.
542 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
543 //X86::JNP, X86::JBE
544 //X86::AND8rr
545 return true; // FIXME: Emit more efficient code for this branch.
546 }
547
548 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
549 BuildMI(BB, Opc, 1).addMBB(Dest);
550 if (Opc2)
551 BuildMI(BB, Opc2, 1).addMBB(Dest);
552 return false;
553}
554
555void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
556 unsigned Tmp1 = SelectExpr(LHS), Opc;
557 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
558 Opc = 0;
559 switch (RHS.getValueType()) {
560 default: break;
561 case MVT::i1:
562 case MVT::i8: Opc = X86::CMP8ri; break;
563 case MVT::i16: Opc = X86::CMP16ri; break;
564 case MVT::i32: Opc = X86::CMP32ri; break;
565 }
566 if (Opc) {
567 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
568 return;
569 }
570 }
571
572 switch (LHS.getValueType()) {
573 default: assert(0 && "Cannot compare this value!");
574 case MVT::i1:
575 case MVT::i8: Opc = X86::CMP8rr; break;
576 case MVT::i16: Opc = X86::CMP16rr; break;
577 case MVT::i32: Opc = X86::CMP32rr; break;
578 case MVT::f32:
579 case MVT::f64: Opc = X86::FUCOMIr; ContainsFPCode = true; break;
580 }
581 unsigned Tmp2 = SelectExpr(RHS);
582 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
583}
584
585unsigned ISel::SelectExpr(SDOperand N) {
586 unsigned Result;
587 unsigned Tmp1, Tmp2, Tmp3;
588 unsigned Opc = 0;
589
590 if (N.getOpcode() == ISD::CopyFromReg)
591 // Just use the specified register as our input.
592 return dyn_cast<CopyRegSDNode>(N)->getReg();
593
594 // If there are multiple uses of this expression, memorize the
595 // register it is code generated in, instead of emitting it multiple
596 // times.
597 // FIXME: Disabled for our current selection model.
598 if (1 || !N.Val->hasOneUse()) {
599 unsigned &Reg = ExprMap[N];
600 if (Reg) return Reg;
601
602 if (N.getOpcode() != ISD::CALL)
603 Reg = Result = (N.getValueType() != MVT::Other) ?
604 MakeReg(N.getValueType()) : 1;
605 else {
606 // If this is a call instruction, make sure to prepare ALL of the result
607 // values as well as the chain.
608 if (N.Val->getNumValues() == 1)
609 Reg = Result = 1; // Void call, just a chain.
610 else {
611 Result = MakeReg(N.Val->getValueType(0));
612 ExprMap[SDOperand(N.Val,0)] = Result;
613 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
614 ExprMap[SDOperand(N.Val, i)] = MakeReg(N.Val->getValueType(i));
615 ExprMap[SDOperand(N.Val, N.Val->getNumValues()-1)] = 1;
616 }
617 }
618 } else {
619 Result = MakeReg(N.getValueType());
620 }
621
622 switch (N.getOpcode()) {
623 default:
624 N.Val->dump();
625 assert(0 && "Node not handled!\n");
626 case ISD::FrameIndex:
627 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
628 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
629 return Result;
630 case ISD::ConstantPool:
631 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
632 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
633 return Result;
634 case ISD::ConstantFP:
635 ContainsFPCode = true;
636 Tmp1 = Result; // Intermediate Register
637 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
638 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
639 Tmp1 = MakeReg(MVT::f64);
640
641 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
642 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
643 BuildMI(BB, X86::FLD0, 0, Tmp1);
644 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
645 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
646 BuildMI(BB, X86::FLD1, 0, Tmp1);
647 else
648 assert(0 && "Unexpected constant!");
649 if (Tmp1 != Result)
650 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
651 return Result;
652 case ISD::Constant:
653 switch (N.getValueType()) {
654 default: assert(0 && "Cannot use constants of this type!");
655 case MVT::i1:
656 case MVT::i8: Opc = X86::MOV8ri; break;
657 case MVT::i16: Opc = X86::MOV16ri; break;
658 case MVT::i32: Opc = X86::MOV32ri; break;
659 }
660 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
661 return Result;
662 case ISD::GlobalAddress: {
663 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
664 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
665 return Result;
666 }
667 case ISD::ExternalSymbol: {
668 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
669 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
670 return Result;
671 }
672 case ISD::FP_EXTEND:
673 Tmp1 = SelectExpr(N.getOperand(0));
674 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
675 ContainsFPCode = true;
676 return Result;
677 case ISD::ZERO_EXTEND: {
678 int DestIs16 = N.getValueType() == MVT::i16;
679 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
680
681 static const unsigned Opc[3] = {
682 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
683 };
684 Tmp1 = SelectExpr(N.getOperand(0));
685 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
686 return Result;
687 }
688 case ISD::SIGN_EXTEND: {
689 int DestIs16 = N.getValueType() == MVT::i16;
690 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
691
692 static const unsigned Opc[3] = {
693 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
694 };
695 Tmp1 = SelectExpr(N.getOperand(0));
696 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
697 return Result;
698 }
699 case ISD::TRUNCATE:
700 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
701 // a move out of AX or AL.
702 switch (N.getOperand(0).getValueType()) {
703 default: assert(0 && "Unknown truncate!");
704 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
705 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
706 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
707 }
708 Tmp1 = SelectExpr(N.getOperand(0));
709 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
710
711 switch (N.getValueType()) {
712 default: assert(0 && "Unknown truncate!");
713 case MVT::i1:
714 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
715 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
716 }
717 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
718 return Result;
719
720 case ISD::FP_ROUND:
721 // Truncate from double to float by storing to memory as float,
722 // then reading it back into a register.
723
724 // Create as stack slot to use.
725 Tmp1 = TLI.getTargetData().getFloatAlignment();
726 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
727
728 // Codegen the input.
729 Tmp1 = SelectExpr(N.getOperand(0));
730
731 // Emit the store, then the reload.
732 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
733 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
734 ContainsFPCode = true;
735 return Result;
736
737 case ISD::ADD:
738 // See if we can codegen this as an LEA to fold operations together.
739 if (N.getValueType() == MVT::i32) {
740 X86AddressMode AM;
741 if (!SelectAddress(N.getOperand(0), AM) &&
742 !SelectAddress(N.getOperand(1), AM)) {
743 // If this is not just an add, emit the LEA. For a simple add (like
744 // reg+reg or reg+imm), we just emit an add. If might be a good idea to
745 // leave this as LEA, then peephole it to 'ADD' after two address elim
746 // happens.
747 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
748 AM.Base.Reg && AM.IndexReg && (AM.Disp || AM.GV)) {
749 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
750 return Result;
751 }
752 }
753 }
754 Tmp1 = SelectExpr(N.getOperand(0));
755 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
756 Opc = 0;
757 if (CN->getValue() == 1) { // add X, 1 -> inc X
758 switch (N.getValueType()) {
759 default: assert(0 && "Cannot integer add this type!");
760 case MVT::i8: Opc = X86::INC8r; break;
761 case MVT::i16: Opc = X86::INC16r; break;
762 case MVT::i32: Opc = X86::INC32r; break;
763 }
764 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
765 switch (N.getValueType()) {
766 default: assert(0 && "Cannot integer add this type!");
767 case MVT::i8: Opc = X86::DEC8r; break;
768 case MVT::i16: Opc = X86::DEC16r; break;
769 case MVT::i32: Opc = X86::DEC32r; break;
770 }
771 }
772
773 if (Opc) {
774 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
775 return Result;
776 }
777
778 switch (N.getValueType()) {
779 default: assert(0 && "Cannot add this type!");
780 case MVT::i8: Opc = X86::ADD8ri; break;
781 case MVT::i16: Opc = X86::ADD16ri; break;
782 case MVT::i32: Opc = X86::ADD32ri; break;
783 }
784 if (Opc) {
785 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
786 return Result;
787 }
788 }
789
790 Tmp2 = SelectExpr(N.getOperand(1));
791 switch (N.getValueType()) {
792 default: assert(0 && "Cannot add this type!");
793 case MVT::i8: Opc = X86::ADD8rr; break;
794 case MVT::i16: Opc = X86::ADD16rr; break;
795 case MVT::i32: Opc = X86::ADD32rr; break;
796 case MVT::f32:
797 case MVT::f64: Opc = X86::FpADD; ContainsFPCode = true; break;
798 }
799 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
800 return Result;
801 case ISD::SUB:
802 if (MVT::isInteger(N.getValueType()))
803 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
804 if (CN->isNullValue()) { // 0 - N -> neg N
805 switch (N.getValueType()) {
806 default: assert(0 && "Cannot sub this type!");
807 case MVT::i1:
808 case MVT::i8: Opc = X86::NEG8r; break;
809 case MVT::i16: Opc = X86::NEG16r; break;
810 case MVT::i32: Opc = X86::NEG32r; break;
811 }
812 Tmp1 = SelectExpr(N.getOperand(1));
813 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
814 return Result;
815 }
816
817 Tmp1 = SelectExpr(N.getOperand(0));
818 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
819 switch (N.getValueType()) {
820 default: assert(0 && "Cannot sub this type!");
821 case MVT::i1:
822 case MVT::i8: Opc = X86::SUB8ri; break;
823 case MVT::i16: Opc = X86::SUB16ri; break;
824 case MVT::i32: Opc = X86::SUB32ri; break;
825 }
826 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
827 return Result;
828 }
829 Tmp2 = SelectExpr(N.getOperand(1));
830 switch (N.getValueType()) {
831 default: assert(0 && "Cannot add this type!");
832 case MVT::i1:
833 case MVT::i8: Opc = X86::SUB8rr; break;
834 case MVT::i16: Opc = X86::SUB16rr; break;
835 case MVT::i32: Opc = X86::SUB32rr; break;
836 case MVT::f32:
837 case MVT::f64: Opc = X86::FpSUB; ContainsFPCode = true; break;
838 }
839 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
840 return Result;
841
842 case ISD::AND:
843 Tmp1 = SelectExpr(N.getOperand(0));
844 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
845 switch (N.getValueType()) {
846 default: assert(0 && "Cannot add this type!");
847 case MVT::i1:
848 case MVT::i8: Opc = X86::AND8ri; break;
849 case MVT::i16: Opc = X86::AND16ri; break;
850 case MVT::i32: Opc = X86::AND32ri; break;
851 }
852 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
853 return Result;
854 }
855 Tmp2 = SelectExpr(N.getOperand(1));
856 switch (N.getValueType()) {
857 default: assert(0 && "Cannot add this type!");
858 case MVT::i1:
859 case MVT::i8: Opc = X86::AND8rr; break;
860 case MVT::i16: Opc = X86::AND16rr; break;
861 case MVT::i32: Opc = X86::AND32rr; break;
862 }
863 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
864 return Result;
865 case ISD::OR:
866 Tmp1 = SelectExpr(N.getOperand(0));
867 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
868 switch (N.getValueType()) {
869 default: assert(0 && "Cannot add this type!");
870 case MVT::i1:
871 case MVT::i8: Opc = X86::OR8ri; break;
872 case MVT::i16: Opc = X86::OR16ri; break;
873 case MVT::i32: Opc = X86::OR32ri; break;
874 }
875 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
876 return Result;
877 }
878 Tmp2 = SelectExpr(N.getOperand(1));
879 switch (N.getValueType()) {
880 default: assert(0 && "Cannot add this type!");
881 case MVT::i1:
882 case MVT::i8: Opc = X86::OR8rr; break;
883 case MVT::i16: Opc = X86::OR16rr; break;
884 case MVT::i32: Opc = X86::OR32rr; break;
885 }
886 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
887 return Result;
888 case ISD::XOR:
889 Tmp1 = SelectExpr(N.getOperand(0));
890 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
891 switch (N.getValueType()) {
892 default: assert(0 && "Cannot add this type!");
893 case MVT::i1:
894 case MVT::i8: Opc = X86::XOR8ri; break;
895 case MVT::i16: Opc = X86::XOR16ri; break;
896 case MVT::i32: Opc = X86::XOR32ri; break;
897 }
898 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
899 return Result;
900 }
901 Tmp2 = SelectExpr(N.getOperand(1));
902 switch (N.getValueType()) {
903 default: assert(0 && "Cannot add this type!");
904 case MVT::i1:
905 case MVT::i8: Opc = X86::XOR8rr; break;
906 case MVT::i16: Opc = X86::XOR16rr; break;
907 case MVT::i32: Opc = X86::XOR32rr; break;
908 }
909 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
910 return Result;
911
912 case ISD::MUL:
913 Tmp1 = SelectExpr(N.getOperand(0));
914 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
915 Opc = 0;
916 switch (N.getValueType()) {
917 default: assert(0 && "Cannot multiply this type!");
918 case MVT::i8: break;
919 case MVT::i16: Opc = X86::IMUL16rri; break;
920 case MVT::i32: Opc = X86::IMUL32rri; break;
921 }
922 if (Opc) {
923 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
924 return Result;
925 }
926 }
927 Tmp2 = SelectExpr(N.getOperand(1));
928 switch (N.getValueType()) {
929 default: assert(0 && "Cannot add this type!");
930 case MVT::i8: assert(0 && "FIXME: MUL i8 not implemented yet!");
931 case MVT::i16: Opc = X86::IMUL16rr; break;
932 case MVT::i32: Opc = X86::IMUL32rr; break;
933 case MVT::f32:
934 case MVT::f64: Opc = X86::FpMUL; ContainsFPCode = true; break;
935 }
936 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
937 return Result;
938
939 case ISD::SELECT:
940 // FIXME: implement folding of setcc into select.
941 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
942 Tmp2 = SelectExpr(N.getOperand(1));
943 Tmp3 = SelectExpr(N.getOperand(2));
944 Tmp1 = SelectExpr(N.getOperand(0));
945
946 switch (N.getValueType()) {
947 default: assert(0 && "Cannot select this type!");
948 case MVT::i16: Opc = X86::CMOVE16rr; break;
949 case MVT::i32: Opc = X86::CMOVE32rr; break;
950 case MVT::f32:
951 case MVT::f64: Opc = X86::FCMOVE; ContainsFPCode = true; break;
952 }
953
954 // Get the condition into the zero flag.
955 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
956 BuildMI(BB, Opc, 2, Result).addReg(Tmp2).addReg(Tmp3);
957 return Result;
958 } else {
959 // FIXME: This should not be implemented here, it should be in the generic
960 // code!
961 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
962 N.getOperand(1)));
963 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
964 N.getOperand(2)));
965 Tmp1 = SelectExpr(N.getOperand(0));
966 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
967 // FIXME: need subregs to do better than this!
968 unsigned TmpReg = MakeReg(MVT::i16);
969 BuildMI(BB, X86::CMOVE16rr, 2, TmpReg).addReg(Tmp2).addReg(Tmp3);
970 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
971 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
972 return Result;
973 }
974
975 case ISD::SDIV:
976 case ISD::UDIV:
977 case ISD::SREM:
978 case ISD::UREM: {
979 Tmp1 = SelectExpr(N.getOperand(0));
980
981 if (N.getOpcode() == ISD::SDIV)
982 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
983 // FIXME: These special cases should be handled by the lowering impl!
984 unsigned RHS = CN->getValue();
985 bool isNeg = false;
986 if ((int)RHS < 0) {
987 isNeg = true;
988 RHS = -RHS;
989 }
990 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
991 unsigned Log = log2(RHS);
992 unsigned TmpReg = MakeReg(N.getValueType());
993 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
994 switch (N.getValueType()) {
995 default: assert("Unknown type to signed divide!");
996 case MVT::i8:
997 SAROpc = X86::SAR8ri;
998 SHROpc = X86::SHR8ri;
999 ADDOpc = X86::ADD8rr;
1000 NEGOpc = X86::NEG8r;
1001 break;
1002 case MVT::i16:
1003 SAROpc = X86::SAR16ri;
1004 SHROpc = X86::SHR16ri;
1005 ADDOpc = X86::ADD16rr;
1006 NEGOpc = X86::NEG16r;
1007 break;
1008 case MVT::i32:
1009 SAROpc = X86::SAR32ri;
1010 SHROpc = X86::SHR32ri;
1011 ADDOpc = X86::ADD32rr;
1012 NEGOpc = X86::NEG32r;
1013 break;
1014 }
1015 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1016 unsigned TmpReg2 = MakeReg(N.getValueType());
1017 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1018 unsigned TmpReg3 = MakeReg(N.getValueType());
1019 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1020
1021 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1022 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1023 if (isNeg)
1024 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1025 return Result;
1026 }
1027 }
1028
1029 Tmp2 = SelectExpr(N.getOperand(1));
1030
1031 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1032 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1033 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1034 switch (N.getValueType()) {
1035 default: assert(0 && "Cannot sdiv this type!");
1036 case MVT::i8:
1037 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1038 LoReg = X86::AL;
1039 HiReg = X86::AH;
1040 MovOpcode = X86::MOV8rr;
1041 ClrOpcode = X86::MOV8ri;
1042 SExtOpcode = X86::CBW;
1043 break;
1044 case MVT::i16:
1045 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1046 LoReg = X86::AX;
1047 HiReg = X86::DX;
1048 MovOpcode = X86::MOV16rr;
1049 ClrOpcode = X86::MOV16ri;
1050 SExtOpcode = X86::CWD;
1051 break;
1052 case MVT::i32:
1053 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1054 LoReg =X86::EAX;
1055 HiReg = X86::EDX;
1056 MovOpcode = X86::MOV32rr;
1057 ClrOpcode = X86::MOV32ri;
1058 SExtOpcode = X86::CDQ;
1059 break;
1060 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1061 case MVT::f32:
1062 case MVT::f64:
1063 ContainsFPCode = true;
1064 if (N.getOpcode() == ISD::SDIV)
1065 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1066 else
1067 assert(0 && "FIXME: Emit frem libcall to fmod!");
1068 return Result;
1069 }
1070
1071 // Set up the low part.
1072 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1073
1074 if (isSigned) {
1075 // Sign extend the low part into the high part.
1076 BuildMI(BB, SExtOpcode, 0);
1077 } else {
1078 // Zero out the high part, effectively zero extending the input.
1079 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1080 }
1081
1082 // Emit the DIV/IDIV instruction.
1083 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1084
1085 // Get the result of the divide or rem.
1086 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1087 return Result;
1088 }
1089
1090 case ISD::SHL:
1091 Tmp1 = SelectExpr(N.getOperand(0));
1092 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1093 switch (N.getValueType()) {
1094 default: assert(0 && "Cannot shift this type!");
1095 case MVT::i8: Opc = X86::SHL8ri; break;
1096 case MVT::i16: Opc = X86::SHL16ri; break;
1097 case MVT::i32: Opc = X86::SHL32ri; break;
1098 }
1099 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1100 return Result;
1101 }
1102 Tmp2 = SelectExpr(N.getOperand(1));
1103 switch (N.getValueType()) {
1104 default: assert(0 && "Cannot shift this type!");
1105 case MVT::i8 : Opc = X86::SHL8rCL; break;
1106 case MVT::i16: Opc = X86::SHL16rCL; break;
1107 case MVT::i32: Opc = X86::SHL32rCL; break;
1108 }
1109 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1110 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1111 return Result;
1112 case ISD::SRL:
1113 Tmp1 = SelectExpr(N.getOperand(0));
1114 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1115 switch (N.getValueType()) {
1116 default: assert(0 && "Cannot shift this type!");
1117 case MVT::i8: Opc = X86::SHR8ri; break;
1118 case MVT::i16: Opc = X86::SHR16ri; break;
1119 case MVT::i32: Opc = X86::SHR32ri; break;
1120 }
1121 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1122 return Result;
1123 }
1124 Tmp2 = SelectExpr(N.getOperand(1));
1125 switch (N.getValueType()) {
1126 default: assert(0 && "Cannot shift this type!");
1127 case MVT::i8 : Opc = X86::SHR8rCL; break;
1128 case MVT::i16: Opc = X86::SHR16rCL; break;
1129 case MVT::i32: Opc = X86::SHR32rCL; break;
1130 }
1131 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1132 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1133 return Result;
1134 case ISD::SRA:
1135 Tmp1 = SelectExpr(N.getOperand(0));
1136 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1137 switch (N.getValueType()) {
1138 default: assert(0 && "Cannot shift this type!");
1139 case MVT::i8: Opc = X86::SAR8ri; break;
1140 case MVT::i16: Opc = X86::SAR16ri; break;
1141 case MVT::i32: Opc = X86::SAR32ri; break;
1142 }
1143 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1144 return Result;
1145 }
1146 Tmp2 = SelectExpr(N.getOperand(1));
1147 switch (N.getValueType()) {
1148 default: assert(0 && "Cannot shift this type!");
1149 case MVT::i8 : Opc = X86::SAR8rCL; break;
1150 case MVT::i16: Opc = X86::SAR16rCL; break;
1151 case MVT::i32: Opc = X86::SAR32rCL; break;
1152 }
1153 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1154 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1155 return Result;
1156
1157 case ISD::SETCC:
1158 if (MVT::isFloatingPoint(N.getOperand(0).getValueType()))
1159 ContainsFPCode = true;
1160 EmitCMP(N.getOperand(0), N.getOperand(1));
1161 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1162 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1163 return Result;
1164 case ISD::LOAD: {
1165 Select(N.getOperand(0));
1166
1167 // Make sure we generate both values.
1168 if (Result != 1)
1169 ExprMap[N.getValue(1)] = 1; // Generate the token
1170 else
1171 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1172
1173 switch (N.Val->getValueType(0)) {
1174 default: assert(0 && "Cannot load this type!");
1175 case MVT::i1:
1176 case MVT::i8: Opc = X86::MOV8rm; break;
1177 case MVT::i16: Opc = X86::MOV16rm; break;
1178 case MVT::i32: Opc = X86::MOV32rm; break;
1179 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1180 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1181 }
1182 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
1183 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1184 } else {
1185 X86AddressMode AM;
1186 SelectAddress(N.getOperand(1), AM);
1187 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1188 }
1189 return Result;
1190 }
1191 case ISD::DYNAMIC_STACKALLOC:
1192 Select(N.getOperand(0));
1193
1194 // Generate both result values.
1195 if (Result != 1)
1196 ExprMap[N.getValue(1)] = 1; // Generate the token
1197 else
1198 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1199
1200 // FIXME: We are currently ignoring the requested alignment for handling
1201 // greater than the stack alignment. This will need to be revisited at some
1202 // point. Align = N.getOperand(2);
1203
1204 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1205 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1206 std::cerr << "Cannot allocate stack object with greater alignment than"
1207 << " the stack alignment yet!";
1208 abort();
1209 }
1210
1211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1212 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1213 .addImm(CN->getValue());
1214 } else {
1215 Tmp1 = SelectExpr(N.getOperand(1));
1216
1217 // Subtract size from stack pointer, thereby allocating some space.
1218 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1219 }
1220
1221 // Put a pointer to the space into the result register, by copying the stack
1222 // pointer.
1223 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1224 return Result;
1225
1226 case ISD::CALL:
1227 Select(N.getOperand(0));
1228 if (GlobalAddressSDNode *GASD =
1229 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1230 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1231 } else if (ExternalSymbolSDNode *ESSDN =
1232 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1233 BuildMI(BB, X86::CALLpcrel32,
1234 1).addExternalSymbol(ESSDN->getSymbol(), true);
1235 } else {
1236 Tmp1 = SelectExpr(N.getOperand(1));
1237 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1238 }
1239 switch (N.Val->getValueType(0)) {
1240 default: assert(0 && "Unknown value type for call result!");
1241 case MVT::Other: return 1;
1242 case MVT::i1:
1243 case MVT::i8:
1244 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1245 break;
1246 case MVT::i16:
1247 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1248 break;
1249 case MVT::i32:
1250 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
1251 if (N.Val->getValueType(1) == MVT::i32)
1252 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1253 break;
1254 case MVT::f32:
1255 case MVT::f64: // Floating-point return values live in %ST(0)
1256 ContainsFPCode = true;
1257 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1258 break;
1259 }
1260 return Result+N.ResNo;
1261 }
1262
1263 return 0;
1264}
1265
1266void ISel::Select(SDOperand N) {
1267 unsigned Tmp1, Tmp2, Opc;
1268
1269 // FIXME: Disable for our current expansion model!
1270 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1271 return; // Already selected.
1272
1273 switch (N.getOpcode()) {
1274 default:
1275 N.Val->dump(); std::cerr << "\n";
1276 assert(0 && "Node not handled yet!");
1277 case ISD::EntryToken: return; // Noop
1278 case ISD::CopyToReg:
1279 Select(N.getOperand(0));
1280 Tmp1 = SelectExpr(N.getOperand(1));
1281 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1282
1283 if (Tmp1 != Tmp2) {
1284 switch (N.getOperand(1).getValueType()) {
1285 default: assert(0 && "Invalid type for operation!");
1286 case MVT::i1:
1287 case MVT::i8: Opc = X86::MOV8rr; break;
1288 case MVT::i16: Opc = X86::MOV16rr; break;
1289 case MVT::i32: Opc = X86::MOV32rr; break;
1290 case MVT::f32:
1291 case MVT::f64: Opc = X86::FpMOV; break;
1292 }
1293 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1294 }
1295 return;
1296 case ISD::RET:
1297 Select(N.getOperand(0));
1298 switch (N.getNumOperands()) {
1299 default:
1300 assert(0 && "Unknown return instruction!");
1301 case 3:
1302 Tmp1 = SelectExpr(N.getOperand(1));
1303 Tmp2 = SelectExpr(N.getOperand(2));
1304 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1305 N.getOperand(2).getValueType() == MVT::i32 &&
1306 "Unknown two-register value!");
1307 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1308 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1309 // Declare that EAX & EDX are live on exit.
1310 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1311 .addReg(X86::ESP);
1312 break;
1313 case 2:
1314 Tmp1 = SelectExpr(N.getOperand(1));
1315 switch (N.getOperand(1).getValueType()) {
1316 default: assert(0 && "All other types should have been promoted!!");
1317 case MVT::f64:
1318 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1319 // Declare that top-of-stack is live on exit
1320 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1321 break;
1322 case MVT::i32:
1323 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1324 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1325 break;
1326 }
1327 break;
1328 case 1:
1329 break;
1330 }
1331 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1332 return;
1333 case ISD::BR: {
1334 Select(N.getOperand(0));
1335 MachineBasicBlock *Dest =
1336 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1337 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1338 return;
1339 }
1340
1341 case ISD::BRCOND: {
1342 Select(N.getOperand(0));
1343 MachineBasicBlock *Dest =
1344 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1345 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1346 // pair.
1347 if (EmitBranchCC(Dest, N.getOperand(1))) {
1348 Tmp1 = SelectExpr(N.getOperand(1));
1349 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1350 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1351 }
1352 return;
1353 }
1354 case ISD::LOAD:
1355 case ISD::CALL:
1356 case ISD::DYNAMIC_STACKALLOC:
1357 SelectExpr(N);
1358 return;
1359 case ISD::STORE: {
1360 Select(N.getOperand(0));
1361 // Select the address.
1362 X86AddressMode AM;
1363 SelectAddress(N.getOperand(2), AM);
1364
1365 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1366 Opc = 0;
1367 switch (CN->getValueType(0)) {
1368 default: assert(0 && "Invalid type for operation!");
1369 case MVT::i1:
1370 case MVT::i8: Opc = X86::MOV8mi; break;
1371 case MVT::i16: Opc = X86::MOV16mi; break;
1372 case MVT::i32: Opc = X86::MOV32mi; break;
1373 case MVT::f32:
1374 case MVT::f64: break;
1375 }
1376 if (Opc) {
1377 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
1378 return;
1379 }
1380 }
1381 Tmp1 = SelectExpr(N.getOperand(1));
1382
1383 switch (N.getOperand(1).getValueType()) {
1384 default: assert(0 && "Cannot store this type!");
1385 case MVT::i1:
1386 case MVT::i8: Opc = X86::MOV8mr; break;
1387 case MVT::i16: Opc = X86::MOV16mr; break;
1388 case MVT::i32: Opc = X86::MOV32mr; break;
1389 case MVT::f32: Opc = X86::FST32m; ContainsFPCode = true; break;
1390 case MVT::f64: Opc = X86::FST64m; ContainsFPCode = true; break;
1391 }
1392 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
1393 return;
1394 }
1395 case ISD::ADJCALLSTACKDOWN:
1396 case ISD::ADJCALLSTACKUP:
1397 Select(N.getOperand(0));
1398 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1399
1400 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
1401 X86::ADJCALLSTACKUP;
1402 BuildMI(BB, Opc, 1).addImm(Tmp1);
1403 return;
1404 }
1405 assert(0 && "Should not be reached!");
1406}
1407
1408
1409/// createX86PatternInstructionSelector - This pass converts an LLVM function
1410/// into a machine code representation using pattern matching and a machine
1411/// description file.
1412///
1413FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
1414 return new ISel(TM);
1415}