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