blob: 146ee752e98654204be127caf3ebb25b40190f15 [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>
Jeff Cohen407aa012005-01-12 04:29:05 +000037#include <algorithm>
Chris Lattner88c8a232005-01-07 07:49:41 +000038using namespace llvm;
39
Chris Lattnera36117b2005-05-14 06:52:07 +000040//===----------------------------------------------------------------------===//
41// Pattern Matcher Implementation
42//===----------------------------------------------------------------------===//
Chris Lattner88c8a232005-01-07 07:49:41 +000043
Chris Lattnera7acdda2005-01-18 01:06:26 +000044namespace {
45 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
46 /// SDOperand's instead of register numbers for the leaves of the matched
47 /// tree.
48 struct X86ISelAddressMode {
49 enum {
50 RegBase,
51 FrameIndexBase,
52 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +000053
Chris Lattnera7acdda2005-01-18 01:06:26 +000054 struct { // This is really a union, discriminated by BaseType!
55 SDOperand Reg;
56 int FrameIndex;
57 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +000058
Chris Lattnera7acdda2005-01-18 01:06:26 +000059 unsigned Scale;
60 SDOperand IndexReg;
61 unsigned Disp;
62 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +000063
Chris Lattnera7acdda2005-01-18 01:06:26 +000064 X86ISelAddressMode()
65 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
66 }
67 };
68}
Chris Lattner88c8a232005-01-07 07:49:41 +000069
70
71namespace {
72 Statistic<>
73 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
74
75 //===--------------------------------------------------------------------===//
76 /// ISel - X86 specific code to select X86 machine instructions for
77 /// SelectionDAG operations.
78 ///
79 class ISel : public SelectionDAGISel {
80 /// ContainsFPCode - Every instruction we select that uses or defines a FP
81 /// register should set this to true.
82 bool ContainsFPCode;
83
84 /// X86Lowering - This object fully describes how to lower LLVM code to an
85 /// X86-specific SelectionDAG.
86 X86TargetLowering X86Lowering;
87
Chris Lattner0d1f82a2005-01-11 03:11:44 +000088 /// RegPressureMap - This keeps an approximate count of the number of
89 /// registers required to evaluate each node in the graph.
90 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +000091
92 /// ExprMap - As shared expressions are codegen'd, we keep track of which
93 /// vreg the value is produced in, so we only emit one copy of each compiled
94 /// tree.
95 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +000096
Chris Lattnerdd66a412005-05-15 05:46:45 +000097 /// TheDAG - The DAG being selected during Select* operations.
98 SelectionDAG *TheDAG;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000099
100 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanf26625e2005-07-12 01:41:54 +0000101 /// make the right decision when generating code for different targets.
102 const X86Subtarget *Subtarget;
Chris Lattner88c8a232005-01-07 07:49:41 +0000103 public:
104 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Chris Lattner158acab2005-08-05 21:54:27 +0000105 Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattner88c8a232005-01-07 07:49:41 +0000106 }
107
Chris Lattnere1e844c2005-01-21 21:35:14 +0000108 virtual const char *getPassName() const {
109 return "X86 Pattern Instruction Selection";
110 }
111
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000112 unsigned getRegPressure(SDOperand O) {
113 return RegPressureMap[O.Val];
114 }
115 unsigned ComputeRegPressure(SDOperand O);
116
Chris Lattner88c8a232005-01-07 07:49:41 +0000117 /// InstructionSelectBasicBlock - This callback is invoked by
118 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +0000119 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000120
Chris Lattner0b17b452005-05-13 07:38:09 +0000121 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
122
Chris Lattner30607ec2005-01-25 20:03:11 +0000123 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
124 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +0000125 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +0000126 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner29f58192005-01-19 07:37:26 +0000127 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +0000128 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +0000129 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Nate Begeman8d394eb2005-08-03 23:26:28 +0000130 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
131 MVT::ValueType SVT, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +0000132 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000133
134 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
135 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
136 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattnerdd66a412005-05-15 05:46:45 +0000137 bool EmitPotentialTailCall(SDNode *Node);
138 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner88c8a232005-01-07 07:49:41 +0000139 void Select(SDOperand N);
140 };
141}
142
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000143/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
144/// the main function.
145static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
146 MachineFrameInfo *MFI) {
147 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
148 int CWFrameIdx = MFI->CreateStackObject(2, 2);
149 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
150
151 // Set the high part to be 64-bit precision.
152 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
153 CWFrameIdx, 1).addImm(2);
154
155 // Reload the modified control word now.
156 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
157}
158
Chris Lattner0b17b452005-05-13 07:38:09 +0000159void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000160 // If this is main, emit special code for main.
Chris Lattnerb42e9622005-09-14 06:06:45 +0000161 MachineBasicBlock *BB = MF.begin();
Chris Lattner0b17b452005-05-13 07:38:09 +0000162 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
163 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
164}
165
166
Chris Lattner6fba62d62005-01-12 04:21:28 +0000167/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
168/// when it has created a SelectionDAG for us to codegen.
169void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
170 // While we're doing this, keep track of whether we see any FP code for
171 // FP_REG_KILL insertion.
172 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000173 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000174
175 // Scan the PHI nodes that already are inserted into this basic block. If any
176 // of them is a PHI of a floating point value, we need to insert an
177 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000178 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +0000179 if (BB != MF->begin())
180 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
181 I != E; ++I) {
182 assert(I->getOpcode() == X86::PHI &&
183 "Isn't just PHI nodes?");
184 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
185 X86::RFPRegisterClass) {
186 ContainsFPCode = true;
187 break;
188 }
Chris Lattner6fba62d62005-01-12 04:21:28 +0000189 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000190
Chris Lattner6fba62d62005-01-12 04:21:28 +0000191 // Compute the RegPressureMap, which is an approximation for the number of
192 // registers required to compute each node.
193 ComputeRegPressure(DAG.getRoot());
194
Chris Lattnerdd66a412005-05-15 05:46:45 +0000195 TheDAG = &DAG;
196
Chris Lattner6fba62d62005-01-12 04:21:28 +0000197 // Codegen the basic block.
198 Select(DAG.getRoot());
199
Chris Lattnerdd66a412005-05-15 05:46:45 +0000200 TheDAG = 0;
201
Chris Lattner6fba62d62005-01-12 04:21:28 +0000202 // Finally, look at all of the successors of this block. If any contain a PHI
203 // node of FP type, we need to insert an FP_REG_KILL in this block.
204 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
205 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
206 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
207 I != E && I->getOpcode() == X86::PHI; ++I) {
208 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
209 X86::RFPRegisterClass) {
210 ContainsFPCode = true;
211 break;
212 }
213 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000214
Chris Lattner6972c312005-05-09 03:36:39 +0000215 // Final check, check LLVM BB's that are successors to the LLVM BB
216 // corresponding to BB for FP PHI nodes.
217 const BasicBlock *LLVMBB = BB->getBasicBlock();
218 const PHINode *PN;
219 if (!ContainsFPCode)
220 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
221 SI != E && !ContainsFPCode; ++SI)
222 for (BasicBlock::const_iterator II = SI->begin();
223 (PN = dyn_cast<PHINode>(II)); ++II)
224 if (PN->getType()->isFloatingPoint()) {
225 ContainsFPCode = true;
226 break;
227 }
228
Chris Lattner6fba62d62005-01-12 04:21:28 +0000229 // Insert FP_REG_KILL instructions into basic blocks that need them. This
230 // only occurs due to the floating point stackifier not being aggressive
231 // enough to handle arbitrary global stackification.
232 //
233 // Currently we insert an FP_REG_KILL instruction into each block that uses or
234 // defines a floating point virtual register.
235 //
236 // When the global register allocators (like linear scan) finally update live
237 // variable analysis, we can keep floating point values in registers across
238 // basic blocks. This will be a huge win, but we are waiting on the global
239 // allocators before we can do this.
240 //
Chris Lattner472a2652005-03-30 01:10:00 +0000241 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +0000242 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
243 ++NumFPKill;
244 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000245
Chris Lattner6fba62d62005-01-12 04:21:28 +0000246 // Clear state used for selection.
247 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000248 RegPressureMap.clear();
249}
250
251
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000252// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
253// for the number of registers required to compute each node. This is basically
254// computing a generalized form of the Sethi-Ullman number for each node.
255unsigned ISel::ComputeRegPressure(SDOperand O) {
256 SDNode *N = O.Val;
257 unsigned &Result = RegPressureMap[N];
258 if (Result) return Result;
259
Chris Lattner8fea42b2005-01-11 03:37:59 +0000260 // FIXME: Should operations like CALL (which clobber lots o regs) have a
261 // higher fixed cost??
262
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000263 if (N->getNumOperands() == 0) {
264 Result = 1;
265 } else {
266 unsigned MaxRegUse = 0;
267 unsigned NumExtraMaxRegUsers = 0;
268 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
269 unsigned Regs;
270 if (N->getOperand(i).getOpcode() == ISD::Constant)
271 Regs = 0;
272 else
273 Regs = ComputeRegPressure(N->getOperand(i));
274 if (Regs > MaxRegUse) {
275 MaxRegUse = Regs;
276 NumExtraMaxRegUsers = 0;
277 } else if (Regs == MaxRegUse &&
278 N->getOperand(i).getValueType() != MVT::Other) {
279 ++NumExtraMaxRegUsers;
280 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000281 }
Chris Lattnerca318ed2005-01-17 22:56:09 +0000282
283 if (O.getOpcode() != ISD::TokenFactor)
284 Result = MaxRegUse+NumExtraMaxRegUsers;
285 else
Chris Lattnera5d137f2005-01-17 23:02:13 +0000286 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000287 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +0000288
Chris Lattner75bac9f2005-01-11 23:21:30 +0000289 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000290 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000291}
292
Chris Lattner5b04f332005-01-20 16:50:16 +0000293/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
294/// The DAG cannot have cycles in it, by definition, so the visited set is not
295/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
296/// reuse, so it prevents exponential cases.
297///
298static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
299 std::set<SDNode*> &Visited) {
300 if (N == Op) return true; // Found it.
301 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +0000302 if (Node->getNumOperands() == 0 || // Leaf?
303 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +0000304 if (!Visited.insert(Node).second) return false; // Already visited?
305
306 // Recurse for the first N-1 operands.
307 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
308 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
309 return true;
310
311 // Tail recurse for the last operand.
312 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
313}
314
Chris Lattnera7acdda2005-01-18 01:06:26 +0000315X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
316 X86AddressMode Result;
317
318 // If we need to emit two register operands, emit the one with the highest
319 // register pressure first.
320 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
321 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000322 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000323 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000324 std::set<SDNode*> Visited;
325 EmitBaseThenIndex = true;
326 // If Base ends up pointing to Index, we must emit index first. This is
327 // because of the way we fold loads, we may end up doing bad things with
328 // the folded add.
329 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
330 EmitBaseThenIndex = false;
331 } else {
332 std::set<SDNode*> Visited;
333 EmitBaseThenIndex = false;
334 // If Base ends up pointing to Index, we must emit index first. This is
335 // because of the way we fold loads, we may end up doing bad things with
336 // the folded add.
337 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
338 EmitBaseThenIndex = true;
339 }
340
341 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000342 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
343 Result.IndexReg = SelectExpr(IAM.IndexReg);
344 } else {
345 Result.IndexReg = SelectExpr(IAM.IndexReg);
346 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
347 }
Chris Lattner5b04f332005-01-20 16:50:16 +0000348
Chris Lattnera7acdda2005-01-18 01:06:26 +0000349 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
350 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
351 } else if (IAM.IndexReg.Val) {
352 Result.IndexReg = SelectExpr(IAM.IndexReg);
353 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000354
Chris Lattnera7acdda2005-01-18 01:06:26 +0000355 switch (IAM.BaseType) {
356 case X86ISelAddressMode::RegBase:
357 Result.BaseType = X86AddressMode::RegBase;
358 break;
359 case X86ISelAddressMode::FrameIndexBase:
360 Result.BaseType = X86AddressMode::FrameIndexBase;
361 Result.Base.FrameIndex = IAM.Base.FrameIndex;
362 break;
363 default:
364 assert(0 && "Unknown base type!");
365 break;
366 }
367 Result.Scale = IAM.Scale;
368 Result.Disp = IAM.Disp;
369 Result.GV = IAM.GV;
370 return Result;
371}
372
373/// SelectAddress - Pattern match the maximal addressing mode for this node and
374/// emit all of the leaf registers.
375void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
376 X86ISelAddressMode IAM;
377 MatchAddress(N, IAM);
378 AM = SelectAddrExprs(IAM);
379}
380
381/// MatchAddress - Add the specified node to the specified addressing mode,
382/// returning true if it cannot be done. This just pattern matches for the
383/// addressing mode, it does not cause any code to be emitted. For that, use
384/// SelectAddress.
385bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000386 switch (N.getOpcode()) {
387 default: break;
388 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +0000389 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
390 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +0000391 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
392 return false;
393 }
394 break;
395 case ISD::GlobalAddress:
396 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +0000397 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
398 // For Darwin, external and weak symbols are indirect, so we want to load
399 // the value at address GV, not the value of GV itself. This means that
400 // the GlobalAddress must be in the base or index register of the address,
401 // not the GV offset field.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000402 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +0000403 (GV->hasWeakLinkage() || GV->isExternal())) {
404 break;
405 } else {
406 AM.GV = GV;
407 return false;
408 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000409 }
410 break;
411 case ISD::Constant:
412 AM.Disp += cast<ConstantSDNode>(N)->getValue();
413 return false;
414 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000415 // We might have folded the load into this shift, so don't regen the value
416 // if so.
417 if (ExprMap.count(N)) break;
418
Chris Lattnera7acdda2005-01-18 01:06:26 +0000419 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +0000420 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
421 unsigned Val = CN->getValue();
422 if (Val == 1 || Val == 2 || Val == 3) {
423 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000424 SDOperand ShVal = N.Val->getOperand(0);
425
426 // Okay, we know that we have a scale by now. However, if the scaled
427 // value is an add of something and a constant, we can fold the
428 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000429 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000430 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000431 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000432 ConstantSDNode *AddVal =
433 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
434 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +0000435 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000436 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000437 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000438 return false;
439 }
440 }
441 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000442 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000443 // We might have folded the load into this mul, so don't regen the value if
444 // so.
445 if (ExprMap.count(N)) break;
446
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000447 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +0000448 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
449 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000450 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
451 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
452 AM.Scale = unsigned(CN->getValue())-1;
453
454 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000455 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000456
457 // Okay, we know that we have a scale by now. However, if the scaled
458 // value is an add of something and a constant, we can fold the
459 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000460 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000461 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000462 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000463 ConstantSDNode *AddVal =
464 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
465 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +0000466 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000467 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000468 }
469
470 AM.IndexReg = AM.Base.Reg = Reg;
471 return false;
472 }
473 break;
Chris Lattner88c8a232005-01-07 07:49:41 +0000474
475 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +0000476 // We might have folded the load into this mul, so don't regen the value if
477 // so.
478 if (ExprMap.count(N)) break;
479
Chris Lattnera7acdda2005-01-18 01:06:26 +0000480 X86ISelAddressMode Backup = AM;
481 if (!MatchAddress(N.Val->getOperand(0), AM) &&
482 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +0000483 return false;
484 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000485 if (!MatchAddress(N.Val->getOperand(1), AM) &&
486 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +0000487 return false;
488 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +0000489 break;
490 }
491 }
492
Chris Lattner378262d2005-01-11 04:40:19 +0000493 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +0000494 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +0000495 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000496 if (AM.IndexReg.Val == 0) {
497 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +0000498 AM.Scale = 1;
499 return false;
500 }
501
502 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +0000503 return true;
Chris Lattner378262d2005-01-11 04:40:19 +0000504 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000505
506 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000507 AM.BaseType = X86ISelAddressMode::RegBase;
508 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +0000509 return false;
510}
511
512/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
513/// assuming that the temporary registers are in the 8-bit register class.
514///
515/// Tmp1 = setcc1
516/// Tmp2 = setcc2
517/// DestReg = logicalop Tmp1, Tmp2
518///
519static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
520 unsigned SetCC2, unsigned LogicalOp,
521 unsigned DestReg) {
522 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
523 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
524 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
525 BuildMI(BB, SetCC1, 0, Tmp1);
526 BuildMI(BB, SetCC2, 0, Tmp2);
527 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
528}
529
530/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
531/// condition codes match the specified SetCCOpcode. Note that some conditions
532/// require multiple instructions to generate the correct value.
533static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
534 ISD::CondCode SetCCOpcode, bool isFP) {
535 unsigned Opc;
536 if (!isFP) {
537 switch (SetCCOpcode) {
538 default: assert(0 && "Illegal integer SetCC!");
539 case ISD::SETEQ: Opc = X86::SETEr; break;
540 case ISD::SETGT: Opc = X86::SETGr; break;
541 case ISD::SETGE: Opc = X86::SETGEr; break;
542 case ISD::SETLT: Opc = X86::SETLr; break;
543 case ISD::SETLE: Opc = X86::SETLEr; break;
544 case ISD::SETNE: Opc = X86::SETNEr; break;
545 case ISD::SETULT: Opc = X86::SETBr; break;
546 case ISD::SETUGT: Opc = X86::SETAr; break;
547 case ISD::SETULE: Opc = X86::SETBEr; break;
548 case ISD::SETUGE: Opc = X86::SETAEr; break;
549 }
550 } else {
551 // On a floating point condition, the flags are set as follows:
552 // ZF PF CF op
553 // 0 | 0 | 0 | X > Y
554 // 0 | 0 | 1 | X < Y
555 // 1 | 0 | 0 | X == Y
556 // 1 | 1 | 1 | unordered
557 //
558 switch (SetCCOpcode) {
559 default: assert(0 && "Invalid FP setcc!");
560 case ISD::SETUEQ:
561 case ISD::SETEQ:
562 Opc = X86::SETEr; // True if ZF = 1
563 break;
564 case ISD::SETOGT:
565 case ISD::SETGT:
566 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
567 break;
568 case ISD::SETOGE:
569 case ISD::SETGE:
570 Opc = X86::SETAEr; // True if CF = 0
571 break;
572 case ISD::SETULT:
573 case ISD::SETLT:
574 Opc = X86::SETBr; // True if CF = 1
575 break;
576 case ISD::SETULE:
577 case ISD::SETLE:
578 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
579 break;
580 case ISD::SETONE:
581 case ISD::SETNE:
582 Opc = X86::SETNEr; // True if ZF = 0
583 break;
584 case ISD::SETUO:
585 Opc = X86::SETPr; // True if PF = 1
586 break;
587 case ISD::SETO:
588 Opc = X86::SETNPr; // True if PF = 0
589 break;
590 case ISD::SETOEQ: // !PF & ZF
591 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
592 return;
593 case ISD::SETOLT: // !PF & CF
594 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
595 return;
596 case ISD::SETOLE: // !PF & (CF || ZF)
597 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
598 return;
599 case ISD::SETUGT: // PF | (!ZF & !CF)
600 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
601 return;
602 case ISD::SETUGE: // PF | !CF
603 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
604 return;
605 case ISD::SETUNE: // PF | !ZF
606 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
607 return;
608 }
609 }
610 BuildMI(BB, Opc, 0, DestReg);
611}
612
613
614/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
615/// the Dest block if the Cond condition is true. If we cannot fold this
616/// condition into the branch, return true.
617///
Chris Lattner37ed2852005-01-11 04:06:27 +0000618bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
619 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000620 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
621 // B) using two conditional branches instead of one condbr, two setcc's, and
622 // an or.
623 if ((Cond.getOpcode() == ISD::OR ||
624 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
625 // And and or set the flags for us, so there is no need to emit a TST of the
626 // result. It is only safe to do this if there is only a single use of the
627 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +0000628 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000629 SelectExpr(Cond);
630 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
631 return false;
632 }
633
634 // Codegen br not C -> JE.
635 if (Cond.getOpcode() == ISD::XOR)
636 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
637 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +0000638 unsigned CondR;
639 if (getRegPressure(Chain) > getRegPressure(Cond)) {
640 Select(Chain);
641 CondR = SelectExpr(Cond.Val->getOperand(0));
642 } else {
643 CondR = SelectExpr(Cond.Val->getOperand(0));
644 Select(Chain);
645 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000646 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
647 BuildMI(BB, X86::JE, 1).addMBB(Dest);
648 return false;
649 }
650
Chris Lattner6ec77452005-08-09 20:21:10 +0000651 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner88c8a232005-01-07 07:49:41 +0000652 return true; // Can only handle simple setcc's so far.
Chris Lattner6ec77452005-08-09 20:21:10 +0000653 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner88c8a232005-01-07 07:49:41 +0000654
655 unsigned Opc;
656
657 // Handle integer conditions first.
Chris Lattner6ec77452005-08-09 20:21:10 +0000658 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
659 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000660 default: assert(0 && "Illegal integer SetCC!");
661 case ISD::SETEQ: Opc = X86::JE; break;
662 case ISD::SETGT: Opc = X86::JG; break;
663 case ISD::SETGE: Opc = X86::JGE; break;
664 case ISD::SETLT: Opc = X86::JL; break;
665 case ISD::SETLE: Opc = X86::JLE; break;
666 case ISD::SETNE: Opc = X86::JNE; break;
667 case ISD::SETULT: Opc = X86::JB; break;
668 case ISD::SETUGT: Opc = X86::JA; break;
669 case ISD::SETULE: Opc = X86::JBE; break;
670 case ISD::SETUGE: Opc = X86::JAE; break;
671 }
Chris Lattner37ed2852005-01-11 04:06:27 +0000672 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000673 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000674 BuildMI(BB, Opc, 1).addMBB(Dest);
675 return false;
676 }
677
Chris Lattner88c8a232005-01-07 07:49:41 +0000678 unsigned Opc2 = 0; // Second branch if needed.
679
680 // On a floating point condition, the flags are set as follows:
681 // ZF PF CF op
682 // 0 | 0 | 0 | X > Y
683 // 0 | 0 | 1 | X < Y
684 // 1 | 0 | 0 | X == Y
685 // 1 | 1 | 1 | unordered
686 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000687 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000688 default: assert(0 && "Invalid FP setcc!");
689 case ISD::SETUEQ:
690 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
691 case ISD::SETOGT:
692 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
693 case ISD::SETOGE:
694 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
695 case ISD::SETULT:
696 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
697 case ISD::SETULE:
698 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
699 case ISD::SETONE:
700 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
701 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
702 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
703 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
704 Opc = X86::JA; // ZF = 0 & CF = 0
705 Opc2 = X86::JP; // PF = 1
706 break;
707 case ISD::SETUGE: // PF = 1 | CF = 0
708 Opc = X86::JAE; // CF = 0
709 Opc2 = X86::JP; // PF = 1
710 break;
711 case ISD::SETUNE: // PF = 1 | ZF = 0
712 Opc = X86::JNE; // ZF = 0
713 Opc2 = X86::JP; // PF = 1
714 break;
715 case ISD::SETOEQ: // PF = 0 & ZF = 1
716 //X86::JNP, X86::JE
717 //X86::AND8rr
718 return true; // FIXME: Emit more efficient code for this branch.
719 case ISD::SETOLT: // PF = 0 & CF = 1
720 //X86::JNP, X86::JB
721 //X86::AND8rr
722 return true; // FIXME: Emit more efficient code for this branch.
723 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
724 //X86::JNP, X86::JBE
725 //X86::AND8rr
726 return true; // FIXME: Emit more efficient code for this branch.
727 }
728
Chris Lattner37ed2852005-01-11 04:06:27 +0000729 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000730 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000731 BuildMI(BB, Opc, 1).addMBB(Dest);
732 if (Opc2)
733 BuildMI(BB, Opc2, 1).addMBB(Dest);
734 return false;
735}
736
Chris Lattner1d13a922005-01-10 22:10:13 +0000737/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman8d394eb2005-08-03 23:26:28 +0000738/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner1d13a922005-01-10 22:10:13 +0000739///
Nate Begeman8d394eb2005-08-03 23:26:28 +0000740void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
741 MVT::ValueType SVT, unsigned RDest) {
742 unsigned RTrue, RFalse;
Chris Lattner1d13a922005-01-10 22:10:13 +0000743 enum Condition {
744 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
745 NOT_SET
746 } CondCode = NOT_SET;
747
748 static const unsigned CMOVTAB16[] = {
749 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
750 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000751 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000752 };
753 static const unsigned CMOVTAB32[] = {
754 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
755 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000756 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000757 };
758 static const unsigned CMOVTABFP[] = {
Chris Lattnerf431ad42005-12-21 07:47:04 +0000759 X86::FpCMOVE, X86::FpCMOVNE, /*missing*/0, /*missing*/0,
760 /*missing*/0, /*missing*/ 0, X86::FpCMOVB, X86::FpCMOVBE,
761 X86::FpCMOVA, X86::FpCMOVAE, X86::FpCMOVP, X86::FpCMOVNP
Chris Lattner1d13a922005-01-10 22:10:13 +0000762 };
Nate Begemana0b5e032005-07-15 00:38:55 +0000763 static const int SSE_CMOVTAB[] = {
Nate Begeman8d394eb2005-08-03 23:26:28 +0000764 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
765 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
766 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begeman8a093362005-07-06 18:59:04 +0000767 };
Nate Begeman8d394eb2005-08-03 23:26:28 +0000768
Chris Lattner6ec77452005-08-09 20:21:10 +0000769 if (Cond.getOpcode() == ISD::SETCC) {
770 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
771 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
772 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000773 default: assert(0 && "Unknown integer comparison!");
774 case ISD::SETEQ: CondCode = EQ; break;
775 case ISD::SETGT: CondCode = GT; break;
776 case ISD::SETGE: CondCode = GE; break;
777 case ISD::SETLT: CondCode = LT; break;
778 case ISD::SETLE: CondCode = LE; break;
779 case ISD::SETNE: CondCode = NE; break;
780 case ISD::SETULT: CondCode = B; break;
781 case ISD::SETUGT: CondCode = A; break;
782 case ISD::SETULE: CondCode = BE; break;
783 case ISD::SETUGE: CondCode = AE; break;
784 }
785 } else {
786 // On a floating point condition, the flags are set as follows:
787 // ZF PF CF op
788 // 0 | 0 | 0 | X > Y
789 // 0 | 0 | 1 | X < Y
790 // 1 | 0 | 0 | X == Y
791 // 1 | 1 | 1 | unordered
792 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000793 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000794 default: assert(0 && "Unknown FP comparison!");
795 case ISD::SETUEQ:
796 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
797 case ISD::SETOGT:
798 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
799 case ISD::SETOGE:
800 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
801 case ISD::SETULT:
802 case ISD::SETLT: CondCode = B; break; // True if CF = 1
803 case ISD::SETULE:
804 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
805 case ISD::SETONE:
806 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
807 case ISD::SETUO: CondCode = P; break; // True if PF = 1
808 case ISD::SETO: CondCode = NP; break; // True if PF = 0
809 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
810 case ISD::SETUGE: // PF = 1 | CF = 0
811 case ISD::SETUNE: // PF = 1 | ZF = 0
812 case ISD::SETOEQ: // PF = 0 & ZF = 1
813 case ISD::SETOLT: // PF = 0 & CF = 1
814 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
815 // We cannot emit this comparison as a single cmov.
816 break;
817 }
818 }
Chris Lattner6ec77452005-08-09 20:21:10 +0000819
Chris Lattner1d13a922005-01-10 22:10:13 +0000820
Chris Lattner6ec77452005-08-09 20:21:10 +0000821 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
822 // code above and one of the results of the select is +0.0, then we can fake
823 // it up through a clever AND with mask. Otherwise, we will fall through to
824 // the code below that will use a PHI node to select the right value.
825 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
826 if (Cond.getOperand(0).getValueType() == SVT &&
827 NOT_SET != CondCode) {
828 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
829 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
830 bool TrueZero = CT && CT->isExactlyValue(0.0);
831 bool FalseZero = CF && CF->isExactlyValue(0.0);
832 if (TrueZero || FalseZero) {
833 SDOperand LHS = Cond.getOperand(0);
834 SDOperand RHS = Cond.getOperand(1);
835
836 // Select the two halves of the condition
837 unsigned RLHS, RRHS;
838 if (getRegPressure(LHS) > getRegPressure(RHS)) {
839 RLHS = SelectExpr(LHS);
840 RRHS = SelectExpr(RHS);
841 } else {
842 RRHS = SelectExpr(RHS);
843 RLHS = SelectExpr(LHS);
844 }
845
846 // Emit the comparison and generate a mask from it
847 unsigned MaskReg = MakeReg(SVT);
848 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
849 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
850 .addImm(SSE_CMOVTAB[CondCode]);
851
852 if (TrueZero) {
853 RFalse = SelectExpr(False);
854 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
855 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
856 } else {
857 RTrue = SelectExpr(True);
858 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
859 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
860 }
861 return;
Nate Begeman8d394eb2005-08-03 23:26:28 +0000862 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000863 }
Nate Begeman8a093362005-07-06 18:59:04 +0000864 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000865 }
866
867 // Select the true and false values for use in both the SSE PHI case, and the
868 // integer or x87 cmov cases below.
869 if (getRegPressure(True) > getRegPressure(False)) {
870 RTrue = SelectExpr(True);
871 RFalse = SelectExpr(False);
872 } else {
873 RFalse = SelectExpr(False);
874 RTrue = SelectExpr(True);
875 }
876
877 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
878 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
879 // node to select between the true and false values.
880 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
881 // FIXME: emit a direct compare and branch rather than setting a cond reg
882 // and testing it.
883 unsigned CondReg = SelectExpr(Cond);
884 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
885
886 // Create an iterator with which to insert the MBB for copying the false
887 // value and the MBB to hold the PHI instruction for this SetCC.
888 MachineBasicBlock *thisMBB = BB;
889 const BasicBlock *LLVM_BB = BB->getBasicBlock();
890 ilist<MachineBasicBlock>::iterator It = BB;
891 ++It;
892
893 // thisMBB:
894 // ...
895 // TrueVal = ...
896 // cmpTY ccX, r1, r2
897 // bCC sinkMBB
898 // fallthrough --> copy0MBB
899 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
900 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
901 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
902 MachineFunction *F = BB->getParent();
903 F->getBasicBlockList().insert(It, copy0MBB);
904 F->getBasicBlockList().insert(It, sinkMBB);
905 // Update machine-CFG edges
906 BB->addSuccessor(copy0MBB);
907 BB->addSuccessor(sinkMBB);
908
909 // copy0MBB:
910 // %FalseValue = ...
911 // # fallthrough to sinkMBB
912 BB = copy0MBB;
913 // Update machine-CFG edges
914 BB->addSuccessor(sinkMBB);
915
916 // sinkMBB:
917 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
918 // ...
919 BB = sinkMBB;
920 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
921 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begeman8a093362005-07-06 18:59:04 +0000922 return;
923 }
924
Chris Lattner1d13a922005-01-10 22:10:13 +0000925 unsigned Opc = 0;
926 if (CondCode != NOT_SET) {
927 switch (SVT) {
928 default: assert(0 && "Cannot select this type!");
929 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
930 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +0000931 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000932 }
933 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000934
Chris Lattner1d13a922005-01-10 22:10:13 +0000935 // Finally, if we weren't able to fold this, just emit the condition and test
936 // it.
937 if (CondCode == NOT_SET || Opc == 0) {
938 // Get the condition into the zero flag.
939 unsigned CondReg = SelectExpr(Cond);
940 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
941
942 switch (SVT) {
943 default: assert(0 && "Cannot select this type!");
944 case MVT::i16: Opc = X86::CMOVE16rr; break;
945 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +0000946 case MVT::f64: Opc = X86::FpCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000947 }
948 } else {
949 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +0000950 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +0000951 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +0000952 }
953 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
954}
955
Chris Lattner3be6cd52005-01-17 01:34:14 +0000956void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000957 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +0000958 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
959 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +0000960 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +0000961 switch (RHS.getValueType()) {
962 default: break;
963 case MVT::i1:
964 case MVT::i8: Opc = X86::CMP8mi; break;
965 case MVT::i16: Opc = X86::CMP16mi; break;
966 case MVT::i32: Opc = X86::CMP32mi; break;
967 }
968 if (Opc) {
969 X86AddressMode AM;
970 EmitFoldedLoad(LHS, AM);
971 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
972 return;
973 }
974 }
975
Chris Lattner88c8a232005-01-07 07:49:41 +0000976 switch (RHS.getValueType()) {
977 default: break;
978 case MVT::i1:
979 case MVT::i8: Opc = X86::CMP8ri; break;
980 case MVT::i16: Opc = X86::CMP16ri; break;
981 case MVT::i32: Opc = X86::CMP32ri; break;
982 }
983 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000984 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +0000985 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
986 return;
987 }
Chris Lattner720a62e2005-01-14 22:37:41 +0000988 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +0000989 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
990 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +0000991 unsigned Reg = SelectExpr(LHS);
Chris Lattnerf431ad42005-12-21 07:47:04 +0000992 BuildMI(BB, X86::FpTST, 1).addReg(Reg);
Chris Lattner720a62e2005-01-14 22:37:41 +0000993 BuildMI(BB, X86::FNSTSW8r, 0);
994 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +0000995 return;
Chris Lattner720a62e2005-01-14 22:37:41 +0000996 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000997 }
998
Chris Lattner2cfce682005-01-12 02:02:48 +0000999 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001000 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001001 switch (RHS.getValueType()) {
1002 default: break;
1003 case MVT::i1:
1004 case MVT::i8: Opc = X86::CMP8mr; break;
1005 case MVT::i16: Opc = X86::CMP16mr; break;
1006 case MVT::i32: Opc = X86::CMP32mr; break;
1007 }
1008 if (Opc) {
1009 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001010 EmitFoldedLoad(LHS, AM);
1011 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001012 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1013 return;
1014 }
1015 }
1016
Chris Lattner88c8a232005-01-07 07:49:41 +00001017 switch (LHS.getValueType()) {
1018 default: assert(0 && "Cannot compare this value!");
1019 case MVT::i1:
1020 case MVT::i8: Opc = X86::CMP8rr; break;
1021 case MVT::i16: Opc = X86::CMP16rr; break;
1022 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001023 case MVT::f32: Opc = X86::UCOMISSrr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00001024 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FpUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001025 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001026 unsigned Tmp1, Tmp2;
1027 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1028 Tmp1 = SelectExpr(LHS);
1029 Tmp2 = SelectExpr(RHS);
1030 } else {
1031 Tmp2 = SelectExpr(RHS);
1032 Tmp1 = SelectExpr(LHS);
1033 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001034 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1035}
1036
Chris Lattner62b22422005-01-11 21:19:59 +00001037/// isFoldableLoad - Return true if this is a load instruction that can safely
1038/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001039bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1040 if (Op.getOpcode() == ISD::LOAD) {
1041 // FIXME: currently can't fold constant pool indexes.
1042 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1043 return false;
1044 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00001045 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001046 // FIXME: currently can't fold constant pool indexes.
1047 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1048 return false;
1049 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001050 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001051 }
Chris Lattner62b22422005-01-11 21:19:59 +00001052
1053 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001054 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1055 if (ExprMap.count(Op.getValue(1))) return false;
1056 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001057 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001058
Chris Lattnera56d29d2005-01-17 06:26:58 +00001059 // If there is not just one use of its value, we cannot fold.
1060 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1061
1062 // Finally, we cannot fold the load into the operation if this would induce a
1063 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1064 // operand of the operation we are folding the load into) can possible use the
1065 // chain node defined by the load.
1066 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1067 std::set<SDNode*> Visited;
1068 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1069 return false;
1070 }
1071 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001072}
1073
Chris Lattnera56d29d2005-01-17 06:26:58 +00001074
Chris Lattner62b22422005-01-11 21:19:59 +00001075/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1076/// and compute the address being loaded into AM.
1077void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1078 SDOperand Chain = Op.getOperand(0);
1079 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001080
Chris Lattner62b22422005-01-11 21:19:59 +00001081 if (getRegPressure(Chain) > getRegPressure(Address)) {
1082 Select(Chain);
1083 SelectAddress(Address, AM);
1084 } else {
1085 SelectAddress(Address, AM);
1086 Select(Chain);
1087 }
1088
1089 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001090 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1091 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00001092 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00001093 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00001094}
1095
Chris Lattner29f58192005-01-19 07:37:26 +00001096// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1097// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1098// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1099// return true.
1100bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001101 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1102 // good!
1103 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1104 std::swap(Op1, Op2); // Op1 is the SHL now.
1105 } else {
1106 return false; // No match
1107 }
1108
1109 SDOperand ShlVal = Op1.getOperand(0);
1110 SDOperand ShlAmt = Op1.getOperand(1);
1111 SDOperand ShrVal = Op2.getOperand(0);
1112 SDOperand ShrAmt = Op2.getOperand(1);
1113
Chris Lattner29f58192005-01-19 07:37:26 +00001114 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1115
Chris Lattner41fe2012005-01-19 06:18:43 +00001116 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1117 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1118 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00001119 if (SubCST->getValue() == RegSize) {
1120 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00001121 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00001122 if (ShrVal == ShlVal) {
1123 unsigned Reg, ShAmt;
1124 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1125 Reg = SelectExpr(ShrVal);
1126 ShAmt = SelectExpr(ShrAmt);
1127 } else {
1128 ShAmt = SelectExpr(ShrAmt);
1129 Reg = SelectExpr(ShrVal);
1130 }
1131 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1132 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1133 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1134 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1135 return true;
1136 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001137 unsigned AReg, BReg;
1138 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001139 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001140 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001141 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00001142 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001143 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001144 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00001145 unsigned ShAmt = SelectExpr(ShrAmt);
1146 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1147 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1148 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00001149 return true;
1150 }
1151 }
1152
Chris Lattnerde87d1462005-01-19 08:07:05 +00001153 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1154 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1155 if (SubCST->getValue() == RegSize) {
1156 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1157 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1158 if (ShrVal == ShlVal) {
1159 unsigned Reg, ShAmt;
1160 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1161 Reg = SelectExpr(ShrVal);
1162 ShAmt = SelectExpr(ShlAmt);
1163 } else {
1164 ShAmt = SelectExpr(ShlAmt);
1165 Reg = SelectExpr(ShrVal);
1166 }
1167 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1168 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1169 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1170 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1171 return true;
1172 } else if (RegSize != 8) {
1173 unsigned AReg, BReg;
1174 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00001175 AReg = SelectExpr(ShlVal);
1176 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001177 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00001178 BReg = SelectExpr(ShrVal);
1179 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001180 }
1181 unsigned ShAmt = SelectExpr(ShlAmt);
1182 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1183 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1184 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1185 return true;
1186 }
1187 }
Chris Lattner41fe2012005-01-19 06:18:43 +00001188
Chris Lattnerde87d1462005-01-19 08:07:05 +00001189 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1190 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1191 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1192 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1193 // (A >> 5) | (A << 27) --> ROR A, 5
1194 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1195 if (ShrVal == ShlVal) {
1196 unsigned Reg = SelectExpr(ShrVal);
1197 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1198 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1199 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1200 return true;
1201 } else if (RegSize != 8) {
1202 unsigned AReg, BReg;
1203 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001204 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001205 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001206 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001207 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001208 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001209 }
1210 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1211 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1212 .addImm(ShrCst->getValue());
1213 return true;
1214 }
1215 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001216
Chris Lattner41fe2012005-01-19 06:18:43 +00001217 return false;
1218}
1219
Chris Lattner88c8a232005-01-07 07:49:41 +00001220unsigned ISel::SelectExpr(SDOperand N) {
1221 unsigned Result;
Chris Lattner9982da22005-10-02 16:29:36 +00001222 unsigned Tmp1 = 0, Tmp2 = 0, Tmp3 = 0, Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001223 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00001224 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001225
Chris Lattner720a62e2005-01-14 22:37:41 +00001226 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7c762782005-08-16 21:56:37 +00001227 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1228 // Just use the specified register as our input if we can.
1229 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == X86::ESP)
1230 return Reg;
Chris Lattner720a62e2005-01-14 22:37:41 +00001231 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001232
Chris Lattner62b22422005-01-11 21:19:59 +00001233 unsigned &Reg = ExprMap[N];
1234 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001235
Chris Lattnera31d4c72005-04-02 04:01:14 +00001236 switch (N.getOpcode()) {
1237 default:
Chris Lattner62b22422005-01-11 21:19:59 +00001238 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00001239 MakeReg(N.getValueType()) : 1;
1240 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00001241 case X86ISD::TAILCALL:
1242 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00001243 // If this is a call instruction, make sure to prepare ALL of the result
1244 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00001245 ExprMap[N.getValue(0)] = 1;
1246 if (Node->getNumValues() > 1) {
1247 Result = MakeReg(Node->getValueType(1));
1248 ExprMap[N.getValue(1)] = Result;
1249 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00001250 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00001251 } else {
1252 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00001253 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00001254 break;
1255 case ISD::ADD_PARTS:
1256 case ISD::SUB_PARTS:
1257 case ISD::SHL_PARTS:
1258 case ISD::SRL_PARTS:
1259 case ISD::SRA_PARTS:
1260 Result = MakeReg(Node->getValueType(0));
1261 ExprMap[N.getValue(0)] = Result;
1262 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1263 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1264 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001265 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001266
Chris Lattner88c8a232005-01-07 07:49:41 +00001267 switch (N.getOpcode()) {
1268 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00001269 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00001270 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00001271 case ISD::FP_EXTEND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001272 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman8a093362005-07-06 18:59:04 +00001273 Tmp1 = SelectExpr(N.getOperand(0));
1274 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
1275 return Result;
Nate Begemana0b5e032005-07-15 00:38:55 +00001276 case ISD::FP_ROUND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001277 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemana0b5e032005-07-15 00:38:55 +00001278 Tmp1 = SelectExpr(N.getOperand(0));
1279 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
1280 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001281 case ISD::CopyFromReg:
1282 Select(N.getOperand(0));
1283 if (Result == 1) {
1284 Reg = Result = ExprMap[N.getValue(0)] =
1285 MakeReg(N.getValue(0).getValueType());
1286 }
Chris Lattner7c762782005-08-16 21:56:37 +00001287 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001288 switch (Node->getValueType(0)) {
1289 default: assert(0 && "Cannot CopyFromReg this!");
1290 case MVT::i1:
1291 case MVT::i8:
Chris Lattner7c762782005-08-16 21:56:37 +00001292 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001293 return Result;
1294 case MVT::i16:
Chris Lattner7c762782005-08-16 21:56:37 +00001295 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001296 return Result;
1297 case MVT::i32:
Chris Lattner7c762782005-08-16 21:56:37 +00001298 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001299 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001300 }
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001301
Chris Lattner88c8a232005-01-07 07:49:41 +00001302 case ISD::FrameIndex:
1303 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1304 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1305 return Result;
1306 case ISD::ConstantPool:
Chris Lattnerc30405e2005-08-26 17:15:30 +00001307 Tmp1 = BB->getParent()->getConstantPool()->
1308 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner88c8a232005-01-07 07:49:41 +00001309 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1310 return Result;
1311 case ISD::ConstantFP:
Nate Begeman8d394eb2005-08-03 23:26:28 +00001312 if (X86ScalarSSE) {
1313 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
1314 "SSE only supports +0.0");
1315 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
1316 BuildMI(BB, Opc, 0, Result);
1317 return Result;
1318 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001319 ContainsFPCode = true;
1320 Tmp1 = Result; // Intermediate Register
1321 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1322 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1323 Tmp1 = MakeReg(MVT::f64);
1324
1325 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1326 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
Chris Lattnerf431ad42005-12-21 07:47:04 +00001327 BuildMI(BB, X86::FpLD0, 0, Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001328 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1329 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
Chris Lattnerf431ad42005-12-21 07:47:04 +00001330 BuildMI(BB, X86::FpLD1, 0, Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001331 else
1332 assert(0 && "Unexpected constant!");
1333 if (Tmp1 != Result)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001334 BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001335 return Result;
1336 case ISD::Constant:
1337 switch (N.getValueType()) {
1338 default: assert(0 && "Cannot use constants of this type!");
1339 case MVT::i1:
1340 case MVT::i8: Opc = X86::MOV8ri; break;
1341 case MVT::i16: Opc = X86::MOV16ri; break;
1342 case MVT::i32: Opc = X86::MOV32ri; break;
1343 }
1344 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1345 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001346 case ISD::UNDEF:
1347 if (Node->getValueType(0) == MVT::f64) {
1348 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
Chris Lattnerf431ad42005-12-21 07:47:04 +00001349 BuildMI(BB, X86::FpLD0, 0, Result);
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001350 } else {
1351 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1352 }
1353 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001354 case ISD::GlobalAddress: {
1355 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00001356 // For Darwin, external and weak symbols are indirect, so we want to load
1357 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001358 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001359 (GV->hasWeakLinkage() || GV->isExternal())) {
1360 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
1361 .addGlobalAddress(GV, false, 0);
1362 } else {
1363 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1364 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001365 return Result;
1366 }
1367 case ISD::ExternalSymbol: {
1368 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1369 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1370 return Result;
1371 }
Chris Lattner210975c2005-09-02 00:16:09 +00001372 case ISD::ANY_EXTEND: // treat any extend like zext
Chris Lattner88c8a232005-01-07 07:49:41 +00001373 case ISD::ZERO_EXTEND: {
1374 int DestIs16 = N.getValueType() == MVT::i16;
1375 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001376
1377 // FIXME: This hack is here for zero extension casts from bool to i8. This
1378 // would not be needed if bools were promoted by Legalize.
1379 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001380 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001381 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1382 return Result;
1383 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001384
Chris Lattnera56d29d2005-01-17 06:26:58 +00001385 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001386 static const unsigned Opc[3] = {
1387 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1388 };
1389
1390 X86AddressMode AM;
1391 EmitFoldedLoad(N.getOperand(0), AM);
1392 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001393
Chris Lattnerb0eef822005-01-11 23:33:00 +00001394 return Result;
1395 }
1396
Chris Lattner88c8a232005-01-07 07:49:41 +00001397 static const unsigned Opc[3] = {
1398 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1399 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001400 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001401 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1402 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001403 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001404 case ISD::SIGN_EXTEND: {
1405 int DestIs16 = N.getValueType() == MVT::i16;
1406 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1407
Chris Lattner282781c2005-01-09 18:52:44 +00001408 // FIXME: Legalize should promote bools to i8!
1409 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1410 "Sign extend from bool not implemented!");
1411
Chris Lattnera56d29d2005-01-17 06:26:58 +00001412 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001413 static const unsigned Opc[3] = {
1414 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1415 };
1416
1417 X86AddressMode AM;
1418 EmitFoldedLoad(N.getOperand(0), AM);
1419 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1420 return Result;
1421 }
1422
Chris Lattner88c8a232005-01-07 07:49:41 +00001423 static const unsigned Opc[3] = {
1424 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1425 };
1426 Tmp1 = SelectExpr(N.getOperand(0));
1427 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1428 return Result;
1429 }
1430 case ISD::TRUNCATE:
1431 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1432 // a move out of AX or AL.
1433 switch (N.getOperand(0).getValueType()) {
1434 default: assert(0 && "Unknown truncate!");
1435 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1436 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1437 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1438 }
1439 Tmp1 = SelectExpr(N.getOperand(0));
1440 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1441
1442 switch (N.getValueType()) {
1443 default: assert(0 && "Unknown truncate!");
1444 case MVT::i1:
1445 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1446 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1447 }
1448 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1449 return Result;
1450
Chris Lattner507a2752005-07-16 00:28:20 +00001451 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00001452 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1453 unsigned PromoteOpcode = 0;
1454
Nate Begeman7e74c832005-07-16 02:02:34 +00001455 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00001456 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00001457 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00001458 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1459 return Result;
1460 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001461
Chris Lattnere44e6d12005-01-11 03:50:45 +00001462 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00001463
Chris Lattner282781c2005-01-09 18:52:44 +00001464 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00001465 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00001466 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1467 MachineFunction *F = BB->getParent();
1468 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1469
1470 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00001471 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00001472 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001473 addFrameReference(BuildMI(BB, X86::FpILD32m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001474 break;
1475 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00001476 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001477 addFrameReference(BuildMI(BB, X86::FpILD16m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001478 break;
1479 default: break; // No promotion required.
1480 }
Chris Lattner507a2752005-07-16 00:28:20 +00001481 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00001482 }
Chris Lattner4738d1b2005-07-30 00:05:54 +00001483 case ISD::FP_TO_SINT:
Chris Lattner282781c2005-01-09 18:52:44 +00001484 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1485
Nate Begeman8a093362005-07-06 18:59:04 +00001486 // If the target supports SSE2 and is performing FP operations in SSE regs
1487 // instead of the FP stack, then we can use the efficient CVTSS2SI and
1488 // CVTSD2SI instructions.
Chris Lattner4738d1b2005-07-30 00:05:54 +00001489 assert(X86ScalarSSE);
1490 if (MVT::f32 == N.getOperand(0).getValueType()) {
1491 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
1492 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
1493 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
1494 } else {
1495 assert(0 && "Not an f32 or f64?");
1496 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001497 }
Chris Lattner282781c2005-01-09 18:52:44 +00001498 return Result;
Chris Lattner4738d1b2005-07-30 00:05:54 +00001499
Chris Lattner0815dcae2005-09-28 22:29:17 +00001500 case ISD::FADD:
Chris Lattner88c8a232005-01-07 07:49:41 +00001501 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00001502 Op0 = N.getOperand(0);
1503 Op1 = N.getOperand(1);
1504
Chris Lattner30607ec2005-01-25 20:03:11 +00001505 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00001506 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001507 goto FoldAdd;
1508 }
Chris Lattner62b22422005-01-11 21:19:59 +00001509
Chris Lattner30607ec2005-01-25 20:03:11 +00001510 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001511 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00001512 switch (N.getValueType()) {
1513 default: assert(0 && "Cannot add this type!");
1514 case MVT::i1:
1515 case MVT::i8: Opc = X86::ADD8rm; break;
1516 case MVT::i16: Opc = X86::ADD16rm; break;
1517 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001518 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001519 case MVT::f64:
1520 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00001521 if (X86ScalarSSE) {
1522 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
1523 Opc = X86::ADDSDrm;
1524 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001525 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FpADD64m : X86::FpADD32m;
Nate Begeman8a093362005-07-06 18:59:04 +00001526 }
Chris Lattner30607ec2005-01-25 20:03:11 +00001527 break;
Chris Lattner62b22422005-01-11 21:19:59 +00001528 }
1529 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001530 EmitFoldedLoad(Op1, AM);
1531 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001532 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1533 return Result;
1534 }
1535
Chris Lattner88c8a232005-01-07 07:49:41 +00001536 // See if we can codegen this as an LEA to fold operations together.
1537 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00001538 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001539 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00001540 MatchAddress(N, AM);
1541 ExprMap[N] = Result;
1542
1543 // If this is not just an add, emit the LEA. For a simple add (like
1544 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1545 // leave this as LEA, then peephole it to 'ADD' after two address elim
1546 // happens.
1547 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1548 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1549 X86AddressMode XAM = SelectAddrExprs(AM);
1550 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1551 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001552 }
1553 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001554
Chris Lattner62b22422005-01-11 21:19:59 +00001555 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001556 Opc = 0;
1557 if (CN->getValue() == 1) { // add X, 1 -> inc X
1558 switch (N.getValueType()) {
1559 default: assert(0 && "Cannot integer add this type!");
1560 case MVT::i8: Opc = X86::INC8r; break;
1561 case MVT::i16: Opc = X86::INC16r; break;
1562 case MVT::i32: Opc = X86::INC32r; break;
1563 }
1564 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1565 switch (N.getValueType()) {
1566 default: assert(0 && "Cannot integer add this type!");
1567 case MVT::i8: Opc = X86::DEC8r; break;
1568 case MVT::i16: Opc = X86::DEC16r; break;
1569 case MVT::i32: Opc = X86::DEC32r; break;
1570 }
1571 }
1572
1573 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001574 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001575 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1576 return Result;
1577 }
1578
1579 switch (N.getValueType()) {
1580 default: assert(0 && "Cannot add this type!");
1581 case MVT::i8: Opc = X86::ADD8ri; break;
1582 case MVT::i16: Opc = X86::ADD16ri; break;
1583 case MVT::i32: Opc = X86::ADD32ri; break;
1584 }
1585 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001586 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001587 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1588 return Result;
1589 }
1590 }
1591
Chris Lattner88c8a232005-01-07 07:49:41 +00001592 switch (N.getValueType()) {
1593 default: assert(0 && "Cannot add this type!");
1594 case MVT::i8: Opc = X86::ADD8rr; break;
1595 case MVT::i16: Opc = X86::ADD16rr; break;
1596 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001597 case MVT::f32: Opc = X86::ADDSSrr; break;
1598 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001599 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001600
Chris Lattner62b22422005-01-11 21:19:59 +00001601 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1602 Tmp1 = SelectExpr(Op0);
1603 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001604 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001605 Tmp2 = SelectExpr(Op1);
1606 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001607 }
1608
Chris Lattner88c8a232005-01-07 07:49:41 +00001609 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1610 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00001611
Nate Begeman8a093362005-07-06 18:59:04 +00001612 case ISD::FSQRT:
1613 Tmp1 = SelectExpr(Node->getOperand(0));
1614 if (X86ScalarSSE) {
1615 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
1616 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1617 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001618 BuildMI(BB, X86::FpSQRT, 1, Result).addReg(Tmp1);
Nate Begeman8a093362005-07-06 18:59:04 +00001619 }
1620 return Result;
1621
1622 // FIXME:
1623 // Once we can spill 16 byte constants into the constant pool, we can
1624 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00001625 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00001626 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00001627 case ISD::FSIN:
1628 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00001629 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00001630 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00001631 switch (N.getOpcode()) {
1632 default: assert(0 && "Unreachable!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00001633 case ISD::FABS: BuildMI(BB, X86::FpABS, 1, Result).addReg(Tmp1); break;
1634 case ISD::FNEG: BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1); break;
1635 case ISD::FSIN: BuildMI(BB, X86::FpSIN, 1, Result).addReg(Tmp1); break;
1636 case ISD::FCOS: BuildMI(BB, X86::FpCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00001637 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00001638 return Result;
1639
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001640 case ISD::MULHU:
1641 switch (N.getValueType()) {
1642 default: assert(0 && "Unsupported VT!");
1643 case MVT::i8: Tmp2 = X86::MUL8r; break;
1644 case MVT::i16: Tmp2 = X86::MUL16r; break;
1645 case MVT::i32: Tmp2 = X86::MUL32r; break;
1646 }
1647 // FALL THROUGH
1648 case ISD::MULHS: {
1649 unsigned MovOpc, LowReg, HiReg;
1650 switch (N.getValueType()) {
1651 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00001652 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001653 MovOpc = X86::MOV8rr;
1654 LowReg = X86::AL;
1655 HiReg = X86::AH;
1656 Opc = X86::IMUL8r;
1657 break;
1658 case MVT::i16:
1659 MovOpc = X86::MOV16rr;
1660 LowReg = X86::AX;
1661 HiReg = X86::DX;
1662 Opc = X86::IMUL16r;
1663 break;
1664 case MVT::i32:
1665 MovOpc = X86::MOV32rr;
1666 LowReg = X86::EAX;
1667 HiReg = X86::EDX;
1668 Opc = X86::IMUL32r;
1669 break;
1670 }
1671 if (Node->getOpcode() != ISD::MULHS)
1672 Opc = Tmp2; // Get the MULHU opcode.
1673
1674 Op0 = Node->getOperand(0);
1675 Op1 = Node->getOperand(1);
1676 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1677 Tmp1 = SelectExpr(Op0);
1678 Tmp2 = SelectExpr(Op1);
1679 } else {
1680 Tmp2 = SelectExpr(Op1);
1681 Tmp1 = SelectExpr(Op0);
1682 }
1683
1684 // FIXME: Implement folding of loads into the memory operands here!
1685 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1686 BuildMI(BB, Opc, 1).addReg(Tmp2);
1687 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1688 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001689 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001690
Chris Lattner0815dcae2005-09-28 22:29:17 +00001691 case ISD::FSUB:
1692 case ISD::FMUL:
Chris Lattner88c8a232005-01-07 07:49:41 +00001693 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00001694 case ISD::MUL:
1695 case ISD::AND:
1696 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00001697 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00001698 static const unsigned SUBTab[] = {
1699 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001700 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FpSUB32m, X86::FpSUB64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001701 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1702 };
Nate Begeman8a093362005-07-06 18:59:04 +00001703 static const unsigned SSE_SUBTab[] = {
1704 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1705 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
1706 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
1707 };
Chris Lattner62b22422005-01-11 21:19:59 +00001708 static const unsigned MULTab[] = {
1709 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001710 0, X86::IMUL16rm , X86::IMUL32rm, X86::FpMUL32m, X86::FpMUL64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001711 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1712 };
Nate Begeman8a093362005-07-06 18:59:04 +00001713 static const unsigned SSE_MULTab[] = {
1714 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1715 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
1716 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
1717 };
Chris Lattner62b22422005-01-11 21:19:59 +00001718 static const unsigned ANDTab[] = {
1719 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1720 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001721 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00001722 };
1723 static const unsigned ORTab[] = {
1724 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1725 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1726 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1727 };
1728 static const unsigned XORTab[] = {
1729 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1730 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1731 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1732 };
1733
1734 Op0 = Node->getOperand(0);
1735 Op1 = Node->getOperand(1);
1736
Chris Lattner29f58192005-01-19 07:37:26 +00001737 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1738 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00001739 return Result;
1740
1741 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00001742 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1743 if (CN->isNullValue()) { // 0 - N -> neg N
1744 switch (N.getValueType()) {
1745 default: assert(0 && "Cannot sub this type!");
1746 case MVT::i1:
1747 case MVT::i8: Opc = X86::NEG8r; break;
1748 case MVT::i16: Opc = X86::NEG16r; break;
1749 case MVT::i32: Opc = X86::NEG32r; break;
1750 }
1751 Tmp1 = SelectExpr(N.getOperand(1));
1752 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1753 return Result;
1754 }
1755
Chris Lattner62b22422005-01-11 21:19:59 +00001756 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1757 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001758 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00001759 switch (N.getValueType()) {
1760 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001761 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00001762 case MVT::i8: Opc = X86::NOT8r; break;
1763 case MVT::i16: Opc = X86::NOT16r; break;
1764 case MVT::i32: Opc = X86::NOT32r; break;
1765 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001766 if (Opc) {
1767 Tmp1 = SelectExpr(Op0);
1768 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1769 return Result;
1770 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00001771 }
1772
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001773 // Fold common multiplies into LEA instructions.
1774 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1775 switch ((int)CN->getValue()) {
1776 default: break;
1777 case 3:
1778 case 5:
1779 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001780 // Remove N from exprmap so SelectAddress doesn't get confused.
1781 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001782 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001783 SelectAddress(N, AM);
1784 // Restore it to the map.
1785 ExprMap[N] = Result;
1786 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1787 return Result;
1788 }
1789 }
1790
Chris Lattner88c8a232005-01-07 07:49:41 +00001791 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00001792 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00001793 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00001794 case MVT::i8: Opc = 0; break;
1795 case MVT::i16: Opc = 1; break;
1796 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001797 }
Chris Lattner62b22422005-01-11 21:19:59 +00001798 switch (Node->getOpcode()) {
1799 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001800 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001801 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001802 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001803 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001804 case ISD::AND: Opc = ANDTab[Opc]; break;
1805 case ISD::OR: Opc = ORTab[Opc]; break;
1806 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001807 }
Chris Lattner62b22422005-01-11 21:19:59 +00001808 if (Opc) { // Can't fold MUL:i8 R, imm
1809 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001810 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1811 return Result;
1812 }
1813 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001814
Chris Lattner30607ec2005-01-25 20:03:11 +00001815 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner0815dcae2005-09-28 22:29:17 +00001816 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
Chris Lattner62b22422005-01-11 21:19:59 +00001817 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001818 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00001819 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00001820 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00001821 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001822 if (Op0.getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001823 Opc = X86::FpSUBR32m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001824 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00001825 Opc = X86::FpSUBR64m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001826
Chris Lattner62b22422005-01-11 21:19:59 +00001827 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001828 EmitFoldedLoad(Op0, AM);
1829 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00001830 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1831 return Result;
1832 }
1833 }
1834
Chris Lattner30607ec2005-01-25 20:03:11 +00001835 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001836 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00001837 switch (N.getValueType()) {
1838 default: assert(0 && "Cannot operate on this type!");
1839 case MVT::i1:
1840 case MVT::i8: Opc = 5; break;
1841 case MVT::i16: Opc = 6; break;
1842 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001843 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001844 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001845 case MVT::f64:
1846 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00001847 "SSE load should have been promoted");
1848 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001849 }
1850 switch (Node->getOpcode()) {
1851 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001852 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001853 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001854 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001855 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001856 case ISD::AND: Opc = ANDTab[Opc]; break;
1857 case ISD::OR: Opc = ORTab[Opc]; break;
1858 case ISD::XOR: Opc = XORTab[Opc]; break;
1859 }
1860
1861 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001862 EmitFoldedLoad(Op1, AM);
1863 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001864 if (Opc) {
1865 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1866 } else {
1867 assert(Node->getOpcode() == ISD::MUL &&
1868 N.getValueType() == MVT::i8 && "Unexpected situation!");
1869 // Must use the MUL instruction, which forces use of AL.
1870 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1871 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1872 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1873 }
1874 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001875 }
Chris Lattner62b22422005-01-11 21:19:59 +00001876
1877 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1878 Tmp1 = SelectExpr(Op0);
1879 Tmp2 = SelectExpr(Op1);
1880 } else {
1881 Tmp2 = SelectExpr(Op1);
1882 Tmp1 = SelectExpr(Op0);
1883 }
1884
Chris Lattner88c8a232005-01-07 07:49:41 +00001885 switch (N.getValueType()) {
1886 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00001887 case MVT::i1:
1888 case MVT::i8: Opc = 10; break;
1889 case MVT::i16: Opc = 11; break;
1890 case MVT::i32: Opc = 12; break;
1891 case MVT::f32: Opc = 13; break;
1892 case MVT::f64: Opc = 14; break;
1893 }
1894 switch (Node->getOpcode()) {
1895 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001896 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001897 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001898 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001899 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001900 case ISD::AND: Opc = ANDTab[Opc]; break;
1901 case ISD::OR: Opc = ORTab[Opc]; break;
1902 case ISD::XOR: Opc = XORTab[Opc]; break;
1903 }
1904 if (Opc) {
1905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1906 } else {
1907 assert(Node->getOpcode() == ISD::MUL &&
1908 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00001909 // Must use the MUL instruction, which forces use of AL.
1910 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1911 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1912 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00001913 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001914 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00001915 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00001916 case ISD::ADD_PARTS:
1917 case ISD::SUB_PARTS: {
1918 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1919 "Not an i64 add/sub!");
1920 // Emit all of the operands.
1921 std::vector<unsigned> InVals;
1922 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1923 InVals.push_back(SelectExpr(N.getOperand(i)));
1924 if (N.getOpcode() == ISD::ADD_PARTS) {
1925 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1926 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1927 } else {
1928 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1929 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1930 }
1931 return Result+N.ResNo;
1932 }
1933
Chris Lattnera31d4c72005-04-02 04:01:14 +00001934 case ISD::SHL_PARTS:
1935 case ISD::SRA_PARTS:
1936 case ISD::SRL_PARTS: {
1937 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1938 "Not an i64 shift!");
1939 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1940 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1941 unsigned TmpReg = MakeReg(MVT::i32);
1942 if (N.getOpcode() == ISD::SRA_PARTS) {
1943 // If this is a SHR of a Long, then we need to do funny sign extension
1944 // stuff. TmpReg gets the value to use as the high-part if we are
1945 // shifting more than 32 bits.
1946 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
1947 } else {
1948 // Other shifts use a fixed zero value if the shift is more than 32 bits.
1949 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
1950 }
1951
1952 // Initialize CL with the shift amount.
1953 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
1954 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
1955
1956 unsigned TmpReg2 = MakeReg(MVT::i32);
1957 unsigned TmpReg3 = MakeReg(MVT::i32);
1958 if (N.getOpcode() == ISD::SHL_PARTS) {
1959 // TmpReg2 = shld inHi, inLo
1960 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
1961 .addReg(ShiftOpLo);
1962 // TmpReg3 = shl inLo, CL
1963 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001964
Chris Lattnera31d4c72005-04-02 04:01:14 +00001965 // Set the flags to indicate whether the shift was by more than 32 bits.
1966 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001967
Chris Lattnera31d4c72005-04-02 04:01:14 +00001968 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001969 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001970 Result+1).addReg(TmpReg2).addReg(TmpReg3);
1971 // DestLo = (>32) ? TmpReg : TmpReg3;
1972 BuildMI(BB, X86::CMOVNE32rr, 2,
1973 Result).addReg(TmpReg3).addReg(TmpReg);
1974 } else {
1975 // TmpReg2 = shrd inLo, inHi
1976 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
1977 .addReg(ShiftOpHi);
1978 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00001979 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00001980 : X86::SHR32rCL, 1, TmpReg3)
1981 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001982
Chris Lattnera31d4c72005-04-02 04:01:14 +00001983 // Set the flags to indicate whether the shift was by more than 32 bits.
1984 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001985
Chris Lattnera31d4c72005-04-02 04:01:14 +00001986 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001987 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001988 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001989
Chris Lattnera31d4c72005-04-02 04:01:14 +00001990 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001991 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001992 Result+1).addReg(TmpReg3).addReg(TmpReg);
1993 }
1994 return Result+N.ResNo;
1995 }
1996
Chris Lattner88c8a232005-01-07 07:49:41 +00001997 case ISD::SELECT:
Nate Begeman8d394eb2005-08-03 23:26:28 +00001998 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
1999 N.getValueType(), Result);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002000 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002001
Chris Lattner0815dcae2005-09-28 22:29:17 +00002002 case ISD::FDIV:
2003 case ISD::FREM:
Chris Lattner88c8a232005-01-07 07:49:41 +00002004 case ISD::SDIV:
2005 case ISD::UDIV:
2006 case ISD::SREM:
2007 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002008 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2009 "We don't support this operator!");
2010
Chris Lattner0815dcae2005-09-28 22:29:17 +00002011 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002012 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00002013 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00002014 // Check for reversed and unreversed DIV.
2015 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2016 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002017 Opc = X86::FpDIVR32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002018 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002019 Opc = X86::FpDIVR64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002020 X86AddressMode AM;
2021 EmitFoldedLoad(N.getOperand(0), AM);
2022 Tmp1 = SelectExpr(N.getOperand(1));
2023 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2024 return Result;
2025 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2026 N.getOperand(1).getOpcode() == ISD::LOAD) {
2027 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002028 Opc = X86::FpDIV32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002029 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002030 Opc = X86::FpDIV64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002031 X86AddressMode AM;
2032 EmitFoldedLoad(N.getOperand(1), AM);
2033 Tmp1 = SelectExpr(N.getOperand(0));
2034 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2035 return Result;
2036 }
2037 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002038 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002039
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002040 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2041 Tmp1 = SelectExpr(N.getOperand(0));
2042 Tmp2 = SelectExpr(N.getOperand(1));
2043 } else {
2044 Tmp2 = SelectExpr(N.getOperand(1));
2045 Tmp1 = SelectExpr(N.getOperand(0));
2046 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002047
2048 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2049 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2050 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2051 switch (N.getValueType()) {
2052 default: assert(0 && "Cannot sdiv this type!");
2053 case MVT::i8:
2054 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2055 LoReg = X86::AL;
2056 HiReg = X86::AH;
2057 MovOpcode = X86::MOV8rr;
2058 ClrOpcode = X86::MOV8ri;
2059 SExtOpcode = X86::CBW;
2060 break;
2061 case MVT::i16:
2062 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2063 LoReg = X86::AX;
2064 HiReg = X86::DX;
2065 MovOpcode = X86::MOV16rr;
2066 ClrOpcode = X86::MOV16ri;
2067 SExtOpcode = X86::CWD;
2068 break;
2069 case MVT::i32:
2070 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002071 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002072 HiReg = X86::EDX;
2073 MovOpcode = X86::MOV32rr;
2074 ClrOpcode = X86::MOV32ri;
2075 SExtOpcode = X86::CDQ;
2076 break;
Nate Begeman8a093362005-07-06 18:59:04 +00002077 case MVT::f32:
2078 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
2079 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002080 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002081 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
2082 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002083 return Result;
2084 }
2085
2086 // Set up the low part.
2087 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2088
2089 if (isSigned) {
2090 // Sign extend the low part into the high part.
2091 BuildMI(BB, SExtOpcode, 0);
2092 } else {
2093 // Zero out the high part, effectively zero extending the input.
2094 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2095 }
2096
2097 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002098 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002099
2100 // Get the result of the divide or rem.
2101 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2102 return Result;
2103 }
2104
2105 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002106 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002107 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2108 switch (N.getValueType()) {
2109 default: assert(0 && "Cannot shift this type!");
2110 case MVT::i8: Opc = X86::ADD8rr; break;
2111 case MVT::i16: Opc = X86::ADD16rr; break;
2112 case MVT::i32: Opc = X86::ADD32rr; break;
2113 }
2114 Tmp1 = SelectExpr(N.getOperand(0));
2115 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2116 return Result;
2117 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002118
Chris Lattner88c8a232005-01-07 07:49:41 +00002119 switch (N.getValueType()) {
2120 default: assert(0 && "Cannot shift this type!");
2121 case MVT::i8: Opc = X86::SHL8ri; break;
2122 case MVT::i16: Opc = X86::SHL16ri; break;
2123 case MVT::i32: Opc = X86::SHL32ri; break;
2124 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002125 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002126 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2127 return Result;
2128 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002129
2130 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2131 Tmp1 = SelectExpr(N.getOperand(0));
2132 Tmp2 = SelectExpr(N.getOperand(1));
2133 } else {
2134 Tmp2 = SelectExpr(N.getOperand(1));
2135 Tmp1 = SelectExpr(N.getOperand(0));
2136 }
2137
Chris Lattner88c8a232005-01-07 07:49:41 +00002138 switch (N.getValueType()) {
2139 default: assert(0 && "Cannot shift this type!");
2140 case MVT::i8 : Opc = X86::SHL8rCL; break;
2141 case MVT::i16: Opc = X86::SHL16rCL; break;
2142 case MVT::i32: Opc = X86::SHL32rCL; break;
2143 }
2144 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002145 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002146 return Result;
2147 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002148 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2149 switch (N.getValueType()) {
2150 default: assert(0 && "Cannot shift this type!");
2151 case MVT::i8: Opc = X86::SHR8ri; break;
2152 case MVT::i16: Opc = X86::SHR16ri; break;
2153 case MVT::i32: Opc = X86::SHR32ri; break;
2154 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002155 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002156 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2157 return Result;
2158 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002159
2160 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2161 Tmp1 = SelectExpr(N.getOperand(0));
2162 Tmp2 = SelectExpr(N.getOperand(1));
2163 } else {
2164 Tmp2 = SelectExpr(N.getOperand(1));
2165 Tmp1 = SelectExpr(N.getOperand(0));
2166 }
2167
Chris Lattner88c8a232005-01-07 07:49:41 +00002168 switch (N.getValueType()) {
2169 default: assert(0 && "Cannot shift this type!");
2170 case MVT::i8 : Opc = X86::SHR8rCL; break;
2171 case MVT::i16: Opc = X86::SHR16rCL; break;
2172 case MVT::i32: Opc = X86::SHR32rCL; break;
2173 }
2174 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002175 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002176 return Result;
2177 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002178 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2179 switch (N.getValueType()) {
2180 default: assert(0 && "Cannot shift this type!");
2181 case MVT::i8: Opc = X86::SAR8ri; break;
2182 case MVT::i16: Opc = X86::SAR16ri; break;
2183 case MVT::i32: Opc = X86::SAR32ri; break;
2184 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002185 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002186 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2187 return Result;
2188 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002189
2190 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2191 Tmp1 = SelectExpr(N.getOperand(0));
2192 Tmp2 = SelectExpr(N.getOperand(1));
2193 } else {
2194 Tmp2 = SelectExpr(N.getOperand(1));
2195 Tmp1 = SelectExpr(N.getOperand(0));
2196 }
2197
Chris Lattner88c8a232005-01-07 07:49:41 +00002198 switch (N.getValueType()) {
2199 default: assert(0 && "Cannot shift this type!");
2200 case MVT::i8 : Opc = X86::SAR8rCL; break;
2201 case MVT::i16: Opc = X86::SAR16rCL; break;
2202 case MVT::i32: Opc = X86::SAR32rCL; break;
2203 }
2204 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnera9d68f12005-08-19 00:31:37 +00002205 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002206 return Result;
2207
2208 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002209 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner6ec77452005-08-09 20:21:10 +00002210 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner88c8a232005-01-07 07:49:41 +00002211 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2212 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002213 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002214 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002215 if (Result != 1) { // Generate the token
2216 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2217 assert(0 && "Load already emitted!?");
2218 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002219 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2220
Chris Lattnerb52e0412005-01-08 19:28:19 +00002221 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002222 default: assert(0 && "Cannot load this type!");
2223 case MVT::i1:
2224 case MVT::i8: Opc = X86::MOV8rm; break;
2225 case MVT::i16: Opc = X86::MOV16rm; break;
2226 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002227 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002228 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002229 if (X86ScalarSSE) {
2230 Opc = X86::MOVSDrm;
2231 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00002232 Opc = X86::FpLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002233 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00002234 }
2235 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002236 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002237
Chris Lattner88c8a232005-01-07 07:49:41 +00002238 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattnerc30405e2005-08-26 17:15:30 +00002239 unsigned CPIdx = BB->getParent()->getConstantPool()->
2240 getConstantPoolIndex(CP->get());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002241 Select(N.getOperand(0));
Chris Lattnerc30405e2005-08-26 17:15:30 +00002242 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner88c8a232005-01-07 07:49:41 +00002243 } else {
2244 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002245
2246 SDOperand Chain = N.getOperand(0);
2247 SDOperand Address = N.getOperand(1);
2248 if (getRegPressure(Chain) > getRegPressure(Address)) {
2249 Select(Chain);
2250 SelectAddress(Address, AM);
2251 } else {
2252 SelectAddress(Address, AM);
2253 Select(Chain);
2254 }
2255
Chris Lattner88c8a232005-01-07 07:49:41 +00002256 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2257 }
2258 return Result;
Chris Lattnera36117b2005-05-14 06:52:07 +00002259 case X86ISD::FILD64m:
2260 // Make sure we generate both values.
2261 assert(Result != 1 && N.getValueType() == MVT::f64);
2262 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2263 assert(0 && "Load already emitted!?");
2264
2265 {
2266 X86AddressMode AM;
2267
2268 SDOperand Chain = N.getOperand(0);
2269 SDOperand Address = N.getOperand(1);
2270 if (getRegPressure(Chain) > getRegPressure(Address)) {
2271 Select(Chain);
2272 SelectAddress(Address, AM);
2273 } else {
2274 SelectAddress(Address, AM);
2275 Select(Chain);
2276 }
Chris Lattner67756e22005-07-29 00:40:01 +00002277
Chris Lattnerf431ad42005-12-21 07:47:04 +00002278 addFullAddress(BuildMI(BB, X86::FpILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00002279 }
2280 return Result;
Jeff Cohen546fd592005-07-30 18:33:25 +00002281
Chris Lattnere18a4c42005-01-15 05:22:24 +00002282 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2283 case ISD::ZEXTLOAD: {
2284 // Make sure we generate both values.
2285 if (Result != 1)
2286 ExprMap[N.getValue(1)] = 1; // Generate the token
2287 else
2288 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2289
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002290 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2291 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00002292 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002293 "Bad EXTLOAD!");
Chris Lattnerc30405e2005-08-26 17:15:30 +00002294 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00002295 getConstantPoolIndex(CP->get());
Chris Lattnerc30405e2005-08-26 17:15:30 +00002296
Chris Lattnerf431ad42005-12-21 07:47:04 +00002297 addConstantPoolReference(BuildMI(BB, X86::FpLD32m, 4, Result), CPIdx);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002298 return Result;
2299 }
2300
Chris Lattnere18a4c42005-01-15 05:22:24 +00002301 X86AddressMode AM;
2302 if (getRegPressure(Node->getOperand(0)) >
2303 getRegPressure(Node->getOperand(1))) {
2304 Select(Node->getOperand(0)); // chain
2305 SelectAddress(Node->getOperand(1), AM);
2306 } else {
2307 SelectAddress(Node->getOperand(1), AM);
2308 Select(Node->getOperand(0)); // chain
2309 }
2310
2311 switch (Node->getValueType(0)) {
2312 default: assert(0 && "Unknown type to sign extend to.");
2313 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00002314 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002315 "Bad EXTLOAD!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00002316 addFullAddress(BuildMI(BB, X86::FpLD32m, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002317 break;
2318 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002319 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002320 default:
2321 assert(0 && "Bad zero extend!");
2322 case MVT::i1:
2323 case MVT::i8:
2324 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2325 break;
2326 case MVT::i16:
2327 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2328 break;
2329 }
2330 break;
2331 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002332 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002333 "Bad zero extend!");
Evan Cheng023aef22005-12-14 22:28:18 +00002334 addFullAddress(BuildMI(BB, X86::MOVZX16rm8, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002335 break;
2336 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00002337 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002338 "Bad zero extend!");
2339 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2340 break;
2341 }
2342 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002343 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002344 case ISD::SEXTLOAD: {
2345 // Make sure we generate both values.
2346 if (Result != 1)
2347 ExprMap[N.getValue(1)] = 1; // Generate the token
2348 else
2349 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2350
2351 X86AddressMode AM;
2352 if (getRegPressure(Node->getOperand(0)) >
2353 getRegPressure(Node->getOperand(1))) {
2354 Select(Node->getOperand(0)); // chain
2355 SelectAddress(Node->getOperand(1), AM);
2356 } else {
2357 SelectAddress(Node->getOperand(1), AM);
2358 Select(Node->getOperand(0)); // chain
2359 }
2360
2361 switch (Node->getValueType(0)) {
2362 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2363 default: assert(0 && "Unknown type to sign extend to.");
2364 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002365 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002366 default:
2367 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2368 case MVT::i8:
2369 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2370 break;
2371 case MVT::i16:
2372 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2373 break;
2374 }
2375 break;
2376 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002377 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002378 "Cannot sign extend from bool!");
2379 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2380 break;
2381 }
2382 return Result;
2383 }
2384
Chris Lattner88c8a232005-01-07 07:49:41 +00002385 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00002386 // Generate both result values.
2387 if (Result != 1)
2388 ExprMap[N.getValue(1)] = 1; // Generate the token
2389 else
2390 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2391
2392 // FIXME: We are currently ignoring the requested alignment for handling
2393 // greater than the stack alignment. This will need to be revisited at some
2394 // point. Align = N.getOperand(2);
2395
2396 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2397 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2398 std::cerr << "Cannot allocate stack object with greater alignment than"
2399 << " the stack alignment yet!";
2400 abort();
2401 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002402
Chris Lattner88c8a232005-01-07 07:49:41 +00002403 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002404 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002405 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2406 .addImm(CN->getValue());
2407 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002408 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2409 Select(N.getOperand(0));
2410 Tmp1 = SelectExpr(N.getOperand(1));
2411 } else {
2412 Tmp1 = SelectExpr(N.getOperand(1));
2413 Select(N.getOperand(0));
2414 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002415
2416 // Subtract size from stack pointer, thereby allocating some space.
2417 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2418 }
2419
2420 // Put a pointer to the space into the result register, by copying the stack
2421 // pointer.
2422 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2423 return Result;
2424
Chris Lattner1b3520c2005-05-14 08:48:15 +00002425 case X86ISD::TAILCALL:
2426 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00002427 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002428 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00002429
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002430 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
2431 isa<ExternalSymbolSDNode>(N.getOperand(1));
2432 unsigned Callee = 0;
2433 if (isDirect) {
2434 Select(N.getOperand(0));
2435 } else {
2436 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2437 Select(N.getOperand(0));
2438 Callee = SelectExpr(N.getOperand(1));
2439 } else {
2440 Callee = SelectExpr(N.getOperand(1));
2441 Select(N.getOperand(0));
2442 }
2443 }
2444
2445 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002446 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002447 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002448 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002449 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00002450 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002451
Chris Lattner1b3520c2005-05-14 08:48:15 +00002452 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002453 default: assert(0 && "Bad thing to pass in regs");
2454 case MVT::i1:
2455 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
2456 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
2457 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
2458 }
2459 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00002460 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002461 default: assert(0 && "Bad thing to pass in regs");
2462 case MVT::i1:
2463 case MVT::i8:
2464 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
2465 break;
2466 case MVT::i16:
2467 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
2468 break;
2469 case MVT::i32:
2470 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
2471 break;
2472 }
2473 }
2474
Chris Lattner88c8a232005-01-07 07:49:41 +00002475 if (GlobalAddressSDNode *GASD =
2476 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
2477 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2478 } else if (ExternalSymbolSDNode *ESSDN =
2479 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
2480 BuildMI(BB, X86::CALLpcrel32,
2481 1).addExternalSymbol(ESSDN->getSymbol(), true);
2482 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002483 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2484 Select(N.getOperand(0));
2485 Tmp1 = SelectExpr(N.getOperand(1));
2486 } else {
2487 Tmp1 = SelectExpr(N.getOperand(1));
2488 Select(N.getOperand(0));
2489 }
2490
Chris Lattner88c8a232005-01-07 07:49:41 +00002491 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2492 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002493
2494 // Get caller stack amount and amount the callee added to the stack pointer.
2495 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
2496 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
2497 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
2498
2499 if (Node->getNumValues() != 1)
2500 switch (Node->getValueType(1)) {
2501 default: assert(0 && "Unknown value type for call result!");
2502 case MVT::Other: return 1;
2503 case MVT::i1:
2504 case MVT::i8:
2505 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2506 break;
2507 case MVT::i16:
2508 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2509 break;
2510 case MVT::i32:
2511 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2512 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
2513 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2514 break;
2515 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00002516 if (X86ScalarSSE) {
2517 ContainsFPCode = true;
2518 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
2519
2520 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
2521 MachineFunction *F = BB->getParent();
2522 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerf431ad42005-12-21 07:47:04 +00002523 addFrameReference(BuildMI(BB, X86::FpST64m, 5), FrameIdx).addReg(X86::FP0);
Nate Begeman8a093362005-07-06 18:59:04 +00002524 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
2525 break;
2526 } else {
2527 ContainsFPCode = true;
2528 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2529 break;
2530 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002531 }
2532 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002533 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00002534 case ISD::READPORT:
2535 // First, determine that the size of the operand falls within the acceptable
2536 // range for this architecture.
2537 //
2538 if (Node->getOperand(1).getValueType() != MVT::i16) {
2539 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2540 exit(1);
2541 }
2542
2543 // Make sure we generate both values.
2544 if (Result != 1) { // Generate the token
2545 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2546 assert(0 && "readport already emitted!?");
2547 } else
2548 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002549
Chris Lattner70ea07c2005-05-09 21:17:38 +00002550 Select(Node->getOperand(0)); // Select the chain.
2551
2552 // If the port is a single-byte constant, use the immediate form.
2553 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2554 if ((Port->getValue() & 255) == Port->getValue()) {
2555 switch (Node->getValueType(0)) {
2556 case MVT::i8:
2557 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2558 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2559 return Result;
2560 case MVT::i16:
2561 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2562 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2563 return Result;
2564 case MVT::i32:
2565 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2566 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2567 return Result;
2568 default: break;
2569 }
2570 }
2571
2572 // Now, move the I/O port address into the DX register and use the IN
2573 // instruction to get the input data.
2574 //
2575 Tmp1 = SelectExpr(Node->getOperand(1));
2576 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2577 switch (Node->getValueType(0)) {
2578 case MVT::i8:
2579 BuildMI(BB, X86::IN8rr, 0);
2580 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2581 return Result;
2582 case MVT::i16:
2583 BuildMI(BB, X86::IN16rr, 0);
2584 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2585 return Result;
2586 case MVT::i32:
2587 BuildMI(BB, X86::IN32rr, 0);
2588 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2589 return Result;
2590 default:
2591 std::cerr << "Cannot do input on this data type";
2592 exit(1);
2593 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002594
Chris Lattner88c8a232005-01-07 07:49:41 +00002595 }
2596
2597 return 0;
2598}
2599
Chris Lattner96113fd2005-01-17 19:25:26 +00002600/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2601/// load/op/store instruction. If successful return true.
2602bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2603 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2604 SDOperand Chain = Node->getOperand(0);
2605 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00002606 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00002607
2608 // The chain has to be a load, the stored value must be an integer binary
2609 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00002610 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00002611 MVT::isFloatingPoint(StVal.getValueType()))
2612 return false;
2613
Chris Lattnere86c9332005-01-17 22:10:42 +00002614 // Token chain must either be a factor node or the load to fold.
2615 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2616 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00002617
Chris Lattnere86c9332005-01-17 22:10:42 +00002618 SDOperand TheLoad;
2619
2620 // Check to see if there is a load from the same pointer that we're storing
2621 // to in either operand of the binop.
2622 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2623 StVal.getOperand(0).getOperand(1) == StPtr)
2624 TheLoad = StVal.getOperand(0);
2625 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2626 StVal.getOperand(1).getOperand(1) == StPtr)
2627 TheLoad = StVal.getOperand(1);
2628 else
2629 return false; // No matching load operand.
2630
2631 // We can only fold the load if there are no intervening side-effecting
2632 // operations. This means that the store uses the load as its token chain, or
2633 // there are only token factor nodes in between the store and load.
2634 if (Chain != TheLoad.getValue(1)) {
2635 // Okay, the other option is that we have a store referring to (possibly
2636 // nested) token factor nodes. For now, just try peeking through one level
2637 // of token factors to see if this is the case.
2638 bool ChainOk = false;
2639 if (Chain.getOpcode() == ISD::TokenFactor) {
2640 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2641 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2642 ChainOk = true;
2643 break;
2644 }
2645 }
2646
2647 if (!ChainOk) return false;
2648 }
2649
2650 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00002651 return false;
2652
2653 // Make sure that one of the operands of the binop is the load, and that the
2654 // load folds into the binop.
2655 if (((StVal.getOperand(0) != TheLoad ||
2656 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2657 (StVal.getOperand(1) != TheLoad ||
2658 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2659 return false;
2660
2661 // Finally, check to see if this is one of the ops we can handle!
2662 static const unsigned ADDTAB[] = {
2663 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2664 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2665 };
2666 static const unsigned SUBTAB[] = {
2667 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2668 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2669 };
2670 static const unsigned ANDTAB[] = {
2671 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2672 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2673 };
2674 static const unsigned ORTAB[] = {
2675 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2676 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2677 };
2678 static const unsigned XORTAB[] = {
2679 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2680 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2681 };
2682 static const unsigned SHLTAB[] = {
2683 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2684 /*Have to put the reg in CL*/0, 0, 0,
2685 };
2686 static const unsigned SARTAB[] = {
2687 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2688 /*Have to put the reg in CL*/0, 0, 0,
2689 };
2690 static const unsigned SHRTAB[] = {
2691 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2692 /*Have to put the reg in CL*/0, 0, 0,
2693 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00002694
Chris Lattner96113fd2005-01-17 19:25:26 +00002695 const unsigned *TabPtr = 0;
2696 switch (StVal.getOpcode()) {
2697 default:
2698 std::cerr << "CANNOT [mem] op= val: ";
2699 StVal.Val->dump(); std::cerr << "\n";
Chris Lattner0815dcae2005-09-28 22:29:17 +00002700 case ISD::FMUL:
Chris Lattner96113fd2005-01-17 19:25:26 +00002701 case ISD::MUL:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002702 case ISD::FDIV:
Chris Lattner96113fd2005-01-17 19:25:26 +00002703 case ISD::SDIV:
2704 case ISD::UDIV:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002705 case ISD::FREM:
Chris Lattner96113fd2005-01-17 19:25:26 +00002706 case ISD::SREM:
2707 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002708
Chris Lattner96113fd2005-01-17 19:25:26 +00002709 case ISD::ADD: TabPtr = ADDTAB; break;
2710 case ISD::SUB: TabPtr = SUBTAB; break;
2711 case ISD::AND: TabPtr = ANDTAB; break;
2712 case ISD:: OR: TabPtr = ORTAB; break;
2713 case ISD::XOR: TabPtr = XORTAB; break;
2714 case ISD::SHL: TabPtr = SHLTAB; break;
2715 case ISD::SRA: TabPtr = SARTAB; break;
2716 case ISD::SRL: TabPtr = SHRTAB; break;
2717 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002718
Chris Lattner96113fd2005-01-17 19:25:26 +00002719 // Handle: [mem] op= CST
2720 SDOperand Op0 = StVal.getOperand(0);
2721 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00002722 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00002723 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2724 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2725 default: break;
2726 case MVT::i1:
2727 case MVT::i8: Opc = TabPtr[0]; break;
2728 case MVT::i16: Opc = TabPtr[1]; break;
2729 case MVT::i32: Opc = TabPtr[2]; break;
2730 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002731
Chris Lattner96113fd2005-01-17 19:25:26 +00002732 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00002733 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2734 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002735 Select(Chain);
2736
Chris Lattner96113fd2005-01-17 19:25:26 +00002737 X86AddressMode AM;
2738 if (getRegPressure(TheLoad.getOperand(0)) >
2739 getRegPressure(TheLoad.getOperand(1))) {
2740 Select(TheLoad.getOperand(0));
2741 SelectAddress(TheLoad.getOperand(1), AM);
2742 } else {
2743 SelectAddress(TheLoad.getOperand(1), AM);
2744 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00002745 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002746
2747 if (StVal.getOpcode() == ISD::ADD) {
2748 if (CN->getValue() == 1) {
2749 switch (Op0.getValueType()) {
2750 default: break;
2751 case MVT::i8:
2752 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2753 return true;
2754 case MVT::i16: Opc = TabPtr[1];
2755 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2756 return true;
2757 case MVT::i32: Opc = TabPtr[2];
2758 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2759 return true;
2760 }
2761 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2762 switch (Op0.getValueType()) {
2763 default: break;
2764 case MVT::i8:
2765 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2766 return true;
2767 case MVT::i16: Opc = TabPtr[1];
2768 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2769 return true;
2770 case MVT::i32: Opc = TabPtr[2];
2771 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2772 return true;
2773 }
2774 }
2775 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002776
Chris Lattner96113fd2005-01-17 19:25:26 +00002777 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2778 return true;
2779 }
2780 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002781
Chris Lattner96113fd2005-01-17 19:25:26 +00002782 // If we have [mem] = V op [mem], try to turn it into:
2783 // [mem] = [mem] op V.
Chris Lattner0815dcae2005-09-28 22:29:17 +00002784 if (Op1 == TheLoad &&
2785 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
Chris Lattner96113fd2005-01-17 19:25:26 +00002786 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2787 StVal.getOpcode() != ISD::SRL)
2788 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002789
Chris Lattner96113fd2005-01-17 19:25:26 +00002790 if (Op0 != TheLoad) return false;
2791
2792 switch (Op0.getValueType()) {
2793 default: return false;
2794 case MVT::i1:
2795 case MVT::i8: Opc = TabPtr[3]; break;
2796 case MVT::i16: Opc = TabPtr[4]; break;
2797 case MVT::i32: Opc = TabPtr[5]; break;
2798 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002799
Chris Lattner479c7112005-01-18 17:35:28 +00002800 // Table entry doesn't exist?
2801 if (Opc == 0) return false;
2802
Chris Lattner78d30282005-01-18 03:51:59 +00002803 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2804 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002805 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00002806 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00002807
Chris Lattner96113fd2005-01-17 19:25:26 +00002808 X86AddressMode AM;
2809 SelectAddress(TheLoad.getOperand(1), AM);
2810 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002811 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00002812 return true;
2813}
2814
Chris Lattnerdd66a412005-05-15 05:46:45 +00002815/// If node is a ret(tailcall) node, emit the specified tail call and return
2816/// true, otherwise return false.
2817///
2818/// FIXME: This whole thing should be a post-legalize optimization pass which
2819/// recognizes and transforms the dag. We don't want the selection phase doing
2820/// this stuff!!
2821///
2822bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
2823 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
2824
2825 SDOperand Chain = RetNode->getOperand(0);
2826
2827 // If this is a token factor node where one operand is a call, dig into it.
2828 SDOperand TokFactor;
2829 unsigned TokFactorOperand = 0;
2830 if (Chain.getOpcode() == ISD::TokenFactor) {
2831 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2832 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
2833 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
2834 TokFactorOperand = i;
2835 TokFactor = Chain;
2836 Chain = Chain.getOperand(i);
2837 break;
2838 }
2839 if (TokFactor.Val == 0) return false; // No call operand.
2840 }
2841
2842 // Skip the CALLSEQ_END node if present.
2843 if (Chain.getOpcode() == ISD::CALLSEQ_END)
2844 Chain = Chain.getOperand(0);
2845
2846 // Is a tailcall the last control operation that occurs before the return?
2847 if (Chain.getOpcode() != X86ISD::TAILCALL)
2848 return false;
2849
2850 // If we return a value, is it the value produced by the call?
2851 if (RetNode->getNumOperands() > 1) {
2852 // Not returning the ret val of the call?
2853 if (Chain.Val->getNumValues() == 1 ||
2854 RetNode->getOperand(1) != Chain.getValue(1))
2855 return false;
2856
2857 if (RetNode->getNumOperands() > 2) {
2858 if (Chain.Val->getNumValues() == 2 ||
2859 RetNode->getOperand(2) != Chain.getValue(2))
2860 return false;
2861 }
2862 assert(RetNode->getNumOperands() <= 3);
2863 }
2864
2865 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
2866 // For FastCC, this will always be > 0.
2867 unsigned CalleeCallArgAmt =
2868 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
2869
2870 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
2871 // callee. For FastCC this will always be > 0, for CCC this is always 0.
2872 unsigned CalleeCallArgPopAmt =
2873 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
2874
2875 // There are several cases we can handle here. First, if the caller and
2876 // callee are both CCC functions, we can tailcall if the callee takes <= the
2877 // number of argument bytes that the caller does.
2878 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
2879 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
2880 // Check to see if caller arg area size >= callee arg area size.
2881 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
2882 //std::cerr << "CCC TAILCALL UNIMP!\n";
2883 // If TokFactor is non-null, emit all operands.
2884
2885 //EmitCCCToCCCTailCall(Chain.Val);
2886 //return true;
2887 }
2888 return false;
2889 }
2890
2891 // Second, if both are FastCC functions, we can always perform the tail call.
2892 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
2893 // If TokFactor is non-null, emit all operands before the call.
2894 if (TokFactor.Val) {
2895 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
2896 if (i != TokFactorOperand)
2897 Select(TokFactor.getOperand(i));
2898 }
2899
2900 EmitFastCCToFastCCTailCall(Chain.Val);
2901 return true;
2902 }
2903
2904 // We don't support mixed calls, due to issues with alignment. We could in
2905 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
2906 // aligned (which depends on the number of arguments to the callee). TODO.
2907 return false;
2908}
2909
2910static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
2911 SelectionDAG &DAG) {
2912 MVT::ValueType StoreVT;
2913 switch (Chain.getOpcode()) {
Chris Lattnerc1469402005-08-25 00:05:15 +00002914 default: assert(0 && "Unexpected node!");
Chris Lattnerdd66a412005-05-15 05:46:45 +00002915 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00002916 // If we found the start of the call sequence, we're done. We actually
2917 // strip off the CALLSEQ_START node, to avoid generating the
2918 // ADJCALLSTACKDOWN marker for the tail call.
2919 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002920 case ISD::TokenFactor: {
2921 std::vector<SDOperand> Ops;
2922 Ops.reserve(Chain.getNumOperands());
2923 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2924 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
2925 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
2926 }
2927 case ISD::STORE: // Normal store
2928 StoreVT = Chain.getOperand(1).getValueType();
2929 break;
2930 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00002931 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00002932 break;
2933 }
2934
2935 SDOperand OrigDest = Chain.getOperand(2);
2936 unsigned OrigOffset;
2937
2938 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
2939 OrigOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +00002940 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002941 } else {
2942 // We expect only (ESP+C)
2943 assert(OrigDest.getOpcode() == ISD::ADD &&
2944 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
2945 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
Chris Lattner7c762782005-08-16 21:56:37 +00002946 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
2947 == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002948 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2949 }
2950
2951 // Compute the new offset from the incoming ESP value we wish to use.
2952 unsigned NewOffset = OrigOffset + Offset;
2953
2954 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
2955 MachineFunction &MF = DAG.getMachineFunction();
2956 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
2957 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
2958
2959 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
2960 DAG);
2961 if (Chain.getOpcode() == ISD::STORE)
2962 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
2963 FIN);
2964 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
2965 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00002966 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00002967}
2968
2969
2970/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
2971/// fastcc function from a fastcc function, emit the code to emit a 'proper'
2972/// tail call.
2973void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
2974 unsigned CalleeCallArgSize =
2975 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
2976 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
2977
2978 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
2979
2980 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
2981 // indexes that are relative to the incoming ESP. If the incoming and
2982 // outgoing arg sizes are the same we will store to [InESP] instead of
2983 // [CurESP] and the ESP referenced will be relative to the incoming function
2984 // ESP.
2985 int ESPOffset = CallerArgSize-CalleeCallArgSize;
2986 SDOperand AdjustedArgStores =
2987 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
2988
2989 // Copy the return address of the caller into a virtual register so we don't
2990 // clobber it.
Chris Lattnerefbb8da2006-01-06 17:56:38 +00002991 SDOperand RetVal(0, 0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002992 if (ESPOffset) {
2993 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
2994 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
2995 RetValAddr, TheDAG->getSrcValue(NULL));
2996 SelectExpr(RetVal);
2997 }
2998
2999 // Codegen all of the argument stores.
3000 Select(AdjustedArgStores);
3001
3002 if (RetVal.Val) {
3003 // Emit a store of the saved ret value to the new location.
3004 MachineFunction &MF = TheDAG->getMachineFunction();
3005 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
3006 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
3007 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
3008 RetVal, RetValAddr));
3009 }
3010
3011 // Get the destination value.
3012 SDOperand Callee = TailCallNode->getOperand(1);
3013 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
3014 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00003015 unsigned CalleeReg = 0;
Chris Lattner7e792922005-12-04 06:03:50 +00003016 if (!isDirect) {
3017 // If this is not a direct tail call, evaluate the callee's address.
3018 CalleeReg = SelectExpr(Callee);
3019 }
Chris Lattnerdd66a412005-05-15 05:46:45 +00003020
3021 unsigned RegOp1 = 0;
3022 unsigned RegOp2 = 0;
3023
3024 if (TailCallNode->getNumOperands() > 4) {
3025 // The first value is passed in (a part of) EAX, the second in EDX.
3026 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
3027 if (TailCallNode->getNumOperands() > 5)
3028 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003029
Chris Lattnerdd66a412005-05-15 05:46:45 +00003030 switch (TailCallNode->getOperand(4).getValueType()) {
3031 default: assert(0 && "Bad thing to pass in regs");
3032 case MVT::i1:
3033 case MVT::i8:
3034 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
3035 RegOp1 = X86::AL;
3036 break;
3037 case MVT::i16:
3038 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
3039 RegOp1 = X86::AX;
3040 break;
3041 case MVT::i32:
3042 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
3043 RegOp1 = X86::EAX;
3044 break;
3045 }
3046 if (RegOp2)
3047 switch (TailCallNode->getOperand(5).getValueType()) {
3048 default: assert(0 && "Bad thing to pass in regs");
3049 case MVT::i1:
3050 case MVT::i8:
3051 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
3052 RegOp2 = X86::DL;
3053 break;
3054 case MVT::i16:
3055 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3056 RegOp2 = X86::DX;
3057 break;
3058 case MVT::i32:
3059 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3060 RegOp2 = X86::EDX;
3061 break;
3062 }
3063 }
Chris Lattner7e792922005-12-04 06:03:50 +00003064
3065 // If this is not a direct tail call, put the callee's address into ECX.
3066 // The address has to be evaluated into a non-callee save register that is
3067 // not used for arguments. This means either ECX, as EAX and EDX may be
3068 // used for argument passing. We do this here to make sure that the
3069 // expressions for arguments and callee are all evaluated before the copies
3070 // into physical registers.
3071 if (!isDirect)
3072 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CalleeReg);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003073
3074 // Adjust ESP.
3075 if (ESPOffset)
3076 BuildMI(BB, X86::ADJSTACKPTRri, 2,
3077 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
3078
3079 // TODO: handle jmp [mem]
3080 if (!isDirect) {
Chris Lattner7e792922005-12-04 06:03:50 +00003081 BuildMI(BB, X86::TAILJMPr, 1).addReg(X86::ECX);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003082 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00003083 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003084 } else {
3085 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
3086 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
3087 }
3088 // ADD IMPLICIT USE RegOp1/RegOp2's
3089}
3090
Chris Lattner96113fd2005-01-17 19:25:26 +00003091
Chris Lattner88c8a232005-01-07 07:49:41 +00003092void ISel::Select(SDOperand N) {
Chris Lattner9982da22005-10-02 16:29:36 +00003093 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
Chris Lattner88c8a232005-01-07 07:49:41 +00003094
Nate Begeman95210522005-03-24 04:39:54 +00003095 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003096 return; // Already selected.
3097
Chris Lattner36f78482005-01-11 06:14:36 +00003098 SDNode *Node = N.Val;
3099
3100 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003101 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003102 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003103 assert(0 && "Node not handled yet!");
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00003104 case X86ISD::RDTSC_DAG:
3105 Select(Node->getOperand(0)); //Chain
3106 BuildMI(BB, X86::RDTSC, 0);
3107 return;
3108
Chris Lattner88c8a232005-01-07 07:49:41 +00003109 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003110 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003111 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003112 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003113 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3114 Select(Node->getOperand(OneFirst));
3115 Select(Node->getOperand(!OneFirst));
3116 } else {
3117 std::vector<std::pair<unsigned, unsigned> > OpsP;
3118 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3119 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3120 std::sort(OpsP.begin(), OpsP.end());
3121 std::reverse(OpsP.begin(), OpsP.end());
3122 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3123 Select(Node->getOperand(OpsP[i].second));
3124 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003125 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003126 case ISD::CopyToReg:
Chris Lattner7c762782005-08-16 21:56:37 +00003127 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattner2cfce682005-01-12 02:02:48 +00003128 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00003129 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003130 } else {
Chris Lattner7c762782005-08-16 21:56:37 +00003131 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003132 Select(N.getOperand(0));
3133 }
Chris Lattner7c762782005-08-16 21:56:37 +00003134 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003135
Chris Lattner88c8a232005-01-07 07:49:41 +00003136 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00003137 switch (N.getOperand(2).getValueType()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003138 default: assert(0 && "Invalid type for operation!");
3139 case MVT::i1:
3140 case MVT::i8: Opc = X86::MOV8rr; break;
3141 case MVT::i16: Opc = X86::MOV16rr; break;
3142 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman9d7008b2005-10-14 22:06:00 +00003143 case MVT::f32: Opc = X86::MOVSSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003144 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003145 if (X86ScalarSSE) {
Nate Begeman9d7008b2005-10-14 22:06:00 +00003146 Opc = X86::MOVSDrr;
Nate Begeman8a093362005-07-06 18:59:04 +00003147 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003148 Opc = X86::FpMOV;
3149 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003150 }
3151 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003152 }
3153 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3154 }
3155 return;
3156 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00003157 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
3158 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
3159 N.getOperand(0).getOpcode() == ISD::TokenFactor)
3160 if (EmitPotentialTailCall(Node))
3161 return;
3162
Chris Lattner88c8a232005-01-07 07:49:41 +00003163 switch (N.getNumOperands()) {
3164 default:
3165 assert(0 && "Unknown return instruction!");
3166 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003167 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003168 N.getOperand(2).getValueType() == MVT::i32 &&
3169 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003170 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3171 Tmp1 = SelectExpr(N.getOperand(1));
3172 Tmp2 = SelectExpr(N.getOperand(2));
3173 } else {
3174 Tmp2 = SelectExpr(N.getOperand(2));
3175 Tmp1 = SelectExpr(N.getOperand(1));
3176 }
3177 Select(N.getOperand(0));
3178
Chris Lattner88c8a232005-01-07 07:49:41 +00003179 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3180 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003181 break;
3182 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003183 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3184 Select(N.getOperand(0));
3185 Tmp1 = SelectExpr(N.getOperand(1));
3186 } else {
3187 Tmp1 = SelectExpr(N.getOperand(1));
3188 Select(N.getOperand(0));
3189 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003190 switch (N.getOperand(1).getValueType()) {
3191 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00003192 case MVT::f32:
3193 if (X86ScalarSSE) {
3194 // Spill the value to memory and reload it into top of stack.
3195 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
3196 MachineFunction *F = BB->getParent();
3197 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3198 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003199 addFrameReference(BuildMI(BB, X86::FpLD32m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003200 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003201 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003202 } else {
3203 assert(0 && "MVT::f32 only legal with scalar sse fp");
3204 abort();
3205 }
3206 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003207 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003208 if (X86ScalarSSE) {
3209 // Spill the value to memory and reload it into top of stack.
3210 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3211 MachineFunction *F = BB->getParent();
3212 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3213 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003214 addFrameReference(BuildMI(BB, X86::FpLD64m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003215 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003216 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003217 } else {
3218 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
3219 }
3220 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003221 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00003222 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3223 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003224 }
3225 break;
3226 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003227 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003228 break;
3229 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003230 if (X86Lowering.getBytesToPopOnReturn() == 0)
3231 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3232 else
3233 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00003234 return;
3235 case ISD::BR: {
3236 Select(N.getOperand(0));
3237 MachineBasicBlock *Dest =
3238 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3239 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3240 return;
3241 }
3242
3243 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003244 MachineBasicBlock *Dest =
3245 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003246
Chris Lattner88c8a232005-01-07 07:49:41 +00003247 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3248 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003249 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3250 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3251 Select(N.getOperand(0));
3252 Tmp1 = SelectExpr(N.getOperand(1));
3253 } else {
3254 Tmp1 = SelectExpr(N.getOperand(1));
3255 Select(N.getOperand(0));
3256 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003257 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3258 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3259 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003260
Chris Lattner88c8a232005-01-07 07:49:41 +00003261 return;
3262 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003263
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003264 case ISD::LOAD:
3265 // If this load could be folded into the only using instruction, and if it
3266 // is safe to emit the instruction here, try to do so now.
3267 if (Node->hasNUsesOfValue(1, 0)) {
3268 SDOperand TheVal = N.getValue(0);
3269 SDNode *User = 0;
3270 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3271 assert(UI != Node->use_end() && "Didn't find use!");
3272 SDNode *UN = *UI;
3273 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3274 if (UN->getOperand(i) == TheVal) {
3275 User = UN;
3276 goto FoundIt;
3277 }
3278 }
3279 FoundIt:
3280 // Only handle unary operators right now.
3281 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003282 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003283 SelectExpr(SDOperand(User, 0));
3284 return;
3285 }
3286 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003287 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003288 SelectExpr(N);
3289 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003290 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003291 case ISD::EXTLOAD:
3292 case ISD::SEXTLOAD:
3293 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003294 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner1b3520c2005-05-14 08:48:15 +00003295 case X86ISD::TAILCALL:
3296 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00003297 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003298 SelectExpr(N);
3299 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003300 case ISD::CopyFromReg:
Chris Lattnera36117b2005-05-14 06:52:07 +00003301 case X86ISD::FILD64m:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003302 ExprMap.erase(N);
3303 SelectExpr(N.getValue(0));
3304 return;
Jeff Cohen546fd592005-07-30 18:33:25 +00003305
Chris Lattner4738d1b2005-07-30 00:05:54 +00003306 case X86ISD::FP_TO_INT16_IN_MEM:
3307 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattner6dc60e82005-07-29 00:54:34 +00003308 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00003309 assert(N.getOperand(1).getValueType() == MVT::f64);
3310 X86AddressMode AM;
3311 Select(N.getOperand(0)); // Select the token chain
3312
3313 unsigned ValReg;
3314 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3315 ValReg = SelectExpr(N.getOperand(1));
3316 SelectAddress(N.getOperand(2), AM);
3317 } else {
3318 SelectAddress(N.getOperand(2), AM);
3319 ValReg = SelectExpr(N.getOperand(1));
3320 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003321
Chris Lattner6dc60e82005-07-29 00:54:34 +00003322 // Change the floating point control register to use "round towards zero"
3323 // mode when truncating to an integer value.
3324 //
3325 MachineFunction *F = BB->getParent();
3326 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3327 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003328
Chris Lattner6dc60e82005-07-29 00:54:34 +00003329 // Load the old value of the high byte of the control word...
Chris Lattneraeef51b2005-07-30 00:17:52 +00003330 unsigned OldCW = MakeReg(MVT::i16);
3331 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003332
Chris Lattner6dc60e82005-07-29 00:54:34 +00003333 // Set the high part to be round to zero...
Chris Lattner49134572005-07-30 00:43:00 +00003334 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohen546fd592005-07-30 18:33:25 +00003335
Chris Lattner6dc60e82005-07-29 00:54:34 +00003336 // Reload the modified control word now...
3337 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003338
Chris Lattner6dc60e82005-07-29 00:54:34 +00003339 // Restore the memory image of control word to original value
Chris Lattneraeef51b2005-07-30 00:17:52 +00003340 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner4738d1b2005-07-30 00:05:54 +00003341
3342 // Get the X86 opcode to use.
3343 switch (N.getOpcode()) {
Chris Lattnerf431ad42005-12-21 07:47:04 +00003344 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FpIST16m; break;
3345 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FpIST32m; break;
3346 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FpIST64m; break;
Chris Lattner4738d1b2005-07-30 00:05:54 +00003347 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003348
Chris Lattner4738d1b2005-07-30 00:05:54 +00003349 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohen546fd592005-07-30 18:33:25 +00003350
Chris Lattner6dc60e82005-07-29 00:54:34 +00003351 // Reload the original control word now.
3352 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00003353 return;
3354 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003355
Chris Lattner36db1ed2005-07-10 00:29:18 +00003356 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00003357 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00003358 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003359 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3360 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3361 && "Unsupported TRUNCSTORE for this target!");
3362
3363 if (StoredTy == MVT::i16) {
3364 // FIXME: This is here just to allow testing. X86 doesn't really have a
3365 // TRUNCSTORE i16 operation, but this is required for targets that do not
3366 // have 16-bit integer registers. We occasionally disable 16-bit integer
3367 // registers to test the promotion code.
3368 Select(N.getOperand(0));
3369 Tmp1 = SelectExpr(N.getOperand(1));
3370 SelectAddress(N.getOperand(2), AM);
3371
3372 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3373 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3374 return;
3375 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003376
3377 // Store of constant bool?
3378 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3379 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3380 Select(N.getOperand(0));
3381 SelectAddress(N.getOperand(2), AM);
3382 } else {
3383 SelectAddress(N.getOperand(2), AM);
3384 Select(N.getOperand(0));
3385 }
3386 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3387 return;
3388 }
3389
3390 switch (StoredTy) {
3391 default: assert(0 && "Cannot truncstore this type!");
3392 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003393 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003394 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Chris Lattnerf431ad42005-12-21 07:47:04 +00003395 Opc = X86::FpST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003396 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003397
Chris Lattnere18a4c42005-01-15 05:22:24 +00003398 std::vector<std::pair<unsigned, unsigned> > RP;
3399 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3400 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3401 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3402 std::sort(RP.begin(), RP.end());
3403
Chris Lattner80c5b972005-02-23 05:57:21 +00003404 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003405 for (unsigned i = 0; i != 3; ++i)
3406 switch (RP[2-i].second) {
3407 default: assert(0 && "Unknown operand number!");
3408 case 0: Select(N.getOperand(0)); break;
3409 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3410 case 2: SelectAddress(N.getOperand(2), AM); break;
3411 }
3412
3413 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3414 return;
3415 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003416 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003417 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003418
3419 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3420 Opc = 0;
3421 switch (CN->getValueType(0)) {
3422 default: assert(0 && "Invalid type for operation!");
3423 case MVT::i1:
3424 case MVT::i8: Opc = X86::MOV8mi; break;
3425 case MVT::i16: Opc = X86::MOV16mi; break;
3426 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003427 }
3428 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003429 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3430 Select(N.getOperand(0));
3431 SelectAddress(N.getOperand(2), AM);
3432 } else {
3433 SelectAddress(N.getOperand(2), AM);
3434 Select(N.getOperand(0));
3435 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003436 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3437 return;
3438 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003439 } else if (GlobalAddressSDNode *GA =
3440 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3441 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3442
3443 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3444 Select(N.getOperand(0));
3445 SelectAddress(N.getOperand(2), AM);
3446 } else {
3447 SelectAddress(N.getOperand(2), AM);
3448 Select(N.getOperand(0));
3449 }
Nate Begemana0b5e032005-07-15 00:38:55 +00003450 GlobalValue *GV = GA->getGlobal();
3451 // For Darwin, external and weak symbols are indirect, so we want to load
3452 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003453 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00003454 (GV->hasWeakLinkage() || GV->isExternal())) {
3455 Tmp1 = MakeReg(MVT::i32);
3456 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
3457 .addGlobalAddress(GV, false, 0);
3458 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
3459 } else {
3460 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
3461 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003462 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003463 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003464
3465 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003466 if (TryToFoldLoadOpStore(Node))
3467 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003468
Chris Lattner88c8a232005-01-07 07:49:41 +00003469 switch (N.getOperand(1).getValueType()) {
3470 default: assert(0 && "Cannot store this type!");
3471 case MVT::i1:
3472 case MVT::i8: Opc = X86::MOV8mr; break;
3473 case MVT::i16: Opc = X86::MOV16mr; break;
3474 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003475 case MVT::f32: Opc = X86::MOVSSmr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00003476 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FpST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003477 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003478
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003479 std::vector<std::pair<unsigned, unsigned> > RP;
3480 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3481 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3482 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3483 std::sort(RP.begin(), RP.end());
3484
Chris Lattner80c5b972005-02-23 05:57:21 +00003485 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003486 for (unsigned i = 0; i != 3; ++i)
3487 switch (RP[2-i].second) {
3488 default: assert(0 && "Unknown operand number!");
3489 case 0: Select(N.getOperand(0)); break;
3490 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003491 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003492 }
3493
Chris Lattner88c8a232005-01-07 07:49:41 +00003494 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3495 return;
3496 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003497 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003498 Select(N.getOperand(0));
3499 // Stack amount
3500 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3501 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3502 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00003503 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003504 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003505 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003506 case ISD::MEMSET: {
3507 Select(N.getOperand(0)); // Select the chain.
3508 unsigned Align =
3509 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3510 if (Align == 0) Align = 1;
3511
3512 // Turn the byte code into # iterations
3513 unsigned CountReg;
3514 unsigned Opcode;
3515 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3516 unsigned Val = ValC->getValue() & 255;
3517
3518 // If the value is a constant, then we can potentially use larger sets.
3519 switch (Align & 3) {
3520 case 2: // WORD aligned
3521 CountReg = MakeReg(MVT::i32);
3522 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3523 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3524 } else {
3525 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3526 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3527 }
3528 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3529 Opcode = X86::REP_STOSW;
3530 break;
3531 case 0: // DWORD aligned
3532 CountReg = MakeReg(MVT::i32);
3533 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3534 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3535 } else {
3536 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3537 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3538 }
3539 Val = (Val << 8) | Val;
3540 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3541 Opcode = X86::REP_STOSD;
3542 break;
3543 default: // BYTE aligned
3544 CountReg = SelectExpr(Node->getOperand(3));
3545 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3546 Opcode = X86::REP_STOSB;
3547 break;
3548 }
3549 } else {
3550 // If it's not a constant value we are storing, just fall back. We could
3551 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3552 unsigned ValReg = SelectExpr(Node->getOperand(2));
3553 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3554 CountReg = SelectExpr(Node->getOperand(3));
3555 Opcode = X86::REP_STOSB;
3556 }
3557
3558 // No matter what the alignment is, we put the source in ESI, the
3559 // destination in EDI, and the count in ECX.
3560 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3561 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3562 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3563 BuildMI(BB, Opcode, 0);
3564 return;
3565 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003566 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003567 Select(N.getOperand(0)); // Select the chain.
3568 unsigned Align =
3569 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3570 if (Align == 0) Align = 1;
3571
3572 // Turn the byte code into # iterations
3573 unsigned CountReg;
3574 unsigned Opcode;
3575 switch (Align & 3) {
3576 case 2: // WORD aligned
3577 CountReg = MakeReg(MVT::i32);
3578 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3579 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3580 } else {
3581 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3582 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3583 }
3584 Opcode = X86::REP_MOVSW;
3585 break;
3586 case 0: // DWORD aligned
3587 CountReg = MakeReg(MVT::i32);
3588 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3589 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3590 } else {
3591 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3592 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3593 }
3594 Opcode = X86::REP_MOVSD;
3595 break;
3596 default: // BYTE aligned
3597 CountReg = SelectExpr(Node->getOperand(3));
3598 Opcode = X86::REP_MOVSB;
3599 break;
3600 }
3601
3602 // No matter what the alignment is, we put the source in ESI, the
3603 // destination in EDI, and the count in ECX.
3604 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3605 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3606 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3607 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3608 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3609 BuildMI(BB, Opcode, 0);
3610 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003611 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003612 case ISD::WRITEPORT:
3613 if (Node->getOperand(2).getValueType() != MVT::i16) {
3614 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3615 exit(1);
3616 }
3617 Select(Node->getOperand(0)); // Emit the chain.
3618
3619 Tmp1 = SelectExpr(Node->getOperand(1));
3620 switch (Node->getOperand(1).getValueType()) {
3621 case MVT::i8:
3622 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3623 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3624 break;
3625 case MVT::i16:
3626 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3627 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3628 break;
3629 case MVT::i32:
3630 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3631 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3632 break;
3633 default:
3634 std::cerr << "llvm.writeport: invalid data type for X86 target";
3635 exit(1);
3636 }
3637
3638 // If the port is a single-byte constant, use the immediate form.
3639 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3640 if ((CN->getValue() & 255) == CN->getValue()) {
3641 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3642 return;
3643 }
3644
3645 // Otherwise, move the I/O port address into the DX register.
3646 unsigned Reg = SelectExpr(Node->getOperand(2));
3647 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3648 BuildMI(BB, Opc, 0);
3649 return;
3650 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003651 assert(0 && "Should not be reached!");
3652}
3653
3654
Chris Lattner76ac0682005-11-15 00:40:23 +00003655/// createX86ISelPattern - This pass converts an LLVM function
Chris Lattner88c8a232005-01-07 07:49:41 +00003656/// into a machine code representation using pattern matching and a machine
3657/// description file.
3658///
Chris Lattner76ac0682005-11-15 00:40:23 +00003659FunctionPass *llvm::createX86ISelPattern(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003660 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003661}