blob: 58c60692b9d52aaf6b952092b85aaf9bc730b83b [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
229
Chris Lattner6fba62d62005-01-12 04:21:28 +0000230 // Insert FP_REG_KILL instructions into basic blocks that need them. This
231 // only occurs due to the floating point stackifier not being aggressive
232 // enough to handle arbitrary global stackification.
233 //
234 // Currently we insert an FP_REG_KILL instruction into each block that uses or
235 // defines a floating point virtual register.
236 //
237 // When the global register allocators (like linear scan) finally update live
238 // variable analysis, we can keep floating point values in registers across
239 // basic blocks. This will be a huge win, but we are waiting on the global
240 // allocators before we can do this.
241 //
Chris Lattner472a2652005-03-30 01:10:00 +0000242 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +0000243 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
244 ++NumFPKill;
245 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000246
Chris Lattner6fba62d62005-01-12 04:21:28 +0000247 // Clear state used for selection.
248 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000249 RegPressureMap.clear();
250}
251
252
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000253// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
254// for the number of registers required to compute each node. This is basically
255// computing a generalized form of the Sethi-Ullman number for each node.
256unsigned ISel::ComputeRegPressure(SDOperand O) {
257 SDNode *N = O.Val;
258 unsigned &Result = RegPressureMap[N];
259 if (Result) return Result;
260
Chris Lattner8fea42b2005-01-11 03:37:59 +0000261 // FIXME: Should operations like CALL (which clobber lots o regs) have a
262 // higher fixed cost??
263
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000264 if (N->getNumOperands() == 0) {
265 Result = 1;
266 } else {
267 unsigned MaxRegUse = 0;
268 unsigned NumExtraMaxRegUsers = 0;
269 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
270 unsigned Regs;
271 if (N->getOperand(i).getOpcode() == ISD::Constant)
272 Regs = 0;
273 else
274 Regs = ComputeRegPressure(N->getOperand(i));
275 if (Regs > MaxRegUse) {
276 MaxRegUse = Regs;
277 NumExtraMaxRegUsers = 0;
278 } else if (Regs == MaxRegUse &&
279 N->getOperand(i).getValueType() != MVT::Other) {
280 ++NumExtraMaxRegUsers;
281 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000282 }
Chris Lattnerca318ed2005-01-17 22:56:09 +0000283
284 if (O.getOpcode() != ISD::TokenFactor)
285 Result = MaxRegUse+NumExtraMaxRegUsers;
286 else
Chris Lattnera5d137f2005-01-17 23:02:13 +0000287 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000288 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +0000289
Chris Lattner75bac9f2005-01-11 23:21:30 +0000290 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000291 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000292}
293
Chris Lattner5b04f332005-01-20 16:50:16 +0000294/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
295/// The DAG cannot have cycles in it, by definition, so the visited set is not
296/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
297/// reuse, so it prevents exponential cases.
298///
299static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
300 std::set<SDNode*> &Visited) {
301 if (N == Op) return true; // Found it.
302 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +0000303 if (Node->getNumOperands() == 0 || // Leaf?
304 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +0000305 if (!Visited.insert(Node).second) return false; // Already visited?
306
307 // Recurse for the first N-1 operands.
308 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
309 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
310 return true;
311
312 // Tail recurse for the last operand.
313 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
314}
315
Chris Lattnera7acdda2005-01-18 01:06:26 +0000316X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
317 X86AddressMode Result;
318
319 // If we need to emit two register operands, emit the one with the highest
320 // register pressure first.
321 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
322 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000323 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000324 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000325 std::set<SDNode*> Visited;
326 EmitBaseThenIndex = true;
327 // If Base ends up pointing to Index, we must emit index first. This is
328 // because of the way we fold loads, we may end up doing bad things with
329 // the folded add.
330 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
331 EmitBaseThenIndex = false;
332 } else {
333 std::set<SDNode*> Visited;
334 EmitBaseThenIndex = false;
335 // If Base ends up pointing to Index, we must emit index first. This is
336 // because of the way we fold loads, we may end up doing bad things with
337 // the folded add.
338 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
339 EmitBaseThenIndex = true;
340 }
341
342 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000343 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
344 Result.IndexReg = SelectExpr(IAM.IndexReg);
345 } else {
346 Result.IndexReg = SelectExpr(IAM.IndexReg);
347 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
348 }
Chris Lattner5b04f332005-01-20 16:50:16 +0000349
Chris Lattnera7acdda2005-01-18 01:06:26 +0000350 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
351 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
352 } else if (IAM.IndexReg.Val) {
353 Result.IndexReg = SelectExpr(IAM.IndexReg);
354 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000355
Chris Lattnera7acdda2005-01-18 01:06:26 +0000356 switch (IAM.BaseType) {
357 case X86ISelAddressMode::RegBase:
358 Result.BaseType = X86AddressMode::RegBase;
359 break;
360 case X86ISelAddressMode::FrameIndexBase:
361 Result.BaseType = X86AddressMode::FrameIndexBase;
362 Result.Base.FrameIndex = IAM.Base.FrameIndex;
363 break;
364 default:
365 assert(0 && "Unknown base type!");
366 break;
367 }
368 Result.Scale = IAM.Scale;
369 Result.Disp = IAM.Disp;
370 Result.GV = IAM.GV;
371 return Result;
372}
373
374/// SelectAddress - Pattern match the maximal addressing mode for this node and
375/// emit all of the leaf registers.
376void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
377 X86ISelAddressMode IAM;
378 MatchAddress(N, IAM);
379 AM = SelectAddrExprs(IAM);
380}
381
382/// MatchAddress - Add the specified node to the specified addressing mode,
383/// returning true if it cannot be done. This just pattern matches for the
384/// addressing mode, it does not cause any code to be emitted. For that, use
385/// SelectAddress.
386bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000387 switch (N.getOpcode()) {
388 default: break;
389 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +0000390 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
391 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +0000392 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
393 return false;
394 }
395 break;
396 case ISD::GlobalAddress:
397 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +0000398 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
399 // For Darwin, external and weak symbols are indirect, so we want to load
400 // the value at address GV, not the value of GV itself. This means that
401 // the GlobalAddress must be in the base or index register of the address,
402 // not the GV offset field.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000403 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +0000404 (GV->hasWeakLinkage() || GV->isExternal())) {
405 break;
406 } else {
407 AM.GV = GV;
408 return false;
409 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000410 }
411 break;
412 case ISD::Constant:
413 AM.Disp += cast<ConstantSDNode>(N)->getValue();
414 return false;
415 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000416 // We might have folded the load into this shift, so don't regen the value
417 // if so.
418 if (ExprMap.count(N)) break;
419
Chris Lattnera7acdda2005-01-18 01:06:26 +0000420 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +0000421 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
422 unsigned Val = CN->getValue();
423 if (Val == 1 || Val == 2 || Val == 3) {
424 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000425 SDOperand ShVal = N.Val->getOperand(0);
426
427 // Okay, we know that we have a scale by now. However, if the scaled
428 // value is an add of something and a constant, we can fold the
429 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000430 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000431 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000432 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000433 ConstantSDNode *AddVal =
434 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
435 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +0000436 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000437 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000438 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000439 return false;
440 }
441 }
442 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000443 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000444 // We might have folded the load into this mul, so don't regen the value if
445 // so.
446 if (ExprMap.count(N)) break;
447
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000448 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +0000449 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
450 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000451 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
452 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
453 AM.Scale = unsigned(CN->getValue())-1;
454
455 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000456 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000457
458 // Okay, we know that we have a scale by now. However, if the scaled
459 // value is an add of something and a constant, we can fold the
460 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000461 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000462 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000463 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000464 ConstantSDNode *AddVal =
465 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
466 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +0000467 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000468 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000469 }
470
471 AM.IndexReg = AM.Base.Reg = Reg;
472 return false;
473 }
474 break;
Chris Lattner88c8a232005-01-07 07:49:41 +0000475
476 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +0000477 // We might have folded the load into this mul, so don't regen the value if
478 // so.
479 if (ExprMap.count(N)) break;
480
Chris Lattnera7acdda2005-01-18 01:06:26 +0000481 X86ISelAddressMode Backup = AM;
482 if (!MatchAddress(N.Val->getOperand(0), AM) &&
483 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +0000484 return false;
485 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000486 if (!MatchAddress(N.Val->getOperand(1), AM) &&
487 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +0000488 return false;
489 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +0000490 break;
491 }
492 }
493
Chris Lattner378262d2005-01-11 04:40:19 +0000494 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +0000495 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +0000496 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000497 if (AM.IndexReg.Val == 0) {
498 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +0000499 AM.Scale = 1;
500 return false;
501 }
502
503 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +0000504 return true;
Chris Lattner378262d2005-01-11 04:40:19 +0000505 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000506
507 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000508 AM.BaseType = X86ISelAddressMode::RegBase;
509 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +0000510 return false;
511}
512
513/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
514/// assuming that the temporary registers are in the 8-bit register class.
515///
516/// Tmp1 = setcc1
517/// Tmp2 = setcc2
518/// DestReg = logicalop Tmp1, Tmp2
519///
520static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
521 unsigned SetCC2, unsigned LogicalOp,
522 unsigned DestReg) {
523 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
524 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
525 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
526 BuildMI(BB, SetCC1, 0, Tmp1);
527 BuildMI(BB, SetCC2, 0, Tmp2);
528 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
529}
530
531/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
532/// condition codes match the specified SetCCOpcode. Note that some conditions
533/// require multiple instructions to generate the correct value.
534static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
535 ISD::CondCode SetCCOpcode, bool isFP) {
536 unsigned Opc;
537 if (!isFP) {
538 switch (SetCCOpcode) {
539 default: assert(0 && "Illegal integer SetCC!");
540 case ISD::SETEQ: Opc = X86::SETEr; break;
541 case ISD::SETGT: Opc = X86::SETGr; break;
542 case ISD::SETGE: Opc = X86::SETGEr; break;
543 case ISD::SETLT: Opc = X86::SETLr; break;
544 case ISD::SETLE: Opc = X86::SETLEr; break;
545 case ISD::SETNE: Opc = X86::SETNEr; break;
546 case ISD::SETULT: Opc = X86::SETBr; break;
547 case ISD::SETUGT: Opc = X86::SETAr; break;
548 case ISD::SETULE: Opc = X86::SETBEr; break;
549 case ISD::SETUGE: Opc = X86::SETAEr; break;
550 }
551 } else {
552 // On a floating point condition, the flags are set as follows:
553 // ZF PF CF op
554 // 0 | 0 | 0 | X > Y
555 // 0 | 0 | 1 | X < Y
556 // 1 | 0 | 0 | X == Y
557 // 1 | 1 | 1 | unordered
558 //
559 switch (SetCCOpcode) {
560 default: assert(0 && "Invalid FP setcc!");
561 case ISD::SETUEQ:
562 case ISD::SETEQ:
563 Opc = X86::SETEr; // True if ZF = 1
564 break;
565 case ISD::SETOGT:
566 case ISD::SETGT:
567 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
568 break;
569 case ISD::SETOGE:
570 case ISD::SETGE:
571 Opc = X86::SETAEr; // True if CF = 0
572 break;
573 case ISD::SETULT:
574 case ISD::SETLT:
575 Opc = X86::SETBr; // True if CF = 1
576 break;
577 case ISD::SETULE:
578 case ISD::SETLE:
579 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
580 break;
581 case ISD::SETONE:
582 case ISD::SETNE:
583 Opc = X86::SETNEr; // True if ZF = 0
584 break;
585 case ISD::SETUO:
586 Opc = X86::SETPr; // True if PF = 1
587 break;
588 case ISD::SETO:
589 Opc = X86::SETNPr; // True if PF = 0
590 break;
591 case ISD::SETOEQ: // !PF & ZF
592 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
593 return;
594 case ISD::SETOLT: // !PF & CF
595 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
596 return;
597 case ISD::SETOLE: // !PF & (CF || ZF)
598 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
599 return;
600 case ISD::SETUGT: // PF | (!ZF & !CF)
601 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
602 return;
603 case ISD::SETUGE: // PF | !CF
604 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
605 return;
606 case ISD::SETUNE: // PF | !ZF
607 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
608 return;
609 }
610 }
611 BuildMI(BB, Opc, 0, DestReg);
612}
613
614
615/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
616/// the Dest block if the Cond condition is true. If we cannot fold this
617/// condition into the branch, return true.
618///
Chris Lattner37ed2852005-01-11 04:06:27 +0000619bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
620 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000621 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
622 // B) using two conditional branches instead of one condbr, two setcc's, and
623 // an or.
624 if ((Cond.getOpcode() == ISD::OR ||
625 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
626 // And and or set the flags for us, so there is no need to emit a TST of the
627 // result. It is only safe to do this if there is only a single use of the
628 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +0000629 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000630 SelectExpr(Cond);
631 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
632 return false;
633 }
634
635 // Codegen br not C -> JE.
636 if (Cond.getOpcode() == ISD::XOR)
637 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
638 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +0000639 unsigned CondR;
640 if (getRegPressure(Chain) > getRegPressure(Cond)) {
641 Select(Chain);
642 CondR = SelectExpr(Cond.Val->getOperand(0));
643 } else {
644 CondR = SelectExpr(Cond.Val->getOperand(0));
645 Select(Chain);
646 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000647 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
648 BuildMI(BB, X86::JE, 1).addMBB(Dest);
649 return false;
650 }
651
Chris Lattner6ec77452005-08-09 20:21:10 +0000652 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner88c8a232005-01-07 07:49:41 +0000653 return true; // Can only handle simple setcc's so far.
Chris Lattner6ec77452005-08-09 20:21:10 +0000654 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner88c8a232005-01-07 07:49:41 +0000655
656 unsigned Opc;
657
658 // Handle integer conditions first.
Chris Lattner6ec77452005-08-09 20:21:10 +0000659 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
660 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000661 default: assert(0 && "Illegal integer SetCC!");
662 case ISD::SETEQ: Opc = X86::JE; break;
663 case ISD::SETGT: Opc = X86::JG; break;
664 case ISD::SETGE: Opc = X86::JGE; break;
665 case ISD::SETLT: Opc = X86::JL; break;
666 case ISD::SETLE: Opc = X86::JLE; break;
667 case ISD::SETNE: Opc = X86::JNE; break;
668 case ISD::SETULT: Opc = X86::JB; break;
669 case ISD::SETUGT: Opc = X86::JA; break;
670 case ISD::SETULE: Opc = X86::JBE; break;
671 case ISD::SETUGE: Opc = X86::JAE; break;
672 }
Chris Lattner37ed2852005-01-11 04:06:27 +0000673 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000674 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000675 BuildMI(BB, Opc, 1).addMBB(Dest);
676 return false;
677 }
678
Chris Lattner88c8a232005-01-07 07:49:41 +0000679 unsigned Opc2 = 0; // Second branch if needed.
680
681 // On a floating point condition, the flags are set as follows:
682 // ZF PF CF op
683 // 0 | 0 | 0 | X > Y
684 // 0 | 0 | 1 | X < Y
685 // 1 | 0 | 0 | X == Y
686 // 1 | 1 | 1 | unordered
687 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000688 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000689 default: assert(0 && "Invalid FP setcc!");
690 case ISD::SETUEQ:
691 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
692 case ISD::SETOGT:
693 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
694 case ISD::SETOGE:
695 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
696 case ISD::SETULT:
697 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
698 case ISD::SETULE:
699 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
700 case ISD::SETONE:
701 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
702 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
703 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
704 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
705 Opc = X86::JA; // ZF = 0 & CF = 0
706 Opc2 = X86::JP; // PF = 1
707 break;
708 case ISD::SETUGE: // PF = 1 | CF = 0
709 Opc = X86::JAE; // CF = 0
710 Opc2 = X86::JP; // PF = 1
711 break;
712 case ISD::SETUNE: // PF = 1 | ZF = 0
713 Opc = X86::JNE; // ZF = 0
714 Opc2 = X86::JP; // PF = 1
715 break;
716 case ISD::SETOEQ: // PF = 0 & ZF = 1
717 //X86::JNP, X86::JE
718 //X86::AND8rr
719 return true; // FIXME: Emit more efficient code for this branch.
720 case ISD::SETOLT: // PF = 0 & CF = 1
721 //X86::JNP, X86::JB
722 //X86::AND8rr
723 return true; // FIXME: Emit more efficient code for this branch.
724 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
725 //X86::JNP, X86::JBE
726 //X86::AND8rr
727 return true; // FIXME: Emit more efficient code for this branch.
728 }
729
Chris Lattner37ed2852005-01-11 04:06:27 +0000730 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000731 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000732 BuildMI(BB, Opc, 1).addMBB(Dest);
733 if (Opc2)
734 BuildMI(BB, Opc2, 1).addMBB(Dest);
735 return false;
736}
737
Chris Lattner1d13a922005-01-10 22:10:13 +0000738/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman8d394eb2005-08-03 23:26:28 +0000739/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner1d13a922005-01-10 22:10:13 +0000740///
Nate Begeman8d394eb2005-08-03 23:26:28 +0000741void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
742 MVT::ValueType SVT, unsigned RDest) {
743 unsigned RTrue, RFalse;
Chris Lattner1d13a922005-01-10 22:10:13 +0000744 enum Condition {
745 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
746 NOT_SET
747 } CondCode = NOT_SET;
748
749 static const unsigned CMOVTAB16[] = {
750 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
751 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000752 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000753 };
754 static const unsigned CMOVTAB32[] = {
755 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
756 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000757 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000758 };
759 static const unsigned CMOVTABFP[] = {
760 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
761 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
762 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
763 };
Nate Begemana0b5e032005-07-15 00:38:55 +0000764 static const int SSE_CMOVTAB[] = {
Nate Begeman8d394eb2005-08-03 23:26:28 +0000765 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
766 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
767 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begeman8a093362005-07-06 18:59:04 +0000768 };
Nate Begeman8d394eb2005-08-03 23:26:28 +0000769
Chris Lattner6ec77452005-08-09 20:21:10 +0000770 if (Cond.getOpcode() == ISD::SETCC) {
771 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
772 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
773 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000774 default: assert(0 && "Unknown integer comparison!");
775 case ISD::SETEQ: CondCode = EQ; break;
776 case ISD::SETGT: CondCode = GT; break;
777 case ISD::SETGE: CondCode = GE; break;
778 case ISD::SETLT: CondCode = LT; break;
779 case ISD::SETLE: CondCode = LE; break;
780 case ISD::SETNE: CondCode = NE; break;
781 case ISD::SETULT: CondCode = B; break;
782 case ISD::SETUGT: CondCode = A; break;
783 case ISD::SETULE: CondCode = BE; break;
784 case ISD::SETUGE: CondCode = AE; break;
785 }
786 } else {
787 // On a floating point condition, the flags are set as follows:
788 // ZF PF CF op
789 // 0 | 0 | 0 | X > Y
790 // 0 | 0 | 1 | X < Y
791 // 1 | 0 | 0 | X == Y
792 // 1 | 1 | 1 | unordered
793 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000794 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000795 default: assert(0 && "Unknown FP comparison!");
796 case ISD::SETUEQ:
797 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
798 case ISD::SETOGT:
799 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
800 case ISD::SETOGE:
801 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
802 case ISD::SETULT:
803 case ISD::SETLT: CondCode = B; break; // True if CF = 1
804 case ISD::SETULE:
805 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
806 case ISD::SETONE:
807 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
808 case ISD::SETUO: CondCode = P; break; // True if PF = 1
809 case ISD::SETO: CondCode = NP; break; // True if PF = 0
810 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
811 case ISD::SETUGE: // PF = 1 | CF = 0
812 case ISD::SETUNE: // PF = 1 | ZF = 0
813 case ISD::SETOEQ: // PF = 0 & ZF = 1
814 case ISD::SETOLT: // PF = 0 & CF = 1
815 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
816 // We cannot emit this comparison as a single cmov.
817 break;
818 }
819 }
Chris Lattner6ec77452005-08-09 20:21:10 +0000820
Chris Lattner1d13a922005-01-10 22:10:13 +0000821
Chris Lattner6ec77452005-08-09 20:21:10 +0000822 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
823 // code above and one of the results of the select is +0.0, then we can fake
824 // it up through a clever AND with mask. Otherwise, we will fall through to
825 // the code below that will use a PHI node to select the right value.
826 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
827 if (Cond.getOperand(0).getValueType() == SVT &&
828 NOT_SET != CondCode) {
829 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
830 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
831 bool TrueZero = CT && CT->isExactlyValue(0.0);
832 bool FalseZero = CF && CF->isExactlyValue(0.0);
833 if (TrueZero || FalseZero) {
834 SDOperand LHS = Cond.getOperand(0);
835 SDOperand RHS = Cond.getOperand(1);
836
837 // Select the two halves of the condition
838 unsigned RLHS, RRHS;
839 if (getRegPressure(LHS) > getRegPressure(RHS)) {
840 RLHS = SelectExpr(LHS);
841 RRHS = SelectExpr(RHS);
842 } else {
843 RRHS = SelectExpr(RHS);
844 RLHS = SelectExpr(LHS);
845 }
846
847 // Emit the comparison and generate a mask from it
848 unsigned MaskReg = MakeReg(SVT);
849 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
850 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
851 .addImm(SSE_CMOVTAB[CondCode]);
852
853 if (TrueZero) {
854 RFalse = SelectExpr(False);
855 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
856 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
857 } else {
858 RTrue = SelectExpr(True);
859 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
860 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
861 }
862 return;
Nate Begeman8d394eb2005-08-03 23:26:28 +0000863 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000864 }
Nate Begeman8a093362005-07-06 18:59:04 +0000865 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000866 }
867
868 // Select the true and false values for use in both the SSE PHI case, and the
869 // integer or x87 cmov cases below.
870 if (getRegPressure(True) > getRegPressure(False)) {
871 RTrue = SelectExpr(True);
872 RFalse = SelectExpr(False);
873 } else {
874 RFalse = SelectExpr(False);
875 RTrue = SelectExpr(True);
876 }
877
878 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
879 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
880 // node to select between the true and false values.
881 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
882 // FIXME: emit a direct compare and branch rather than setting a cond reg
883 // and testing it.
884 unsigned CondReg = SelectExpr(Cond);
885 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
886
887 // Create an iterator with which to insert the MBB for copying the false
888 // value and the MBB to hold the PHI instruction for this SetCC.
889 MachineBasicBlock *thisMBB = BB;
890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
891 ilist<MachineBasicBlock>::iterator It = BB;
892 ++It;
893
894 // thisMBB:
895 // ...
896 // TrueVal = ...
897 // cmpTY ccX, r1, r2
898 // bCC sinkMBB
899 // fallthrough --> copy0MBB
900 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
901 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
902 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
903 MachineFunction *F = BB->getParent();
904 F->getBasicBlockList().insert(It, copy0MBB);
905 F->getBasicBlockList().insert(It, sinkMBB);
906 // Update machine-CFG edges
907 BB->addSuccessor(copy0MBB);
908 BB->addSuccessor(sinkMBB);
909
910 // copy0MBB:
911 // %FalseValue = ...
912 // # fallthrough to sinkMBB
913 BB = copy0MBB;
914 // Update machine-CFG edges
915 BB->addSuccessor(sinkMBB);
916
917 // sinkMBB:
918 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
919 // ...
920 BB = sinkMBB;
921 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
922 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begeman8a093362005-07-06 18:59:04 +0000923 return;
924 }
925
Chris Lattner1d13a922005-01-10 22:10:13 +0000926 unsigned Opc = 0;
927 if (CondCode != NOT_SET) {
928 switch (SVT) {
929 default: assert(0 && "Cannot select this type!");
930 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
931 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +0000932 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000933 }
934 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000935
Chris Lattner1d13a922005-01-10 22:10:13 +0000936 // Finally, if we weren't able to fold this, just emit the condition and test
937 // it.
938 if (CondCode == NOT_SET || Opc == 0) {
939 // Get the condition into the zero flag.
940 unsigned CondReg = SelectExpr(Cond);
941 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
942
943 switch (SVT) {
944 default: assert(0 && "Cannot select this type!");
945 case MVT::i16: Opc = X86::CMOVE16rr; break;
946 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +0000947 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000948 }
949 } else {
950 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +0000951 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +0000952 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +0000953 }
954 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
955}
956
Chris Lattner3be6cd52005-01-17 01:34:14 +0000957void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000958 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +0000959 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
960 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +0000961 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +0000962 switch (RHS.getValueType()) {
963 default: break;
964 case MVT::i1:
965 case MVT::i8: Opc = X86::CMP8mi; break;
966 case MVT::i16: Opc = X86::CMP16mi; break;
967 case MVT::i32: Opc = X86::CMP32mi; break;
968 }
969 if (Opc) {
970 X86AddressMode AM;
971 EmitFoldedLoad(LHS, AM);
972 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
973 return;
974 }
975 }
976
Chris Lattner88c8a232005-01-07 07:49:41 +0000977 switch (RHS.getValueType()) {
978 default: break;
979 case MVT::i1:
980 case MVT::i8: Opc = X86::CMP8ri; break;
981 case MVT::i16: Opc = X86::CMP16ri; break;
982 case MVT::i32: Opc = X86::CMP32ri; break;
983 }
984 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000985 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +0000986 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
987 return;
988 }
Chris Lattner720a62e2005-01-14 22:37:41 +0000989 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +0000990 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
991 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +0000992 unsigned Reg = SelectExpr(LHS);
993 BuildMI(BB, X86::FTST, 1).addReg(Reg);
994 BuildMI(BB, X86::FNSTSW8r, 0);
995 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +0000996 return;
Chris Lattner720a62e2005-01-14 22:37:41 +0000997 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000998 }
999
Chris Lattner2cfce682005-01-12 02:02:48 +00001000 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001001 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001002 switch (RHS.getValueType()) {
1003 default: break;
1004 case MVT::i1:
1005 case MVT::i8: Opc = X86::CMP8mr; break;
1006 case MVT::i16: Opc = X86::CMP16mr; break;
1007 case MVT::i32: Opc = X86::CMP32mr; break;
1008 }
1009 if (Opc) {
1010 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001011 EmitFoldedLoad(LHS, AM);
1012 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001013 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1014 return;
1015 }
1016 }
1017
Chris Lattner88c8a232005-01-07 07:49:41 +00001018 switch (LHS.getValueType()) {
1019 default: assert(0 && "Cannot compare this value!");
1020 case MVT::i1:
1021 case MVT::i8: Opc = X86::CMP8rr; break;
1022 case MVT::i16: Opc = X86::CMP16rr; break;
1023 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001024 case MVT::f32: Opc = X86::UCOMISSrr; break;
1025 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001026 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001027 unsigned Tmp1, Tmp2;
1028 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1029 Tmp1 = SelectExpr(LHS);
1030 Tmp2 = SelectExpr(RHS);
1031 } else {
1032 Tmp2 = SelectExpr(RHS);
1033 Tmp1 = SelectExpr(LHS);
1034 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001035 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1036}
1037
Chris Lattner62b22422005-01-11 21:19:59 +00001038/// isFoldableLoad - Return true if this is a load instruction that can safely
1039/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001040bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1041 if (Op.getOpcode() == ISD::LOAD) {
1042 // FIXME: currently can't fold constant pool indexes.
1043 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1044 return false;
1045 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00001046 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001047 // FIXME: currently can't fold constant pool indexes.
1048 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1049 return false;
1050 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001051 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001052 }
Chris Lattner62b22422005-01-11 21:19:59 +00001053
1054 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001055 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1056 if (ExprMap.count(Op.getValue(1))) return false;
1057 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001058 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001059
Chris Lattnera56d29d2005-01-17 06:26:58 +00001060 // If there is not just one use of its value, we cannot fold.
1061 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1062
1063 // Finally, we cannot fold the load into the operation if this would induce a
1064 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1065 // operand of the operation we are folding the load into) can possible use the
1066 // chain node defined by the load.
1067 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1068 std::set<SDNode*> Visited;
1069 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1070 return false;
1071 }
1072 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001073}
1074
Chris Lattnera56d29d2005-01-17 06:26:58 +00001075
Chris Lattner62b22422005-01-11 21:19:59 +00001076/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1077/// and compute the address being loaded into AM.
1078void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1079 SDOperand Chain = Op.getOperand(0);
1080 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001081
Chris Lattner62b22422005-01-11 21:19:59 +00001082 if (getRegPressure(Chain) > getRegPressure(Address)) {
1083 Select(Chain);
1084 SelectAddress(Address, AM);
1085 } else {
1086 SelectAddress(Address, AM);
1087 Select(Chain);
1088 }
1089
1090 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001091 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1092 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00001093 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00001094 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00001095}
1096
Chris Lattner29f58192005-01-19 07:37:26 +00001097// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1098// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1099// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1100// return true.
1101bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001102 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1103 // good!
1104 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1105 std::swap(Op1, Op2); // Op1 is the SHL now.
1106 } else {
1107 return false; // No match
1108 }
1109
1110 SDOperand ShlVal = Op1.getOperand(0);
1111 SDOperand ShlAmt = Op1.getOperand(1);
1112 SDOperand ShrVal = Op2.getOperand(0);
1113 SDOperand ShrAmt = Op2.getOperand(1);
1114
Chris Lattner29f58192005-01-19 07:37:26 +00001115 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1116
Chris Lattner41fe2012005-01-19 06:18:43 +00001117 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1118 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1119 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00001120 if (SubCST->getValue() == RegSize) {
1121 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00001122 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00001123 if (ShrVal == ShlVal) {
1124 unsigned Reg, ShAmt;
1125 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1126 Reg = SelectExpr(ShrVal);
1127 ShAmt = SelectExpr(ShrAmt);
1128 } else {
1129 ShAmt = SelectExpr(ShrAmt);
1130 Reg = SelectExpr(ShrVal);
1131 }
1132 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1133 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1134 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1135 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1136 return true;
1137 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001138 unsigned AReg, BReg;
1139 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001140 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001141 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001142 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00001143 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001144 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001145 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00001146 unsigned ShAmt = SelectExpr(ShrAmt);
1147 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1148 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1149 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00001150 return true;
1151 }
1152 }
1153
Chris Lattnerde87d1462005-01-19 08:07:05 +00001154 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1155 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1156 if (SubCST->getValue() == RegSize) {
1157 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1158 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1159 if (ShrVal == ShlVal) {
1160 unsigned Reg, ShAmt;
1161 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1162 Reg = SelectExpr(ShrVal);
1163 ShAmt = SelectExpr(ShlAmt);
1164 } else {
1165 ShAmt = SelectExpr(ShlAmt);
1166 Reg = SelectExpr(ShrVal);
1167 }
1168 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1169 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1170 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1171 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1172 return true;
1173 } else if (RegSize != 8) {
1174 unsigned AReg, BReg;
1175 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00001176 AReg = SelectExpr(ShlVal);
1177 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001178 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00001179 BReg = SelectExpr(ShrVal);
1180 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001181 }
1182 unsigned ShAmt = SelectExpr(ShlAmt);
1183 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1184 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1185 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1186 return true;
1187 }
1188 }
Chris Lattner41fe2012005-01-19 06:18:43 +00001189
Chris Lattnerde87d1462005-01-19 08:07:05 +00001190 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1191 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1192 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1193 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1194 // (A >> 5) | (A << 27) --> ROR A, 5
1195 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1196 if (ShrVal == ShlVal) {
1197 unsigned Reg = SelectExpr(ShrVal);
1198 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1199 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1200 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1201 return true;
1202 } else if (RegSize != 8) {
1203 unsigned AReg, BReg;
1204 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001205 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001206 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001207 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001208 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001209 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001210 }
1211 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1212 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1213 .addImm(ShrCst->getValue());
1214 return true;
1215 }
1216 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001217
Chris Lattner41fe2012005-01-19 06:18:43 +00001218 return false;
1219}
1220
Chris Lattner88c8a232005-01-07 07:49:41 +00001221unsigned ISel::SelectExpr(SDOperand N) {
1222 unsigned Result;
Chris Lattner9982da22005-10-02 16:29:36 +00001223 unsigned Tmp1 = 0, Tmp2 = 0, Tmp3 = 0, Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001224 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00001225 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001226
Chris Lattner720a62e2005-01-14 22:37:41 +00001227 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7c762782005-08-16 21:56:37 +00001228 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1229 // Just use the specified register as our input if we can.
1230 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == X86::ESP)
1231 return Reg;
Chris Lattner720a62e2005-01-14 22:37:41 +00001232 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001233
Chris Lattner62b22422005-01-11 21:19:59 +00001234 unsigned &Reg = ExprMap[N];
1235 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001236
Chris Lattnera31d4c72005-04-02 04:01:14 +00001237 switch (N.getOpcode()) {
1238 default:
Chris Lattner62b22422005-01-11 21:19:59 +00001239 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00001240 MakeReg(N.getValueType()) : 1;
1241 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00001242 case X86ISD::TAILCALL:
1243 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00001244 // If this is a call instruction, make sure to prepare ALL of the result
1245 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00001246 ExprMap[N.getValue(0)] = 1;
1247 if (Node->getNumValues() > 1) {
1248 Result = MakeReg(Node->getValueType(1));
1249 ExprMap[N.getValue(1)] = Result;
1250 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00001251 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00001252 } else {
1253 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00001254 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00001255 break;
1256 case ISD::ADD_PARTS:
1257 case ISD::SUB_PARTS:
1258 case ISD::SHL_PARTS:
1259 case ISD::SRL_PARTS:
1260 case ISD::SRA_PARTS:
1261 Result = MakeReg(Node->getValueType(0));
1262 ExprMap[N.getValue(0)] = Result;
1263 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1264 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1265 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001266 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001267
Chris Lattner88c8a232005-01-07 07:49:41 +00001268 switch (N.getOpcode()) {
1269 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00001270 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00001271 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00001272 case ISD::FP_EXTEND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001273 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman8a093362005-07-06 18:59:04 +00001274 Tmp1 = SelectExpr(N.getOperand(0));
1275 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
1276 return Result;
Nate Begemana0b5e032005-07-15 00:38:55 +00001277 case ISD::FP_ROUND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001278 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemana0b5e032005-07-15 00:38:55 +00001279 Tmp1 = SelectExpr(N.getOperand(0));
1280 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
1281 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001282 case ISD::CopyFromReg:
1283 Select(N.getOperand(0));
1284 if (Result == 1) {
1285 Reg = Result = ExprMap[N.getValue(0)] =
1286 MakeReg(N.getValue(0).getValueType());
1287 }
Chris Lattner7c762782005-08-16 21:56:37 +00001288 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001289 switch (Node->getValueType(0)) {
1290 default: assert(0 && "Cannot CopyFromReg this!");
1291 case MVT::i1:
1292 case MVT::i8:
Chris Lattner7c762782005-08-16 21:56:37 +00001293 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001294 return Result;
1295 case MVT::i16:
Chris Lattner7c762782005-08-16 21:56:37 +00001296 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001297 return Result;
1298 case MVT::i32:
Chris Lattner7c762782005-08-16 21:56:37 +00001299 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001300 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001301 }
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001302
Chris Lattner88c8a232005-01-07 07:49:41 +00001303 case ISD::FrameIndex:
1304 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1305 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1306 return Result;
1307 case ISD::ConstantPool:
Chris Lattnerc30405e2005-08-26 17:15:30 +00001308 Tmp1 = BB->getParent()->getConstantPool()->
1309 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner88c8a232005-01-07 07:49:41 +00001310 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1311 return Result;
1312 case ISD::ConstantFP:
Nate Begeman8d394eb2005-08-03 23:26:28 +00001313 if (X86ScalarSSE) {
1314 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
1315 "SSE only supports +0.0");
1316 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
1317 BuildMI(BB, Opc, 0, Result);
1318 return Result;
1319 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001320 ContainsFPCode = true;
1321 Tmp1 = Result; // Intermediate Register
1322 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1323 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1324 Tmp1 = MakeReg(MVT::f64);
1325
1326 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1327 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1328 BuildMI(BB, X86::FLD0, 0, Tmp1);
1329 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1330 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1331 BuildMI(BB, X86::FLD1, 0, Tmp1);
1332 else
1333 assert(0 && "Unexpected constant!");
1334 if (Tmp1 != Result)
1335 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1336 return Result;
1337 case ISD::Constant:
1338 switch (N.getValueType()) {
1339 default: assert(0 && "Cannot use constants of this type!");
1340 case MVT::i1:
1341 case MVT::i8: Opc = X86::MOV8ri; break;
1342 case MVT::i16: Opc = X86::MOV16ri; break;
1343 case MVT::i32: Opc = X86::MOV32ri; break;
1344 }
1345 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1346 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001347 case ISD::UNDEF:
1348 if (Node->getValueType(0) == MVT::f64) {
1349 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1350 BuildMI(BB, X86::FLD0, 0, Result);
1351 } else {
1352 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1353 }
1354 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001355 case ISD::GlobalAddress: {
1356 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00001357 // For Darwin, external and weak symbols are indirect, so we want to load
1358 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001359 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001360 (GV->hasWeakLinkage() || GV->isExternal())) {
1361 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
1362 .addGlobalAddress(GV, false, 0);
1363 } else {
1364 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1365 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001366 return Result;
1367 }
1368 case ISD::ExternalSymbol: {
1369 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1370 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1371 return Result;
1372 }
Chris Lattner210975c2005-09-02 00:16:09 +00001373 case ISD::ANY_EXTEND: // treat any extend like zext
Chris Lattner88c8a232005-01-07 07:49:41 +00001374 case ISD::ZERO_EXTEND: {
1375 int DestIs16 = N.getValueType() == MVT::i16;
1376 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001377
1378 // FIXME: This hack is here for zero extension casts from bool to i8. This
1379 // would not be needed if bools were promoted by Legalize.
1380 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001381 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001382 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1383 return Result;
1384 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001385
Chris Lattnera56d29d2005-01-17 06:26:58 +00001386 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001387 static const unsigned Opc[3] = {
1388 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1389 };
1390
1391 X86AddressMode AM;
1392 EmitFoldedLoad(N.getOperand(0), AM);
1393 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001394
Chris Lattnerb0eef822005-01-11 23:33:00 +00001395 return Result;
1396 }
1397
Chris Lattner88c8a232005-01-07 07:49:41 +00001398 static const unsigned Opc[3] = {
1399 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1400 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001401 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001402 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1403 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001404 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001405 case ISD::SIGN_EXTEND: {
1406 int DestIs16 = N.getValueType() == MVT::i16;
1407 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1408
Chris Lattner282781c2005-01-09 18:52:44 +00001409 // FIXME: Legalize should promote bools to i8!
1410 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1411 "Sign extend from bool not implemented!");
1412
Chris Lattnera56d29d2005-01-17 06:26:58 +00001413 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001414 static const unsigned Opc[3] = {
1415 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1416 };
1417
1418 X86AddressMode AM;
1419 EmitFoldedLoad(N.getOperand(0), AM);
1420 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1421 return Result;
1422 }
1423
Chris Lattner88c8a232005-01-07 07:49:41 +00001424 static const unsigned Opc[3] = {
1425 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1426 };
1427 Tmp1 = SelectExpr(N.getOperand(0));
1428 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1429 return Result;
1430 }
1431 case ISD::TRUNCATE:
1432 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1433 // a move out of AX or AL.
1434 switch (N.getOperand(0).getValueType()) {
1435 default: assert(0 && "Unknown truncate!");
1436 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1437 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1438 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1439 }
1440 Tmp1 = SelectExpr(N.getOperand(0));
1441 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1442
1443 switch (N.getValueType()) {
1444 default: assert(0 && "Unknown truncate!");
1445 case MVT::i1:
1446 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1447 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1448 }
1449 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1450 return Result;
1451
Chris Lattner507a2752005-07-16 00:28:20 +00001452 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00001453 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1454 unsigned PromoteOpcode = 0;
1455
Nate Begeman7e74c832005-07-16 02:02:34 +00001456 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00001457 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00001458 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00001459 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1460 return Result;
1461 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001462
Chris Lattnere44e6d12005-01-11 03:50:45 +00001463 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00001464
Chris Lattner282781c2005-01-09 18:52:44 +00001465 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00001466 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00001467 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1468 MachineFunction *F = BB->getParent();
1469 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1470
1471 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00001472 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00001473 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00001474 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1475 break;
1476 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00001477 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00001478 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1479 break;
1480 default: break; // No promotion required.
1481 }
Chris Lattner507a2752005-07-16 00:28:20 +00001482 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00001483 }
Chris Lattner4738d1b2005-07-30 00:05:54 +00001484 case ISD::FP_TO_SINT:
Chris Lattner282781c2005-01-09 18:52:44 +00001485 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1486
Nate Begeman8a093362005-07-06 18:59:04 +00001487 // If the target supports SSE2 and is performing FP operations in SSE regs
1488 // instead of the FP stack, then we can use the efficient CVTSS2SI and
1489 // CVTSD2SI instructions.
Chris Lattner4738d1b2005-07-30 00:05:54 +00001490 assert(X86ScalarSSE);
1491 if (MVT::f32 == N.getOperand(0).getValueType()) {
1492 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
1493 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
1494 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
1495 } else {
1496 assert(0 && "Not an f32 or f64?");
1497 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001498 }
Chris Lattner282781c2005-01-09 18:52:44 +00001499 return Result;
Chris Lattner4738d1b2005-07-30 00:05:54 +00001500
Chris Lattner0815dcae2005-09-28 22:29:17 +00001501 case ISD::FADD:
Chris Lattner88c8a232005-01-07 07:49:41 +00001502 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00001503 Op0 = N.getOperand(0);
1504 Op1 = N.getOperand(1);
1505
Chris Lattner30607ec2005-01-25 20:03:11 +00001506 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00001507 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001508 goto FoldAdd;
1509 }
Chris Lattner62b22422005-01-11 21:19:59 +00001510
Chris Lattner30607ec2005-01-25 20:03:11 +00001511 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001512 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00001513 switch (N.getValueType()) {
1514 default: assert(0 && "Cannot add this type!");
1515 case MVT::i1:
1516 case MVT::i8: Opc = X86::ADD8rm; break;
1517 case MVT::i16: Opc = X86::ADD16rm; break;
1518 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001519 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001520 case MVT::f64:
1521 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00001522 if (X86ScalarSSE) {
1523 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
1524 Opc = X86::ADDSDrm;
1525 } else {
1526 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1527 }
Chris Lattner30607ec2005-01-25 20:03:11 +00001528 break;
Chris Lattner62b22422005-01-11 21:19:59 +00001529 }
1530 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001531 EmitFoldedLoad(Op1, AM);
1532 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001533 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1534 return Result;
1535 }
1536
Chris Lattner88c8a232005-01-07 07:49:41 +00001537 // See if we can codegen this as an LEA to fold operations together.
1538 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00001539 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001540 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00001541 MatchAddress(N, AM);
1542 ExprMap[N] = Result;
1543
1544 // If this is not just an add, emit the LEA. For a simple add (like
1545 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1546 // leave this as LEA, then peephole it to 'ADD' after two address elim
1547 // happens.
1548 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1549 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1550 X86AddressMode XAM = SelectAddrExprs(AM);
1551 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1552 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001553 }
1554 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001555
Chris Lattner62b22422005-01-11 21:19:59 +00001556 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001557 Opc = 0;
1558 if (CN->getValue() == 1) { // add X, 1 -> inc X
1559 switch (N.getValueType()) {
1560 default: assert(0 && "Cannot integer add this type!");
1561 case MVT::i8: Opc = X86::INC8r; break;
1562 case MVT::i16: Opc = X86::INC16r; break;
1563 case MVT::i32: Opc = X86::INC32r; break;
1564 }
1565 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1566 switch (N.getValueType()) {
1567 default: assert(0 && "Cannot integer add this type!");
1568 case MVT::i8: Opc = X86::DEC8r; break;
1569 case MVT::i16: Opc = X86::DEC16r; break;
1570 case MVT::i32: Opc = X86::DEC32r; break;
1571 }
1572 }
1573
1574 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001575 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001576 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1577 return Result;
1578 }
1579
1580 switch (N.getValueType()) {
1581 default: assert(0 && "Cannot add this type!");
1582 case MVT::i8: Opc = X86::ADD8ri; break;
1583 case MVT::i16: Opc = X86::ADD16ri; break;
1584 case MVT::i32: Opc = X86::ADD32ri; break;
1585 }
1586 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001587 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001588 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1589 return Result;
1590 }
1591 }
1592
Chris Lattner88c8a232005-01-07 07:49:41 +00001593 switch (N.getValueType()) {
1594 default: assert(0 && "Cannot add this type!");
1595 case MVT::i8: Opc = X86::ADD8rr; break;
1596 case MVT::i16: Opc = X86::ADD16rr; break;
1597 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001598 case MVT::f32: Opc = X86::ADDSSrr; break;
1599 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001600 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001601
Chris Lattner62b22422005-01-11 21:19:59 +00001602 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1603 Tmp1 = SelectExpr(Op0);
1604 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001605 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001606 Tmp2 = SelectExpr(Op1);
1607 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001608 }
1609
Chris Lattner88c8a232005-01-07 07:49:41 +00001610 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1611 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00001612
Nate Begeman8a093362005-07-06 18:59:04 +00001613 case ISD::FSQRT:
1614 Tmp1 = SelectExpr(Node->getOperand(0));
1615 if (X86ScalarSSE) {
1616 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
1617 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1618 } else {
1619 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
1620 }
1621 return Result;
1622
1623 // FIXME:
1624 // Once we can spill 16 byte constants into the constant pool, we can
1625 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00001626 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00001627 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00001628 case ISD::FSIN:
1629 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00001630 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00001631 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00001632 switch (N.getOpcode()) {
1633 default: assert(0 && "Unreachable!");
1634 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1635 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerdb68d392005-04-30 04:25:35 +00001636 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1637 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00001638 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00001639 return Result;
1640
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001641 case ISD::MULHU:
1642 switch (N.getValueType()) {
1643 default: assert(0 && "Unsupported VT!");
1644 case MVT::i8: Tmp2 = X86::MUL8r; break;
1645 case MVT::i16: Tmp2 = X86::MUL16r; break;
1646 case MVT::i32: Tmp2 = X86::MUL32r; break;
1647 }
1648 // FALL THROUGH
1649 case ISD::MULHS: {
1650 unsigned MovOpc, LowReg, HiReg;
1651 switch (N.getValueType()) {
1652 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00001653 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001654 MovOpc = X86::MOV8rr;
1655 LowReg = X86::AL;
1656 HiReg = X86::AH;
1657 Opc = X86::IMUL8r;
1658 break;
1659 case MVT::i16:
1660 MovOpc = X86::MOV16rr;
1661 LowReg = X86::AX;
1662 HiReg = X86::DX;
1663 Opc = X86::IMUL16r;
1664 break;
1665 case MVT::i32:
1666 MovOpc = X86::MOV32rr;
1667 LowReg = X86::EAX;
1668 HiReg = X86::EDX;
1669 Opc = X86::IMUL32r;
1670 break;
1671 }
1672 if (Node->getOpcode() != ISD::MULHS)
1673 Opc = Tmp2; // Get the MULHU opcode.
1674
1675 Op0 = Node->getOperand(0);
1676 Op1 = Node->getOperand(1);
1677 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1678 Tmp1 = SelectExpr(Op0);
1679 Tmp2 = SelectExpr(Op1);
1680 } else {
1681 Tmp2 = SelectExpr(Op1);
1682 Tmp1 = SelectExpr(Op0);
1683 }
1684
1685 // FIXME: Implement folding of loads into the memory operands here!
1686 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1687 BuildMI(BB, Opc, 1).addReg(Tmp2);
1688 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1689 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001690 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001691
Chris Lattner0815dcae2005-09-28 22:29:17 +00001692 case ISD::FSUB:
1693 case ISD::FMUL:
Chris Lattner88c8a232005-01-07 07:49:41 +00001694 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00001695 case ISD::MUL:
1696 case ISD::AND:
1697 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00001698 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00001699 static const unsigned SUBTab[] = {
1700 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1701 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1702 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1703 };
Nate Begeman8a093362005-07-06 18:59:04 +00001704 static const unsigned SSE_SUBTab[] = {
1705 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1706 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
1707 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
1708 };
Chris Lattner62b22422005-01-11 21:19:59 +00001709 static const unsigned MULTab[] = {
1710 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1711 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1712 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1713 };
Nate Begeman8a093362005-07-06 18:59:04 +00001714 static const unsigned SSE_MULTab[] = {
1715 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1716 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
1717 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
1718 };
Chris Lattner62b22422005-01-11 21:19:59 +00001719 static const unsigned ANDTab[] = {
1720 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1721 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001722 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00001723 };
1724 static const unsigned ORTab[] = {
1725 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1726 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1727 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1728 };
1729 static const unsigned XORTab[] = {
1730 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1731 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1732 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1733 };
1734
1735 Op0 = Node->getOperand(0);
1736 Op1 = Node->getOperand(1);
1737
Chris Lattner29f58192005-01-19 07:37:26 +00001738 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1739 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00001740 return Result;
1741
1742 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00001743 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1744 if (CN->isNullValue()) { // 0 - N -> neg N
1745 switch (N.getValueType()) {
1746 default: assert(0 && "Cannot sub this type!");
1747 case MVT::i1:
1748 case MVT::i8: Opc = X86::NEG8r; break;
1749 case MVT::i16: Opc = X86::NEG16r; break;
1750 case MVT::i32: Opc = X86::NEG32r; break;
1751 }
1752 Tmp1 = SelectExpr(N.getOperand(1));
1753 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1754 return Result;
1755 }
1756
Chris Lattner62b22422005-01-11 21:19:59 +00001757 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1758 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001759 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00001760 switch (N.getValueType()) {
1761 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001762 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00001763 case MVT::i8: Opc = X86::NOT8r; break;
1764 case MVT::i16: Opc = X86::NOT16r; break;
1765 case MVT::i32: Opc = X86::NOT32r; break;
1766 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001767 if (Opc) {
1768 Tmp1 = SelectExpr(Op0);
1769 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1770 return Result;
1771 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00001772 }
1773
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001774 // Fold common multiplies into LEA instructions.
1775 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1776 switch ((int)CN->getValue()) {
1777 default: break;
1778 case 3:
1779 case 5:
1780 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001781 // Remove N from exprmap so SelectAddress doesn't get confused.
1782 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001783 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001784 SelectAddress(N, AM);
1785 // Restore it to the map.
1786 ExprMap[N] = Result;
1787 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1788 return Result;
1789 }
1790 }
1791
Chris Lattner88c8a232005-01-07 07:49:41 +00001792 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00001793 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00001794 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00001795 case MVT::i8: Opc = 0; break;
1796 case MVT::i16: Opc = 1; break;
1797 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001798 }
Chris Lattner62b22422005-01-11 21:19:59 +00001799 switch (Node->getOpcode()) {
1800 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001801 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001802 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001803 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001804 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001805 case ISD::AND: Opc = ANDTab[Opc]; break;
1806 case ISD::OR: Opc = ORTab[Opc]; break;
1807 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001808 }
Chris Lattner62b22422005-01-11 21:19:59 +00001809 if (Opc) { // Can't fold MUL:i8 R, imm
1810 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001811 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1812 return Result;
1813 }
1814 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001815
Chris Lattner30607ec2005-01-25 20:03:11 +00001816 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner0815dcae2005-09-28 22:29:17 +00001817 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
Chris Lattner62b22422005-01-11 21:19:59 +00001818 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001819 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00001820 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00001821 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00001822 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001823 if (Op0.getOpcode() == ISD::EXTLOAD)
1824 Opc = X86::FSUBR32m;
1825 else
1826 Opc = X86::FSUBR64m;
1827
Chris Lattner62b22422005-01-11 21:19:59 +00001828 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001829 EmitFoldedLoad(Op0, AM);
1830 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00001831 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1832 return Result;
1833 }
1834 }
1835
Chris Lattner30607ec2005-01-25 20:03:11 +00001836 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001837 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00001838 switch (N.getValueType()) {
1839 default: assert(0 && "Cannot operate on this type!");
1840 case MVT::i1:
1841 case MVT::i8: Opc = 5; break;
1842 case MVT::i16: Opc = 6; break;
1843 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001844 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001845 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001846 case MVT::f64:
1847 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00001848 "SSE load should have been promoted");
1849 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001850 }
1851 switch (Node->getOpcode()) {
1852 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001853 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001854 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001855 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001856 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001857 case ISD::AND: Opc = ANDTab[Opc]; break;
1858 case ISD::OR: Opc = ORTab[Opc]; break;
1859 case ISD::XOR: Opc = XORTab[Opc]; break;
1860 }
1861
1862 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001863 EmitFoldedLoad(Op1, AM);
1864 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001865 if (Opc) {
1866 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1867 } else {
1868 assert(Node->getOpcode() == ISD::MUL &&
1869 N.getValueType() == MVT::i8 && "Unexpected situation!");
1870 // Must use the MUL instruction, which forces use of AL.
1871 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1872 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1873 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1874 }
1875 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001876 }
Chris Lattner62b22422005-01-11 21:19:59 +00001877
1878 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1879 Tmp1 = SelectExpr(Op0);
1880 Tmp2 = SelectExpr(Op1);
1881 } else {
1882 Tmp2 = SelectExpr(Op1);
1883 Tmp1 = SelectExpr(Op0);
1884 }
1885
Chris Lattner88c8a232005-01-07 07:49:41 +00001886 switch (N.getValueType()) {
1887 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00001888 case MVT::i1:
1889 case MVT::i8: Opc = 10; break;
1890 case MVT::i16: Opc = 11; break;
1891 case MVT::i32: Opc = 12; break;
1892 case MVT::f32: Opc = 13; break;
1893 case MVT::f64: Opc = 14; break;
1894 }
1895 switch (Node->getOpcode()) {
1896 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001897 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001898 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001899 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001900 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001901 case ISD::AND: Opc = ANDTab[Opc]; break;
1902 case ISD::OR: Opc = ORTab[Opc]; break;
1903 case ISD::XOR: Opc = XORTab[Opc]; break;
1904 }
1905 if (Opc) {
1906 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1907 } else {
1908 assert(Node->getOpcode() == ISD::MUL &&
1909 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00001910 // Must use the MUL instruction, which forces use of AL.
1911 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1912 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1913 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00001914 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001915 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00001916 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00001917 case ISD::ADD_PARTS:
1918 case ISD::SUB_PARTS: {
1919 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1920 "Not an i64 add/sub!");
1921 // Emit all of the operands.
1922 std::vector<unsigned> InVals;
1923 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1924 InVals.push_back(SelectExpr(N.getOperand(i)));
1925 if (N.getOpcode() == ISD::ADD_PARTS) {
1926 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1927 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1928 } else {
1929 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1930 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1931 }
1932 return Result+N.ResNo;
1933 }
1934
Chris Lattnera31d4c72005-04-02 04:01:14 +00001935 case ISD::SHL_PARTS:
1936 case ISD::SRA_PARTS:
1937 case ISD::SRL_PARTS: {
1938 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1939 "Not an i64 shift!");
1940 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1941 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1942 unsigned TmpReg = MakeReg(MVT::i32);
1943 if (N.getOpcode() == ISD::SRA_PARTS) {
1944 // If this is a SHR of a Long, then we need to do funny sign extension
1945 // stuff. TmpReg gets the value to use as the high-part if we are
1946 // shifting more than 32 bits.
1947 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
1948 } else {
1949 // Other shifts use a fixed zero value if the shift is more than 32 bits.
1950 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
1951 }
1952
1953 // Initialize CL with the shift amount.
1954 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
1955 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
1956
1957 unsigned TmpReg2 = MakeReg(MVT::i32);
1958 unsigned TmpReg3 = MakeReg(MVT::i32);
1959 if (N.getOpcode() == ISD::SHL_PARTS) {
1960 // TmpReg2 = shld inHi, inLo
1961 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
1962 .addReg(ShiftOpLo);
1963 // TmpReg3 = shl inLo, CL
1964 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001965
Chris Lattnera31d4c72005-04-02 04:01:14 +00001966 // Set the flags to indicate whether the shift was by more than 32 bits.
1967 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001968
Chris Lattnera31d4c72005-04-02 04:01:14 +00001969 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001970 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001971 Result+1).addReg(TmpReg2).addReg(TmpReg3);
1972 // DestLo = (>32) ? TmpReg : TmpReg3;
1973 BuildMI(BB, X86::CMOVNE32rr, 2,
1974 Result).addReg(TmpReg3).addReg(TmpReg);
1975 } else {
1976 // TmpReg2 = shrd inLo, inHi
1977 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
1978 .addReg(ShiftOpHi);
1979 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00001980 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00001981 : X86::SHR32rCL, 1, TmpReg3)
1982 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001983
Chris Lattnera31d4c72005-04-02 04:01:14 +00001984 // Set the flags to indicate whether the shift was by more than 32 bits.
1985 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001986
Chris Lattnera31d4c72005-04-02 04:01:14 +00001987 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001988 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001989 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001990
Chris Lattnera31d4c72005-04-02 04:01:14 +00001991 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001992 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001993 Result+1).addReg(TmpReg3).addReg(TmpReg);
1994 }
1995 return Result+N.ResNo;
1996 }
1997
Chris Lattner88c8a232005-01-07 07:49:41 +00001998 case ISD::SELECT:
Nate Begeman8d394eb2005-08-03 23:26:28 +00001999 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
2000 N.getValueType(), Result);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002001 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002002
Chris Lattner0815dcae2005-09-28 22:29:17 +00002003 case ISD::FDIV:
2004 case ISD::FREM:
Chris Lattner88c8a232005-01-07 07:49:41 +00002005 case ISD::SDIV:
2006 case ISD::UDIV:
2007 case ISD::SREM:
2008 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002009 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2010 "We don't support this operator!");
2011
Chris Lattner0815dcae2005-09-28 22:29:17 +00002012 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002013 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00002014 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00002015 // Check for reversed and unreversed DIV.
2016 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2017 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2018 Opc = X86::FDIVR32m;
2019 else
2020 Opc = X86::FDIVR64m;
2021 X86AddressMode AM;
2022 EmitFoldedLoad(N.getOperand(0), AM);
2023 Tmp1 = SelectExpr(N.getOperand(1));
2024 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2025 return Result;
2026 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2027 N.getOperand(1).getOpcode() == ISD::LOAD) {
2028 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2029 Opc = X86::FDIV32m;
2030 else
2031 Opc = X86::FDIV64m;
2032 X86AddressMode AM;
2033 EmitFoldedLoad(N.getOperand(1), AM);
2034 Tmp1 = SelectExpr(N.getOperand(0));
2035 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2036 return Result;
2037 }
2038 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002039 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002040
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002041 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2042 Tmp1 = SelectExpr(N.getOperand(0));
2043 Tmp2 = SelectExpr(N.getOperand(1));
2044 } else {
2045 Tmp2 = SelectExpr(N.getOperand(1));
2046 Tmp1 = SelectExpr(N.getOperand(0));
2047 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002048
2049 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2050 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2051 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2052 switch (N.getValueType()) {
2053 default: assert(0 && "Cannot sdiv this type!");
2054 case MVT::i8:
2055 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2056 LoReg = X86::AL;
2057 HiReg = X86::AH;
2058 MovOpcode = X86::MOV8rr;
2059 ClrOpcode = X86::MOV8ri;
2060 SExtOpcode = X86::CBW;
2061 break;
2062 case MVT::i16:
2063 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2064 LoReg = X86::AX;
2065 HiReg = X86::DX;
2066 MovOpcode = X86::MOV16rr;
2067 ClrOpcode = X86::MOV16ri;
2068 SExtOpcode = X86::CWD;
2069 break;
2070 case MVT::i32:
2071 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002072 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002073 HiReg = X86::EDX;
2074 MovOpcode = X86::MOV32rr;
2075 ClrOpcode = X86::MOV32ri;
2076 SExtOpcode = X86::CDQ;
2077 break;
Nate Begeman8a093362005-07-06 18:59:04 +00002078 case MVT::f32:
2079 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
2080 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002081 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002082 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
2083 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002084 return Result;
2085 }
2086
2087 // Set up the low part.
2088 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2089
2090 if (isSigned) {
2091 // Sign extend the low part into the high part.
2092 BuildMI(BB, SExtOpcode, 0);
2093 } else {
2094 // Zero out the high part, effectively zero extending the input.
2095 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2096 }
2097
2098 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002099 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002100
2101 // Get the result of the divide or rem.
2102 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2103 return Result;
2104 }
2105
2106 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002107 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002108 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2109 switch (N.getValueType()) {
2110 default: assert(0 && "Cannot shift this type!");
2111 case MVT::i8: Opc = X86::ADD8rr; break;
2112 case MVT::i16: Opc = X86::ADD16rr; break;
2113 case MVT::i32: Opc = X86::ADD32rr; break;
2114 }
2115 Tmp1 = SelectExpr(N.getOperand(0));
2116 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2117 return Result;
2118 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002119
Chris Lattner88c8a232005-01-07 07:49:41 +00002120 switch (N.getValueType()) {
2121 default: assert(0 && "Cannot shift this type!");
2122 case MVT::i8: Opc = X86::SHL8ri; break;
2123 case MVT::i16: Opc = X86::SHL16ri; break;
2124 case MVT::i32: Opc = X86::SHL32ri; break;
2125 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002126 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002127 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2128 return Result;
2129 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002130
2131 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2132 Tmp1 = SelectExpr(N.getOperand(0));
2133 Tmp2 = SelectExpr(N.getOperand(1));
2134 } else {
2135 Tmp2 = SelectExpr(N.getOperand(1));
2136 Tmp1 = SelectExpr(N.getOperand(0));
2137 }
2138
Chris Lattner88c8a232005-01-07 07:49:41 +00002139 switch (N.getValueType()) {
2140 default: assert(0 && "Cannot shift this type!");
2141 case MVT::i8 : Opc = X86::SHL8rCL; break;
2142 case MVT::i16: Opc = X86::SHL16rCL; break;
2143 case MVT::i32: Opc = X86::SHL32rCL; break;
2144 }
2145 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002146 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002147 return Result;
2148 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002149 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2150 switch (N.getValueType()) {
2151 default: assert(0 && "Cannot shift this type!");
2152 case MVT::i8: Opc = X86::SHR8ri; break;
2153 case MVT::i16: Opc = X86::SHR16ri; break;
2154 case MVT::i32: Opc = X86::SHR32ri; break;
2155 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002156 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002157 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2158 return Result;
2159 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002160
2161 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2162 Tmp1 = SelectExpr(N.getOperand(0));
2163 Tmp2 = SelectExpr(N.getOperand(1));
2164 } else {
2165 Tmp2 = SelectExpr(N.getOperand(1));
2166 Tmp1 = SelectExpr(N.getOperand(0));
2167 }
2168
Chris Lattner88c8a232005-01-07 07:49:41 +00002169 switch (N.getValueType()) {
2170 default: assert(0 && "Cannot shift this type!");
2171 case MVT::i8 : Opc = X86::SHR8rCL; break;
2172 case MVT::i16: Opc = X86::SHR16rCL; break;
2173 case MVT::i32: Opc = X86::SHR32rCL; break;
2174 }
2175 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002176 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002177 return Result;
2178 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002179 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2180 switch (N.getValueType()) {
2181 default: assert(0 && "Cannot shift this type!");
2182 case MVT::i8: Opc = X86::SAR8ri; break;
2183 case MVT::i16: Opc = X86::SAR16ri; break;
2184 case MVT::i32: Opc = X86::SAR32ri; break;
2185 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002186 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002187 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2188 return Result;
2189 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002190
2191 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2192 Tmp1 = SelectExpr(N.getOperand(0));
2193 Tmp2 = SelectExpr(N.getOperand(1));
2194 } else {
2195 Tmp2 = SelectExpr(N.getOperand(1));
2196 Tmp1 = SelectExpr(N.getOperand(0));
2197 }
2198
Chris Lattner88c8a232005-01-07 07:49:41 +00002199 switch (N.getValueType()) {
2200 default: assert(0 && "Cannot shift this type!");
2201 case MVT::i8 : Opc = X86::SAR8rCL; break;
2202 case MVT::i16: Opc = X86::SAR16rCL; break;
2203 case MVT::i32: Opc = X86::SAR32rCL; break;
2204 }
2205 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnera9d68f12005-08-19 00:31:37 +00002206 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002207 return Result;
2208
2209 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002210 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner6ec77452005-08-09 20:21:10 +00002211 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner88c8a232005-01-07 07:49:41 +00002212 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2213 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002214 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002215 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002216 if (Result != 1) { // Generate the token
2217 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2218 assert(0 && "Load already emitted!?");
2219 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002220 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2221
Chris Lattnerb52e0412005-01-08 19:28:19 +00002222 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002223 default: assert(0 && "Cannot load this type!");
2224 case MVT::i1:
2225 case MVT::i8: Opc = X86::MOV8rm; break;
2226 case MVT::i16: Opc = X86::MOV16rm; break;
2227 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002228 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002229 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002230 if (X86ScalarSSE) {
2231 Opc = X86::MOVSDrm;
2232 } else {
2233 Opc = X86::FLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002234 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00002235 }
2236 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002237 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002238
Chris Lattner88c8a232005-01-07 07:49:41 +00002239 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattnerc30405e2005-08-26 17:15:30 +00002240 unsigned CPIdx = BB->getParent()->getConstantPool()->
2241 getConstantPoolIndex(CP->get());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002242 Select(N.getOperand(0));
Chris Lattnerc30405e2005-08-26 17:15:30 +00002243 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner88c8a232005-01-07 07:49:41 +00002244 } else {
2245 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002246
2247 SDOperand Chain = N.getOperand(0);
2248 SDOperand Address = N.getOperand(1);
2249 if (getRegPressure(Chain) > getRegPressure(Address)) {
2250 Select(Chain);
2251 SelectAddress(Address, AM);
2252 } else {
2253 SelectAddress(Address, AM);
2254 Select(Chain);
2255 }
2256
Chris Lattner88c8a232005-01-07 07:49:41 +00002257 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2258 }
2259 return Result;
Chris Lattnera36117b2005-05-14 06:52:07 +00002260 case X86ISD::FILD64m:
2261 // Make sure we generate both values.
2262 assert(Result != 1 && N.getValueType() == MVT::f64);
2263 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2264 assert(0 && "Load already emitted!?");
2265
2266 {
2267 X86AddressMode AM;
2268
2269 SDOperand Chain = N.getOperand(0);
2270 SDOperand Address = N.getOperand(1);
2271 if (getRegPressure(Chain) > getRegPressure(Address)) {
2272 Select(Chain);
2273 SelectAddress(Address, AM);
2274 } else {
2275 SelectAddress(Address, AM);
2276 Select(Chain);
2277 }
Chris Lattner67756e22005-07-29 00:40:01 +00002278
2279 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00002280 }
2281 return Result;
Jeff Cohen546fd592005-07-30 18:33:25 +00002282
Chris Lattnere18a4c42005-01-15 05:22:24 +00002283 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2284 case ISD::ZEXTLOAD: {
2285 // Make sure we generate both values.
2286 if (Result != 1)
2287 ExprMap[N.getValue(1)] = 1; // Generate the token
2288 else
2289 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2290
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002291 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2292 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00002293 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002294 "Bad EXTLOAD!");
Chris Lattnerc30405e2005-08-26 17:15:30 +00002295 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00002296 getConstantPoolIndex(CP->get());
Chris Lattnerc30405e2005-08-26 17:15:30 +00002297
2298 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result), CPIdx);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002299 return Result;
2300 }
2301
Chris Lattnere18a4c42005-01-15 05:22:24 +00002302 X86AddressMode AM;
2303 if (getRegPressure(Node->getOperand(0)) >
2304 getRegPressure(Node->getOperand(1))) {
2305 Select(Node->getOperand(0)); // chain
2306 SelectAddress(Node->getOperand(1), AM);
2307 } else {
2308 SelectAddress(Node->getOperand(1), AM);
2309 Select(Node->getOperand(0)); // chain
2310 }
2311
2312 switch (Node->getValueType(0)) {
2313 default: assert(0 && "Unknown type to sign extend to.");
2314 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00002315 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002316 "Bad EXTLOAD!");
2317 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2318 break;
2319 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002320 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002321 default:
2322 assert(0 && "Bad zero extend!");
2323 case MVT::i1:
2324 case MVT::i8:
2325 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2326 break;
2327 case MVT::i16:
2328 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2329 break;
2330 }
2331 break;
2332 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002333 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002334 "Bad zero extend!");
2335 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2336 break;
2337 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00002338 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002339 "Bad zero extend!");
2340 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2341 break;
2342 }
2343 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002344 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002345 case ISD::SEXTLOAD: {
2346 // Make sure we generate both values.
2347 if (Result != 1)
2348 ExprMap[N.getValue(1)] = 1; // Generate the token
2349 else
2350 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2351
2352 X86AddressMode AM;
2353 if (getRegPressure(Node->getOperand(0)) >
2354 getRegPressure(Node->getOperand(1))) {
2355 Select(Node->getOperand(0)); // chain
2356 SelectAddress(Node->getOperand(1), AM);
2357 } else {
2358 SelectAddress(Node->getOperand(1), AM);
2359 Select(Node->getOperand(0)); // chain
2360 }
2361
2362 switch (Node->getValueType(0)) {
2363 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2364 default: assert(0 && "Unknown type to sign extend to.");
2365 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002366 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002367 default:
2368 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2369 case MVT::i8:
2370 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2371 break;
2372 case MVT::i16:
2373 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2374 break;
2375 }
2376 break;
2377 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002378 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002379 "Cannot sign extend from bool!");
2380 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2381 break;
2382 }
2383 return Result;
2384 }
2385
Chris Lattner88c8a232005-01-07 07:49:41 +00002386 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00002387 // Generate both result values.
2388 if (Result != 1)
2389 ExprMap[N.getValue(1)] = 1; // Generate the token
2390 else
2391 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2392
2393 // FIXME: We are currently ignoring the requested alignment for handling
2394 // greater than the stack alignment. This will need to be revisited at some
2395 // point. Align = N.getOperand(2);
2396
2397 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2398 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2399 std::cerr << "Cannot allocate stack object with greater alignment than"
2400 << " the stack alignment yet!";
2401 abort();
2402 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002403
Chris Lattner88c8a232005-01-07 07:49:41 +00002404 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002405 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002406 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2407 .addImm(CN->getValue());
2408 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002409 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2410 Select(N.getOperand(0));
2411 Tmp1 = SelectExpr(N.getOperand(1));
2412 } else {
2413 Tmp1 = SelectExpr(N.getOperand(1));
2414 Select(N.getOperand(0));
2415 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002416
2417 // Subtract size from stack pointer, thereby allocating some space.
2418 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2419 }
2420
2421 // Put a pointer to the space into the result register, by copying the stack
2422 // pointer.
2423 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2424 return Result;
2425
Chris Lattner1b3520c2005-05-14 08:48:15 +00002426 case X86ISD::TAILCALL:
2427 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00002428 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002429 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00002430
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002431 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
2432 isa<ExternalSymbolSDNode>(N.getOperand(1));
2433 unsigned Callee = 0;
2434 if (isDirect) {
2435 Select(N.getOperand(0));
2436 } else {
2437 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2438 Select(N.getOperand(0));
2439 Callee = SelectExpr(N.getOperand(1));
2440 } else {
2441 Callee = SelectExpr(N.getOperand(1));
2442 Select(N.getOperand(0));
2443 }
2444 }
2445
2446 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002447 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002448 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002449 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002450 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00002451 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002452
Chris Lattner1b3520c2005-05-14 08:48:15 +00002453 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002454 default: assert(0 && "Bad thing to pass in regs");
2455 case MVT::i1:
2456 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
2457 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
2458 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
2459 }
2460 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00002461 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002462 default: assert(0 && "Bad thing to pass in regs");
2463 case MVT::i1:
2464 case MVT::i8:
2465 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
2466 break;
2467 case MVT::i16:
2468 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
2469 break;
2470 case MVT::i32:
2471 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
2472 break;
2473 }
2474 }
2475
Chris Lattner88c8a232005-01-07 07:49:41 +00002476 if (GlobalAddressSDNode *GASD =
2477 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
2478 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2479 } else if (ExternalSymbolSDNode *ESSDN =
2480 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
2481 BuildMI(BB, X86::CALLpcrel32,
2482 1).addExternalSymbol(ESSDN->getSymbol(), true);
2483 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002484 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2485 Select(N.getOperand(0));
2486 Tmp1 = SelectExpr(N.getOperand(1));
2487 } else {
2488 Tmp1 = SelectExpr(N.getOperand(1));
2489 Select(N.getOperand(0));
2490 }
2491
Chris Lattner88c8a232005-01-07 07:49:41 +00002492 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2493 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002494
2495 // Get caller stack amount and amount the callee added to the stack pointer.
2496 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
2497 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
2498 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
2499
2500 if (Node->getNumValues() != 1)
2501 switch (Node->getValueType(1)) {
2502 default: assert(0 && "Unknown value type for call result!");
2503 case MVT::Other: return 1;
2504 case MVT::i1:
2505 case MVT::i8:
2506 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2507 break;
2508 case MVT::i16:
2509 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2510 break;
2511 case MVT::i32:
2512 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2513 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
2514 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2515 break;
2516 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00002517 if (X86ScalarSSE) {
2518 ContainsFPCode = true;
2519 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
2520
2521 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
2522 MachineFunction *F = BB->getParent();
2523 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2524 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
2525 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
2526 break;
2527 } else {
2528 ContainsFPCode = true;
2529 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2530 break;
2531 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002532 }
2533 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002534 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00002535 case ISD::READPORT:
2536 // First, determine that the size of the operand falls within the acceptable
2537 // range for this architecture.
2538 //
2539 if (Node->getOperand(1).getValueType() != MVT::i16) {
2540 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2541 exit(1);
2542 }
2543
2544 // Make sure we generate both values.
2545 if (Result != 1) { // Generate the token
2546 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2547 assert(0 && "readport already emitted!?");
2548 } else
2549 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002550
Chris Lattner70ea07c2005-05-09 21:17:38 +00002551 Select(Node->getOperand(0)); // Select the chain.
2552
2553 // If the port is a single-byte constant, use the immediate form.
2554 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2555 if ((Port->getValue() & 255) == Port->getValue()) {
2556 switch (Node->getValueType(0)) {
2557 case MVT::i8:
2558 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2559 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2560 return Result;
2561 case MVT::i16:
2562 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2563 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2564 return Result;
2565 case MVT::i32:
2566 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2567 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2568 return Result;
2569 default: break;
2570 }
2571 }
2572
2573 // Now, move the I/O port address into the DX register and use the IN
2574 // instruction to get the input data.
2575 //
2576 Tmp1 = SelectExpr(Node->getOperand(1));
2577 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2578 switch (Node->getValueType(0)) {
2579 case MVT::i8:
2580 BuildMI(BB, X86::IN8rr, 0);
2581 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2582 return Result;
2583 case MVT::i16:
2584 BuildMI(BB, X86::IN16rr, 0);
2585 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2586 return Result;
2587 case MVT::i32:
2588 BuildMI(BB, X86::IN32rr, 0);
2589 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2590 return Result;
2591 default:
2592 std::cerr << "Cannot do input on this data type";
2593 exit(1);
2594 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002595
Chris Lattner88c8a232005-01-07 07:49:41 +00002596 }
2597
2598 return 0;
2599}
2600
Chris Lattner96113fd2005-01-17 19:25:26 +00002601/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2602/// load/op/store instruction. If successful return true.
2603bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2604 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2605 SDOperand Chain = Node->getOperand(0);
2606 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00002607 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00002608
2609 // The chain has to be a load, the stored value must be an integer binary
2610 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00002611 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00002612 MVT::isFloatingPoint(StVal.getValueType()))
2613 return false;
2614
Chris Lattnere86c9332005-01-17 22:10:42 +00002615 // Token chain must either be a factor node or the load to fold.
2616 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2617 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00002618
Chris Lattnere86c9332005-01-17 22:10:42 +00002619 SDOperand TheLoad;
2620
2621 // Check to see if there is a load from the same pointer that we're storing
2622 // to in either operand of the binop.
2623 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2624 StVal.getOperand(0).getOperand(1) == StPtr)
2625 TheLoad = StVal.getOperand(0);
2626 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2627 StVal.getOperand(1).getOperand(1) == StPtr)
2628 TheLoad = StVal.getOperand(1);
2629 else
2630 return false; // No matching load operand.
2631
2632 // We can only fold the load if there are no intervening side-effecting
2633 // operations. This means that the store uses the load as its token chain, or
2634 // there are only token factor nodes in between the store and load.
2635 if (Chain != TheLoad.getValue(1)) {
2636 // Okay, the other option is that we have a store referring to (possibly
2637 // nested) token factor nodes. For now, just try peeking through one level
2638 // of token factors to see if this is the case.
2639 bool ChainOk = false;
2640 if (Chain.getOpcode() == ISD::TokenFactor) {
2641 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2642 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2643 ChainOk = true;
2644 break;
2645 }
2646 }
2647
2648 if (!ChainOk) return false;
2649 }
2650
2651 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00002652 return false;
2653
2654 // Make sure that one of the operands of the binop is the load, and that the
2655 // load folds into the binop.
2656 if (((StVal.getOperand(0) != TheLoad ||
2657 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2658 (StVal.getOperand(1) != TheLoad ||
2659 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2660 return false;
2661
2662 // Finally, check to see if this is one of the ops we can handle!
2663 static const unsigned ADDTAB[] = {
2664 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2665 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2666 };
2667 static const unsigned SUBTAB[] = {
2668 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2669 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2670 };
2671 static const unsigned ANDTAB[] = {
2672 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2673 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2674 };
2675 static const unsigned ORTAB[] = {
2676 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2677 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2678 };
2679 static const unsigned XORTAB[] = {
2680 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2681 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2682 };
2683 static const unsigned SHLTAB[] = {
2684 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2685 /*Have to put the reg in CL*/0, 0, 0,
2686 };
2687 static const unsigned SARTAB[] = {
2688 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2689 /*Have to put the reg in CL*/0, 0, 0,
2690 };
2691 static const unsigned SHRTAB[] = {
2692 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2693 /*Have to put the reg in CL*/0, 0, 0,
2694 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00002695
Chris Lattner96113fd2005-01-17 19:25:26 +00002696 const unsigned *TabPtr = 0;
2697 switch (StVal.getOpcode()) {
2698 default:
2699 std::cerr << "CANNOT [mem] op= val: ";
2700 StVal.Val->dump(); std::cerr << "\n";
Chris Lattner0815dcae2005-09-28 22:29:17 +00002701 case ISD::FMUL:
Chris Lattner96113fd2005-01-17 19:25:26 +00002702 case ISD::MUL:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002703 case ISD::FDIV:
Chris Lattner96113fd2005-01-17 19:25:26 +00002704 case ISD::SDIV:
2705 case ISD::UDIV:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002706 case ISD::FREM:
Chris Lattner96113fd2005-01-17 19:25:26 +00002707 case ISD::SREM:
2708 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002709
Chris Lattner96113fd2005-01-17 19:25:26 +00002710 case ISD::ADD: TabPtr = ADDTAB; break;
2711 case ISD::SUB: TabPtr = SUBTAB; break;
2712 case ISD::AND: TabPtr = ANDTAB; break;
2713 case ISD:: OR: TabPtr = ORTAB; break;
2714 case ISD::XOR: TabPtr = XORTAB; break;
2715 case ISD::SHL: TabPtr = SHLTAB; break;
2716 case ISD::SRA: TabPtr = SARTAB; break;
2717 case ISD::SRL: TabPtr = SHRTAB; break;
2718 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002719
Chris Lattner96113fd2005-01-17 19:25:26 +00002720 // Handle: [mem] op= CST
2721 SDOperand Op0 = StVal.getOperand(0);
2722 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00002723 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00002724 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2725 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2726 default: break;
2727 case MVT::i1:
2728 case MVT::i8: Opc = TabPtr[0]; break;
2729 case MVT::i16: Opc = TabPtr[1]; break;
2730 case MVT::i32: Opc = TabPtr[2]; break;
2731 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002732
Chris Lattner96113fd2005-01-17 19:25:26 +00002733 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00002734 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2735 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002736 Select(Chain);
2737
Chris Lattner96113fd2005-01-17 19:25:26 +00002738 X86AddressMode AM;
2739 if (getRegPressure(TheLoad.getOperand(0)) >
2740 getRegPressure(TheLoad.getOperand(1))) {
2741 Select(TheLoad.getOperand(0));
2742 SelectAddress(TheLoad.getOperand(1), AM);
2743 } else {
2744 SelectAddress(TheLoad.getOperand(1), AM);
2745 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00002746 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002747
2748 if (StVal.getOpcode() == ISD::ADD) {
2749 if (CN->getValue() == 1) {
2750 switch (Op0.getValueType()) {
2751 default: break;
2752 case MVT::i8:
2753 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2754 return true;
2755 case MVT::i16: Opc = TabPtr[1];
2756 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2757 return true;
2758 case MVT::i32: Opc = TabPtr[2];
2759 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2760 return true;
2761 }
2762 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2763 switch (Op0.getValueType()) {
2764 default: break;
2765 case MVT::i8:
2766 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2767 return true;
2768 case MVT::i16: Opc = TabPtr[1];
2769 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2770 return true;
2771 case MVT::i32: Opc = TabPtr[2];
2772 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2773 return true;
2774 }
2775 }
2776 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002777
Chris Lattner96113fd2005-01-17 19:25:26 +00002778 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2779 return true;
2780 }
2781 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002782
Chris Lattner96113fd2005-01-17 19:25:26 +00002783 // If we have [mem] = V op [mem], try to turn it into:
2784 // [mem] = [mem] op V.
Chris Lattner0815dcae2005-09-28 22:29:17 +00002785 if (Op1 == TheLoad &&
2786 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
Chris Lattner96113fd2005-01-17 19:25:26 +00002787 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2788 StVal.getOpcode() != ISD::SRL)
2789 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002790
Chris Lattner96113fd2005-01-17 19:25:26 +00002791 if (Op0 != TheLoad) return false;
2792
2793 switch (Op0.getValueType()) {
2794 default: return false;
2795 case MVT::i1:
2796 case MVT::i8: Opc = TabPtr[3]; break;
2797 case MVT::i16: Opc = TabPtr[4]; break;
2798 case MVT::i32: Opc = TabPtr[5]; break;
2799 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002800
Chris Lattner479c7112005-01-18 17:35:28 +00002801 // Table entry doesn't exist?
2802 if (Opc == 0) return false;
2803
Chris Lattner78d30282005-01-18 03:51:59 +00002804 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2805 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002806 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00002807 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00002808
Chris Lattner96113fd2005-01-17 19:25:26 +00002809 X86AddressMode AM;
2810 SelectAddress(TheLoad.getOperand(1), AM);
2811 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002812 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00002813 return true;
2814}
2815
Chris Lattnerdd66a412005-05-15 05:46:45 +00002816/// If node is a ret(tailcall) node, emit the specified tail call and return
2817/// true, otherwise return false.
2818///
2819/// FIXME: This whole thing should be a post-legalize optimization pass which
2820/// recognizes and transforms the dag. We don't want the selection phase doing
2821/// this stuff!!
2822///
2823bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
2824 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
2825
2826 SDOperand Chain = RetNode->getOperand(0);
2827
2828 // If this is a token factor node where one operand is a call, dig into it.
2829 SDOperand TokFactor;
2830 unsigned TokFactorOperand = 0;
2831 if (Chain.getOpcode() == ISD::TokenFactor) {
2832 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2833 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
2834 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
2835 TokFactorOperand = i;
2836 TokFactor = Chain;
2837 Chain = Chain.getOperand(i);
2838 break;
2839 }
2840 if (TokFactor.Val == 0) return false; // No call operand.
2841 }
2842
2843 // Skip the CALLSEQ_END node if present.
2844 if (Chain.getOpcode() == ISD::CALLSEQ_END)
2845 Chain = Chain.getOperand(0);
2846
2847 // Is a tailcall the last control operation that occurs before the return?
2848 if (Chain.getOpcode() != X86ISD::TAILCALL)
2849 return false;
2850
2851 // If we return a value, is it the value produced by the call?
2852 if (RetNode->getNumOperands() > 1) {
2853 // Not returning the ret val of the call?
2854 if (Chain.Val->getNumValues() == 1 ||
2855 RetNode->getOperand(1) != Chain.getValue(1))
2856 return false;
2857
2858 if (RetNode->getNumOperands() > 2) {
2859 if (Chain.Val->getNumValues() == 2 ||
2860 RetNode->getOperand(2) != Chain.getValue(2))
2861 return false;
2862 }
2863 assert(RetNode->getNumOperands() <= 3);
2864 }
2865
2866 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
2867 // For FastCC, this will always be > 0.
2868 unsigned CalleeCallArgAmt =
2869 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
2870
2871 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
2872 // callee. For FastCC this will always be > 0, for CCC this is always 0.
2873 unsigned CalleeCallArgPopAmt =
2874 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
2875
2876 // There are several cases we can handle here. First, if the caller and
2877 // callee are both CCC functions, we can tailcall if the callee takes <= the
2878 // number of argument bytes that the caller does.
2879 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
2880 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
2881 // Check to see if caller arg area size >= callee arg area size.
2882 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
2883 //std::cerr << "CCC TAILCALL UNIMP!\n";
2884 // If TokFactor is non-null, emit all operands.
2885
2886 //EmitCCCToCCCTailCall(Chain.Val);
2887 //return true;
2888 }
2889 return false;
2890 }
2891
2892 // Second, if both are FastCC functions, we can always perform the tail call.
2893 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
2894 // If TokFactor is non-null, emit all operands before the call.
2895 if (TokFactor.Val) {
2896 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
2897 if (i != TokFactorOperand)
2898 Select(TokFactor.getOperand(i));
2899 }
2900
2901 EmitFastCCToFastCCTailCall(Chain.Val);
2902 return true;
2903 }
2904
2905 // We don't support mixed calls, due to issues with alignment. We could in
2906 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
2907 // aligned (which depends on the number of arguments to the callee). TODO.
2908 return false;
2909}
2910
2911static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
2912 SelectionDAG &DAG) {
2913 MVT::ValueType StoreVT;
2914 switch (Chain.getOpcode()) {
Chris Lattnerc1469402005-08-25 00:05:15 +00002915 default: assert(0 && "Unexpected node!");
Chris Lattnerdd66a412005-05-15 05:46:45 +00002916 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00002917 // If we found the start of the call sequence, we're done. We actually
2918 // strip off the CALLSEQ_START node, to avoid generating the
2919 // ADJCALLSTACKDOWN marker for the tail call.
2920 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002921 case ISD::TokenFactor: {
2922 std::vector<SDOperand> Ops;
2923 Ops.reserve(Chain.getNumOperands());
2924 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2925 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
2926 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
2927 }
2928 case ISD::STORE: // Normal store
2929 StoreVT = Chain.getOperand(1).getValueType();
2930 break;
2931 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00002932 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00002933 break;
2934 }
2935
2936 SDOperand OrigDest = Chain.getOperand(2);
2937 unsigned OrigOffset;
2938
2939 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
2940 OrigOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +00002941 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002942 } else {
2943 // We expect only (ESP+C)
2944 assert(OrigDest.getOpcode() == ISD::ADD &&
2945 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
2946 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
Chris Lattner7c762782005-08-16 21:56:37 +00002947 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
2948 == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002949 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2950 }
2951
2952 // Compute the new offset from the incoming ESP value we wish to use.
2953 unsigned NewOffset = OrigOffset + Offset;
2954
2955 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
2956 MachineFunction &MF = DAG.getMachineFunction();
2957 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
2958 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
2959
2960 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
2961 DAG);
2962 if (Chain.getOpcode() == ISD::STORE)
2963 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
2964 FIN);
2965 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
2966 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00002967 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00002968}
2969
2970
2971/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
2972/// fastcc function from a fastcc function, emit the code to emit a 'proper'
2973/// tail call.
2974void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
2975 unsigned CalleeCallArgSize =
2976 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
2977 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
2978
2979 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
2980
2981 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
2982 // indexes that are relative to the incoming ESP. If the incoming and
2983 // outgoing arg sizes are the same we will store to [InESP] instead of
2984 // [CurESP] and the ESP referenced will be relative to the incoming function
2985 // ESP.
2986 int ESPOffset = CallerArgSize-CalleeCallArgSize;
2987 SDOperand AdjustedArgStores =
2988 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
2989
2990 // Copy the return address of the caller into a virtual register so we don't
2991 // clobber it.
2992 SDOperand RetVal;
2993 if (ESPOffset) {
2994 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
2995 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
2996 RetValAddr, TheDAG->getSrcValue(NULL));
2997 SelectExpr(RetVal);
2998 }
2999
3000 // Codegen all of the argument stores.
3001 Select(AdjustedArgStores);
3002
3003 if (RetVal.Val) {
3004 // Emit a store of the saved ret value to the new location.
3005 MachineFunction &MF = TheDAG->getMachineFunction();
3006 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
3007 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
3008 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
3009 RetVal, RetValAddr));
3010 }
3011
3012 // Get the destination value.
3013 SDOperand Callee = TailCallNode->getOperand(1);
3014 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
3015 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00003016 unsigned CalleeReg = 0;
Chris Lattnerdd66a412005-05-15 05:46:45 +00003017 if (!isDirect) CalleeReg = SelectExpr(Callee);
3018
3019 unsigned RegOp1 = 0;
3020 unsigned RegOp2 = 0;
3021
3022 if (TailCallNode->getNumOperands() > 4) {
3023 // The first value is passed in (a part of) EAX, the second in EDX.
3024 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
3025 if (TailCallNode->getNumOperands() > 5)
3026 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003027
Chris Lattnerdd66a412005-05-15 05:46:45 +00003028 switch (TailCallNode->getOperand(4).getValueType()) {
3029 default: assert(0 && "Bad thing to pass in regs");
3030 case MVT::i1:
3031 case MVT::i8:
3032 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
3033 RegOp1 = X86::AL;
3034 break;
3035 case MVT::i16:
3036 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
3037 RegOp1 = X86::AX;
3038 break;
3039 case MVT::i32:
3040 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
3041 RegOp1 = X86::EAX;
3042 break;
3043 }
3044 if (RegOp2)
3045 switch (TailCallNode->getOperand(5).getValueType()) {
3046 default: assert(0 && "Bad thing to pass in regs");
3047 case MVT::i1:
3048 case MVT::i8:
3049 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
3050 RegOp2 = X86::DL;
3051 break;
3052 case MVT::i16:
3053 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3054 RegOp2 = X86::DX;
3055 break;
3056 case MVT::i32:
3057 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3058 RegOp2 = X86::EDX;
3059 break;
3060 }
3061 }
3062
3063 // Adjust ESP.
3064 if (ESPOffset)
3065 BuildMI(BB, X86::ADJSTACKPTRri, 2,
3066 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
3067
3068 // TODO: handle jmp [mem]
3069 if (!isDirect) {
3070 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
3071 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00003072 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003073 } else {
3074 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
3075 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
3076 }
3077 // ADD IMPLICIT USE RegOp1/RegOp2's
3078}
3079
Chris Lattner96113fd2005-01-17 19:25:26 +00003080
Chris Lattner88c8a232005-01-07 07:49:41 +00003081void ISel::Select(SDOperand N) {
Chris Lattner9982da22005-10-02 16:29:36 +00003082 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
Chris Lattner88c8a232005-01-07 07:49:41 +00003083
Nate Begeman95210522005-03-24 04:39:54 +00003084 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003085 return; // Already selected.
3086
Chris Lattner36f78482005-01-11 06:14:36 +00003087 SDNode *Node = N.Val;
3088
3089 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003090 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003091 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003092 assert(0 && "Node not handled yet!");
3093 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003094 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003095 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003096 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003097 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3098 Select(Node->getOperand(OneFirst));
3099 Select(Node->getOperand(!OneFirst));
3100 } else {
3101 std::vector<std::pair<unsigned, unsigned> > OpsP;
3102 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3103 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3104 std::sort(OpsP.begin(), OpsP.end());
3105 std::reverse(OpsP.begin(), OpsP.end());
3106 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3107 Select(Node->getOperand(OpsP[i].second));
3108 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003109 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003110 case ISD::CopyToReg:
Chris Lattner7c762782005-08-16 21:56:37 +00003111 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattner2cfce682005-01-12 02:02:48 +00003112 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00003113 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003114 } else {
Chris Lattner7c762782005-08-16 21:56:37 +00003115 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003116 Select(N.getOperand(0));
3117 }
Chris Lattner7c762782005-08-16 21:56:37 +00003118 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003119
Chris Lattner88c8a232005-01-07 07:49:41 +00003120 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00003121 switch (N.getOperand(2).getValueType()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003122 default: assert(0 && "Invalid type for operation!");
3123 case MVT::i1:
3124 case MVT::i8: Opc = X86::MOV8rr; break;
3125 case MVT::i16: Opc = X86::MOV16rr; break;
3126 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman9d7008b2005-10-14 22:06:00 +00003127 case MVT::f32: Opc = X86::MOVSSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003128 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003129 if (X86ScalarSSE) {
Nate Begeman9d7008b2005-10-14 22:06:00 +00003130 Opc = X86::MOVSDrr;
Nate Begeman8a093362005-07-06 18:59:04 +00003131 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003132 Opc = X86::FpMOV;
3133 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003134 }
3135 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003136 }
3137 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3138 }
3139 return;
3140 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00003141 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
3142 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
3143 N.getOperand(0).getOpcode() == ISD::TokenFactor)
3144 if (EmitPotentialTailCall(Node))
3145 return;
3146
Chris Lattner88c8a232005-01-07 07:49:41 +00003147 switch (N.getNumOperands()) {
3148 default:
3149 assert(0 && "Unknown return instruction!");
3150 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003151 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003152 N.getOperand(2).getValueType() == MVT::i32 &&
3153 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003154 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3155 Tmp1 = SelectExpr(N.getOperand(1));
3156 Tmp2 = SelectExpr(N.getOperand(2));
3157 } else {
3158 Tmp2 = SelectExpr(N.getOperand(2));
3159 Tmp1 = SelectExpr(N.getOperand(1));
3160 }
3161 Select(N.getOperand(0));
3162
Chris Lattner88c8a232005-01-07 07:49:41 +00003163 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3164 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003165 break;
3166 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003167 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3168 Select(N.getOperand(0));
3169 Tmp1 = SelectExpr(N.getOperand(1));
3170 } else {
3171 Tmp1 = SelectExpr(N.getOperand(1));
3172 Select(N.getOperand(0));
3173 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003174 switch (N.getOperand(1).getValueType()) {
3175 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00003176 case MVT::f32:
3177 if (X86ScalarSSE) {
3178 // Spill the value to memory and reload it into top of stack.
3179 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
3180 MachineFunction *F = BB->getParent();
3181 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3182 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
3183 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
3184 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003185 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003186 } else {
3187 assert(0 && "MVT::f32 only legal with scalar sse fp");
3188 abort();
3189 }
3190 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003191 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003192 if (X86ScalarSSE) {
3193 // Spill the value to memory and reload it into top of stack.
3194 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3195 MachineFunction *F = BB->getParent();
3196 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3197 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
3198 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
3199 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003200 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003201 } else {
3202 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
3203 }
3204 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003205 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00003206 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3207 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003208 }
3209 break;
3210 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003211 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003212 break;
3213 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003214 if (X86Lowering.getBytesToPopOnReturn() == 0)
3215 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3216 else
3217 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00003218 return;
3219 case ISD::BR: {
3220 Select(N.getOperand(0));
3221 MachineBasicBlock *Dest =
3222 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3223 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3224 return;
3225 }
3226
3227 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003228 MachineBasicBlock *Dest =
3229 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003230
Chris Lattner88c8a232005-01-07 07:49:41 +00003231 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3232 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003233 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3234 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3235 Select(N.getOperand(0));
3236 Tmp1 = SelectExpr(N.getOperand(1));
3237 } else {
3238 Tmp1 = SelectExpr(N.getOperand(1));
3239 Select(N.getOperand(0));
3240 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003241 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3242 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3243 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003244
Chris Lattner88c8a232005-01-07 07:49:41 +00003245 return;
3246 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003247
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003248 case ISD::LOAD:
3249 // If this load could be folded into the only using instruction, and if it
3250 // is safe to emit the instruction here, try to do so now.
3251 if (Node->hasNUsesOfValue(1, 0)) {
3252 SDOperand TheVal = N.getValue(0);
3253 SDNode *User = 0;
3254 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3255 assert(UI != Node->use_end() && "Didn't find use!");
3256 SDNode *UN = *UI;
3257 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3258 if (UN->getOperand(i) == TheVal) {
3259 User = UN;
3260 goto FoundIt;
3261 }
3262 }
3263 FoundIt:
3264 // Only handle unary operators right now.
3265 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003266 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003267 SelectExpr(SDOperand(User, 0));
3268 return;
3269 }
3270 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003271 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003272 SelectExpr(N);
3273 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003274 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003275 case ISD::EXTLOAD:
3276 case ISD::SEXTLOAD:
3277 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003278 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner1b3520c2005-05-14 08:48:15 +00003279 case X86ISD::TAILCALL:
3280 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00003281 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003282 SelectExpr(N);
3283 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003284 case ISD::CopyFromReg:
Chris Lattnera36117b2005-05-14 06:52:07 +00003285 case X86ISD::FILD64m:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003286 ExprMap.erase(N);
3287 SelectExpr(N.getValue(0));
3288 return;
Jeff Cohen546fd592005-07-30 18:33:25 +00003289
Chris Lattner4738d1b2005-07-30 00:05:54 +00003290 case X86ISD::FP_TO_INT16_IN_MEM:
3291 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattner6dc60e82005-07-29 00:54:34 +00003292 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00003293 assert(N.getOperand(1).getValueType() == MVT::f64);
3294 X86AddressMode AM;
3295 Select(N.getOperand(0)); // Select the token chain
3296
3297 unsigned ValReg;
3298 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3299 ValReg = SelectExpr(N.getOperand(1));
3300 SelectAddress(N.getOperand(2), AM);
3301 } else {
3302 SelectAddress(N.getOperand(2), AM);
3303 ValReg = SelectExpr(N.getOperand(1));
3304 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003305
Chris Lattner6dc60e82005-07-29 00:54:34 +00003306 // Change the floating point control register to use "round towards zero"
3307 // mode when truncating to an integer value.
3308 //
3309 MachineFunction *F = BB->getParent();
3310 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3311 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003312
Chris Lattner6dc60e82005-07-29 00:54:34 +00003313 // Load the old value of the high byte of the control word...
Chris Lattneraeef51b2005-07-30 00:17:52 +00003314 unsigned OldCW = MakeReg(MVT::i16);
3315 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003316
Chris Lattner6dc60e82005-07-29 00:54:34 +00003317 // Set the high part to be round to zero...
Chris Lattner49134572005-07-30 00:43:00 +00003318 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohen546fd592005-07-30 18:33:25 +00003319
Chris Lattner6dc60e82005-07-29 00:54:34 +00003320 // Reload the modified control word now...
3321 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003322
Chris Lattner6dc60e82005-07-29 00:54:34 +00003323 // Restore the memory image of control word to original value
Chris Lattneraeef51b2005-07-30 00:17:52 +00003324 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner4738d1b2005-07-30 00:05:54 +00003325
3326 // Get the X86 opcode to use.
3327 switch (N.getOpcode()) {
3328 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FIST16m; break;
3329 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FIST32m; break;
3330 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FISTP64m; break;
3331 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003332
Chris Lattner4738d1b2005-07-30 00:05:54 +00003333 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohen546fd592005-07-30 18:33:25 +00003334
Chris Lattner6dc60e82005-07-29 00:54:34 +00003335 // Reload the original control word now.
3336 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00003337 return;
3338 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003339
Chris Lattner36db1ed2005-07-10 00:29:18 +00003340 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00003341 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00003342 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003343 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3344 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3345 && "Unsupported TRUNCSTORE for this target!");
3346
3347 if (StoredTy == MVT::i16) {
3348 // FIXME: This is here just to allow testing. X86 doesn't really have a
3349 // TRUNCSTORE i16 operation, but this is required for targets that do not
3350 // have 16-bit integer registers. We occasionally disable 16-bit integer
3351 // registers to test the promotion code.
3352 Select(N.getOperand(0));
3353 Tmp1 = SelectExpr(N.getOperand(1));
3354 SelectAddress(N.getOperand(2), AM);
3355
3356 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3357 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3358 return;
3359 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003360
3361 // Store of constant bool?
3362 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3363 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3364 Select(N.getOperand(0));
3365 SelectAddress(N.getOperand(2), AM);
3366 } else {
3367 SelectAddress(N.getOperand(2), AM);
3368 Select(N.getOperand(0));
3369 }
3370 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3371 return;
3372 }
3373
3374 switch (StoredTy) {
3375 default: assert(0 && "Cannot truncstore this type!");
3376 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003377 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003378 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begeman8a093362005-07-06 18:59:04 +00003379 Opc = X86::FST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003380 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003381
Chris Lattnere18a4c42005-01-15 05:22:24 +00003382 std::vector<std::pair<unsigned, unsigned> > RP;
3383 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3384 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3385 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3386 std::sort(RP.begin(), RP.end());
3387
Chris Lattner80c5b972005-02-23 05:57:21 +00003388 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003389 for (unsigned i = 0; i != 3; ++i)
3390 switch (RP[2-i].second) {
3391 default: assert(0 && "Unknown operand number!");
3392 case 0: Select(N.getOperand(0)); break;
3393 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3394 case 2: SelectAddress(N.getOperand(2), AM); break;
3395 }
3396
3397 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3398 return;
3399 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003400 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003401 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003402
3403 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3404 Opc = 0;
3405 switch (CN->getValueType(0)) {
3406 default: assert(0 && "Invalid type for operation!");
3407 case MVT::i1:
3408 case MVT::i8: Opc = X86::MOV8mi; break;
3409 case MVT::i16: Opc = X86::MOV16mi; break;
3410 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003411 }
3412 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003413 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3414 Select(N.getOperand(0));
3415 SelectAddress(N.getOperand(2), AM);
3416 } else {
3417 SelectAddress(N.getOperand(2), AM);
3418 Select(N.getOperand(0));
3419 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003420 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3421 return;
3422 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003423 } else if (GlobalAddressSDNode *GA =
3424 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3425 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3426
3427 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3428 Select(N.getOperand(0));
3429 SelectAddress(N.getOperand(2), AM);
3430 } else {
3431 SelectAddress(N.getOperand(2), AM);
3432 Select(N.getOperand(0));
3433 }
Nate Begemana0b5e032005-07-15 00:38:55 +00003434 GlobalValue *GV = GA->getGlobal();
3435 // For Darwin, external and weak symbols are indirect, so we want to load
3436 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003437 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00003438 (GV->hasWeakLinkage() || GV->isExternal())) {
3439 Tmp1 = MakeReg(MVT::i32);
3440 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
3441 .addGlobalAddress(GV, false, 0);
3442 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
3443 } else {
3444 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
3445 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003446 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003447 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003448
3449 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003450 if (TryToFoldLoadOpStore(Node))
3451 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003452
Chris Lattner88c8a232005-01-07 07:49:41 +00003453 switch (N.getOperand(1).getValueType()) {
3454 default: assert(0 && "Cannot store this type!");
3455 case MVT::i1:
3456 case MVT::i8: Opc = X86::MOV8mr; break;
3457 case MVT::i16: Opc = X86::MOV16mr; break;
3458 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003459 case MVT::f32: Opc = X86::MOVSSmr; break;
3460 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003461 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003462
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003463 std::vector<std::pair<unsigned, unsigned> > RP;
3464 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3465 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3466 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3467 std::sort(RP.begin(), RP.end());
3468
Chris Lattner80c5b972005-02-23 05:57:21 +00003469 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003470 for (unsigned i = 0; i != 3; ++i)
3471 switch (RP[2-i].second) {
3472 default: assert(0 && "Unknown operand number!");
3473 case 0: Select(N.getOperand(0)); break;
3474 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003475 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003476 }
3477
Chris Lattner88c8a232005-01-07 07:49:41 +00003478 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3479 return;
3480 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003481 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003482 Select(N.getOperand(0));
3483 // Stack amount
3484 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3485 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3486 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00003487 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003488 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003489 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003490 case ISD::MEMSET: {
3491 Select(N.getOperand(0)); // Select the chain.
3492 unsigned Align =
3493 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3494 if (Align == 0) Align = 1;
3495
3496 // Turn the byte code into # iterations
3497 unsigned CountReg;
3498 unsigned Opcode;
3499 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3500 unsigned Val = ValC->getValue() & 255;
3501
3502 // If the value is a constant, then we can potentially use larger sets.
3503 switch (Align & 3) {
3504 case 2: // WORD aligned
3505 CountReg = MakeReg(MVT::i32);
3506 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3507 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3508 } else {
3509 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3510 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3511 }
3512 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3513 Opcode = X86::REP_STOSW;
3514 break;
3515 case 0: // DWORD aligned
3516 CountReg = MakeReg(MVT::i32);
3517 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3518 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3519 } else {
3520 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3521 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3522 }
3523 Val = (Val << 8) | Val;
3524 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3525 Opcode = X86::REP_STOSD;
3526 break;
3527 default: // BYTE aligned
3528 CountReg = SelectExpr(Node->getOperand(3));
3529 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3530 Opcode = X86::REP_STOSB;
3531 break;
3532 }
3533 } else {
3534 // If it's not a constant value we are storing, just fall back. We could
3535 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3536 unsigned ValReg = SelectExpr(Node->getOperand(2));
3537 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3538 CountReg = SelectExpr(Node->getOperand(3));
3539 Opcode = X86::REP_STOSB;
3540 }
3541
3542 // No matter what the alignment is, we put the source in ESI, the
3543 // destination in EDI, and the count in ECX.
3544 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3545 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3546 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3547 BuildMI(BB, Opcode, 0);
3548 return;
3549 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003550 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003551 Select(N.getOperand(0)); // Select the chain.
3552 unsigned Align =
3553 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3554 if (Align == 0) Align = 1;
3555
3556 // Turn the byte code into # iterations
3557 unsigned CountReg;
3558 unsigned Opcode;
3559 switch (Align & 3) {
3560 case 2: // WORD aligned
3561 CountReg = MakeReg(MVT::i32);
3562 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3563 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3564 } else {
3565 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3566 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3567 }
3568 Opcode = X86::REP_MOVSW;
3569 break;
3570 case 0: // DWORD aligned
3571 CountReg = MakeReg(MVT::i32);
3572 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3573 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3574 } else {
3575 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3576 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3577 }
3578 Opcode = X86::REP_MOVSD;
3579 break;
3580 default: // BYTE aligned
3581 CountReg = SelectExpr(Node->getOperand(3));
3582 Opcode = X86::REP_MOVSB;
3583 break;
3584 }
3585
3586 // No matter what the alignment is, we put the source in ESI, the
3587 // destination in EDI, and the count in ECX.
3588 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3589 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3590 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3591 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3592 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3593 BuildMI(BB, Opcode, 0);
3594 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003595 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003596 case ISD::WRITEPORT:
3597 if (Node->getOperand(2).getValueType() != MVT::i16) {
3598 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3599 exit(1);
3600 }
3601 Select(Node->getOperand(0)); // Emit the chain.
3602
3603 Tmp1 = SelectExpr(Node->getOperand(1));
3604 switch (Node->getOperand(1).getValueType()) {
3605 case MVT::i8:
3606 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3607 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3608 break;
3609 case MVT::i16:
3610 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3611 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3612 break;
3613 case MVT::i32:
3614 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3615 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3616 break;
3617 default:
3618 std::cerr << "llvm.writeport: invalid data type for X86 target";
3619 exit(1);
3620 }
3621
3622 // If the port is a single-byte constant, use the immediate form.
3623 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3624 if ((CN->getValue() & 255) == CN->getValue()) {
3625 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3626 return;
3627 }
3628
3629 // Otherwise, move the I/O port address into the DX register.
3630 unsigned Reg = SelectExpr(Node->getOperand(2));
3631 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3632 BuildMI(BB, Opc, 0);
3633 return;
3634 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003635 assert(0 && "Should not be reached!");
3636}
3637
3638
Chris Lattner76ac0682005-11-15 00:40:23 +00003639/// createX86ISelPattern - This pass converts an LLVM function
Chris Lattner88c8a232005-01-07 07:49:41 +00003640/// into a machine code representation using pattern matching and a machine
3641/// description file.
3642///
Chris Lattner76ac0682005-11-15 00:40:23 +00003643FunctionPass *llvm::createX86ISelPattern(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003644 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003645}