blob: 2a2ebd97739d31f942124e786d14096bff883a28 [file] [log] [blame]
Chris Lattner88c8a232005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner1d13a922005-01-10 22:10:13 +00002//
Chris Lattner88c8a232005-01-07 07:49:41 +00003// 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.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
Chris Lattner88c8a232005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
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"
Nate Begemanf26625e2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Chris Lattner7ce7a8f2005-05-12 23:06:28 +000019#include "llvm/CallingConv.h"
Chris Lattner6972c312005-05-09 03:36:39 +000020#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000022#include "llvm/Function.h"
Chris Lattner6972c312005-05-09 03:36:39 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetLowering.h"
Nate Begemanf26625e2005-07-12 01:41:54 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattnerdb68d392005-04-30 04:25:35 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner6972c312005-05-09 03:36:39 +000033#include "llvm/Support/CFG.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000034#include "llvm/Support/MathExtras.h"
35#include "llvm/ADT/Statistic.h"
36#include <set>
Chris Lattnerde02d772006-01-22 23:41:00 +000037#include <iostream>
Jeff Cohen407aa012005-01-12 04:29:05 +000038#include <algorithm>
Chris Lattner88c8a232005-01-07 07:49:41 +000039using namespace llvm;
40
Chris Lattnera36117b2005-05-14 06:52:07 +000041//===----------------------------------------------------------------------===//
42// Pattern Matcher Implementation
43//===----------------------------------------------------------------------===//
Chris Lattner88c8a232005-01-07 07:49:41 +000044
Chris Lattnera7acdda2005-01-18 01:06:26 +000045namespace {
46 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
47 /// SDOperand's instead of register numbers for the leaves of the matched
48 /// tree.
49 struct X86ISelAddressMode {
50 enum {
51 RegBase,
52 FrameIndexBase,
53 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +000054
Chris Lattnera7acdda2005-01-18 01:06:26 +000055 struct { // This is really a union, discriminated by BaseType!
56 SDOperand Reg;
57 int FrameIndex;
58 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +000059
Chris Lattnera7acdda2005-01-18 01:06:26 +000060 unsigned Scale;
61 SDOperand IndexReg;
62 unsigned Disp;
63 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +000064
Chris Lattnera7acdda2005-01-18 01:06:26 +000065 X86ISelAddressMode()
66 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
67 }
68 };
69}
Chris Lattner88c8a232005-01-07 07:49:41 +000070
71
72namespace {
73 Statistic<>
74 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
75
76 //===--------------------------------------------------------------------===//
77 /// ISel - X86 specific code to select X86 machine instructions for
78 /// SelectionDAG operations.
79 ///
80 class ISel : public SelectionDAGISel {
81 /// ContainsFPCode - Every instruction we select that uses or defines a FP
82 /// register should set this to true.
83 bool ContainsFPCode;
84
85 /// X86Lowering - This object fully describes how to lower LLVM code to an
86 /// X86-specific SelectionDAG.
87 X86TargetLowering X86Lowering;
88
Chris Lattner0d1f82a2005-01-11 03:11:44 +000089 /// RegPressureMap - This keeps an approximate count of the number of
90 /// registers required to evaluate each node in the graph.
91 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +000092
93 /// ExprMap - As shared expressions are codegen'd, we keep track of which
94 /// vreg the value is produced in, so we only emit one copy of each compiled
95 /// tree.
96 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +000097
Chris Lattnerdd66a412005-05-15 05:46:45 +000098 /// TheDAG - The DAG being selected during Select* operations.
99 SelectionDAG *TheDAG;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000100
101 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanf26625e2005-07-12 01:41:54 +0000102 /// make the right decision when generating code for different targets.
103 const X86Subtarget *Subtarget;
Chris Lattner88c8a232005-01-07 07:49:41 +0000104 public:
105 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Chris Lattner158acab2005-08-05 21:54:27 +0000106 Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattner88c8a232005-01-07 07:49:41 +0000107 }
108
Chris Lattnere1e844c2005-01-21 21:35:14 +0000109 virtual const char *getPassName() const {
110 return "X86 Pattern Instruction Selection";
111 }
112
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000113 unsigned getRegPressure(SDOperand O) {
114 return RegPressureMap[O.Val];
115 }
116 unsigned ComputeRegPressure(SDOperand O);
117
Chris Lattner88c8a232005-01-07 07:49:41 +0000118 /// InstructionSelectBasicBlock - This callback is invoked by
119 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +0000120 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000121
Chris Lattner0b17b452005-05-13 07:38:09 +0000122 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
123
Chris Lattner30607ec2005-01-25 20:03:11 +0000124 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
125 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +0000126 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +0000127 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner29f58192005-01-19 07:37:26 +0000128 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +0000129 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +0000130 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Nate Begeman8d394eb2005-08-03 23:26:28 +0000131 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
132 MVT::ValueType SVT, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +0000133 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000134
135 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
136 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
137 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattnerdd66a412005-05-15 05:46:45 +0000138 bool EmitPotentialTailCall(SDNode *Node);
139 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner88c8a232005-01-07 07:49:41 +0000140 void Select(SDOperand N);
141 };
142}
143
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000144/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
145/// the main function.
146static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
147 MachineFrameInfo *MFI) {
148 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
149 int CWFrameIdx = MFI->CreateStackObject(2, 2);
150 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
151
152 // Set the high part to be 64-bit precision.
153 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
154 CWFrameIdx, 1).addImm(2);
155
156 // Reload the modified control word now.
157 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
158}
159
Chris Lattner0b17b452005-05-13 07:38:09 +0000160void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000161 // If this is main, emit special code for main.
Chris Lattnerb42e9622005-09-14 06:06:45 +0000162 MachineBasicBlock *BB = MF.begin();
Chris Lattner0b17b452005-05-13 07:38:09 +0000163 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
164 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
165}
166
167
Chris Lattner6fba62d62005-01-12 04:21:28 +0000168/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
169/// when it has created a SelectionDAG for us to codegen.
170void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
171 // While we're doing this, keep track of whether we see any FP code for
172 // FP_REG_KILL insertion.
173 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000174 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000175
176 // Scan the PHI nodes that already are inserted into this basic block. If any
177 // of them is a PHI of a floating point value, we need to insert an
178 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000179 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +0000180 if (BB != MF->begin())
181 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
182 I != E; ++I) {
183 assert(I->getOpcode() == X86::PHI &&
184 "Isn't just PHI nodes?");
185 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
186 X86::RFPRegisterClass) {
187 ContainsFPCode = true;
188 break;
189 }
Chris Lattner6fba62d62005-01-12 04:21:28 +0000190 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000191
Chris Lattner6fba62d62005-01-12 04:21:28 +0000192 // Compute the RegPressureMap, which is an approximation for the number of
193 // registers required to compute each node.
194 ComputeRegPressure(DAG.getRoot());
195
Chris Lattnerdd66a412005-05-15 05:46:45 +0000196 TheDAG = &DAG;
197
Chris Lattner6fba62d62005-01-12 04:21:28 +0000198 // Codegen the basic block.
199 Select(DAG.getRoot());
200
Chris Lattnerdd66a412005-05-15 05:46:45 +0000201 TheDAG = 0;
202
Chris Lattner6fba62d62005-01-12 04:21:28 +0000203 // Finally, look at all of the successors of this block. If any contain a PHI
204 // node of FP type, we need to insert an FP_REG_KILL in this block.
205 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
206 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
207 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
208 I != E && I->getOpcode() == X86::PHI; ++I) {
209 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
210 X86::RFPRegisterClass) {
211 ContainsFPCode = true;
212 break;
213 }
214 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000215
Chris Lattner6972c312005-05-09 03:36:39 +0000216 // Final check, check LLVM BB's that are successors to the LLVM BB
217 // corresponding to BB for FP PHI nodes.
218 const BasicBlock *LLVMBB = BB->getBasicBlock();
219 const PHINode *PN;
220 if (!ContainsFPCode)
221 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
222 SI != E && !ContainsFPCode; ++SI)
223 for (BasicBlock::const_iterator II = SI->begin();
224 (PN = dyn_cast<PHINode>(II)); ++II)
225 if (PN->getType()->isFloatingPoint()) {
226 ContainsFPCode = true;
227 break;
228 }
229
Chris Lattner6fba62d62005-01-12 04:21:28 +0000230 // Insert FP_REG_KILL instructions into basic blocks that need them. This
231 // only occurs due to the floating point stackifier not being aggressive
232 // enough to handle arbitrary global stackification.
233 //
234 // Currently we insert an FP_REG_KILL instruction into each block that uses or
235 // defines a floating point virtual register.
236 //
237 // When the global register allocators (like linear scan) finally update live
238 // variable analysis, we can keep floating point values in registers across
239 // basic blocks. This will be a huge win, but we are waiting on the global
240 // allocators before we can do this.
241 //
Chris Lattner472a2652005-03-30 01:10:00 +0000242 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +0000243 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
244 ++NumFPKill;
245 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000246
Chris Lattner6fba62d62005-01-12 04:21:28 +0000247 // Clear state used for selection.
248 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000249 RegPressureMap.clear();
250}
251
252
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000253// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
254// for the number of registers required to compute each node. This is basically
255// computing a generalized form of the Sethi-Ullman number for each node.
256unsigned ISel::ComputeRegPressure(SDOperand O) {
257 SDNode *N = O.Val;
258 unsigned &Result = RegPressureMap[N];
259 if (Result) return Result;
260
Chris Lattner8fea42b2005-01-11 03:37:59 +0000261 // FIXME: Should operations like CALL (which clobber lots o regs) have a
262 // higher fixed cost??
263
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000264 if (N->getNumOperands() == 0) {
265 Result = 1;
266 } else {
267 unsigned MaxRegUse = 0;
268 unsigned NumExtraMaxRegUsers = 0;
269 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
270 unsigned Regs;
271 if (N->getOperand(i).getOpcode() == ISD::Constant)
272 Regs = 0;
273 else
274 Regs = ComputeRegPressure(N->getOperand(i));
275 if (Regs > MaxRegUse) {
276 MaxRegUse = Regs;
277 NumExtraMaxRegUsers = 0;
278 } else if (Regs == MaxRegUse &&
279 N->getOperand(i).getValueType() != MVT::Other) {
280 ++NumExtraMaxRegUsers;
281 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000282 }
Chris Lattnerca318ed2005-01-17 22:56:09 +0000283
284 if (O.getOpcode() != ISD::TokenFactor)
285 Result = MaxRegUse+NumExtraMaxRegUsers;
286 else
Chris Lattnera5d137f2005-01-17 23:02:13 +0000287 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000288 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +0000289
Chris Lattner75bac9f2005-01-11 23:21:30 +0000290 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000291 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000292}
293
Chris Lattner5b04f332005-01-20 16:50:16 +0000294/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
295/// The DAG cannot have cycles in it, by definition, so the visited set is not
296/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
297/// reuse, so it prevents exponential cases.
298///
299static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
300 std::set<SDNode*> &Visited) {
301 if (N == Op) return true; // Found it.
302 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +0000303 if (Node->getNumOperands() == 0 || // Leaf?
304 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +0000305 if (!Visited.insert(Node).second) return false; // Already visited?
306
307 // Recurse for the first N-1 operands.
308 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
309 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
310 return true;
311
312 // Tail recurse for the last operand.
313 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
314}
315
Chris Lattnera7acdda2005-01-18 01:06:26 +0000316X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
317 X86AddressMode Result;
318
319 // If we need to emit two register operands, emit the one with the highest
320 // register pressure first.
321 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
322 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000323 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000324 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000325 std::set<SDNode*> Visited;
326 EmitBaseThenIndex = true;
327 // If Base ends up pointing to Index, we must emit index first. This is
328 // because of the way we fold loads, we may end up doing bad things with
329 // the folded add.
330 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
331 EmitBaseThenIndex = false;
332 } else {
333 std::set<SDNode*> Visited;
334 EmitBaseThenIndex = false;
335 // If Base ends up pointing to Index, we must emit index first. This is
336 // because of the way we fold loads, we may end up doing bad things with
337 // the folded add.
338 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
339 EmitBaseThenIndex = true;
340 }
341
342 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000343 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
344 Result.IndexReg = SelectExpr(IAM.IndexReg);
345 } else {
346 Result.IndexReg = SelectExpr(IAM.IndexReg);
347 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
348 }
Chris Lattner5b04f332005-01-20 16:50:16 +0000349
Chris Lattnera7acdda2005-01-18 01:06:26 +0000350 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
351 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
352 } else if (IAM.IndexReg.Val) {
353 Result.IndexReg = SelectExpr(IAM.IndexReg);
354 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000355
Chris Lattnera7acdda2005-01-18 01:06:26 +0000356 switch (IAM.BaseType) {
357 case X86ISelAddressMode::RegBase:
358 Result.BaseType = X86AddressMode::RegBase;
359 break;
360 case X86ISelAddressMode::FrameIndexBase:
361 Result.BaseType = X86AddressMode::FrameIndexBase;
362 Result.Base.FrameIndex = IAM.Base.FrameIndex;
363 break;
364 default:
365 assert(0 && "Unknown base type!");
366 break;
367 }
368 Result.Scale = IAM.Scale;
369 Result.Disp = IAM.Disp;
370 Result.GV = IAM.GV;
371 return Result;
372}
373
374/// SelectAddress - Pattern match the maximal addressing mode for this node and
375/// emit all of the leaf registers.
376void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
377 X86ISelAddressMode IAM;
378 MatchAddress(N, IAM);
379 AM = SelectAddrExprs(IAM);
380}
381
382/// MatchAddress - Add the specified node to the specified addressing mode,
383/// returning true if it cannot be done. This just pattern matches for the
384/// addressing mode, it does not cause any code to be emitted. For that, use
385/// SelectAddress.
386bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000387 switch (N.getOpcode()) {
388 default: break;
389 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +0000390 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
391 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +0000392 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
393 return false;
394 }
395 break;
396 case ISD::GlobalAddress:
397 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +0000398 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
399 // For Darwin, external and weak symbols are indirect, so we want to load
400 // the value at address GV, not the value of GV itself. This means that
401 // the GlobalAddress must be in the base or index register of the address,
402 // not the GV offset field.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000403 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +0000404 (GV->hasWeakLinkage() || GV->isExternal())) {
405 break;
406 } else {
407 AM.GV = GV;
408 return false;
409 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000410 }
411 break;
412 case ISD::Constant:
413 AM.Disp += cast<ConstantSDNode>(N)->getValue();
414 return false;
415 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000416 // We might have folded the load into this shift, so don't regen the value
417 // if so.
418 if (ExprMap.count(N)) break;
419
Chris Lattnera7acdda2005-01-18 01:06:26 +0000420 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +0000421 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
422 unsigned Val = CN->getValue();
423 if (Val == 1 || Val == 2 || Val == 3) {
424 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000425 SDOperand ShVal = N.Val->getOperand(0);
426
427 // Okay, we know that we have a scale by now. However, if the scaled
428 // value is an add of something and a constant, we can fold the
429 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000430 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000431 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000432 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000433 ConstantSDNode *AddVal =
434 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
435 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +0000436 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000437 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000438 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000439 return false;
440 }
441 }
442 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000443 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000444 // We might have folded the load into this mul, so don't regen the value if
445 // so.
446 if (ExprMap.count(N)) break;
447
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000448 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +0000449 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
450 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000451 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
452 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
453 AM.Scale = unsigned(CN->getValue())-1;
454
455 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000456 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000457
458 // Okay, we know that we have a scale by now. However, if the scaled
459 // value is an add of something and a constant, we can fold the
460 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000461 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000462 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000463 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000464 ConstantSDNode *AddVal =
465 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
466 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +0000467 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000468 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000469 }
470
471 AM.IndexReg = AM.Base.Reg = Reg;
472 return false;
473 }
474 break;
Chris Lattner88c8a232005-01-07 07:49:41 +0000475
476 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +0000477 // We might have folded the load into this mul, so don't regen the value if
478 // so.
479 if (ExprMap.count(N)) break;
480
Chris Lattnera7acdda2005-01-18 01:06:26 +0000481 X86ISelAddressMode Backup = AM;
482 if (!MatchAddress(N.Val->getOperand(0), AM) &&
483 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +0000484 return false;
485 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000486 if (!MatchAddress(N.Val->getOperand(1), AM) &&
487 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +0000488 return false;
489 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +0000490 break;
491 }
492 }
493
Chris Lattner378262d2005-01-11 04:40:19 +0000494 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +0000495 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +0000496 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000497 if (AM.IndexReg.Val == 0) {
498 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +0000499 AM.Scale = 1;
500 return false;
501 }
502
503 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +0000504 return true;
Chris Lattner378262d2005-01-11 04:40:19 +0000505 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000506
507 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000508 AM.BaseType = X86ISelAddressMode::RegBase;
509 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +0000510 return false;
511}
512
513/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
514/// assuming that the temporary registers are in the 8-bit register class.
515///
516/// Tmp1 = setcc1
517/// Tmp2 = setcc2
518/// DestReg = logicalop Tmp1, Tmp2
519///
520static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
521 unsigned SetCC2, unsigned LogicalOp,
522 unsigned DestReg) {
523 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
524 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
525 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
526 BuildMI(BB, SetCC1, 0, Tmp1);
527 BuildMI(BB, SetCC2, 0, Tmp2);
528 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
529}
530
531/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
532/// condition codes match the specified SetCCOpcode. Note that some conditions
533/// require multiple instructions to generate the correct value.
534static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
535 ISD::CondCode SetCCOpcode, bool isFP) {
536 unsigned Opc;
537 if (!isFP) {
538 switch (SetCCOpcode) {
539 default: assert(0 && "Illegal integer SetCC!");
540 case ISD::SETEQ: Opc = X86::SETEr; break;
541 case ISD::SETGT: Opc = X86::SETGr; break;
542 case ISD::SETGE: Opc = X86::SETGEr; break;
543 case ISD::SETLT: Opc = X86::SETLr; break;
544 case ISD::SETLE: Opc = X86::SETLEr; break;
545 case ISD::SETNE: Opc = X86::SETNEr; break;
546 case ISD::SETULT: Opc = X86::SETBr; break;
547 case ISD::SETUGT: Opc = X86::SETAr; break;
548 case ISD::SETULE: Opc = X86::SETBEr; break;
549 case ISD::SETUGE: Opc = X86::SETAEr; break;
550 }
551 } else {
552 // On a floating point condition, the flags are set as follows:
553 // ZF PF CF op
554 // 0 | 0 | 0 | X > Y
555 // 0 | 0 | 1 | X < Y
556 // 1 | 0 | 0 | X == Y
557 // 1 | 1 | 1 | unordered
558 //
559 switch (SetCCOpcode) {
560 default: assert(0 && "Invalid FP setcc!");
561 case ISD::SETUEQ:
562 case ISD::SETEQ:
563 Opc = X86::SETEr; // True if ZF = 1
564 break;
565 case ISD::SETOGT:
566 case ISD::SETGT:
567 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
568 break;
569 case ISD::SETOGE:
570 case ISD::SETGE:
571 Opc = X86::SETAEr; // True if CF = 0
572 break;
573 case ISD::SETULT:
574 case ISD::SETLT:
575 Opc = X86::SETBr; // True if CF = 1
576 break;
577 case ISD::SETULE:
578 case ISD::SETLE:
579 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
580 break;
581 case ISD::SETONE:
582 case ISD::SETNE:
583 Opc = X86::SETNEr; // True if ZF = 0
584 break;
585 case ISD::SETUO:
586 Opc = X86::SETPr; // True if PF = 1
587 break;
588 case ISD::SETO:
589 Opc = X86::SETNPr; // True if PF = 0
590 break;
591 case ISD::SETOEQ: // !PF & ZF
592 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
593 return;
594 case ISD::SETOLT: // !PF & CF
595 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
596 return;
597 case ISD::SETOLE: // !PF & (CF || ZF)
598 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
599 return;
600 case ISD::SETUGT: // PF | (!ZF & !CF)
601 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
602 return;
603 case ISD::SETUGE: // PF | !CF
604 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
605 return;
606 case ISD::SETUNE: // PF | !ZF
607 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
608 return;
609 }
610 }
611 BuildMI(BB, Opc, 0, DestReg);
612}
613
614
615/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
616/// the Dest block if the Cond condition is true. If we cannot fold this
617/// condition into the branch, return true.
618///
Chris Lattner37ed2852005-01-11 04:06:27 +0000619bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
620 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000621 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
622 // B) using two conditional branches instead of one condbr, two setcc's, and
623 // an or.
624 if ((Cond.getOpcode() == ISD::OR ||
625 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
626 // And and or set the flags for us, so there is no need to emit a TST of the
627 // result. It is only safe to do this if there is only a single use of the
628 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +0000629 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000630 SelectExpr(Cond);
631 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
632 return false;
633 }
634
635 // Codegen br not C -> JE.
636 if (Cond.getOpcode() == ISD::XOR)
637 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
638 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +0000639 unsigned CondR;
640 if (getRegPressure(Chain) > getRegPressure(Cond)) {
641 Select(Chain);
642 CondR = SelectExpr(Cond.Val->getOperand(0));
643 } else {
644 CondR = SelectExpr(Cond.Val->getOperand(0));
645 Select(Chain);
646 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000647 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
648 BuildMI(BB, X86::JE, 1).addMBB(Dest);
649 return false;
650 }
651
Chris Lattner6ec77452005-08-09 20:21:10 +0000652 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner88c8a232005-01-07 07:49:41 +0000653 return true; // Can only handle simple setcc's so far.
Chris Lattner6ec77452005-08-09 20:21:10 +0000654 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner88c8a232005-01-07 07:49:41 +0000655
656 unsigned Opc;
657
658 // Handle integer conditions first.
Chris Lattner6ec77452005-08-09 20:21:10 +0000659 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
660 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000661 default: assert(0 && "Illegal integer SetCC!");
662 case ISD::SETEQ: Opc = X86::JE; break;
663 case ISD::SETGT: Opc = X86::JG; break;
664 case ISD::SETGE: Opc = X86::JGE; break;
665 case ISD::SETLT: Opc = X86::JL; break;
666 case ISD::SETLE: Opc = X86::JLE; break;
667 case ISD::SETNE: Opc = X86::JNE; break;
668 case ISD::SETULT: Opc = X86::JB; break;
669 case ISD::SETUGT: Opc = X86::JA; break;
670 case ISD::SETULE: Opc = X86::JBE; break;
671 case ISD::SETUGE: Opc = X86::JAE; break;
672 }
Chris Lattner37ed2852005-01-11 04:06:27 +0000673 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000674 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000675 BuildMI(BB, Opc, 1).addMBB(Dest);
676 return false;
677 }
678
Chris Lattner88c8a232005-01-07 07:49:41 +0000679 unsigned Opc2 = 0; // Second branch if needed.
680
681 // On a floating point condition, the flags are set as follows:
682 // ZF PF CF op
683 // 0 | 0 | 0 | X > Y
684 // 0 | 0 | 1 | X < Y
685 // 1 | 0 | 0 | X == Y
686 // 1 | 1 | 1 | unordered
687 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000688 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000689 default: assert(0 && "Invalid FP setcc!");
690 case ISD::SETUEQ:
691 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
692 case ISD::SETOGT:
693 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
694 case ISD::SETOGE:
695 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
696 case ISD::SETULT:
697 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
698 case ISD::SETULE:
699 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
700 case ISD::SETONE:
701 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
702 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
703 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
704 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
705 Opc = X86::JA; // ZF = 0 & CF = 0
706 Opc2 = X86::JP; // PF = 1
707 break;
708 case ISD::SETUGE: // PF = 1 | CF = 0
709 Opc = X86::JAE; // CF = 0
710 Opc2 = X86::JP; // PF = 1
711 break;
712 case ISD::SETUNE: // PF = 1 | ZF = 0
713 Opc = X86::JNE; // ZF = 0
714 Opc2 = X86::JP; // PF = 1
715 break;
716 case ISD::SETOEQ: // PF = 0 & ZF = 1
717 //X86::JNP, X86::JE
718 //X86::AND8rr
719 return true; // FIXME: Emit more efficient code for this branch.
720 case ISD::SETOLT: // PF = 0 & CF = 1
721 //X86::JNP, X86::JB
722 //X86::AND8rr
723 return true; // FIXME: Emit more efficient code for this branch.
724 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
725 //X86::JNP, X86::JBE
726 //X86::AND8rr
727 return true; // FIXME: Emit more efficient code for this branch.
728 }
729
Chris Lattner37ed2852005-01-11 04:06:27 +0000730 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000731 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000732 BuildMI(BB, Opc, 1).addMBB(Dest);
733 if (Opc2)
734 BuildMI(BB, Opc2, 1).addMBB(Dest);
735 return false;
736}
737
Chris Lattner1d13a922005-01-10 22:10:13 +0000738/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman8d394eb2005-08-03 23:26:28 +0000739/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner1d13a922005-01-10 22:10:13 +0000740///
Nate Begeman8d394eb2005-08-03 23:26:28 +0000741void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
742 MVT::ValueType SVT, unsigned RDest) {
743 unsigned RTrue, RFalse;
Chris Lattner1d13a922005-01-10 22:10:13 +0000744 enum Condition {
745 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
746 NOT_SET
747 } CondCode = NOT_SET;
748
749 static const unsigned CMOVTAB16[] = {
750 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
751 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000752 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000753 };
754 static const unsigned CMOVTAB32[] = {
755 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
756 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000757 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000758 };
759 static const unsigned CMOVTABFP[] = {
Chris Lattnerf431ad42005-12-21 07:47:04 +0000760 X86::FpCMOVE, X86::FpCMOVNE, /*missing*/0, /*missing*/0,
761 /*missing*/0, /*missing*/ 0, X86::FpCMOVB, X86::FpCMOVBE,
Evan Cheng468fecd2006-01-21 02:55:41 +0000762 X86::FpCMOVNBE,X86::FpCMOVNB, X86::FpCMOVP, X86::FpCMOVNP
Chris Lattner1d13a922005-01-10 22:10:13 +0000763 };
Nate Begemana0b5e032005-07-15 00:38:55 +0000764 static const int SSE_CMOVTAB[] = {
Nate Begeman8d394eb2005-08-03 23:26:28 +0000765 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
766 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
767 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begeman8a093362005-07-06 18:59:04 +0000768 };
Nate Begeman8d394eb2005-08-03 23:26:28 +0000769
Chris Lattner6ec77452005-08-09 20:21:10 +0000770 if (Cond.getOpcode() == ISD::SETCC) {
771 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
772 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
773 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000774 default: assert(0 && "Unknown integer comparison!");
775 case ISD::SETEQ: CondCode = EQ; break;
776 case ISD::SETGT: CondCode = GT; break;
777 case ISD::SETGE: CondCode = GE; break;
778 case ISD::SETLT: CondCode = LT; break;
779 case ISD::SETLE: CondCode = LE; break;
780 case ISD::SETNE: CondCode = NE; break;
781 case ISD::SETULT: CondCode = B; break;
782 case ISD::SETUGT: CondCode = A; break;
783 case ISD::SETULE: CondCode = BE; break;
784 case ISD::SETUGE: CondCode = AE; break;
785 }
786 } else {
787 // On a floating point condition, the flags are set as follows:
788 // ZF PF CF op
789 // 0 | 0 | 0 | X > Y
790 // 0 | 0 | 1 | X < Y
791 // 1 | 0 | 0 | X == Y
792 // 1 | 1 | 1 | unordered
793 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000794 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000795 default: assert(0 && "Unknown FP comparison!");
796 case ISD::SETUEQ:
797 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
798 case ISD::SETOGT:
799 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
800 case ISD::SETOGE:
801 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
802 case ISD::SETULT:
803 case ISD::SETLT: CondCode = B; break; // True if CF = 1
804 case ISD::SETULE:
805 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
806 case ISD::SETONE:
807 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
808 case ISD::SETUO: CondCode = P; break; // True if PF = 1
809 case ISD::SETO: CondCode = NP; break; // True if PF = 0
810 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
811 case ISD::SETUGE: // PF = 1 | CF = 0
812 case ISD::SETUNE: // PF = 1 | ZF = 0
813 case ISD::SETOEQ: // PF = 0 & ZF = 1
814 case ISD::SETOLT: // PF = 0 & CF = 1
815 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
816 // We cannot emit this comparison as a single cmov.
817 break;
818 }
819 }
Chris Lattner6ec77452005-08-09 20:21:10 +0000820
Chris Lattner1d13a922005-01-10 22:10:13 +0000821
Chris Lattner6ec77452005-08-09 20:21:10 +0000822 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
823 // code above and one of the results of the select is +0.0, then we can fake
824 // it up through a clever AND with mask. Otherwise, we will fall through to
825 // the code below that will use a PHI node to select the right value.
826 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
827 if (Cond.getOperand(0).getValueType() == SVT &&
828 NOT_SET != CondCode) {
829 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
830 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
831 bool TrueZero = CT && CT->isExactlyValue(0.0);
832 bool FalseZero = CF && CF->isExactlyValue(0.0);
833 if (TrueZero || FalseZero) {
834 SDOperand LHS = Cond.getOperand(0);
835 SDOperand RHS = Cond.getOperand(1);
836
837 // Select the two halves of the condition
838 unsigned RLHS, RRHS;
839 if (getRegPressure(LHS) > getRegPressure(RHS)) {
840 RLHS = SelectExpr(LHS);
841 RRHS = SelectExpr(RHS);
842 } else {
843 RRHS = SelectExpr(RHS);
844 RLHS = SelectExpr(LHS);
845 }
846
847 // Emit the comparison and generate a mask from it
848 unsigned MaskReg = MakeReg(SVT);
849 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
850 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
851 .addImm(SSE_CMOVTAB[CondCode]);
852
853 if (TrueZero) {
854 RFalse = SelectExpr(False);
855 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
856 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
857 } else {
858 RTrue = SelectExpr(True);
859 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
860 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
861 }
862 return;
Nate Begeman8d394eb2005-08-03 23:26:28 +0000863 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000864 }
Nate Begeman8a093362005-07-06 18:59:04 +0000865 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000866 }
867
868 // Select the true and false values for use in both the SSE PHI case, and the
869 // integer or x87 cmov cases below.
870 if (getRegPressure(True) > getRegPressure(False)) {
871 RTrue = SelectExpr(True);
872 RFalse = SelectExpr(False);
873 } else {
874 RFalse = SelectExpr(False);
875 RTrue = SelectExpr(True);
876 }
877
878 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
879 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
880 // node to select between the true and false values.
881 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
882 // FIXME: emit a direct compare and branch rather than setting a cond reg
883 // and testing it.
884 unsigned CondReg = SelectExpr(Cond);
885 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
886
887 // Create an iterator with which to insert the MBB for copying the false
888 // value and the MBB to hold the PHI instruction for this SetCC.
889 MachineBasicBlock *thisMBB = BB;
890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
891 ilist<MachineBasicBlock>::iterator It = BB;
892 ++It;
893
894 // thisMBB:
895 // ...
896 // TrueVal = ...
897 // cmpTY ccX, r1, r2
898 // bCC sinkMBB
899 // fallthrough --> copy0MBB
900 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
901 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
902 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
903 MachineFunction *F = BB->getParent();
904 F->getBasicBlockList().insert(It, copy0MBB);
905 F->getBasicBlockList().insert(It, sinkMBB);
906 // Update machine-CFG edges
907 BB->addSuccessor(copy0MBB);
908 BB->addSuccessor(sinkMBB);
909
910 // copy0MBB:
911 // %FalseValue = ...
912 // # fallthrough to sinkMBB
913 BB = copy0MBB;
914 // Update machine-CFG edges
915 BB->addSuccessor(sinkMBB);
916
917 // sinkMBB:
918 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
919 // ...
920 BB = sinkMBB;
921 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
922 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begeman8a093362005-07-06 18:59:04 +0000923 return;
924 }
925
Chris Lattner1d13a922005-01-10 22:10:13 +0000926 unsigned Opc = 0;
927 if (CondCode != NOT_SET) {
928 switch (SVT) {
929 default: assert(0 && "Cannot select this type!");
930 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
931 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +0000932 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000933 }
934 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000935
Chris Lattner1d13a922005-01-10 22:10:13 +0000936 // Finally, if we weren't able to fold this, just emit the condition and test
937 // it.
938 if (CondCode == NOT_SET || Opc == 0) {
939 // Get the condition into the zero flag.
940 unsigned CondReg = SelectExpr(Cond);
941 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
942
943 switch (SVT) {
944 default: assert(0 && "Cannot select this type!");
945 case MVT::i16: Opc = X86::CMOVE16rr; break;
946 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +0000947 case MVT::f64: Opc = X86::FpCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000948 }
949 } else {
950 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +0000951 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +0000952 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +0000953 }
954 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
955}
956
Chris Lattner3be6cd52005-01-17 01:34:14 +0000957void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000958 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +0000959 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
960 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +0000961 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +0000962 switch (RHS.getValueType()) {
963 default: break;
964 case MVT::i1:
965 case MVT::i8: Opc = X86::CMP8mi; break;
966 case MVT::i16: Opc = X86::CMP16mi; break;
967 case MVT::i32: Opc = X86::CMP32mi; break;
968 }
969 if (Opc) {
970 X86AddressMode AM;
971 EmitFoldedLoad(LHS, AM);
972 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
973 return;
974 }
975 }
976
Chris Lattner88c8a232005-01-07 07:49:41 +0000977 switch (RHS.getValueType()) {
978 default: break;
979 case MVT::i1:
980 case MVT::i8: Opc = X86::CMP8ri; break;
981 case MVT::i16: Opc = X86::CMP16ri; break;
982 case MVT::i32: Opc = X86::CMP32ri; break;
983 }
984 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000985 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +0000986 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
987 return;
988 }
Chris Lattner720a62e2005-01-14 22:37:41 +0000989 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +0000990 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
991 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +0000992 unsigned Reg = SelectExpr(LHS);
Chris Lattnerf431ad42005-12-21 07:47:04 +0000993 BuildMI(BB, X86::FpTST, 1).addReg(Reg);
Chris Lattner720a62e2005-01-14 22:37:41 +0000994 BuildMI(BB, X86::FNSTSW8r, 0);
995 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +0000996 return;
Chris Lattner720a62e2005-01-14 22:37:41 +0000997 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000998 }
999
Chris Lattner2cfce682005-01-12 02:02:48 +00001000 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001001 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001002 switch (RHS.getValueType()) {
1003 default: break;
1004 case MVT::i1:
1005 case MVT::i8: Opc = X86::CMP8mr; break;
1006 case MVT::i16: Opc = X86::CMP16mr; break;
1007 case MVT::i32: Opc = X86::CMP32mr; break;
1008 }
1009 if (Opc) {
1010 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001011 EmitFoldedLoad(LHS, AM);
1012 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001013 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1014 return;
1015 }
1016 }
1017
Chris Lattner88c8a232005-01-07 07:49:41 +00001018 switch (LHS.getValueType()) {
1019 default: assert(0 && "Cannot compare this value!");
1020 case MVT::i1:
1021 case MVT::i8: Opc = X86::CMP8rr; break;
1022 case MVT::i16: Opc = X86::CMP16rr; break;
1023 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001024 case MVT::f32: Opc = X86::UCOMISSrr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00001025 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FpUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001026 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001027 unsigned Tmp1, Tmp2;
1028 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1029 Tmp1 = SelectExpr(LHS);
1030 Tmp2 = SelectExpr(RHS);
1031 } else {
1032 Tmp2 = SelectExpr(RHS);
1033 Tmp1 = SelectExpr(LHS);
1034 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001035 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1036}
1037
Chris Lattner62b22422005-01-11 21:19:59 +00001038/// isFoldableLoad - Return true if this is a load instruction that can safely
1039/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001040bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1041 if (Op.getOpcode() == ISD::LOAD) {
1042 // FIXME: currently can't fold constant pool indexes.
1043 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1044 return false;
1045 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00001046 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001047 // FIXME: currently can't fold constant pool indexes.
1048 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1049 return false;
1050 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001051 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001052 }
Chris Lattner62b22422005-01-11 21:19:59 +00001053
1054 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001055 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1056 if (ExprMap.count(Op.getValue(1))) return false;
1057 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001058 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001059
Chris Lattnera56d29d2005-01-17 06:26:58 +00001060 // If there is not just one use of its value, we cannot fold.
1061 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1062
1063 // Finally, we cannot fold the load into the operation if this would induce a
1064 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1065 // operand of the operation we are folding the load into) can possible use the
1066 // chain node defined by the load.
1067 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1068 std::set<SDNode*> Visited;
1069 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1070 return false;
1071 }
1072 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001073}
1074
Chris Lattnera56d29d2005-01-17 06:26:58 +00001075
Chris Lattner62b22422005-01-11 21:19:59 +00001076/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1077/// and compute the address being loaded into AM.
1078void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1079 SDOperand Chain = Op.getOperand(0);
1080 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001081
Chris Lattner62b22422005-01-11 21:19:59 +00001082 if (getRegPressure(Chain) > getRegPressure(Address)) {
1083 Select(Chain);
1084 SelectAddress(Address, AM);
1085 } else {
1086 SelectAddress(Address, AM);
1087 Select(Chain);
1088 }
1089
1090 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001091 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1092 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00001093 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00001094 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00001095}
1096
Chris Lattner29f58192005-01-19 07:37:26 +00001097// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1098// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1099// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1100// return true.
1101bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001102 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1103 // good!
1104 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1105 std::swap(Op1, Op2); // Op1 is the SHL now.
1106 } else {
1107 return false; // No match
1108 }
1109
1110 SDOperand ShlVal = Op1.getOperand(0);
1111 SDOperand ShlAmt = Op1.getOperand(1);
1112 SDOperand ShrVal = Op2.getOperand(0);
1113 SDOperand ShrAmt = Op2.getOperand(1);
1114
Chris Lattner29f58192005-01-19 07:37:26 +00001115 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1116
Chris Lattner41fe2012005-01-19 06:18:43 +00001117 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1118 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1119 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00001120 if (SubCST->getValue() == RegSize) {
1121 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00001122 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00001123 if (ShrVal == ShlVal) {
1124 unsigned Reg, ShAmt;
1125 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1126 Reg = SelectExpr(ShrVal);
1127 ShAmt = SelectExpr(ShrAmt);
1128 } else {
1129 ShAmt = SelectExpr(ShrAmt);
1130 Reg = SelectExpr(ShrVal);
1131 }
1132 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1133 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1134 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1135 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1136 return true;
1137 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001138 unsigned AReg, BReg;
1139 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001140 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001141 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001142 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00001143 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001144 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001145 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00001146 unsigned ShAmt = SelectExpr(ShrAmt);
1147 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1148 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1149 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00001150 return true;
1151 }
1152 }
1153
Chris Lattnerde87d1462005-01-19 08:07:05 +00001154 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1155 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1156 if (SubCST->getValue() == RegSize) {
1157 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1158 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1159 if (ShrVal == ShlVal) {
1160 unsigned Reg, ShAmt;
1161 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1162 Reg = SelectExpr(ShrVal);
1163 ShAmt = SelectExpr(ShlAmt);
1164 } else {
1165 ShAmt = SelectExpr(ShlAmt);
1166 Reg = SelectExpr(ShrVal);
1167 }
1168 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1169 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1170 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1171 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1172 return true;
1173 } else if (RegSize != 8) {
1174 unsigned AReg, BReg;
1175 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00001176 AReg = SelectExpr(ShlVal);
1177 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001178 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00001179 BReg = SelectExpr(ShrVal);
1180 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001181 }
1182 unsigned ShAmt = SelectExpr(ShlAmt);
1183 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1184 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1185 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1186 return true;
1187 }
1188 }
Chris Lattner41fe2012005-01-19 06:18:43 +00001189
Chris Lattnerde87d1462005-01-19 08:07:05 +00001190 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1191 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1192 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1193 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1194 // (A >> 5) | (A << 27) --> ROR A, 5
1195 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1196 if (ShrVal == ShlVal) {
1197 unsigned Reg = SelectExpr(ShrVal);
1198 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1199 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1200 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1201 return true;
1202 } else if (RegSize != 8) {
1203 unsigned AReg, BReg;
1204 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001205 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001206 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001207 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001208 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001209 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001210 }
1211 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1212 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1213 .addImm(ShrCst->getValue());
1214 return true;
1215 }
1216 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001217
Chris Lattner41fe2012005-01-19 06:18:43 +00001218 return false;
1219}
1220
Chris Lattner88c8a232005-01-07 07:49:41 +00001221unsigned ISel::SelectExpr(SDOperand N) {
1222 unsigned Result;
Chris Lattner9982da22005-10-02 16:29:36 +00001223 unsigned Tmp1 = 0, Tmp2 = 0, Tmp3 = 0, Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001224 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00001225 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001226
Evan Chengbc7a0f442006-01-11 06:09:51 +00001227 if (Node->getOpcode() == ISD::CopyFromReg ||
1228 Node->getOpcode() == ISD::Register) {
1229 unsigned Reg = (Node->getOpcode() == ISD::CopyFromReg) ?
1230 cast<RegisterSDNode>(Node->getOperand(1))->getReg() :
1231 cast<RegisterSDNode>(Node)->getReg();
Chris Lattner7c762782005-08-16 21:56:37 +00001232 // Just use the specified register as our input if we can.
Chris Lattner5f9c1342006-01-13 20:19:44 +00001233 if (Node->getOpcode() == ISD::Register ||
1234 MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner7c762782005-08-16 21:56:37 +00001235 return Reg;
Evan Chengbc7a0f442006-01-11 06:09:51 +00001236 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001237
Chris Lattner62b22422005-01-11 21:19:59 +00001238 unsigned &Reg = ExprMap[N];
1239 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001240
Chris Lattnera31d4c72005-04-02 04:01:14 +00001241 switch (N.getOpcode()) {
1242 default:
Chris Lattner62b22422005-01-11 21:19:59 +00001243 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00001244 MakeReg(N.getValueType()) : 1;
1245 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00001246 case X86ISD::TAILCALL:
1247 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00001248 // If this is a call instruction, make sure to prepare ALL of the result
1249 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00001250 ExprMap[N.getValue(0)] = 1;
1251 if (Node->getNumValues() > 1) {
1252 Result = MakeReg(Node->getValueType(1));
1253 ExprMap[N.getValue(1)] = Result;
1254 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00001255 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00001256 } else {
1257 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00001258 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00001259 break;
1260 case ISD::ADD_PARTS:
1261 case ISD::SUB_PARTS:
1262 case ISD::SHL_PARTS:
1263 case ISD::SRL_PARTS:
1264 case ISD::SRA_PARTS:
1265 Result = MakeReg(Node->getValueType(0));
1266 ExprMap[N.getValue(0)] = Result;
1267 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1268 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1269 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001270 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001271
Chris Lattner88c8a232005-01-07 07:49:41 +00001272 switch (N.getOpcode()) {
1273 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00001274 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00001275 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00001276 case ISD::FP_EXTEND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001277 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman8a093362005-07-06 18:59:04 +00001278 Tmp1 = SelectExpr(N.getOperand(0));
1279 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
1280 return Result;
Nate Begemana0b5e032005-07-15 00:38:55 +00001281 case ISD::FP_ROUND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001282 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemana0b5e032005-07-15 00:38:55 +00001283 Tmp1 = SelectExpr(N.getOperand(0));
1284 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
1285 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001286 case ISD::CopyFromReg:
1287 Select(N.getOperand(0));
1288 if (Result == 1) {
1289 Reg = Result = ExprMap[N.getValue(0)] =
1290 MakeReg(N.getValue(0).getValueType());
1291 }
Chris Lattner7c762782005-08-16 21:56:37 +00001292 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001293 switch (Node->getValueType(0)) {
1294 default: assert(0 && "Cannot CopyFromReg this!");
1295 case MVT::i1:
1296 case MVT::i8:
Chris Lattner7c762782005-08-16 21:56:37 +00001297 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001298 return Result;
1299 case MVT::i16:
Chris Lattner7c762782005-08-16 21:56:37 +00001300 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001301 return Result;
1302 case MVT::i32:
Chris Lattner7c762782005-08-16 21:56:37 +00001303 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001304 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001305 }
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001306
Chris Lattner88c8a232005-01-07 07:49:41 +00001307 case ISD::FrameIndex:
1308 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1309 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1310 return Result;
1311 case ISD::ConstantPool:
Chris Lattnerc30405e2005-08-26 17:15:30 +00001312 Tmp1 = BB->getParent()->getConstantPool()->
1313 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner88c8a232005-01-07 07:49:41 +00001314 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1315 return Result;
1316 case ISD::ConstantFP:
Nate Begeman8d394eb2005-08-03 23:26:28 +00001317 if (X86ScalarSSE) {
1318 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
1319 "SSE only supports +0.0");
1320 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
1321 BuildMI(BB, Opc, 0, Result);
1322 return Result;
1323 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001324 ContainsFPCode = true;
1325 Tmp1 = Result; // Intermediate Register
1326 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1327 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1328 Tmp1 = MakeReg(MVT::f64);
1329
1330 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1331 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
Chris Lattnerf431ad42005-12-21 07:47:04 +00001332 BuildMI(BB, X86::FpLD0, 0, Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001333 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1334 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
Chris Lattnerf431ad42005-12-21 07:47:04 +00001335 BuildMI(BB, X86::FpLD1, 0, Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001336 else
1337 assert(0 && "Unexpected constant!");
1338 if (Tmp1 != Result)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001339 BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001340 return Result;
1341 case ISD::Constant:
1342 switch (N.getValueType()) {
1343 default: assert(0 && "Cannot use constants of this type!");
1344 case MVT::i1:
1345 case MVT::i8: Opc = X86::MOV8ri; break;
1346 case MVT::i16: Opc = X86::MOV16ri; break;
1347 case MVT::i32: Opc = X86::MOV32ri; break;
1348 }
1349 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1350 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001351 case ISD::UNDEF:
1352 if (Node->getValueType(0) == MVT::f64) {
1353 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
Chris Lattnerf431ad42005-12-21 07:47:04 +00001354 BuildMI(BB, X86::FpLD0, 0, Result);
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001355 } else {
1356 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1357 }
1358 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001359 case ISD::GlobalAddress: {
1360 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00001361 // For Darwin, external and weak symbols are indirect, so we want to load
1362 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001363 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001364 (GV->hasWeakLinkage() || GV->isExternal())) {
1365 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
1366 .addGlobalAddress(GV, false, 0);
1367 } else {
1368 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1369 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001370 return Result;
1371 }
1372 case ISD::ExternalSymbol: {
1373 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1374 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1375 return Result;
1376 }
Chris Lattner210975c2005-09-02 00:16:09 +00001377 case ISD::ANY_EXTEND: // treat any extend like zext
Chris Lattner88c8a232005-01-07 07:49:41 +00001378 case ISD::ZERO_EXTEND: {
1379 int DestIs16 = N.getValueType() == MVT::i16;
1380 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001381
1382 // FIXME: This hack is here for zero extension casts from bool to i8. This
1383 // would not be needed if bools were promoted by Legalize.
1384 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001385 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001386 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1387 return Result;
1388 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001389
Chris Lattnera56d29d2005-01-17 06:26:58 +00001390 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001391 static const unsigned Opc[3] = {
1392 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1393 };
1394
1395 X86AddressMode AM;
1396 EmitFoldedLoad(N.getOperand(0), AM);
1397 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001398
Chris Lattnerb0eef822005-01-11 23:33:00 +00001399 return Result;
1400 }
1401
Chris Lattner88c8a232005-01-07 07:49:41 +00001402 static const unsigned Opc[3] = {
1403 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1404 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001405 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001406 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1407 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001408 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001409 case ISD::SIGN_EXTEND: {
1410 int DestIs16 = N.getValueType() == MVT::i16;
1411 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1412
Chris Lattner282781c2005-01-09 18:52:44 +00001413 // FIXME: Legalize should promote bools to i8!
1414 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1415 "Sign extend from bool not implemented!");
1416
Chris Lattnera56d29d2005-01-17 06:26:58 +00001417 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001418 static const unsigned Opc[3] = {
1419 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1420 };
1421
1422 X86AddressMode AM;
1423 EmitFoldedLoad(N.getOperand(0), AM);
1424 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1425 return Result;
1426 }
1427
Chris Lattner88c8a232005-01-07 07:49:41 +00001428 static const unsigned Opc[3] = {
1429 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1430 };
1431 Tmp1 = SelectExpr(N.getOperand(0));
1432 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1433 return Result;
1434 }
1435 case ISD::TRUNCATE:
1436 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1437 // a move out of AX or AL.
1438 switch (N.getOperand(0).getValueType()) {
1439 default: assert(0 && "Unknown truncate!");
1440 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1441 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1442 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1443 }
1444 Tmp1 = SelectExpr(N.getOperand(0));
1445 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1446
1447 switch (N.getValueType()) {
1448 default: assert(0 && "Unknown truncate!");
1449 case MVT::i1:
1450 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1451 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1452 }
1453 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1454 return Result;
1455
Chris Lattner507a2752005-07-16 00:28:20 +00001456 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00001457 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1458 unsigned PromoteOpcode = 0;
1459
Nate Begeman7e74c832005-07-16 02:02:34 +00001460 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00001461 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00001462 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00001463 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1464 return Result;
1465 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001466
Chris Lattnere44e6d12005-01-11 03:50:45 +00001467 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00001468
Chris Lattner282781c2005-01-09 18:52:44 +00001469 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00001470 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00001471 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1472 MachineFunction *F = BB->getParent();
1473 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1474
1475 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00001476 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00001477 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001478 addFrameReference(BuildMI(BB, X86::FpILD32m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001479 break;
1480 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00001481 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001482 addFrameReference(BuildMI(BB, X86::FpILD16m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001483 break;
1484 default: break; // No promotion required.
1485 }
Chris Lattner507a2752005-07-16 00:28:20 +00001486 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00001487 }
Chris Lattner4738d1b2005-07-30 00:05:54 +00001488 case ISD::FP_TO_SINT:
Chris Lattner282781c2005-01-09 18:52:44 +00001489 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1490
Nate Begeman8a093362005-07-06 18:59:04 +00001491 // If the target supports SSE2 and is performing FP operations in SSE regs
1492 // instead of the FP stack, then we can use the efficient CVTSS2SI and
1493 // CVTSD2SI instructions.
Chris Lattner4738d1b2005-07-30 00:05:54 +00001494 assert(X86ScalarSSE);
1495 if (MVT::f32 == N.getOperand(0).getValueType()) {
1496 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
1497 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
1498 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
1499 } else {
1500 assert(0 && "Not an f32 or f64?");
1501 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001502 }
Chris Lattner282781c2005-01-09 18:52:44 +00001503 return Result;
Chris Lattner4738d1b2005-07-30 00:05:54 +00001504
Chris Lattner0815dcae2005-09-28 22:29:17 +00001505 case ISD::FADD:
Chris Lattner88c8a232005-01-07 07:49:41 +00001506 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00001507 Op0 = N.getOperand(0);
1508 Op1 = N.getOperand(1);
1509
Chris Lattner30607ec2005-01-25 20:03:11 +00001510 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00001511 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001512 goto FoldAdd;
1513 }
Chris Lattner62b22422005-01-11 21:19:59 +00001514
Chris Lattner30607ec2005-01-25 20:03:11 +00001515 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001516 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00001517 switch (N.getValueType()) {
1518 default: assert(0 && "Cannot add this type!");
1519 case MVT::i1:
1520 case MVT::i8: Opc = X86::ADD8rm; break;
1521 case MVT::i16: Opc = X86::ADD16rm; break;
1522 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001523 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001524 case MVT::f64:
1525 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00001526 if (X86ScalarSSE) {
1527 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
1528 Opc = X86::ADDSDrm;
1529 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001530 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FpADD64m : X86::FpADD32m;
Nate Begeman8a093362005-07-06 18:59:04 +00001531 }
Chris Lattner30607ec2005-01-25 20:03:11 +00001532 break;
Chris Lattner62b22422005-01-11 21:19:59 +00001533 }
1534 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001535 EmitFoldedLoad(Op1, AM);
1536 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001537 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1538 return Result;
1539 }
1540
Chris Lattner88c8a232005-01-07 07:49:41 +00001541 // See if we can codegen this as an LEA to fold operations together.
1542 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00001543 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001544 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00001545 MatchAddress(N, AM);
1546 ExprMap[N] = Result;
1547
1548 // If this is not just an add, emit the LEA. For a simple add (like
1549 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1550 // leave this as LEA, then peephole it to 'ADD' after two address elim
1551 // happens.
1552 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1553 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1554 X86AddressMode XAM = SelectAddrExprs(AM);
1555 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1556 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001557 }
1558 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001559
Chris Lattner62b22422005-01-11 21:19:59 +00001560 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001561 Opc = 0;
1562 if (CN->getValue() == 1) { // add X, 1 -> inc X
1563 switch (N.getValueType()) {
1564 default: assert(0 && "Cannot integer add this type!");
1565 case MVT::i8: Opc = X86::INC8r; break;
1566 case MVT::i16: Opc = X86::INC16r; break;
1567 case MVT::i32: Opc = X86::INC32r; break;
1568 }
1569 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1570 switch (N.getValueType()) {
1571 default: assert(0 && "Cannot integer add this type!");
1572 case MVT::i8: Opc = X86::DEC8r; break;
1573 case MVT::i16: Opc = X86::DEC16r; break;
1574 case MVT::i32: Opc = X86::DEC32r; break;
1575 }
1576 }
1577
1578 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001579 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001580 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1581 return Result;
1582 }
1583
1584 switch (N.getValueType()) {
1585 default: assert(0 && "Cannot add this type!");
1586 case MVT::i8: Opc = X86::ADD8ri; break;
1587 case MVT::i16: Opc = X86::ADD16ri; break;
1588 case MVT::i32: Opc = X86::ADD32ri; break;
1589 }
1590 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001591 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001592 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1593 return Result;
1594 }
1595 }
1596
Chris Lattner88c8a232005-01-07 07:49:41 +00001597 switch (N.getValueType()) {
1598 default: assert(0 && "Cannot add this type!");
1599 case MVT::i8: Opc = X86::ADD8rr; break;
1600 case MVT::i16: Opc = X86::ADD16rr; break;
1601 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001602 case MVT::f32: Opc = X86::ADDSSrr; break;
1603 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001604 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001605
Chris Lattner62b22422005-01-11 21:19:59 +00001606 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1607 Tmp1 = SelectExpr(Op0);
1608 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001609 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001610 Tmp2 = SelectExpr(Op1);
1611 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001612 }
1613
Chris Lattner88c8a232005-01-07 07:49:41 +00001614 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1615 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00001616
Nate Begeman8a093362005-07-06 18:59:04 +00001617 case ISD::FSQRT:
1618 Tmp1 = SelectExpr(Node->getOperand(0));
1619 if (X86ScalarSSE) {
1620 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
1621 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1622 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001623 BuildMI(BB, X86::FpSQRT, 1, Result).addReg(Tmp1);
Nate Begeman8a093362005-07-06 18:59:04 +00001624 }
1625 return Result;
1626
1627 // FIXME:
1628 // Once we can spill 16 byte constants into the constant pool, we can
1629 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00001630 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00001631 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00001632 case ISD::FSIN:
1633 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00001634 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00001635 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00001636 switch (N.getOpcode()) {
1637 default: assert(0 && "Unreachable!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00001638 case ISD::FABS: BuildMI(BB, X86::FpABS, 1, Result).addReg(Tmp1); break;
1639 case ISD::FNEG: BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1); break;
1640 case ISD::FSIN: BuildMI(BB, X86::FpSIN, 1, Result).addReg(Tmp1); break;
1641 case ISD::FCOS: BuildMI(BB, X86::FpCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00001642 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00001643 return Result;
1644
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001645 case ISD::MULHU:
1646 switch (N.getValueType()) {
1647 default: assert(0 && "Unsupported VT!");
1648 case MVT::i8: Tmp2 = X86::MUL8r; break;
1649 case MVT::i16: Tmp2 = X86::MUL16r; break;
1650 case MVT::i32: Tmp2 = X86::MUL32r; break;
1651 }
1652 // FALL THROUGH
1653 case ISD::MULHS: {
1654 unsigned MovOpc, LowReg, HiReg;
1655 switch (N.getValueType()) {
1656 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00001657 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001658 MovOpc = X86::MOV8rr;
1659 LowReg = X86::AL;
1660 HiReg = X86::AH;
1661 Opc = X86::IMUL8r;
1662 break;
1663 case MVT::i16:
1664 MovOpc = X86::MOV16rr;
1665 LowReg = X86::AX;
1666 HiReg = X86::DX;
1667 Opc = X86::IMUL16r;
1668 break;
1669 case MVT::i32:
1670 MovOpc = X86::MOV32rr;
1671 LowReg = X86::EAX;
1672 HiReg = X86::EDX;
1673 Opc = X86::IMUL32r;
1674 break;
1675 }
1676 if (Node->getOpcode() != ISD::MULHS)
1677 Opc = Tmp2; // Get the MULHU opcode.
1678
1679 Op0 = Node->getOperand(0);
1680 Op1 = Node->getOperand(1);
1681 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1682 Tmp1 = SelectExpr(Op0);
1683 Tmp2 = SelectExpr(Op1);
1684 } else {
1685 Tmp2 = SelectExpr(Op1);
1686 Tmp1 = SelectExpr(Op0);
1687 }
1688
1689 // FIXME: Implement folding of loads into the memory operands here!
1690 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1691 BuildMI(BB, Opc, 1).addReg(Tmp2);
1692 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1693 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001694 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001695
Chris Lattner0815dcae2005-09-28 22:29:17 +00001696 case ISD::FSUB:
1697 case ISD::FMUL:
Chris Lattner88c8a232005-01-07 07:49:41 +00001698 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00001699 case ISD::MUL:
1700 case ISD::AND:
1701 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00001702 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00001703 static const unsigned SUBTab[] = {
1704 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001705 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FpSUB32m, X86::FpSUB64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001706 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1707 };
Nate Begeman8a093362005-07-06 18:59:04 +00001708 static const unsigned SSE_SUBTab[] = {
1709 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1710 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
1711 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
1712 };
Chris Lattner62b22422005-01-11 21:19:59 +00001713 static const unsigned MULTab[] = {
1714 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001715 0, X86::IMUL16rm , X86::IMUL32rm, X86::FpMUL32m, X86::FpMUL64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001716 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1717 };
Nate Begeman8a093362005-07-06 18:59:04 +00001718 static const unsigned SSE_MULTab[] = {
1719 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1720 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
1721 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
1722 };
Chris Lattner62b22422005-01-11 21:19:59 +00001723 static const unsigned ANDTab[] = {
1724 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1725 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001726 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00001727 };
1728 static const unsigned ORTab[] = {
1729 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1730 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1731 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1732 };
1733 static const unsigned XORTab[] = {
1734 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1735 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1736 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1737 };
1738
1739 Op0 = Node->getOperand(0);
1740 Op1 = Node->getOperand(1);
1741
Chris Lattner29f58192005-01-19 07:37:26 +00001742 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1743 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00001744 return Result;
1745
1746 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00001747 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1748 if (CN->isNullValue()) { // 0 - N -> neg N
1749 switch (N.getValueType()) {
1750 default: assert(0 && "Cannot sub this type!");
1751 case MVT::i1:
1752 case MVT::i8: Opc = X86::NEG8r; break;
1753 case MVT::i16: Opc = X86::NEG16r; break;
1754 case MVT::i32: Opc = X86::NEG32r; break;
1755 }
1756 Tmp1 = SelectExpr(N.getOperand(1));
1757 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1758 return Result;
1759 }
1760
Chris Lattner62b22422005-01-11 21:19:59 +00001761 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1762 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001763 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00001764 switch (N.getValueType()) {
1765 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001766 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00001767 case MVT::i8: Opc = X86::NOT8r; break;
1768 case MVT::i16: Opc = X86::NOT16r; break;
1769 case MVT::i32: Opc = X86::NOT32r; break;
1770 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001771 if (Opc) {
1772 Tmp1 = SelectExpr(Op0);
1773 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1774 return Result;
1775 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00001776 }
1777
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001778 // Fold common multiplies into LEA instructions.
1779 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1780 switch ((int)CN->getValue()) {
1781 default: break;
1782 case 3:
1783 case 5:
1784 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001785 // Remove N from exprmap so SelectAddress doesn't get confused.
1786 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001787 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001788 SelectAddress(N, AM);
1789 // Restore it to the map.
1790 ExprMap[N] = Result;
1791 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1792 return Result;
1793 }
1794 }
1795
Chris Lattner88c8a232005-01-07 07:49:41 +00001796 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00001797 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00001798 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00001799 case MVT::i8: Opc = 0; break;
1800 case MVT::i16: Opc = 1; break;
1801 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001802 }
Chris Lattner62b22422005-01-11 21:19:59 +00001803 switch (Node->getOpcode()) {
1804 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001805 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001806 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001807 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001808 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001809 case ISD::AND: Opc = ANDTab[Opc]; break;
1810 case ISD::OR: Opc = ORTab[Opc]; break;
1811 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001812 }
Chris Lattner62b22422005-01-11 21:19:59 +00001813 if (Opc) { // Can't fold MUL:i8 R, imm
1814 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001815 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1816 return Result;
1817 }
1818 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001819
Chris Lattner30607ec2005-01-25 20:03:11 +00001820 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner0815dcae2005-09-28 22:29:17 +00001821 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
Chris Lattner62b22422005-01-11 21:19:59 +00001822 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001823 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00001824 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00001825 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00001826 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001827 if (Op0.getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001828 Opc = X86::FpSUBR32m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001829 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00001830 Opc = X86::FpSUBR64m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001831
Chris Lattner62b22422005-01-11 21:19:59 +00001832 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001833 EmitFoldedLoad(Op0, AM);
1834 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00001835 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1836 return Result;
1837 }
1838 }
1839
Chris Lattner30607ec2005-01-25 20:03:11 +00001840 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001841 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00001842 switch (N.getValueType()) {
1843 default: assert(0 && "Cannot operate on this type!");
1844 case MVT::i1:
1845 case MVT::i8: Opc = 5; break;
1846 case MVT::i16: Opc = 6; break;
1847 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001848 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001849 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001850 case MVT::f64:
1851 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00001852 "SSE load should have been promoted");
1853 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001854 }
1855 switch (Node->getOpcode()) {
1856 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001857 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001858 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001859 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001860 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001861 case ISD::AND: Opc = ANDTab[Opc]; break;
1862 case ISD::OR: Opc = ORTab[Opc]; break;
1863 case ISD::XOR: Opc = XORTab[Opc]; break;
1864 }
1865
1866 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001867 EmitFoldedLoad(Op1, AM);
1868 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001869 if (Opc) {
1870 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1871 } else {
1872 assert(Node->getOpcode() == ISD::MUL &&
1873 N.getValueType() == MVT::i8 && "Unexpected situation!");
1874 // Must use the MUL instruction, which forces use of AL.
1875 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1876 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1877 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1878 }
1879 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001880 }
Chris Lattner62b22422005-01-11 21:19:59 +00001881
1882 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1883 Tmp1 = SelectExpr(Op0);
1884 Tmp2 = SelectExpr(Op1);
1885 } else {
1886 Tmp2 = SelectExpr(Op1);
1887 Tmp1 = SelectExpr(Op0);
1888 }
1889
Chris Lattner88c8a232005-01-07 07:49:41 +00001890 switch (N.getValueType()) {
1891 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00001892 case MVT::i1:
1893 case MVT::i8: Opc = 10; break;
1894 case MVT::i16: Opc = 11; break;
1895 case MVT::i32: Opc = 12; break;
1896 case MVT::f32: Opc = 13; break;
1897 case MVT::f64: Opc = 14; break;
1898 }
1899 switch (Node->getOpcode()) {
1900 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001901 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001902 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001903 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001904 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001905 case ISD::AND: Opc = ANDTab[Opc]; break;
1906 case ISD::OR: Opc = ORTab[Opc]; break;
1907 case ISD::XOR: Opc = XORTab[Opc]; break;
1908 }
1909 if (Opc) {
1910 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1911 } else {
1912 assert(Node->getOpcode() == ISD::MUL &&
1913 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00001914 // Must use the MUL instruction, which forces use of AL.
1915 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1916 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1917 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00001918 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001919 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00001920 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00001921 case ISD::ADD_PARTS:
1922 case ISD::SUB_PARTS: {
1923 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1924 "Not an i64 add/sub!");
1925 // Emit all of the operands.
1926 std::vector<unsigned> InVals;
1927 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1928 InVals.push_back(SelectExpr(N.getOperand(i)));
1929 if (N.getOpcode() == ISD::ADD_PARTS) {
1930 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1931 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1932 } else {
1933 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1934 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1935 }
1936 return Result+N.ResNo;
1937 }
1938
Chris Lattnera31d4c72005-04-02 04:01:14 +00001939 case ISD::SHL_PARTS:
1940 case ISD::SRA_PARTS:
1941 case ISD::SRL_PARTS: {
1942 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1943 "Not an i64 shift!");
1944 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1945 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1946 unsigned TmpReg = MakeReg(MVT::i32);
1947 if (N.getOpcode() == ISD::SRA_PARTS) {
1948 // If this is a SHR of a Long, then we need to do funny sign extension
1949 // stuff. TmpReg gets the value to use as the high-part if we are
1950 // shifting more than 32 bits.
1951 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
1952 } else {
1953 // Other shifts use a fixed zero value if the shift is more than 32 bits.
1954 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
1955 }
1956
1957 // Initialize CL with the shift amount.
1958 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
1959 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
1960
1961 unsigned TmpReg2 = MakeReg(MVT::i32);
1962 unsigned TmpReg3 = MakeReg(MVT::i32);
1963 if (N.getOpcode() == ISD::SHL_PARTS) {
1964 // TmpReg2 = shld inHi, inLo
1965 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
1966 .addReg(ShiftOpLo);
1967 // TmpReg3 = shl inLo, CL
1968 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001969
Chris Lattnera31d4c72005-04-02 04:01:14 +00001970 // Set the flags to indicate whether the shift was by more than 32 bits.
1971 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001972
Chris Lattnera31d4c72005-04-02 04:01:14 +00001973 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001974 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001975 Result+1).addReg(TmpReg2).addReg(TmpReg3);
1976 // DestLo = (>32) ? TmpReg : TmpReg3;
1977 BuildMI(BB, X86::CMOVNE32rr, 2,
1978 Result).addReg(TmpReg3).addReg(TmpReg);
1979 } else {
1980 // TmpReg2 = shrd inLo, inHi
1981 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
1982 .addReg(ShiftOpHi);
1983 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00001984 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00001985 : X86::SHR32rCL, 1, TmpReg3)
1986 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001987
Chris Lattnera31d4c72005-04-02 04:01:14 +00001988 // Set the flags to indicate whether the shift was by more than 32 bits.
1989 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001990
Chris Lattnera31d4c72005-04-02 04:01:14 +00001991 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001992 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001993 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001994
Chris Lattnera31d4c72005-04-02 04:01:14 +00001995 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001996 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001997 Result+1).addReg(TmpReg3).addReg(TmpReg);
1998 }
1999 return Result+N.ResNo;
2000 }
2001
Chris Lattner88c8a232005-01-07 07:49:41 +00002002 case ISD::SELECT:
Nate Begeman8d394eb2005-08-03 23:26:28 +00002003 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
2004 N.getValueType(), Result);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002005 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002006
Chris Lattner0815dcae2005-09-28 22:29:17 +00002007 case ISD::FDIV:
2008 case ISD::FREM:
Chris Lattner88c8a232005-01-07 07:49:41 +00002009 case ISD::SDIV:
2010 case ISD::UDIV:
2011 case ISD::SREM:
2012 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002013 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2014 "We don't support this operator!");
2015
Chris Lattner0815dcae2005-09-28 22:29:17 +00002016 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002017 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00002018 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00002019 // Check for reversed and unreversed DIV.
2020 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2021 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002022 Opc = X86::FpDIVR32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002023 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002024 Opc = X86::FpDIVR64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002025 X86AddressMode AM;
2026 EmitFoldedLoad(N.getOperand(0), AM);
2027 Tmp1 = SelectExpr(N.getOperand(1));
2028 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2029 return Result;
2030 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2031 N.getOperand(1).getOpcode() == ISD::LOAD) {
2032 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002033 Opc = X86::FpDIV32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002034 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002035 Opc = X86::FpDIV64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002036 X86AddressMode AM;
2037 EmitFoldedLoad(N.getOperand(1), AM);
2038 Tmp1 = SelectExpr(N.getOperand(0));
2039 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2040 return Result;
2041 }
2042 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002043 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002044
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002045 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2046 Tmp1 = SelectExpr(N.getOperand(0));
2047 Tmp2 = SelectExpr(N.getOperand(1));
2048 } else {
2049 Tmp2 = SelectExpr(N.getOperand(1));
2050 Tmp1 = SelectExpr(N.getOperand(0));
2051 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002052
2053 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2054 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2055 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2056 switch (N.getValueType()) {
2057 default: assert(0 && "Cannot sdiv this type!");
2058 case MVT::i8:
2059 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2060 LoReg = X86::AL;
2061 HiReg = X86::AH;
2062 MovOpcode = X86::MOV8rr;
2063 ClrOpcode = X86::MOV8ri;
2064 SExtOpcode = X86::CBW;
2065 break;
2066 case MVT::i16:
2067 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2068 LoReg = X86::AX;
2069 HiReg = X86::DX;
2070 MovOpcode = X86::MOV16rr;
2071 ClrOpcode = X86::MOV16ri;
2072 SExtOpcode = X86::CWD;
2073 break;
2074 case MVT::i32:
2075 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002076 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002077 HiReg = X86::EDX;
2078 MovOpcode = X86::MOV32rr;
2079 ClrOpcode = X86::MOV32ri;
2080 SExtOpcode = X86::CDQ;
2081 break;
Nate Begeman8a093362005-07-06 18:59:04 +00002082 case MVT::f32:
2083 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
2084 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002085 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002086 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
2087 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002088 return Result;
2089 }
2090
2091 // Set up the low part.
2092 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2093
2094 if (isSigned) {
2095 // Sign extend the low part into the high part.
2096 BuildMI(BB, SExtOpcode, 0);
2097 } else {
2098 // Zero out the high part, effectively zero extending the input.
2099 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2100 }
2101
2102 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002103 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002104
2105 // Get the result of the divide or rem.
2106 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2107 return Result;
2108 }
2109
2110 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002111 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002112 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2113 switch (N.getValueType()) {
2114 default: assert(0 && "Cannot shift this type!");
2115 case MVT::i8: Opc = X86::ADD8rr; break;
2116 case MVT::i16: Opc = X86::ADD16rr; break;
2117 case MVT::i32: Opc = X86::ADD32rr; break;
2118 }
2119 Tmp1 = SelectExpr(N.getOperand(0));
2120 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2121 return Result;
2122 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002123
Chris Lattner88c8a232005-01-07 07:49:41 +00002124 switch (N.getValueType()) {
2125 default: assert(0 && "Cannot shift this type!");
2126 case MVT::i8: Opc = X86::SHL8ri; break;
2127 case MVT::i16: Opc = X86::SHL16ri; break;
2128 case MVT::i32: Opc = X86::SHL32ri; break;
2129 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002130 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002131 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2132 return Result;
2133 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002134
2135 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2136 Tmp1 = SelectExpr(N.getOperand(0));
2137 Tmp2 = SelectExpr(N.getOperand(1));
2138 } else {
2139 Tmp2 = SelectExpr(N.getOperand(1));
2140 Tmp1 = SelectExpr(N.getOperand(0));
2141 }
2142
Chris Lattner88c8a232005-01-07 07:49:41 +00002143 switch (N.getValueType()) {
2144 default: assert(0 && "Cannot shift this type!");
2145 case MVT::i8 : Opc = X86::SHL8rCL; break;
2146 case MVT::i16: Opc = X86::SHL16rCL; break;
2147 case MVT::i32: Opc = X86::SHL32rCL; break;
2148 }
2149 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002150 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002151 return Result;
2152 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002153 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2154 switch (N.getValueType()) {
2155 default: assert(0 && "Cannot shift this type!");
2156 case MVT::i8: Opc = X86::SHR8ri; break;
2157 case MVT::i16: Opc = X86::SHR16ri; break;
2158 case MVT::i32: Opc = X86::SHR32ri; break;
2159 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002160 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002161 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2162 return Result;
2163 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002164
2165 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2166 Tmp1 = SelectExpr(N.getOperand(0));
2167 Tmp2 = SelectExpr(N.getOperand(1));
2168 } else {
2169 Tmp2 = SelectExpr(N.getOperand(1));
2170 Tmp1 = SelectExpr(N.getOperand(0));
2171 }
2172
Chris Lattner88c8a232005-01-07 07:49:41 +00002173 switch (N.getValueType()) {
2174 default: assert(0 && "Cannot shift this type!");
2175 case MVT::i8 : Opc = X86::SHR8rCL; break;
2176 case MVT::i16: Opc = X86::SHR16rCL; break;
2177 case MVT::i32: Opc = X86::SHR32rCL; break;
2178 }
2179 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002180 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002181 return Result;
2182 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002183 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2184 switch (N.getValueType()) {
2185 default: assert(0 && "Cannot shift this type!");
2186 case MVT::i8: Opc = X86::SAR8ri; break;
2187 case MVT::i16: Opc = X86::SAR16ri; break;
2188 case MVT::i32: Opc = X86::SAR32ri; break;
2189 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002190 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002191 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2192 return Result;
2193 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002194
2195 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2196 Tmp1 = SelectExpr(N.getOperand(0));
2197 Tmp2 = SelectExpr(N.getOperand(1));
2198 } else {
2199 Tmp2 = SelectExpr(N.getOperand(1));
2200 Tmp1 = SelectExpr(N.getOperand(0));
2201 }
2202
Chris Lattner88c8a232005-01-07 07:49:41 +00002203 switch (N.getValueType()) {
2204 default: assert(0 && "Cannot shift this type!");
2205 case MVT::i8 : Opc = X86::SAR8rCL; break;
2206 case MVT::i16: Opc = X86::SAR16rCL; break;
2207 case MVT::i32: Opc = X86::SAR32rCL; break;
2208 }
2209 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnera9d68f12005-08-19 00:31:37 +00002210 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002211 return Result;
2212
2213 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002214 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner6ec77452005-08-09 20:21:10 +00002215 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner88c8a232005-01-07 07:49:41 +00002216 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2217 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002218 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002219 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002220 if (Result != 1) { // Generate the token
2221 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2222 assert(0 && "Load already emitted!?");
2223 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002224 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2225
Chris Lattnerb52e0412005-01-08 19:28:19 +00002226 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002227 default: assert(0 && "Cannot load this type!");
2228 case MVT::i1:
2229 case MVT::i8: Opc = X86::MOV8rm; break;
2230 case MVT::i16: Opc = X86::MOV16rm; break;
2231 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002232 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002233 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002234 if (X86ScalarSSE) {
2235 Opc = X86::MOVSDrm;
2236 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00002237 Opc = X86::FpLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002238 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00002239 }
2240 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002241 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002242
Chris Lattner88c8a232005-01-07 07:49:41 +00002243 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattnerc30405e2005-08-26 17:15:30 +00002244 unsigned CPIdx = BB->getParent()->getConstantPool()->
2245 getConstantPoolIndex(CP->get());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002246 Select(N.getOperand(0));
Chris Lattnerc30405e2005-08-26 17:15:30 +00002247 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner88c8a232005-01-07 07:49:41 +00002248 } else {
2249 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002250
2251 SDOperand Chain = N.getOperand(0);
2252 SDOperand Address = N.getOperand(1);
2253 if (getRegPressure(Chain) > getRegPressure(Address)) {
2254 Select(Chain);
2255 SelectAddress(Address, AM);
2256 } else {
2257 SelectAddress(Address, AM);
2258 Select(Chain);
2259 }
2260
Chris Lattner88c8a232005-01-07 07:49:41 +00002261 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2262 }
2263 return Result;
Evan Cheng6305e502006-01-12 22:54:21 +00002264 case X86ISD::FILD:
Chris Lattnera36117b2005-05-14 06:52:07 +00002265 // Make sure we generate both values.
2266 assert(Result != 1 && N.getValueType() == MVT::f64);
2267 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2268 assert(0 && "Load already emitted!?");
2269
2270 {
2271 X86AddressMode AM;
2272
2273 SDOperand Chain = N.getOperand(0);
2274 SDOperand Address = N.getOperand(1);
2275 if (getRegPressure(Chain) > getRegPressure(Address)) {
2276 Select(Chain);
2277 SelectAddress(Address, AM);
2278 } else {
2279 SelectAddress(Address, AM);
2280 Select(Chain);
2281 }
Chris Lattner67756e22005-07-29 00:40:01 +00002282
Chris Lattnerf431ad42005-12-21 07:47:04 +00002283 addFullAddress(BuildMI(BB, X86::FpILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00002284 }
2285 return Result;
Jeff Cohen546fd592005-07-30 18:33:25 +00002286
Chris Lattnere18a4c42005-01-15 05:22:24 +00002287 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2288 case ISD::ZEXTLOAD: {
2289 // Make sure we generate both values.
2290 if (Result != 1)
2291 ExprMap[N.getValue(1)] = 1; // Generate the token
2292 else
2293 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2294
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002295 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2296 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00002297 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002298 "Bad EXTLOAD!");
Chris Lattnerc30405e2005-08-26 17:15:30 +00002299 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00002300 getConstantPoolIndex(CP->get());
Chris Lattnerc30405e2005-08-26 17:15:30 +00002301
Chris Lattnerf431ad42005-12-21 07:47:04 +00002302 addConstantPoolReference(BuildMI(BB, X86::FpLD32m, 4, Result), CPIdx);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002303 return Result;
2304 }
2305
Chris Lattnere18a4c42005-01-15 05:22:24 +00002306 X86AddressMode AM;
2307 if (getRegPressure(Node->getOperand(0)) >
2308 getRegPressure(Node->getOperand(1))) {
2309 Select(Node->getOperand(0)); // chain
2310 SelectAddress(Node->getOperand(1), AM);
2311 } else {
2312 SelectAddress(Node->getOperand(1), AM);
2313 Select(Node->getOperand(0)); // chain
2314 }
2315
2316 switch (Node->getValueType(0)) {
2317 default: assert(0 && "Unknown type to sign extend to.");
2318 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00002319 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002320 "Bad EXTLOAD!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00002321 addFullAddress(BuildMI(BB, X86::FpLD32m, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002322 break;
2323 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002324 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002325 default:
2326 assert(0 && "Bad zero extend!");
2327 case MVT::i1:
2328 case MVT::i8:
2329 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2330 break;
2331 case MVT::i16:
2332 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2333 break;
2334 }
2335 break;
2336 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002337 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002338 "Bad zero extend!");
Evan Cheng023aef22005-12-14 22:28:18 +00002339 addFullAddress(BuildMI(BB, X86::MOVZX16rm8, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002340 break;
2341 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00002342 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002343 "Bad zero extend!");
2344 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2345 break;
2346 }
2347 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002348 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002349 case ISD::SEXTLOAD: {
2350 // Make sure we generate both values.
2351 if (Result != 1)
2352 ExprMap[N.getValue(1)] = 1; // Generate the token
2353 else
2354 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2355
2356 X86AddressMode AM;
2357 if (getRegPressure(Node->getOperand(0)) >
2358 getRegPressure(Node->getOperand(1))) {
2359 Select(Node->getOperand(0)); // chain
2360 SelectAddress(Node->getOperand(1), AM);
2361 } else {
2362 SelectAddress(Node->getOperand(1), AM);
2363 Select(Node->getOperand(0)); // chain
2364 }
2365
2366 switch (Node->getValueType(0)) {
2367 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2368 default: assert(0 && "Unknown type to sign extend to.");
2369 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002370 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002371 default:
2372 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2373 case MVT::i8:
2374 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2375 break;
2376 case MVT::i16:
2377 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2378 break;
2379 }
2380 break;
2381 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002382 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002383 "Cannot sign extend from bool!");
2384 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2385 break;
2386 }
2387 return Result;
2388 }
2389
Chris Lattner1b3520c2005-05-14 08:48:15 +00002390 case X86ISD::TAILCALL:
2391 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00002392 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002393 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00002394
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002395 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
2396 isa<ExternalSymbolSDNode>(N.getOperand(1));
2397 unsigned Callee = 0;
2398 if (isDirect) {
2399 Select(N.getOperand(0));
2400 } else {
2401 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2402 Select(N.getOperand(0));
2403 Callee = SelectExpr(N.getOperand(1));
2404 } else {
2405 Callee = SelectExpr(N.getOperand(1));
2406 Select(N.getOperand(0));
2407 }
2408 }
2409
2410 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002411 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002412 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002413 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002414 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00002415 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002416
Chris Lattner1b3520c2005-05-14 08:48:15 +00002417 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002418 default: assert(0 && "Bad thing to pass in regs");
2419 case MVT::i1:
2420 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
2421 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
2422 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
2423 }
2424 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00002425 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002426 default: assert(0 && "Bad thing to pass in regs");
2427 case MVT::i1:
2428 case MVT::i8:
2429 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
2430 break;
2431 case MVT::i16:
2432 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
2433 break;
2434 case MVT::i32:
2435 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
2436 break;
2437 }
2438 }
2439
Chris Lattner88c8a232005-01-07 07:49:41 +00002440 if (GlobalAddressSDNode *GASD =
2441 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
2442 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2443 } else if (ExternalSymbolSDNode *ESSDN =
2444 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
2445 BuildMI(BB, X86::CALLpcrel32,
2446 1).addExternalSymbol(ESSDN->getSymbol(), true);
2447 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002448 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2449 Select(N.getOperand(0));
2450 Tmp1 = SelectExpr(N.getOperand(1));
2451 } else {
2452 Tmp1 = SelectExpr(N.getOperand(1));
2453 Select(N.getOperand(0));
2454 }
2455
Chris Lattner88c8a232005-01-07 07:49:41 +00002456 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2457 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002458
2459 // Get caller stack amount and amount the callee added to the stack pointer.
2460 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
2461 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
2462 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
2463
2464 if (Node->getNumValues() != 1)
2465 switch (Node->getValueType(1)) {
2466 default: assert(0 && "Unknown value type for call result!");
2467 case MVT::Other: return 1;
2468 case MVT::i1:
2469 case MVT::i8:
2470 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2471 break;
2472 case MVT::i16:
2473 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2474 break;
2475 case MVT::i32:
2476 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2477 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
2478 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2479 break;
2480 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00002481 if (X86ScalarSSE) {
2482 ContainsFPCode = true;
2483 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
2484
2485 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
2486 MachineFunction *F = BB->getParent();
2487 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerf431ad42005-12-21 07:47:04 +00002488 addFrameReference(BuildMI(BB, X86::FpST64m, 5), FrameIdx).addReg(X86::FP0);
Nate Begeman8a093362005-07-06 18:59:04 +00002489 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
2490 break;
2491 } else {
2492 ContainsFPCode = true;
2493 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2494 break;
2495 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002496 }
2497 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002498 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00002499 case ISD::READPORT:
2500 // First, determine that the size of the operand falls within the acceptable
2501 // range for this architecture.
2502 //
2503 if (Node->getOperand(1).getValueType() != MVT::i16) {
2504 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2505 exit(1);
2506 }
2507
2508 // Make sure we generate both values.
2509 if (Result != 1) { // Generate the token
2510 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2511 assert(0 && "readport already emitted!?");
2512 } else
2513 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002514
Chris Lattner70ea07c2005-05-09 21:17:38 +00002515 Select(Node->getOperand(0)); // Select the chain.
2516
2517 // If the port is a single-byte constant, use the immediate form.
2518 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2519 if ((Port->getValue() & 255) == Port->getValue()) {
2520 switch (Node->getValueType(0)) {
2521 case MVT::i8:
2522 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2523 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2524 return Result;
2525 case MVT::i16:
2526 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2527 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2528 return Result;
2529 case MVT::i32:
2530 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2531 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2532 return Result;
2533 default: break;
2534 }
2535 }
2536
2537 // Now, move the I/O port address into the DX register and use the IN
2538 // instruction to get the input data.
2539 //
2540 Tmp1 = SelectExpr(Node->getOperand(1));
2541 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2542 switch (Node->getValueType(0)) {
2543 case MVT::i8:
2544 BuildMI(BB, X86::IN8rr, 0);
2545 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2546 return Result;
2547 case MVT::i16:
2548 BuildMI(BB, X86::IN16rr, 0);
2549 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2550 return Result;
2551 case MVT::i32:
2552 BuildMI(BB, X86::IN32rr, 0);
2553 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2554 return Result;
2555 default:
2556 std::cerr << "Cannot do input on this data type";
2557 exit(1);
2558 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002559
Chris Lattner88c8a232005-01-07 07:49:41 +00002560 }
2561
2562 return 0;
2563}
2564
Chris Lattner96113fd2005-01-17 19:25:26 +00002565/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2566/// load/op/store instruction. If successful return true.
2567bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2568 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2569 SDOperand Chain = Node->getOperand(0);
2570 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00002571 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00002572
2573 // The chain has to be a load, the stored value must be an integer binary
2574 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00002575 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00002576 MVT::isFloatingPoint(StVal.getValueType()))
2577 return false;
2578
Chris Lattnere86c9332005-01-17 22:10:42 +00002579 // Token chain must either be a factor node or the load to fold.
2580 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2581 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00002582
Chris Lattnere86c9332005-01-17 22:10:42 +00002583 SDOperand TheLoad;
2584
2585 // Check to see if there is a load from the same pointer that we're storing
2586 // to in either operand of the binop.
2587 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2588 StVal.getOperand(0).getOperand(1) == StPtr)
2589 TheLoad = StVal.getOperand(0);
2590 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2591 StVal.getOperand(1).getOperand(1) == StPtr)
2592 TheLoad = StVal.getOperand(1);
2593 else
2594 return false; // No matching load operand.
2595
2596 // We can only fold the load if there are no intervening side-effecting
2597 // operations. This means that the store uses the load as its token chain, or
2598 // there are only token factor nodes in between the store and load.
2599 if (Chain != TheLoad.getValue(1)) {
2600 // Okay, the other option is that we have a store referring to (possibly
2601 // nested) token factor nodes. For now, just try peeking through one level
2602 // of token factors to see if this is the case.
2603 bool ChainOk = false;
2604 if (Chain.getOpcode() == ISD::TokenFactor) {
2605 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2606 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2607 ChainOk = true;
2608 break;
2609 }
2610 }
2611
2612 if (!ChainOk) return false;
2613 }
2614
2615 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00002616 return false;
2617
2618 // Make sure that one of the operands of the binop is the load, and that the
2619 // load folds into the binop.
2620 if (((StVal.getOperand(0) != TheLoad ||
2621 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2622 (StVal.getOperand(1) != TheLoad ||
2623 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2624 return false;
2625
2626 // Finally, check to see if this is one of the ops we can handle!
2627 static const unsigned ADDTAB[] = {
2628 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2629 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2630 };
2631 static const unsigned SUBTAB[] = {
2632 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2633 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2634 };
2635 static const unsigned ANDTAB[] = {
2636 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2637 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2638 };
2639 static const unsigned ORTAB[] = {
2640 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2641 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2642 };
2643 static const unsigned XORTAB[] = {
2644 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2645 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2646 };
2647 static const unsigned SHLTAB[] = {
2648 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2649 /*Have to put the reg in CL*/0, 0, 0,
2650 };
2651 static const unsigned SARTAB[] = {
2652 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2653 /*Have to put the reg in CL*/0, 0, 0,
2654 };
2655 static const unsigned SHRTAB[] = {
2656 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2657 /*Have to put the reg in CL*/0, 0, 0,
2658 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00002659
Chris Lattner96113fd2005-01-17 19:25:26 +00002660 const unsigned *TabPtr = 0;
2661 switch (StVal.getOpcode()) {
2662 default:
2663 std::cerr << "CANNOT [mem] op= val: ";
2664 StVal.Val->dump(); std::cerr << "\n";
Chris Lattner0815dcae2005-09-28 22:29:17 +00002665 case ISD::FMUL:
Chris Lattner96113fd2005-01-17 19:25:26 +00002666 case ISD::MUL:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002667 case ISD::FDIV:
Chris Lattner96113fd2005-01-17 19:25:26 +00002668 case ISD::SDIV:
2669 case ISD::UDIV:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002670 case ISD::FREM:
Chris Lattner96113fd2005-01-17 19:25:26 +00002671 case ISD::SREM:
2672 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002673
Chris Lattner96113fd2005-01-17 19:25:26 +00002674 case ISD::ADD: TabPtr = ADDTAB; break;
2675 case ISD::SUB: TabPtr = SUBTAB; break;
2676 case ISD::AND: TabPtr = ANDTAB; break;
2677 case ISD:: OR: TabPtr = ORTAB; break;
2678 case ISD::XOR: TabPtr = XORTAB; break;
2679 case ISD::SHL: TabPtr = SHLTAB; break;
2680 case ISD::SRA: TabPtr = SARTAB; break;
2681 case ISD::SRL: TabPtr = SHRTAB; break;
2682 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002683
Chris Lattner96113fd2005-01-17 19:25:26 +00002684 // Handle: [mem] op= CST
2685 SDOperand Op0 = StVal.getOperand(0);
2686 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00002687 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00002688 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2689 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2690 default: break;
2691 case MVT::i1:
2692 case MVT::i8: Opc = TabPtr[0]; break;
2693 case MVT::i16: Opc = TabPtr[1]; break;
2694 case MVT::i32: Opc = TabPtr[2]; break;
2695 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002696
Chris Lattner96113fd2005-01-17 19:25:26 +00002697 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00002698 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2699 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002700 Select(Chain);
2701
Chris Lattner96113fd2005-01-17 19:25:26 +00002702 X86AddressMode AM;
2703 if (getRegPressure(TheLoad.getOperand(0)) >
2704 getRegPressure(TheLoad.getOperand(1))) {
2705 Select(TheLoad.getOperand(0));
2706 SelectAddress(TheLoad.getOperand(1), AM);
2707 } else {
2708 SelectAddress(TheLoad.getOperand(1), AM);
2709 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00002710 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002711
2712 if (StVal.getOpcode() == ISD::ADD) {
2713 if (CN->getValue() == 1) {
2714 switch (Op0.getValueType()) {
2715 default: break;
2716 case MVT::i8:
2717 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2718 return true;
2719 case MVT::i16: Opc = TabPtr[1];
2720 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2721 return true;
2722 case MVT::i32: Opc = TabPtr[2];
2723 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2724 return true;
2725 }
2726 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2727 switch (Op0.getValueType()) {
2728 default: break;
2729 case MVT::i8:
2730 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2731 return true;
2732 case MVT::i16: Opc = TabPtr[1];
2733 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2734 return true;
2735 case MVT::i32: Opc = TabPtr[2];
2736 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2737 return true;
2738 }
2739 }
2740 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002741
Chris Lattner96113fd2005-01-17 19:25:26 +00002742 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2743 return true;
2744 }
2745 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002746
Chris Lattner96113fd2005-01-17 19:25:26 +00002747 // If we have [mem] = V op [mem], try to turn it into:
2748 // [mem] = [mem] op V.
Chris Lattner0815dcae2005-09-28 22:29:17 +00002749 if (Op1 == TheLoad &&
2750 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
Chris Lattner96113fd2005-01-17 19:25:26 +00002751 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2752 StVal.getOpcode() != ISD::SRL)
2753 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002754
Chris Lattner96113fd2005-01-17 19:25:26 +00002755 if (Op0 != TheLoad) return false;
2756
2757 switch (Op0.getValueType()) {
2758 default: return false;
2759 case MVT::i1:
2760 case MVT::i8: Opc = TabPtr[3]; break;
2761 case MVT::i16: Opc = TabPtr[4]; break;
2762 case MVT::i32: Opc = TabPtr[5]; break;
2763 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002764
Chris Lattner479c7112005-01-18 17:35:28 +00002765 // Table entry doesn't exist?
2766 if (Opc == 0) return false;
2767
Chris Lattner78d30282005-01-18 03:51:59 +00002768 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2769 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002770 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00002771 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00002772
Chris Lattner96113fd2005-01-17 19:25:26 +00002773 X86AddressMode AM;
2774 SelectAddress(TheLoad.getOperand(1), AM);
2775 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002776 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00002777 return true;
2778}
2779
Chris Lattnerdd66a412005-05-15 05:46:45 +00002780/// If node is a ret(tailcall) node, emit the specified tail call and return
2781/// true, otherwise return false.
2782///
2783/// FIXME: This whole thing should be a post-legalize optimization pass which
2784/// recognizes and transforms the dag. We don't want the selection phase doing
2785/// this stuff!!
2786///
2787bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
2788 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
2789
2790 SDOperand Chain = RetNode->getOperand(0);
2791
2792 // If this is a token factor node where one operand is a call, dig into it.
2793 SDOperand TokFactor;
2794 unsigned TokFactorOperand = 0;
2795 if (Chain.getOpcode() == ISD::TokenFactor) {
2796 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2797 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
2798 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
2799 TokFactorOperand = i;
2800 TokFactor = Chain;
2801 Chain = Chain.getOperand(i);
2802 break;
2803 }
2804 if (TokFactor.Val == 0) return false; // No call operand.
2805 }
2806
2807 // Skip the CALLSEQ_END node if present.
2808 if (Chain.getOpcode() == ISD::CALLSEQ_END)
2809 Chain = Chain.getOperand(0);
2810
2811 // Is a tailcall the last control operation that occurs before the return?
2812 if (Chain.getOpcode() != X86ISD::TAILCALL)
2813 return false;
2814
2815 // If we return a value, is it the value produced by the call?
2816 if (RetNode->getNumOperands() > 1) {
2817 // Not returning the ret val of the call?
2818 if (Chain.Val->getNumValues() == 1 ||
2819 RetNode->getOperand(1) != Chain.getValue(1))
2820 return false;
2821
2822 if (RetNode->getNumOperands() > 2) {
2823 if (Chain.Val->getNumValues() == 2 ||
2824 RetNode->getOperand(2) != Chain.getValue(2))
2825 return false;
2826 }
2827 assert(RetNode->getNumOperands() <= 3);
2828 }
2829
2830 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
2831 // For FastCC, this will always be > 0.
2832 unsigned CalleeCallArgAmt =
2833 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
2834
2835 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
2836 // callee. For FastCC this will always be > 0, for CCC this is always 0.
2837 unsigned CalleeCallArgPopAmt =
2838 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
2839
2840 // There are several cases we can handle here. First, if the caller and
2841 // callee are both CCC functions, we can tailcall if the callee takes <= the
2842 // number of argument bytes that the caller does.
2843 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
2844 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
2845 // Check to see if caller arg area size >= callee arg area size.
2846 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
2847 //std::cerr << "CCC TAILCALL UNIMP!\n";
2848 // If TokFactor is non-null, emit all operands.
2849
2850 //EmitCCCToCCCTailCall(Chain.Val);
2851 //return true;
2852 }
2853 return false;
2854 }
2855
2856 // Second, if both are FastCC functions, we can always perform the tail call.
2857 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
2858 // If TokFactor is non-null, emit all operands before the call.
2859 if (TokFactor.Val) {
2860 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
2861 if (i != TokFactorOperand)
2862 Select(TokFactor.getOperand(i));
2863 }
2864
2865 EmitFastCCToFastCCTailCall(Chain.Val);
2866 return true;
2867 }
2868
2869 // We don't support mixed calls, due to issues with alignment. We could in
2870 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
2871 // aligned (which depends on the number of arguments to the callee). TODO.
2872 return false;
2873}
2874
2875static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
2876 SelectionDAG &DAG) {
2877 MVT::ValueType StoreVT;
2878 switch (Chain.getOpcode()) {
Chris Lattnerc1469402005-08-25 00:05:15 +00002879 default: assert(0 && "Unexpected node!");
Chris Lattnerdd66a412005-05-15 05:46:45 +00002880 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00002881 // If we found the start of the call sequence, we're done. We actually
2882 // strip off the CALLSEQ_START node, to avoid generating the
2883 // ADJCALLSTACKDOWN marker for the tail call.
2884 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002885 case ISD::TokenFactor: {
2886 std::vector<SDOperand> Ops;
2887 Ops.reserve(Chain.getNumOperands());
2888 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2889 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
2890 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
2891 }
2892 case ISD::STORE: // Normal store
2893 StoreVT = Chain.getOperand(1).getValueType();
2894 break;
2895 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00002896 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00002897 break;
2898 }
2899
2900 SDOperand OrigDest = Chain.getOperand(2);
2901 unsigned OrigOffset;
2902
2903 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
2904 OrigOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +00002905 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002906 } else {
2907 // We expect only (ESP+C)
2908 assert(OrigDest.getOpcode() == ISD::ADD &&
2909 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
2910 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
Chris Lattner7c762782005-08-16 21:56:37 +00002911 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
2912 == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002913 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2914 }
2915
2916 // Compute the new offset from the incoming ESP value we wish to use.
2917 unsigned NewOffset = OrigOffset + Offset;
2918
2919 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
2920 MachineFunction &MF = DAG.getMachineFunction();
2921 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
2922 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
2923
2924 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
2925 DAG);
2926 if (Chain.getOpcode() == ISD::STORE)
2927 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
2928 FIN);
2929 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
2930 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00002931 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00002932}
2933
2934
2935/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
2936/// fastcc function from a fastcc function, emit the code to emit a 'proper'
2937/// tail call.
2938void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
2939 unsigned CalleeCallArgSize =
2940 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
2941 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
2942
2943 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
2944
2945 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
2946 // indexes that are relative to the incoming ESP. If the incoming and
2947 // outgoing arg sizes are the same we will store to [InESP] instead of
2948 // [CurESP] and the ESP referenced will be relative to the incoming function
2949 // ESP.
2950 int ESPOffset = CallerArgSize-CalleeCallArgSize;
2951 SDOperand AdjustedArgStores =
2952 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
2953
2954 // Copy the return address of the caller into a virtual register so we don't
2955 // clobber it.
Chris Lattnerefbb8da2006-01-06 17:56:38 +00002956 SDOperand RetVal(0, 0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002957 if (ESPOffset) {
2958 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
2959 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
2960 RetValAddr, TheDAG->getSrcValue(NULL));
2961 SelectExpr(RetVal);
2962 }
2963
2964 // Codegen all of the argument stores.
2965 Select(AdjustedArgStores);
2966
2967 if (RetVal.Val) {
2968 // Emit a store of the saved ret value to the new location.
2969 MachineFunction &MF = TheDAG->getMachineFunction();
2970 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
2971 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
2972 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
2973 RetVal, RetValAddr));
2974 }
2975
2976 // Get the destination value.
2977 SDOperand Callee = TailCallNode->getOperand(1);
2978 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
2979 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00002980 unsigned CalleeReg = 0;
Chris Lattner7e792922005-12-04 06:03:50 +00002981 if (!isDirect) {
2982 // If this is not a direct tail call, evaluate the callee's address.
2983 CalleeReg = SelectExpr(Callee);
2984 }
Chris Lattnerdd66a412005-05-15 05:46:45 +00002985
2986 unsigned RegOp1 = 0;
2987 unsigned RegOp2 = 0;
2988
2989 if (TailCallNode->getNumOperands() > 4) {
2990 // The first value is passed in (a part of) EAX, the second in EDX.
2991 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
2992 if (TailCallNode->getNumOperands() > 5)
2993 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002994
Chris Lattnerdd66a412005-05-15 05:46:45 +00002995 switch (TailCallNode->getOperand(4).getValueType()) {
2996 default: assert(0 && "Bad thing to pass in regs");
2997 case MVT::i1:
2998 case MVT::i8:
2999 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
3000 RegOp1 = X86::AL;
3001 break;
3002 case MVT::i16:
3003 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
3004 RegOp1 = X86::AX;
3005 break;
3006 case MVT::i32:
3007 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
3008 RegOp1 = X86::EAX;
3009 break;
3010 }
3011 if (RegOp2)
3012 switch (TailCallNode->getOperand(5).getValueType()) {
3013 default: assert(0 && "Bad thing to pass in regs");
3014 case MVT::i1:
3015 case MVT::i8:
3016 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
3017 RegOp2 = X86::DL;
3018 break;
3019 case MVT::i16:
3020 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3021 RegOp2 = X86::DX;
3022 break;
3023 case MVT::i32:
3024 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3025 RegOp2 = X86::EDX;
3026 break;
3027 }
3028 }
Chris Lattner7e792922005-12-04 06:03:50 +00003029
3030 // If this is not a direct tail call, put the callee's address into ECX.
3031 // The address has to be evaluated into a non-callee save register that is
3032 // not used for arguments. This means either ECX, as EAX and EDX may be
3033 // used for argument passing. We do this here to make sure that the
3034 // expressions for arguments and callee are all evaluated before the copies
3035 // into physical registers.
3036 if (!isDirect)
3037 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CalleeReg);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003038
3039 // Adjust ESP.
3040 if (ESPOffset)
3041 BuildMI(BB, X86::ADJSTACKPTRri, 2,
3042 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
3043
3044 // TODO: handle jmp [mem]
3045 if (!isDirect) {
Chris Lattner7e792922005-12-04 06:03:50 +00003046 BuildMI(BB, X86::TAILJMPr, 1).addReg(X86::ECX);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003047 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00003048 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003049 } else {
3050 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
3051 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
3052 }
3053 // ADD IMPLICIT USE RegOp1/RegOp2's
3054}
3055
Chris Lattner96113fd2005-01-17 19:25:26 +00003056
Chris Lattner88c8a232005-01-07 07:49:41 +00003057void ISel::Select(SDOperand N) {
Chris Lattner9982da22005-10-02 16:29:36 +00003058 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
Chris Lattner88c8a232005-01-07 07:49:41 +00003059
Nate Begeman95210522005-03-24 04:39:54 +00003060 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003061 return; // Already selected.
3062
Chris Lattner36f78482005-01-11 06:14:36 +00003063 SDNode *Node = N.Val;
3064
3065 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003066 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003067 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003068 assert(0 && "Node not handled yet!");
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00003069 case X86ISD::RDTSC_DAG:
3070 Select(Node->getOperand(0)); //Chain
3071 BuildMI(BB, X86::RDTSC, 0);
3072 return;
3073
Chris Lattner88c8a232005-01-07 07:49:41 +00003074 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003075 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003076 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003077 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003078 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3079 Select(Node->getOperand(OneFirst));
3080 Select(Node->getOperand(!OneFirst));
3081 } else {
3082 std::vector<std::pair<unsigned, unsigned> > OpsP;
3083 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3084 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3085 std::sort(OpsP.begin(), OpsP.end());
3086 std::reverse(OpsP.begin(), OpsP.end());
3087 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3088 Select(Node->getOperand(OpsP[i].second));
3089 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003090 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003091 case ISD::CopyToReg:
Chris Lattner7c762782005-08-16 21:56:37 +00003092 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattner2cfce682005-01-12 02:02:48 +00003093 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00003094 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003095 } else {
Chris Lattner7c762782005-08-16 21:56:37 +00003096 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003097 Select(N.getOperand(0));
3098 }
Chris Lattner7c762782005-08-16 21:56:37 +00003099 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003100
Chris Lattner88c8a232005-01-07 07:49:41 +00003101 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00003102 switch (N.getOperand(2).getValueType()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003103 default: assert(0 && "Invalid type for operation!");
3104 case MVT::i1:
3105 case MVT::i8: Opc = X86::MOV8rr; break;
3106 case MVT::i16: Opc = X86::MOV16rr; break;
3107 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman9d7008b2005-10-14 22:06:00 +00003108 case MVT::f32: Opc = X86::MOVSSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003109 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003110 if (X86ScalarSSE) {
Nate Begeman9d7008b2005-10-14 22:06:00 +00003111 Opc = X86::MOVSDrr;
Nate Begeman8a093362005-07-06 18:59:04 +00003112 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003113 Opc = X86::FpMOV;
3114 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003115 }
3116 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003117 }
3118 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3119 }
3120 return;
3121 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00003122 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
3123 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
3124 N.getOperand(0).getOpcode() == ISD::TokenFactor)
3125 if (EmitPotentialTailCall(Node))
3126 return;
3127
Chris Lattner88c8a232005-01-07 07:49:41 +00003128 switch (N.getNumOperands()) {
3129 default:
3130 assert(0 && "Unknown return instruction!");
3131 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003132 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003133 N.getOperand(2).getValueType() == MVT::i32 &&
3134 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003135 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3136 Tmp1 = SelectExpr(N.getOperand(1));
3137 Tmp2 = SelectExpr(N.getOperand(2));
3138 } else {
3139 Tmp2 = SelectExpr(N.getOperand(2));
3140 Tmp1 = SelectExpr(N.getOperand(1));
3141 }
3142 Select(N.getOperand(0));
3143
Chris Lattner88c8a232005-01-07 07:49:41 +00003144 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3145 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003146 break;
3147 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003148 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3149 Select(N.getOperand(0));
3150 Tmp1 = SelectExpr(N.getOperand(1));
3151 } else {
3152 Tmp1 = SelectExpr(N.getOperand(1));
3153 Select(N.getOperand(0));
3154 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003155 switch (N.getOperand(1).getValueType()) {
3156 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00003157 case MVT::f32:
3158 if (X86ScalarSSE) {
3159 // Spill the value to memory and reload it into top of stack.
3160 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
3161 MachineFunction *F = BB->getParent();
3162 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3163 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003164 addFrameReference(BuildMI(BB, X86::FpLD32m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003165 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003166 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003167 } else {
3168 assert(0 && "MVT::f32 only legal with scalar sse fp");
3169 abort();
3170 }
3171 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003172 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003173 if (X86ScalarSSE) {
3174 // Spill the value to memory and reload it into top of stack.
3175 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3176 MachineFunction *F = BB->getParent();
3177 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3178 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003179 addFrameReference(BuildMI(BB, X86::FpLD64m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003180 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003181 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003182 } else {
3183 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
3184 }
3185 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003186 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00003187 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3188 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003189 }
3190 break;
3191 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003192 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003193 break;
3194 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003195 if (X86Lowering.getBytesToPopOnReturn() == 0)
3196 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3197 else
3198 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00003199 return;
3200 case ISD::BR: {
3201 Select(N.getOperand(0));
3202 MachineBasicBlock *Dest =
3203 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3204 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3205 return;
3206 }
3207
3208 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003209 MachineBasicBlock *Dest =
3210 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003211
Chris Lattner88c8a232005-01-07 07:49:41 +00003212 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3213 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003214 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3215 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3216 Select(N.getOperand(0));
3217 Tmp1 = SelectExpr(N.getOperand(1));
3218 } else {
3219 Tmp1 = SelectExpr(N.getOperand(1));
3220 Select(N.getOperand(0));
3221 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003222 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3223 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3224 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003225
Chris Lattner88c8a232005-01-07 07:49:41 +00003226 return;
3227 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003228
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003229 case ISD::LOAD:
3230 // If this load could be folded into the only using instruction, and if it
3231 // is safe to emit the instruction here, try to do so now.
3232 if (Node->hasNUsesOfValue(1, 0)) {
3233 SDOperand TheVal = N.getValue(0);
3234 SDNode *User = 0;
3235 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3236 assert(UI != Node->use_end() && "Didn't find use!");
3237 SDNode *UN = *UI;
3238 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3239 if (UN->getOperand(i) == TheVal) {
3240 User = UN;
3241 goto FoundIt;
3242 }
3243 }
3244 FoundIt:
3245 // Only handle unary operators right now.
3246 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003247 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003248 SelectExpr(SDOperand(User, 0));
3249 return;
3250 }
3251 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003252 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003253 SelectExpr(N);
3254 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003255 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003256 case ISD::EXTLOAD:
3257 case ISD::SEXTLOAD:
3258 case ISD::ZEXTLOAD:
Chris Lattner1b3520c2005-05-14 08:48:15 +00003259 case X86ISD::TAILCALL:
3260 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00003261 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003262 SelectExpr(N);
3263 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003264 case ISD::CopyFromReg:
Evan Cheng6305e502006-01-12 22:54:21 +00003265 case X86ISD::FILD:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003266 ExprMap.erase(N);
3267 SelectExpr(N.getValue(0));
3268 return;
Jeff Cohen546fd592005-07-30 18:33:25 +00003269
Chris Lattner4738d1b2005-07-30 00:05:54 +00003270 case X86ISD::FP_TO_INT16_IN_MEM:
3271 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattner6dc60e82005-07-29 00:54:34 +00003272 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00003273 assert(N.getOperand(1).getValueType() == MVT::f64);
3274 X86AddressMode AM;
3275 Select(N.getOperand(0)); // Select the token chain
3276
3277 unsigned ValReg;
3278 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3279 ValReg = SelectExpr(N.getOperand(1));
3280 SelectAddress(N.getOperand(2), AM);
3281 } else {
3282 SelectAddress(N.getOperand(2), AM);
3283 ValReg = SelectExpr(N.getOperand(1));
3284 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003285
Chris Lattner6dc60e82005-07-29 00:54:34 +00003286 // Change the floating point control register to use "round towards zero"
3287 // mode when truncating to an integer value.
3288 //
3289 MachineFunction *F = BB->getParent();
3290 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3291 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003292
Chris Lattner6dc60e82005-07-29 00:54:34 +00003293 // Load the old value of the high byte of the control word...
Chris Lattneraeef51b2005-07-30 00:17:52 +00003294 unsigned OldCW = MakeReg(MVT::i16);
3295 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003296
Chris Lattner6dc60e82005-07-29 00:54:34 +00003297 // Set the high part to be round to zero...
Chris Lattner49134572005-07-30 00:43:00 +00003298 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohen546fd592005-07-30 18:33:25 +00003299
Chris Lattner6dc60e82005-07-29 00:54:34 +00003300 // Reload the modified control word now...
3301 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003302
Chris Lattner6dc60e82005-07-29 00:54:34 +00003303 // Restore the memory image of control word to original value
Chris Lattneraeef51b2005-07-30 00:17:52 +00003304 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner4738d1b2005-07-30 00:05:54 +00003305
3306 // Get the X86 opcode to use.
3307 switch (N.getOpcode()) {
Chris Lattnerf431ad42005-12-21 07:47:04 +00003308 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FpIST16m; break;
3309 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FpIST32m; break;
3310 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FpIST64m; break;
Chris Lattner4738d1b2005-07-30 00:05:54 +00003311 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003312
Chris Lattner4738d1b2005-07-30 00:05:54 +00003313 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohen546fd592005-07-30 18:33:25 +00003314
Chris Lattner6dc60e82005-07-29 00:54:34 +00003315 // Reload the original control word now.
3316 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00003317 return;
3318 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003319
Chris Lattner36db1ed2005-07-10 00:29:18 +00003320 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00003321 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00003322 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003323 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3324 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3325 && "Unsupported TRUNCSTORE for this target!");
3326
3327 if (StoredTy == MVT::i16) {
3328 // FIXME: This is here just to allow testing. X86 doesn't really have a
3329 // TRUNCSTORE i16 operation, but this is required for targets that do not
3330 // have 16-bit integer registers. We occasionally disable 16-bit integer
3331 // registers to test the promotion code.
3332 Select(N.getOperand(0));
3333 Tmp1 = SelectExpr(N.getOperand(1));
3334 SelectAddress(N.getOperand(2), AM);
3335
3336 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3337 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3338 return;
3339 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003340
3341 // Store of constant bool?
3342 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3343 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3344 Select(N.getOperand(0));
3345 SelectAddress(N.getOperand(2), AM);
3346 } else {
3347 SelectAddress(N.getOperand(2), AM);
3348 Select(N.getOperand(0));
3349 }
3350 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3351 return;
3352 }
3353
3354 switch (StoredTy) {
3355 default: assert(0 && "Cannot truncstore this type!");
3356 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003357 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003358 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Chris Lattnerf431ad42005-12-21 07:47:04 +00003359 Opc = X86::FpST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003360 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003361
Chris Lattnere18a4c42005-01-15 05:22:24 +00003362 std::vector<std::pair<unsigned, unsigned> > RP;
3363 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3364 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3365 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3366 std::sort(RP.begin(), RP.end());
3367
Chris Lattner80c5b972005-02-23 05:57:21 +00003368 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003369 for (unsigned i = 0; i != 3; ++i)
3370 switch (RP[2-i].second) {
3371 default: assert(0 && "Unknown operand number!");
3372 case 0: Select(N.getOperand(0)); break;
3373 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3374 case 2: SelectAddress(N.getOperand(2), AM); break;
3375 }
3376
3377 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3378 return;
3379 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003380 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003381 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003382
3383 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3384 Opc = 0;
3385 switch (CN->getValueType(0)) {
3386 default: assert(0 && "Invalid type for operation!");
3387 case MVT::i1:
3388 case MVT::i8: Opc = X86::MOV8mi; break;
3389 case MVT::i16: Opc = X86::MOV16mi; break;
3390 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003391 }
3392 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003393 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3394 Select(N.getOperand(0));
3395 SelectAddress(N.getOperand(2), AM);
3396 } else {
3397 SelectAddress(N.getOperand(2), AM);
3398 Select(N.getOperand(0));
3399 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003400 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3401 return;
3402 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003403 } else if (GlobalAddressSDNode *GA =
3404 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3405 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3406
3407 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3408 Select(N.getOperand(0));
3409 SelectAddress(N.getOperand(2), AM);
3410 } else {
3411 SelectAddress(N.getOperand(2), AM);
3412 Select(N.getOperand(0));
3413 }
Nate Begemana0b5e032005-07-15 00:38:55 +00003414 GlobalValue *GV = GA->getGlobal();
3415 // For Darwin, external and weak symbols are indirect, so we want to load
3416 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003417 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00003418 (GV->hasWeakLinkage() || GV->isExternal())) {
3419 Tmp1 = MakeReg(MVT::i32);
3420 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
3421 .addGlobalAddress(GV, false, 0);
3422 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
3423 } else {
3424 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
3425 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003426 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003427 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003428
3429 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003430 if (TryToFoldLoadOpStore(Node))
3431 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003432
Chris Lattner88c8a232005-01-07 07:49:41 +00003433 switch (N.getOperand(1).getValueType()) {
3434 default: assert(0 && "Cannot store this type!");
3435 case MVT::i1:
3436 case MVT::i8: Opc = X86::MOV8mr; break;
3437 case MVT::i16: Opc = X86::MOV16mr; break;
3438 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003439 case MVT::f32: Opc = X86::MOVSSmr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00003440 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FpST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003441 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003442
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003443 std::vector<std::pair<unsigned, unsigned> > RP;
3444 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3445 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3446 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3447 std::sort(RP.begin(), RP.end());
3448
Chris Lattner80c5b972005-02-23 05:57:21 +00003449 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003450 for (unsigned i = 0; i != 3; ++i)
3451 switch (RP[2-i].second) {
3452 default: assert(0 && "Unknown operand number!");
3453 case 0: Select(N.getOperand(0)); break;
3454 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003455 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003456 }
3457
Chris Lattner88c8a232005-01-07 07:49:41 +00003458 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3459 return;
3460 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003461 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003462 Select(N.getOperand(0));
3463 // Stack amount
3464 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3465 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3466 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00003467 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003468 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003469 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003470 case ISD::MEMSET: {
3471 Select(N.getOperand(0)); // Select the chain.
3472 unsigned Align =
3473 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3474 if (Align == 0) Align = 1;
3475
3476 // Turn the byte code into # iterations
3477 unsigned CountReg;
3478 unsigned Opcode;
3479 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3480 unsigned Val = ValC->getValue() & 255;
3481
3482 // If the value is a constant, then we can potentially use larger sets.
3483 switch (Align & 3) {
3484 case 2: // WORD aligned
3485 CountReg = MakeReg(MVT::i32);
3486 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3487 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3488 } else {
3489 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3490 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3491 }
3492 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3493 Opcode = X86::REP_STOSW;
3494 break;
3495 case 0: // DWORD aligned
3496 CountReg = MakeReg(MVT::i32);
3497 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3498 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3499 } else {
3500 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3501 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3502 }
3503 Val = (Val << 8) | Val;
3504 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3505 Opcode = X86::REP_STOSD;
3506 break;
3507 default: // BYTE aligned
3508 CountReg = SelectExpr(Node->getOperand(3));
3509 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3510 Opcode = X86::REP_STOSB;
3511 break;
3512 }
3513 } else {
3514 // If it's not a constant value we are storing, just fall back. We could
3515 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3516 unsigned ValReg = SelectExpr(Node->getOperand(2));
3517 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3518 CountReg = SelectExpr(Node->getOperand(3));
3519 Opcode = X86::REP_STOSB;
3520 }
3521
Evan Chengae986f12006-01-11 22:15:48 +00003522 // No matter what the alignment is, we put the destination in EDI, and the
3523 // count in ECX.
Chris Lattner36f78482005-01-11 06:14:36 +00003524 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3525 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3526 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3527 BuildMI(BB, Opcode, 0);
3528 return;
3529 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003530 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003531 Select(N.getOperand(0)); // Select the chain.
3532 unsigned Align =
3533 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3534 if (Align == 0) Align = 1;
3535
3536 // Turn the byte code into # iterations
3537 unsigned CountReg;
3538 unsigned Opcode;
3539 switch (Align & 3) {
3540 case 2: // WORD aligned
3541 CountReg = MakeReg(MVT::i32);
3542 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3543 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3544 } else {
3545 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3546 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3547 }
3548 Opcode = X86::REP_MOVSW;
3549 break;
3550 case 0: // DWORD aligned
3551 CountReg = MakeReg(MVT::i32);
3552 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3553 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3554 } else {
3555 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3556 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3557 }
3558 Opcode = X86::REP_MOVSD;
3559 break;
3560 default: // BYTE aligned
3561 CountReg = SelectExpr(Node->getOperand(3));
3562 Opcode = X86::REP_MOVSB;
3563 break;
3564 }
3565
3566 // No matter what the alignment is, we put the source in ESI, the
3567 // destination in EDI, and the count in ECX.
3568 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3569 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3570 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3571 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3572 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3573 BuildMI(BB, Opcode, 0);
3574 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003575 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003576 case ISD::WRITEPORT:
3577 if (Node->getOperand(2).getValueType() != MVT::i16) {
3578 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3579 exit(1);
3580 }
3581 Select(Node->getOperand(0)); // Emit the chain.
3582
3583 Tmp1 = SelectExpr(Node->getOperand(1));
3584 switch (Node->getOperand(1).getValueType()) {
3585 case MVT::i8:
3586 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3587 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3588 break;
3589 case MVT::i16:
3590 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3591 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3592 break;
3593 case MVT::i32:
3594 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3595 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3596 break;
3597 default:
3598 std::cerr << "llvm.writeport: invalid data type for X86 target";
3599 exit(1);
3600 }
3601
3602 // If the port is a single-byte constant, use the immediate form.
3603 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3604 if ((CN->getValue() & 255) == CN->getValue()) {
3605 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3606 return;
3607 }
3608
3609 // Otherwise, move the I/O port address into the DX register.
3610 unsigned Reg = SelectExpr(Node->getOperand(2));
3611 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3612 BuildMI(BB, Opc, 0);
3613 return;
3614 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003615 assert(0 && "Should not be reached!");
3616}
3617
3618
Chris Lattner76ac0682005-11-15 00:40:23 +00003619/// createX86ISelPattern - This pass converts an LLVM function
Chris Lattner88c8a232005-01-07 07:49:41 +00003620/// into a machine code representation using pattern matching and a machine
3621/// description file.
3622///
Chris Lattner76ac0682005-11-15 00:40:23 +00003623FunctionPass *llvm::createX86ISelPattern(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003624 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003625}