blob: 611d2a9bd70970bc7ade7d67ac27a4b1de7d491c [file] [log] [blame]
Chris Lattner88c8a232005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner1d13a922005-01-10 22:10:13 +00002//
Chris Lattner88c8a232005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
Chris Lattner88c8a232005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Nate Begemanf26625e2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Chris Lattner7ce7a8f2005-05-12 23:06:28 +000019#include "llvm/CallingConv.h"
Chris Lattner6972c312005-05-09 03:36:39 +000020#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000022#include "llvm/Function.h"
Chris Lattner6972c312005-05-09 03:36:39 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetLowering.h"
Nate Begemanf26625e2005-07-12 01:41:54 +000031#include "llvm/Target/TargetMachine.h"
Chris Lattnerdb68d392005-04-30 04:25:35 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner6972c312005-05-09 03:36:39 +000033#include "llvm/Support/CFG.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000034#include "llvm/Support/MathExtras.h"
35#include "llvm/ADT/Statistic.h"
36#include <set>
Chris Lattnerde02d772006-01-22 23:41:00 +000037#include <iostream>
Jeff Cohen407aa012005-01-12 04:29:05 +000038#include <algorithm>
Chris Lattner88c8a232005-01-07 07:49:41 +000039using namespace llvm;
40
Chris Lattnera36117b2005-05-14 06:52:07 +000041//===----------------------------------------------------------------------===//
42// Pattern Matcher Implementation
43//===----------------------------------------------------------------------===//
Chris Lattner88c8a232005-01-07 07:49:41 +000044
Chris Lattnera7acdda2005-01-18 01:06:26 +000045namespace {
46 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
47 /// SDOperand's instead of register numbers for the leaves of the matched
48 /// tree.
49 struct X86ISelAddressMode {
50 enum {
51 RegBase,
52 FrameIndexBase,
53 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +000054
Chris Lattnera7acdda2005-01-18 01:06:26 +000055 struct { // This is really a union, discriminated by BaseType!
56 SDOperand Reg;
57 int FrameIndex;
58 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +000059
Chris Lattnera7acdda2005-01-18 01:06:26 +000060 unsigned Scale;
61 SDOperand IndexReg;
62 unsigned Disp;
63 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +000064
Chris Lattnera7acdda2005-01-18 01:06:26 +000065 X86ISelAddressMode()
66 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
67 }
68 };
69}
Chris Lattner88c8a232005-01-07 07:49:41 +000070
71
72namespace {
73 Statistic<>
74 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
75
76 //===--------------------------------------------------------------------===//
77 /// ISel - X86 specific code to select X86 machine instructions for
78 /// SelectionDAG operations.
79 ///
80 class ISel : public SelectionDAGISel {
81 /// ContainsFPCode - Every instruction we select that uses or defines a FP
82 /// register should set this to true.
83 bool ContainsFPCode;
84
85 /// X86Lowering - This object fully describes how to lower LLVM code to an
86 /// X86-specific SelectionDAG.
87 X86TargetLowering X86Lowering;
88
Chris Lattner0d1f82a2005-01-11 03:11:44 +000089 /// RegPressureMap - This keeps an approximate count of the number of
90 /// registers required to evaluate each node in the graph.
91 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +000092
93 /// ExprMap - As shared expressions are codegen'd, we keep track of which
94 /// vreg the value is produced in, so we only emit one copy of each compiled
95 /// tree.
96 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +000097
Chris Lattnerdd66a412005-05-15 05:46:45 +000098 /// TheDAG - The DAG being selected during Select* operations.
99 SelectionDAG *TheDAG;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000100
101 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanf26625e2005-07-12 01:41:54 +0000102 /// make the right decision when generating code for different targets.
103 const X86Subtarget *Subtarget;
Evan Chengcde9e302006-01-27 08:10:46 +0000104
105 /// X86ScalarSSE - Select between SSE2 or x87 floating point ops.
106 bool X86ScalarSSE;
Chris Lattner88c8a232005-01-07 07:49:41 +0000107 public:
108 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Chris Lattner158acab2005-08-05 21:54:27 +0000109 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengcde9e302006-01-27 08:10:46 +0000110 X86ScalarSSE = Subtarget->hasSSE2();
Chris Lattner88c8a232005-01-07 07:49:41 +0000111 }
112
Chris Lattnere1e844c2005-01-21 21:35:14 +0000113 virtual const char *getPassName() const {
114 return "X86 Pattern Instruction Selection";
115 }
116
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000117 unsigned getRegPressure(SDOperand O) {
118 return RegPressureMap[O.Val];
119 }
120 unsigned ComputeRegPressure(SDOperand O);
121
Chris Lattner88c8a232005-01-07 07:49:41 +0000122 /// InstructionSelectBasicBlock - This callback is invoked by
123 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +0000124 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000125
Chris Lattner0b17b452005-05-13 07:38:09 +0000126 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
127
Chris Lattner30607ec2005-01-25 20:03:11 +0000128 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
129 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +0000130 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +0000131 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner29f58192005-01-19 07:37:26 +0000132 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +0000133 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +0000134 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Nate Begeman8d394eb2005-08-03 23:26:28 +0000135 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
136 MVT::ValueType SVT, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +0000137 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000138
139 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
140 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
141 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattnerdd66a412005-05-15 05:46:45 +0000142 bool EmitPotentialTailCall(SDNode *Node);
143 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner88c8a232005-01-07 07:49:41 +0000144 void Select(SDOperand N);
145 };
146}
147
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000148/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
149/// the main function.
150static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
151 MachineFrameInfo *MFI) {
152 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
153 int CWFrameIdx = MFI->CreateStackObject(2, 2);
154 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
155
156 // Set the high part to be 64-bit precision.
157 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
158 CWFrameIdx, 1).addImm(2);
159
160 // Reload the modified control word now.
161 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
162}
163
Chris Lattner0b17b452005-05-13 07:38:09 +0000164void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000165 // If this is main, emit special code for main.
Chris Lattnerb42e9622005-09-14 06:06:45 +0000166 MachineBasicBlock *BB = MF.begin();
Chris Lattner0b17b452005-05-13 07:38:09 +0000167 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
168 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
169}
170
171
Chris Lattner6fba62d62005-01-12 04:21:28 +0000172/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
173/// when it has created a SelectionDAG for us to codegen.
174void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
175 // While we're doing this, keep track of whether we see any FP code for
176 // FP_REG_KILL insertion.
177 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000178 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000179
180 // Scan the PHI nodes that already are inserted into this basic block. If any
181 // of them is a PHI of a floating point value, we need to insert an
182 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000183 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +0000184 if (BB != MF->begin())
185 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
186 I != E; ++I) {
187 assert(I->getOpcode() == X86::PHI &&
188 "Isn't just PHI nodes?");
189 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
190 X86::RFPRegisterClass) {
191 ContainsFPCode = true;
192 break;
193 }
Chris Lattner6fba62d62005-01-12 04:21:28 +0000194 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000195
Chris Lattner6fba62d62005-01-12 04:21:28 +0000196 // Compute the RegPressureMap, which is an approximation for the number of
197 // registers required to compute each node.
198 ComputeRegPressure(DAG.getRoot());
199
Chris Lattnerdd66a412005-05-15 05:46:45 +0000200 TheDAG = &DAG;
201
Chris Lattner6fba62d62005-01-12 04:21:28 +0000202 // Codegen the basic block.
203 Select(DAG.getRoot());
204
Chris Lattnerdd66a412005-05-15 05:46:45 +0000205 TheDAG = 0;
206
Chris Lattner6fba62d62005-01-12 04:21:28 +0000207 // Finally, look at all of the successors of this block. If any contain a PHI
208 // node of FP type, we need to insert an FP_REG_KILL in this block.
209 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
210 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
211 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
212 I != E && I->getOpcode() == X86::PHI; ++I) {
213 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
214 X86::RFPRegisterClass) {
215 ContainsFPCode = true;
216 break;
217 }
218 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000219
Chris Lattner6972c312005-05-09 03:36:39 +0000220 // Final check, check LLVM BB's that are successors to the LLVM BB
221 // corresponding to BB for FP PHI nodes.
222 const BasicBlock *LLVMBB = BB->getBasicBlock();
223 const PHINode *PN;
224 if (!ContainsFPCode)
225 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
226 SI != E && !ContainsFPCode; ++SI)
227 for (BasicBlock::const_iterator II = SI->begin();
228 (PN = dyn_cast<PHINode>(II)); ++II)
229 if (PN->getType()->isFloatingPoint()) {
230 ContainsFPCode = true;
231 break;
232 }
233
Chris Lattner6fba62d62005-01-12 04:21:28 +0000234 // Insert FP_REG_KILL instructions into basic blocks that need them. This
235 // only occurs due to the floating point stackifier not being aggressive
236 // enough to handle arbitrary global stackification.
237 //
238 // Currently we insert an FP_REG_KILL instruction into each block that uses or
239 // defines a floating point virtual register.
240 //
241 // When the global register allocators (like linear scan) finally update live
242 // variable analysis, we can keep floating point values in registers across
243 // basic blocks. This will be a huge win, but we are waiting on the global
244 // allocators before we can do this.
245 //
Chris Lattner472a2652005-03-30 01:10:00 +0000246 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +0000247 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
248 ++NumFPKill;
249 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000250
Chris Lattner6fba62d62005-01-12 04:21:28 +0000251 // Clear state used for selection.
252 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000253 RegPressureMap.clear();
254}
255
256
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000257// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
258// for the number of registers required to compute each node. This is basically
259// computing a generalized form of the Sethi-Ullman number for each node.
260unsigned ISel::ComputeRegPressure(SDOperand O) {
261 SDNode *N = O.Val;
262 unsigned &Result = RegPressureMap[N];
263 if (Result) return Result;
264
Chris Lattner8fea42b2005-01-11 03:37:59 +0000265 // FIXME: Should operations like CALL (which clobber lots o regs) have a
266 // higher fixed cost??
267
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000268 if (N->getNumOperands() == 0) {
269 Result = 1;
270 } else {
271 unsigned MaxRegUse = 0;
272 unsigned NumExtraMaxRegUsers = 0;
273 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
274 unsigned Regs;
275 if (N->getOperand(i).getOpcode() == ISD::Constant)
276 Regs = 0;
277 else
278 Regs = ComputeRegPressure(N->getOperand(i));
279 if (Regs > MaxRegUse) {
280 MaxRegUse = Regs;
281 NumExtraMaxRegUsers = 0;
282 } else if (Regs == MaxRegUse &&
283 N->getOperand(i).getValueType() != MVT::Other) {
284 ++NumExtraMaxRegUsers;
285 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000286 }
Chris Lattnerca318ed2005-01-17 22:56:09 +0000287
288 if (O.getOpcode() != ISD::TokenFactor)
289 Result = MaxRegUse+NumExtraMaxRegUsers;
290 else
Chris Lattnera5d137f2005-01-17 23:02:13 +0000291 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000292 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +0000293
Chris Lattner75bac9f2005-01-11 23:21:30 +0000294 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000295 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000296}
297
Chris Lattner5b04f332005-01-20 16:50:16 +0000298/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
299/// The DAG cannot have cycles in it, by definition, so the visited set is not
300/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
301/// reuse, so it prevents exponential cases.
302///
303static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
304 std::set<SDNode*> &Visited) {
305 if (N == Op) return true; // Found it.
306 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +0000307 if (Node->getNumOperands() == 0 || // Leaf?
308 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +0000309 if (!Visited.insert(Node).second) return false; // Already visited?
310
311 // Recurse for the first N-1 operands.
312 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
313 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
314 return true;
315
316 // Tail recurse for the last operand.
317 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
318}
319
Chris Lattnera7acdda2005-01-18 01:06:26 +0000320X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
321 X86AddressMode Result;
322
323 // If we need to emit two register operands, emit the one with the highest
324 // register pressure first.
325 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
326 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000327 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000328 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000329 std::set<SDNode*> Visited;
330 EmitBaseThenIndex = true;
331 // If Base ends up pointing to Index, we must emit index first. This is
332 // because of the way we fold loads, we may end up doing bad things with
333 // the folded add.
334 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
335 EmitBaseThenIndex = false;
336 } else {
337 std::set<SDNode*> Visited;
338 EmitBaseThenIndex = false;
339 // If Base ends up pointing to Index, we must emit index first. This is
340 // because of the way we fold loads, we may end up doing bad things with
341 // the folded add.
342 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
343 EmitBaseThenIndex = true;
344 }
345
346 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000347 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
348 Result.IndexReg = SelectExpr(IAM.IndexReg);
349 } else {
350 Result.IndexReg = SelectExpr(IAM.IndexReg);
351 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
352 }
Chris Lattner5b04f332005-01-20 16:50:16 +0000353
Chris Lattnera7acdda2005-01-18 01:06:26 +0000354 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
355 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
356 } else if (IAM.IndexReg.Val) {
357 Result.IndexReg = SelectExpr(IAM.IndexReg);
358 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000359
Chris Lattnera7acdda2005-01-18 01:06:26 +0000360 switch (IAM.BaseType) {
361 case X86ISelAddressMode::RegBase:
362 Result.BaseType = X86AddressMode::RegBase;
363 break;
364 case X86ISelAddressMode::FrameIndexBase:
365 Result.BaseType = X86AddressMode::FrameIndexBase;
366 Result.Base.FrameIndex = IAM.Base.FrameIndex;
367 break;
368 default:
369 assert(0 && "Unknown base type!");
370 break;
371 }
372 Result.Scale = IAM.Scale;
373 Result.Disp = IAM.Disp;
374 Result.GV = IAM.GV;
375 return Result;
376}
377
378/// SelectAddress - Pattern match the maximal addressing mode for this node and
379/// emit all of the leaf registers.
380void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
381 X86ISelAddressMode IAM;
382 MatchAddress(N, IAM);
383 AM = SelectAddrExprs(IAM);
384}
385
386/// MatchAddress - Add the specified node to the specified addressing mode,
387/// returning true if it cannot be done. This just pattern matches for the
388/// addressing mode, it does not cause any code to be emitted. For that, use
389/// SelectAddress.
390bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000391 switch (N.getOpcode()) {
392 default: break;
393 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +0000394 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
395 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +0000396 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
397 return false;
398 }
399 break;
400 case ISD::GlobalAddress:
401 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +0000402 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
403 // For Darwin, external and weak symbols are indirect, so we want to load
404 // the value at address GV, not the value of GV itself. This means that
405 // the GlobalAddress must be in the base or index register of the address,
406 // not the GV offset field.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000407 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +0000408 (GV->hasWeakLinkage() || GV->isExternal())) {
409 break;
410 } else {
411 AM.GV = GV;
412 return false;
413 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000414 }
415 break;
416 case ISD::Constant:
417 AM.Disp += cast<ConstantSDNode>(N)->getValue();
418 return false;
419 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000420 // We might have folded the load into this shift, so don't regen the value
421 // if so.
422 if (ExprMap.count(N)) break;
423
Chris Lattnera7acdda2005-01-18 01:06:26 +0000424 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +0000425 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
426 unsigned Val = CN->getValue();
427 if (Val == 1 || Val == 2 || Val == 3) {
428 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000429 SDOperand ShVal = N.Val->getOperand(0);
430
431 // Okay, we know that we have a scale by now. However, if the scaled
432 // value is an add of something and a constant, we can fold the
433 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000434 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000435 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000436 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000437 ConstantSDNode *AddVal =
438 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
439 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +0000440 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000441 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +0000442 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000443 return false;
444 }
445 }
446 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000447 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +0000448 // We might have folded the load into this mul, so don't regen the value if
449 // so.
450 if (ExprMap.count(N)) break;
451
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000452 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +0000453 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
454 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000455 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
456 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
457 AM.Scale = unsigned(CN->getValue())-1;
458
459 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000460 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000461
462 // Okay, we know that we have a scale by now. However, if the scaled
463 // value is an add of something and a constant, we can fold the
464 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +0000465 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000466 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000467 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000468 ConstantSDNode *AddVal =
469 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
470 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +0000471 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +0000472 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +0000473 }
474
475 AM.IndexReg = AM.Base.Reg = Reg;
476 return false;
477 }
478 break;
Chris Lattner88c8a232005-01-07 07:49:41 +0000479
480 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +0000481 // We might have folded the load into this mul, so don't regen the value if
482 // so.
483 if (ExprMap.count(N)) break;
484
Chris Lattnera7acdda2005-01-18 01:06:26 +0000485 X86ISelAddressMode Backup = AM;
486 if (!MatchAddress(N.Val->getOperand(0), AM) &&
487 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +0000488 return false;
489 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000490 if (!MatchAddress(N.Val->getOperand(1), AM) &&
491 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +0000492 return false;
493 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +0000494 break;
495 }
496 }
497
Chris Lattner378262d2005-01-11 04:40:19 +0000498 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +0000499 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +0000500 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000501 if (AM.IndexReg.Val == 0) {
502 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +0000503 AM.Scale = 1;
504 return false;
505 }
506
507 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +0000508 return true;
Chris Lattner378262d2005-01-11 04:40:19 +0000509 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000510
511 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +0000512 AM.BaseType = X86ISelAddressMode::RegBase;
513 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +0000514 return false;
515}
516
517/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
518/// assuming that the temporary registers are in the 8-bit register class.
519///
520/// Tmp1 = setcc1
521/// Tmp2 = setcc2
522/// DestReg = logicalop Tmp1, Tmp2
523///
524static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
525 unsigned SetCC2, unsigned LogicalOp,
526 unsigned DestReg) {
527 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
528 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
529 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
530 BuildMI(BB, SetCC1, 0, Tmp1);
531 BuildMI(BB, SetCC2, 0, Tmp2);
532 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
533}
534
535/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
536/// condition codes match the specified SetCCOpcode. Note that some conditions
537/// require multiple instructions to generate the correct value.
538static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
539 ISD::CondCode SetCCOpcode, bool isFP) {
540 unsigned Opc;
541 if (!isFP) {
542 switch (SetCCOpcode) {
543 default: assert(0 && "Illegal integer SetCC!");
544 case ISD::SETEQ: Opc = X86::SETEr; break;
545 case ISD::SETGT: Opc = X86::SETGr; break;
546 case ISD::SETGE: Opc = X86::SETGEr; break;
547 case ISD::SETLT: Opc = X86::SETLr; break;
548 case ISD::SETLE: Opc = X86::SETLEr; break;
549 case ISD::SETNE: Opc = X86::SETNEr; break;
550 case ISD::SETULT: Opc = X86::SETBr; break;
551 case ISD::SETUGT: Opc = X86::SETAr; break;
552 case ISD::SETULE: Opc = X86::SETBEr; break;
553 case ISD::SETUGE: Opc = X86::SETAEr; break;
554 }
555 } else {
556 // On a floating point condition, the flags are set as follows:
557 // ZF PF CF op
558 // 0 | 0 | 0 | X > Y
559 // 0 | 0 | 1 | X < Y
560 // 1 | 0 | 0 | X == Y
561 // 1 | 1 | 1 | unordered
562 //
563 switch (SetCCOpcode) {
564 default: assert(0 && "Invalid FP setcc!");
565 case ISD::SETUEQ:
566 case ISD::SETEQ:
567 Opc = X86::SETEr; // True if ZF = 1
568 break;
569 case ISD::SETOGT:
570 case ISD::SETGT:
571 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
572 break;
573 case ISD::SETOGE:
574 case ISD::SETGE:
575 Opc = X86::SETAEr; // True if CF = 0
576 break;
577 case ISD::SETULT:
578 case ISD::SETLT:
579 Opc = X86::SETBr; // True if CF = 1
580 break;
581 case ISD::SETULE:
582 case ISD::SETLE:
583 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
584 break;
585 case ISD::SETONE:
586 case ISD::SETNE:
587 Opc = X86::SETNEr; // True if ZF = 0
588 break;
589 case ISD::SETUO:
590 Opc = X86::SETPr; // True if PF = 1
591 break;
592 case ISD::SETO:
593 Opc = X86::SETNPr; // True if PF = 0
594 break;
595 case ISD::SETOEQ: // !PF & ZF
596 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
597 return;
598 case ISD::SETOLT: // !PF & CF
599 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
600 return;
601 case ISD::SETOLE: // !PF & (CF || ZF)
602 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
603 return;
604 case ISD::SETUGT: // PF | (!ZF & !CF)
605 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
606 return;
607 case ISD::SETUGE: // PF | !CF
608 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
609 return;
610 case ISD::SETUNE: // PF | !ZF
611 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
612 return;
613 }
614 }
615 BuildMI(BB, Opc, 0, DestReg);
616}
617
618
619/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
620/// the Dest block if the Cond condition is true. If we cannot fold this
621/// condition into the branch, return true.
622///
Chris Lattner37ed2852005-01-11 04:06:27 +0000623bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
624 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000625 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
626 // B) using two conditional branches instead of one condbr, two setcc's, and
627 // an or.
628 if ((Cond.getOpcode() == ISD::OR ||
629 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
630 // And and or set the flags for us, so there is no need to emit a TST of the
631 // result. It is only safe to do this if there is only a single use of the
632 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +0000633 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000634 SelectExpr(Cond);
635 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
636 return false;
637 }
638
639 // Codegen br not C -> JE.
640 if (Cond.getOpcode() == ISD::XOR)
641 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
642 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +0000643 unsigned CondR;
644 if (getRegPressure(Chain) > getRegPressure(Cond)) {
645 Select(Chain);
646 CondR = SelectExpr(Cond.Val->getOperand(0));
647 } else {
648 CondR = SelectExpr(Cond.Val->getOperand(0));
649 Select(Chain);
650 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000651 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
652 BuildMI(BB, X86::JE, 1).addMBB(Dest);
653 return false;
654 }
655
Chris Lattner6ec77452005-08-09 20:21:10 +0000656 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner88c8a232005-01-07 07:49:41 +0000657 return true; // Can only handle simple setcc's so far.
Chris Lattner6ec77452005-08-09 20:21:10 +0000658 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner88c8a232005-01-07 07:49:41 +0000659
660 unsigned Opc;
661
662 // Handle integer conditions first.
Chris Lattner6ec77452005-08-09 20:21:10 +0000663 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
664 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000665 default: assert(0 && "Illegal integer SetCC!");
666 case ISD::SETEQ: Opc = X86::JE; break;
667 case ISD::SETGT: Opc = X86::JG; break;
668 case ISD::SETGE: Opc = X86::JGE; break;
669 case ISD::SETLT: Opc = X86::JL; break;
670 case ISD::SETLE: Opc = X86::JLE; break;
671 case ISD::SETNE: Opc = X86::JNE; break;
672 case ISD::SETULT: Opc = X86::JB; break;
673 case ISD::SETUGT: Opc = X86::JA; break;
674 case ISD::SETULE: Opc = X86::JBE; break;
675 case ISD::SETUGE: Opc = X86::JAE; break;
676 }
Chris Lattner37ed2852005-01-11 04:06:27 +0000677 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000678 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000679 BuildMI(BB, Opc, 1).addMBB(Dest);
680 return false;
681 }
682
Chris Lattner88c8a232005-01-07 07:49:41 +0000683 unsigned Opc2 = 0; // Second branch if needed.
684
685 // On a floating point condition, the flags are set as follows:
686 // ZF PF CF op
687 // 0 | 0 | 0 | X > Y
688 // 0 | 0 | 1 | X < Y
689 // 1 | 0 | 0 | X == Y
690 // 1 | 1 | 1 | unordered
691 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000692 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000693 default: assert(0 && "Invalid FP setcc!");
694 case ISD::SETUEQ:
695 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
696 case ISD::SETOGT:
697 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
698 case ISD::SETOGE:
699 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
700 case ISD::SETULT:
701 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
702 case ISD::SETULE:
703 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
704 case ISD::SETONE:
705 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
706 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
707 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
708 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
709 Opc = X86::JA; // ZF = 0 & CF = 0
710 Opc2 = X86::JP; // PF = 1
711 break;
712 case ISD::SETUGE: // PF = 1 | CF = 0
713 Opc = X86::JAE; // CF = 0
714 Opc2 = X86::JP; // PF = 1
715 break;
716 case ISD::SETUNE: // PF = 1 | ZF = 0
717 Opc = X86::JNE; // ZF = 0
718 Opc2 = X86::JP; // PF = 1
719 break;
720 case ISD::SETOEQ: // PF = 0 & ZF = 1
721 //X86::JNP, X86::JE
722 //X86::AND8rr
723 return true; // FIXME: Emit more efficient code for this branch.
724 case ISD::SETOLT: // PF = 0 & CF = 1
725 //X86::JNP, X86::JB
726 //X86::AND8rr
727 return true; // FIXME: Emit more efficient code for this branch.
728 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
729 //X86::JNP, X86::JBE
730 //X86::AND8rr
731 return true; // FIXME: Emit more efficient code for this branch.
732 }
733
Chris Lattner37ed2852005-01-11 04:06:27 +0000734 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +0000735 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +0000736 BuildMI(BB, Opc, 1).addMBB(Dest);
737 if (Opc2)
738 BuildMI(BB, Opc2, 1).addMBB(Dest);
739 return false;
740}
741
Chris Lattner1d13a922005-01-10 22:10:13 +0000742/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman8d394eb2005-08-03 23:26:28 +0000743/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner1d13a922005-01-10 22:10:13 +0000744///
Nate Begeman8d394eb2005-08-03 23:26:28 +0000745void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
746 MVT::ValueType SVT, unsigned RDest) {
747 unsigned RTrue, RFalse;
Chris Lattner1d13a922005-01-10 22:10:13 +0000748 enum Condition {
749 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
750 NOT_SET
751 } CondCode = NOT_SET;
752
753 static const unsigned CMOVTAB16[] = {
754 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
755 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000756 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000757 };
758 static const unsigned CMOVTAB32[] = {
759 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
760 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +0000761 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +0000762 };
763 static const unsigned CMOVTABFP[] = {
Chris Lattnerf431ad42005-12-21 07:47:04 +0000764 X86::FpCMOVE, X86::FpCMOVNE, /*missing*/0, /*missing*/0,
765 /*missing*/0, /*missing*/ 0, X86::FpCMOVB, X86::FpCMOVBE,
Evan Cheng468fecd2006-01-21 02:55:41 +0000766 X86::FpCMOVNBE,X86::FpCMOVNB, X86::FpCMOVP, X86::FpCMOVNP
Chris Lattner1d13a922005-01-10 22:10:13 +0000767 };
Nate Begemana0b5e032005-07-15 00:38:55 +0000768 static const int SSE_CMOVTAB[] = {
Nate Begeman8d394eb2005-08-03 23:26:28 +0000769 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
770 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
771 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begeman8a093362005-07-06 18:59:04 +0000772 };
Nate Begeman8d394eb2005-08-03 23:26:28 +0000773
Chris Lattner6ec77452005-08-09 20:21:10 +0000774 if (Cond.getOpcode() == ISD::SETCC) {
775 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
776 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
777 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000778 default: assert(0 && "Unknown integer comparison!");
779 case ISD::SETEQ: CondCode = EQ; break;
780 case ISD::SETGT: CondCode = GT; break;
781 case ISD::SETGE: CondCode = GE; break;
782 case ISD::SETLT: CondCode = LT; break;
783 case ISD::SETLE: CondCode = LE; break;
784 case ISD::SETNE: CondCode = NE; break;
785 case ISD::SETULT: CondCode = B; break;
786 case ISD::SETUGT: CondCode = A; break;
787 case ISD::SETULE: CondCode = BE; break;
788 case ISD::SETUGE: CondCode = AE; break;
789 }
790 } else {
791 // On a floating point condition, the flags are set as follows:
792 // ZF PF CF op
793 // 0 | 0 | 0 | X > Y
794 // 0 | 0 | 1 | X < Y
795 // 1 | 0 | 0 | X == Y
796 // 1 | 1 | 1 | unordered
797 //
Chris Lattner6ec77452005-08-09 20:21:10 +0000798 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +0000799 default: assert(0 && "Unknown FP comparison!");
800 case ISD::SETUEQ:
801 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
802 case ISD::SETOGT:
803 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
804 case ISD::SETOGE:
805 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
806 case ISD::SETULT:
807 case ISD::SETLT: CondCode = B; break; // True if CF = 1
808 case ISD::SETULE:
809 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
810 case ISD::SETONE:
811 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
812 case ISD::SETUO: CondCode = P; break; // True if PF = 1
813 case ISD::SETO: CondCode = NP; break; // True if PF = 0
814 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
815 case ISD::SETUGE: // PF = 1 | CF = 0
816 case ISD::SETUNE: // PF = 1 | ZF = 0
817 case ISD::SETOEQ: // PF = 0 & ZF = 1
818 case ISD::SETOLT: // PF = 0 & CF = 1
819 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
820 // We cannot emit this comparison as a single cmov.
821 break;
822 }
823 }
Chris Lattner6ec77452005-08-09 20:21:10 +0000824
Chris Lattner1d13a922005-01-10 22:10:13 +0000825
Chris Lattner6ec77452005-08-09 20:21:10 +0000826 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
827 // code above and one of the results of the select is +0.0, then we can fake
828 // it up through a clever AND with mask. Otherwise, we will fall through to
829 // the code below that will use a PHI node to select the right value.
830 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
831 if (Cond.getOperand(0).getValueType() == SVT &&
832 NOT_SET != CondCode) {
833 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
834 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
835 bool TrueZero = CT && CT->isExactlyValue(0.0);
836 bool FalseZero = CF && CF->isExactlyValue(0.0);
837 if (TrueZero || FalseZero) {
838 SDOperand LHS = Cond.getOperand(0);
839 SDOperand RHS = Cond.getOperand(1);
840
841 // Select the two halves of the condition
842 unsigned RLHS, RRHS;
843 if (getRegPressure(LHS) > getRegPressure(RHS)) {
844 RLHS = SelectExpr(LHS);
845 RRHS = SelectExpr(RHS);
846 } else {
847 RRHS = SelectExpr(RHS);
848 RLHS = SelectExpr(LHS);
849 }
850
851 // Emit the comparison and generate a mask from it
852 unsigned MaskReg = MakeReg(SVT);
853 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
854 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
855 .addImm(SSE_CMOVTAB[CondCode]);
856
857 if (TrueZero) {
858 RFalse = SelectExpr(False);
859 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
860 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
861 } else {
862 RTrue = SelectExpr(True);
863 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
864 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
865 }
866 return;
Nate Begeman8d394eb2005-08-03 23:26:28 +0000867 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000868 }
Nate Begeman8a093362005-07-06 18:59:04 +0000869 }
Nate Begeman8d394eb2005-08-03 23:26:28 +0000870 }
871
872 // Select the true and false values for use in both the SSE PHI case, and the
873 // integer or x87 cmov cases below.
874 if (getRegPressure(True) > getRegPressure(False)) {
875 RTrue = SelectExpr(True);
876 RFalse = SelectExpr(False);
877 } else {
878 RFalse = SelectExpr(False);
879 RTrue = SelectExpr(True);
880 }
881
882 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
883 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
884 // node to select between the true and false values.
885 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
886 // FIXME: emit a direct compare and branch rather than setting a cond reg
887 // and testing it.
888 unsigned CondReg = SelectExpr(Cond);
889 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
890
891 // Create an iterator with which to insert the MBB for copying the false
892 // value and the MBB to hold the PHI instruction for this SetCC.
893 MachineBasicBlock *thisMBB = BB;
894 const BasicBlock *LLVM_BB = BB->getBasicBlock();
895 ilist<MachineBasicBlock>::iterator It = BB;
896 ++It;
897
898 // thisMBB:
899 // ...
900 // TrueVal = ...
901 // cmpTY ccX, r1, r2
902 // bCC sinkMBB
903 // fallthrough --> copy0MBB
904 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
905 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
906 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
907 MachineFunction *F = BB->getParent();
908 F->getBasicBlockList().insert(It, copy0MBB);
909 F->getBasicBlockList().insert(It, sinkMBB);
910 // Update machine-CFG edges
911 BB->addSuccessor(copy0MBB);
912 BB->addSuccessor(sinkMBB);
913
914 // copy0MBB:
915 // %FalseValue = ...
916 // # fallthrough to sinkMBB
917 BB = copy0MBB;
918 // Update machine-CFG edges
919 BB->addSuccessor(sinkMBB);
920
921 // sinkMBB:
922 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
923 // ...
924 BB = sinkMBB;
925 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
926 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begeman8a093362005-07-06 18:59:04 +0000927 return;
928 }
929
Chris Lattner1d13a922005-01-10 22:10:13 +0000930 unsigned Opc = 0;
931 if (CondCode != NOT_SET) {
932 switch (SVT) {
933 default: assert(0 && "Cannot select this type!");
934 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
935 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +0000936 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000937 }
938 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000939
Chris Lattner1d13a922005-01-10 22:10:13 +0000940 // Finally, if we weren't able to fold this, just emit the condition and test
941 // it.
942 if (CondCode == NOT_SET || Opc == 0) {
943 // Get the condition into the zero flag.
944 unsigned CondReg = SelectExpr(Cond);
945 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
946
947 switch (SVT) {
948 default: assert(0 && "Cannot select this type!");
949 case MVT::i16: Opc = X86::CMOVE16rr; break;
950 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +0000951 case MVT::f64: Opc = X86::FpCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +0000952 }
953 } else {
954 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +0000955 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +0000956 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +0000957 }
958 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
959}
960
Chris Lattner3be6cd52005-01-17 01:34:14 +0000961void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000962 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +0000963 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
964 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +0000965 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +0000966 switch (RHS.getValueType()) {
967 default: break;
968 case MVT::i1:
969 case MVT::i8: Opc = X86::CMP8mi; break;
970 case MVT::i16: Opc = X86::CMP16mi; break;
971 case MVT::i32: Opc = X86::CMP32mi; break;
972 }
973 if (Opc) {
974 X86AddressMode AM;
975 EmitFoldedLoad(LHS, AM);
976 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
977 return;
978 }
979 }
980
Chris Lattner88c8a232005-01-07 07:49:41 +0000981 switch (RHS.getValueType()) {
982 default: break;
983 case MVT::i1:
984 case MVT::i8: Opc = X86::CMP8ri; break;
985 case MVT::i16: Opc = X86::CMP16ri; break;
986 case MVT::i32: Opc = X86::CMP32ri; break;
987 }
988 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000989 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +0000990 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
991 return;
992 }
Chris Lattner720a62e2005-01-14 22:37:41 +0000993 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +0000994 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
995 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +0000996 unsigned Reg = SelectExpr(LHS);
Chris Lattnerf431ad42005-12-21 07:47:04 +0000997 BuildMI(BB, X86::FpTST, 1).addReg(Reg);
Chris Lattner720a62e2005-01-14 22:37:41 +0000998 BuildMI(BB, X86::FNSTSW8r, 0);
999 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +00001000 return;
Chris Lattner720a62e2005-01-14 22:37:41 +00001001 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001002 }
1003
Chris Lattner2cfce682005-01-12 02:02:48 +00001004 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001005 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001006 switch (RHS.getValueType()) {
1007 default: break;
1008 case MVT::i1:
1009 case MVT::i8: Opc = X86::CMP8mr; break;
1010 case MVT::i16: Opc = X86::CMP16mr; break;
1011 case MVT::i32: Opc = X86::CMP32mr; break;
1012 }
1013 if (Opc) {
1014 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001015 EmitFoldedLoad(LHS, AM);
1016 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001017 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1018 return;
1019 }
1020 }
1021
Chris Lattner88c8a232005-01-07 07:49:41 +00001022 switch (LHS.getValueType()) {
1023 default: assert(0 && "Cannot compare this value!");
1024 case MVT::i1:
1025 case MVT::i8: Opc = X86::CMP8rr; break;
1026 case MVT::i16: Opc = X86::CMP16rr; break;
1027 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001028 case MVT::f32: Opc = X86::UCOMISSrr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00001029 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FpUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001030 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001031 unsigned Tmp1, Tmp2;
1032 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1033 Tmp1 = SelectExpr(LHS);
1034 Tmp2 = SelectExpr(RHS);
1035 } else {
1036 Tmp2 = SelectExpr(RHS);
1037 Tmp1 = SelectExpr(LHS);
1038 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001039 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1040}
1041
Chris Lattner62b22422005-01-11 21:19:59 +00001042/// isFoldableLoad - Return true if this is a load instruction that can safely
1043/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001044bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1045 if (Op.getOpcode() == ISD::LOAD) {
1046 // FIXME: currently can't fold constant pool indexes.
1047 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1048 return false;
1049 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00001050 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001051 // FIXME: currently can't fold constant pool indexes.
1052 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1053 return false;
1054 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001055 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001056 }
Chris Lattner62b22422005-01-11 21:19:59 +00001057
1058 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001059 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1060 if (ExprMap.count(Op.getValue(1))) return false;
1061 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001062 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001063
Chris Lattnera56d29d2005-01-17 06:26:58 +00001064 // If there is not just one use of its value, we cannot fold.
1065 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1066
1067 // Finally, we cannot fold the load into the operation if this would induce a
1068 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1069 // operand of the operation we are folding the load into) can possible use the
1070 // chain node defined by the load.
1071 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1072 std::set<SDNode*> Visited;
1073 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1074 return false;
1075 }
1076 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001077}
1078
Chris Lattnera56d29d2005-01-17 06:26:58 +00001079
Chris Lattner62b22422005-01-11 21:19:59 +00001080/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1081/// and compute the address being loaded into AM.
1082void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1083 SDOperand Chain = Op.getOperand(0);
1084 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001085
Chris Lattner62b22422005-01-11 21:19:59 +00001086 if (getRegPressure(Chain) > getRegPressure(Address)) {
1087 Select(Chain);
1088 SelectAddress(Address, AM);
1089 } else {
1090 SelectAddress(Address, AM);
1091 Select(Chain);
1092 }
1093
1094 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001095 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1096 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00001097 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00001098 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00001099}
1100
Chris Lattner29f58192005-01-19 07:37:26 +00001101// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1102// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1103// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1104// return true.
1105bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001106 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1107 // good!
1108 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1109 std::swap(Op1, Op2); // Op1 is the SHL now.
1110 } else {
1111 return false; // No match
1112 }
1113
1114 SDOperand ShlVal = Op1.getOperand(0);
1115 SDOperand ShlAmt = Op1.getOperand(1);
1116 SDOperand ShrVal = Op2.getOperand(0);
1117 SDOperand ShrAmt = Op2.getOperand(1);
1118
Chris Lattner29f58192005-01-19 07:37:26 +00001119 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1120
Chris Lattner41fe2012005-01-19 06:18:43 +00001121 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1122 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1123 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00001124 if (SubCST->getValue() == RegSize) {
1125 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00001126 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00001127 if (ShrVal == ShlVal) {
1128 unsigned Reg, ShAmt;
1129 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1130 Reg = SelectExpr(ShrVal);
1131 ShAmt = SelectExpr(ShrAmt);
1132 } else {
1133 ShAmt = SelectExpr(ShrAmt);
1134 Reg = SelectExpr(ShrVal);
1135 }
1136 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1137 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1138 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1139 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1140 return true;
1141 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001142 unsigned AReg, BReg;
1143 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001144 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001145 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001146 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00001147 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001148 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001149 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00001150 unsigned ShAmt = SelectExpr(ShrAmt);
1151 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1152 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1153 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00001154 return true;
1155 }
1156 }
1157
Chris Lattnerde87d1462005-01-19 08:07:05 +00001158 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1159 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1160 if (SubCST->getValue() == RegSize) {
1161 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1162 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1163 if (ShrVal == ShlVal) {
1164 unsigned Reg, ShAmt;
1165 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1166 Reg = SelectExpr(ShrVal);
1167 ShAmt = SelectExpr(ShlAmt);
1168 } else {
1169 ShAmt = SelectExpr(ShlAmt);
1170 Reg = SelectExpr(ShrVal);
1171 }
1172 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1173 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1174 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1175 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1176 return true;
1177 } else if (RegSize != 8) {
1178 unsigned AReg, BReg;
1179 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00001180 AReg = SelectExpr(ShlVal);
1181 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001182 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00001183 BReg = SelectExpr(ShrVal);
1184 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001185 }
1186 unsigned ShAmt = SelectExpr(ShlAmt);
1187 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1188 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1189 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1190 return true;
1191 }
1192 }
Chris Lattner41fe2012005-01-19 06:18:43 +00001193
Chris Lattnerde87d1462005-01-19 08:07:05 +00001194 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1195 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1196 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1197 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1198 // (A >> 5) | (A << 27) --> ROR A, 5
1199 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1200 if (ShrVal == ShlVal) {
1201 unsigned Reg = SelectExpr(ShrVal);
1202 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1203 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1204 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1205 return true;
1206 } else if (RegSize != 8) {
1207 unsigned AReg, BReg;
1208 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001209 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001210 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001211 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001212 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001213 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001214 }
1215 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1216 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1217 .addImm(ShrCst->getValue());
1218 return true;
1219 }
1220 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001221
Chris Lattner41fe2012005-01-19 06:18:43 +00001222 return false;
1223}
1224
Chris Lattner88c8a232005-01-07 07:49:41 +00001225unsigned ISel::SelectExpr(SDOperand N) {
1226 unsigned Result;
Chris Lattner9982da22005-10-02 16:29:36 +00001227 unsigned Tmp1 = 0, Tmp2 = 0, Tmp3 = 0, Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001228 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00001229 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001230
Evan Chengbc7a0f442006-01-11 06:09:51 +00001231 if (Node->getOpcode() == ISD::CopyFromReg ||
1232 Node->getOpcode() == ISD::Register) {
1233 unsigned Reg = (Node->getOpcode() == ISD::CopyFromReg) ?
1234 cast<RegisterSDNode>(Node->getOperand(1))->getReg() :
1235 cast<RegisterSDNode>(Node)->getReg();
Chris Lattner7c762782005-08-16 21:56:37 +00001236 // Just use the specified register as our input if we can.
Chris Lattner5f9c1342006-01-13 20:19:44 +00001237 if (Node->getOpcode() == ISD::Register ||
1238 MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner7c762782005-08-16 21:56:37 +00001239 return Reg;
Evan Chengbc7a0f442006-01-11 06:09:51 +00001240 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001241
Chris Lattner62b22422005-01-11 21:19:59 +00001242 unsigned &Reg = ExprMap[N];
1243 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001244
Chris Lattnera31d4c72005-04-02 04:01:14 +00001245 switch (N.getOpcode()) {
1246 default:
Chris Lattner62b22422005-01-11 21:19:59 +00001247 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00001248 MakeReg(N.getValueType()) : 1;
1249 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00001250 case X86ISD::TAILCALL:
1251 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00001252 // If this is a call instruction, make sure to prepare ALL of the result
1253 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00001254 ExprMap[N.getValue(0)] = 1;
1255 if (Node->getNumValues() > 1) {
1256 Result = MakeReg(Node->getValueType(1));
1257 ExprMap[N.getValue(1)] = Result;
1258 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00001259 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00001260 } else {
1261 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00001262 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00001263 break;
1264 case ISD::ADD_PARTS:
1265 case ISD::SUB_PARTS:
1266 case ISD::SHL_PARTS:
1267 case ISD::SRL_PARTS:
1268 case ISD::SRA_PARTS:
1269 Result = MakeReg(Node->getValueType(0));
1270 ExprMap[N.getValue(0)] = Result;
1271 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1272 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1273 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001274 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001275
Chris Lattner88c8a232005-01-07 07:49:41 +00001276 switch (N.getOpcode()) {
1277 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00001278 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00001279 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00001280 case ISD::FP_EXTEND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001281 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman8a093362005-07-06 18:59:04 +00001282 Tmp1 = SelectExpr(N.getOperand(0));
1283 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
1284 return Result;
Nate Begemana0b5e032005-07-15 00:38:55 +00001285 case ISD::FP_ROUND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001286 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemana0b5e032005-07-15 00:38:55 +00001287 Tmp1 = SelectExpr(N.getOperand(0));
1288 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
1289 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001290 case ISD::CopyFromReg:
1291 Select(N.getOperand(0));
1292 if (Result == 1) {
1293 Reg = Result = ExprMap[N.getValue(0)] =
1294 MakeReg(N.getValue(0).getValueType());
1295 }
Chris Lattner7c762782005-08-16 21:56:37 +00001296 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001297 switch (Node->getValueType(0)) {
1298 default: assert(0 && "Cannot CopyFromReg this!");
1299 case MVT::i1:
1300 case MVT::i8:
Chris Lattner7c762782005-08-16 21:56:37 +00001301 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001302 return Result;
1303 case MVT::i16:
Chris Lattner7c762782005-08-16 21:56:37 +00001304 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001305 return Result;
1306 case MVT::i32:
Chris Lattner7c762782005-08-16 21:56:37 +00001307 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001308 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001309 }
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001310
Chris Lattner88c8a232005-01-07 07:49:41 +00001311 case ISD::FrameIndex:
1312 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1313 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1314 return Result;
1315 case ISD::ConstantPool:
Chris Lattnerc30405e2005-08-26 17:15:30 +00001316 Tmp1 = BB->getParent()->getConstantPool()->
1317 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner88c8a232005-01-07 07:49:41 +00001318 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1319 return Result;
1320 case ISD::ConstantFP:
Nate Begeman8d394eb2005-08-03 23:26:28 +00001321 if (X86ScalarSSE) {
1322 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
1323 "SSE only supports +0.0");
1324 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
1325 BuildMI(BB, Opc, 0, Result);
1326 return Result;
1327 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001328 ContainsFPCode = true;
1329 Tmp1 = Result; // Intermediate Register
1330 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1331 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1332 Tmp1 = MakeReg(MVT::f64);
1333
1334 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1335 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
Chris Lattnerf431ad42005-12-21 07:47:04 +00001336 BuildMI(BB, X86::FpLD0, 0, Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001337 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1338 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
Chris Lattnerf431ad42005-12-21 07:47:04 +00001339 BuildMI(BB, X86::FpLD1, 0, Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001340 else
1341 assert(0 && "Unexpected constant!");
1342 if (Tmp1 != Result)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001343 BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00001344 return Result;
1345 case ISD::Constant:
1346 switch (N.getValueType()) {
1347 default: assert(0 && "Cannot use constants of this type!");
1348 case MVT::i1:
1349 case MVT::i8: Opc = X86::MOV8ri; break;
1350 case MVT::i16: Opc = X86::MOV16ri; break;
1351 case MVT::i32: Opc = X86::MOV32ri; break;
1352 }
1353 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1354 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001355 case ISD::UNDEF:
1356 if (Node->getValueType(0) == MVT::f64) {
1357 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
Chris Lattnerf431ad42005-12-21 07:47:04 +00001358 BuildMI(BB, X86::FpLD0, 0, Result);
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001359 } else {
1360 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1361 }
1362 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001363 case ISD::GlobalAddress: {
1364 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00001365 // For Darwin, external and weak symbols are indirect, so we want to load
1366 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001367 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001368 (GV->hasWeakLinkage() || GV->isExternal())) {
1369 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
1370 .addGlobalAddress(GV, false, 0);
1371 } else {
1372 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1373 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001374 return Result;
1375 }
1376 case ISD::ExternalSymbol: {
1377 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1378 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1379 return Result;
1380 }
Chris Lattner210975c2005-09-02 00:16:09 +00001381 case ISD::ANY_EXTEND: // treat any extend like zext
Chris Lattner88c8a232005-01-07 07:49:41 +00001382 case ISD::ZERO_EXTEND: {
1383 int DestIs16 = N.getValueType() == MVT::i16;
1384 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001385
1386 // FIXME: This hack is here for zero extension casts from bool to i8. This
1387 // would not be needed if bools were promoted by Legalize.
1388 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001389 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001390 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1391 return Result;
1392 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001393
Chris Lattnera56d29d2005-01-17 06:26:58 +00001394 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001395 static const unsigned Opc[3] = {
1396 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1397 };
1398
1399 X86AddressMode AM;
1400 EmitFoldedLoad(N.getOperand(0), AM);
1401 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001402
Chris Lattnerb0eef822005-01-11 23:33:00 +00001403 return Result;
1404 }
1405
Chris Lattner88c8a232005-01-07 07:49:41 +00001406 static const unsigned Opc[3] = {
1407 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1408 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001409 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001410 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1411 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001412 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001413 case ISD::SIGN_EXTEND: {
1414 int DestIs16 = N.getValueType() == MVT::i16;
1415 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1416
Chris Lattner282781c2005-01-09 18:52:44 +00001417 // FIXME: Legalize should promote bools to i8!
1418 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1419 "Sign extend from bool not implemented!");
1420
Chris Lattnera56d29d2005-01-17 06:26:58 +00001421 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001422 static const unsigned Opc[3] = {
1423 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1424 };
1425
1426 X86AddressMode AM;
1427 EmitFoldedLoad(N.getOperand(0), AM);
1428 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1429 return Result;
1430 }
1431
Chris Lattner88c8a232005-01-07 07:49:41 +00001432 static const unsigned Opc[3] = {
1433 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1434 };
1435 Tmp1 = SelectExpr(N.getOperand(0));
1436 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1437 return Result;
1438 }
1439 case ISD::TRUNCATE:
1440 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1441 // a move out of AX or AL.
1442 switch (N.getOperand(0).getValueType()) {
1443 default: assert(0 && "Unknown truncate!");
1444 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1445 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1446 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1447 }
1448 Tmp1 = SelectExpr(N.getOperand(0));
1449 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1450
1451 switch (N.getValueType()) {
1452 default: assert(0 && "Unknown truncate!");
1453 case MVT::i1:
1454 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1455 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1456 }
1457 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1458 return Result;
1459
Chris Lattner507a2752005-07-16 00:28:20 +00001460 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00001461 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1462 unsigned PromoteOpcode = 0;
1463
Nate Begeman7e74c832005-07-16 02:02:34 +00001464 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00001465 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00001466 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00001467 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1468 return Result;
1469 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001470
Chris Lattnere44e6d12005-01-11 03:50:45 +00001471 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00001472
Chris Lattner282781c2005-01-09 18:52:44 +00001473 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00001474 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00001475 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1476 MachineFunction *F = BB->getParent();
1477 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1478
1479 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00001480 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00001481 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001482 addFrameReference(BuildMI(BB, X86::FpILD32m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001483 break;
1484 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00001485 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001486 addFrameReference(BuildMI(BB, X86::FpILD16m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001487 break;
1488 default: break; // No promotion required.
1489 }
Chris Lattner507a2752005-07-16 00:28:20 +00001490 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00001491 }
Chris Lattner4738d1b2005-07-30 00:05:54 +00001492 case ISD::FP_TO_SINT:
Chris Lattner282781c2005-01-09 18:52:44 +00001493 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1494
Nate Begeman8a093362005-07-06 18:59:04 +00001495 // If the target supports SSE2 and is performing FP operations in SSE regs
1496 // instead of the FP stack, then we can use the efficient CVTSS2SI and
1497 // CVTSD2SI instructions.
Chris Lattner4738d1b2005-07-30 00:05:54 +00001498 assert(X86ScalarSSE);
1499 if (MVT::f32 == N.getOperand(0).getValueType()) {
1500 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
1501 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
1502 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
1503 } else {
1504 assert(0 && "Not an f32 or f64?");
1505 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001506 }
Chris Lattner282781c2005-01-09 18:52:44 +00001507 return Result;
Chris Lattner4738d1b2005-07-30 00:05:54 +00001508
Chris Lattner0815dcae2005-09-28 22:29:17 +00001509 case ISD::FADD:
Chris Lattner88c8a232005-01-07 07:49:41 +00001510 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00001511 Op0 = N.getOperand(0);
1512 Op1 = N.getOperand(1);
1513
Chris Lattner30607ec2005-01-25 20:03:11 +00001514 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00001515 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001516 goto FoldAdd;
1517 }
Chris Lattner62b22422005-01-11 21:19:59 +00001518
Chris Lattner30607ec2005-01-25 20:03:11 +00001519 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001520 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00001521 switch (N.getValueType()) {
1522 default: assert(0 && "Cannot add this type!");
1523 case MVT::i1:
1524 case MVT::i8: Opc = X86::ADD8rm; break;
1525 case MVT::i16: Opc = X86::ADD16rm; break;
1526 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001527 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001528 case MVT::f64:
1529 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00001530 if (X86ScalarSSE) {
1531 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
1532 Opc = X86::ADDSDrm;
1533 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001534 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FpADD64m : X86::FpADD32m;
Nate Begeman8a093362005-07-06 18:59:04 +00001535 }
Chris Lattner30607ec2005-01-25 20:03:11 +00001536 break;
Chris Lattner62b22422005-01-11 21:19:59 +00001537 }
1538 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001539 EmitFoldedLoad(Op1, AM);
1540 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001541 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1542 return Result;
1543 }
1544
Chris Lattner88c8a232005-01-07 07:49:41 +00001545 // See if we can codegen this as an LEA to fold operations together.
1546 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00001547 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001548 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00001549 MatchAddress(N, AM);
1550 ExprMap[N] = Result;
1551
1552 // If this is not just an add, emit the LEA. For a simple add (like
1553 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1554 // leave this as LEA, then peephole it to 'ADD' after two address elim
1555 // happens.
1556 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1557 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1558 X86AddressMode XAM = SelectAddrExprs(AM);
1559 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1560 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001561 }
1562 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001563
Chris Lattner62b22422005-01-11 21:19:59 +00001564 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001565 Opc = 0;
1566 if (CN->getValue() == 1) { // add X, 1 -> inc X
1567 switch (N.getValueType()) {
1568 default: assert(0 && "Cannot integer add this type!");
1569 case MVT::i8: Opc = X86::INC8r; break;
1570 case MVT::i16: Opc = X86::INC16r; break;
1571 case MVT::i32: Opc = X86::INC32r; break;
1572 }
1573 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1574 switch (N.getValueType()) {
1575 default: assert(0 && "Cannot integer add this type!");
1576 case MVT::i8: Opc = X86::DEC8r; break;
1577 case MVT::i16: Opc = X86::DEC16r; break;
1578 case MVT::i32: Opc = X86::DEC32r; break;
1579 }
1580 }
1581
1582 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001583 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001584 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1585 return Result;
1586 }
1587
1588 switch (N.getValueType()) {
1589 default: assert(0 && "Cannot add this type!");
1590 case MVT::i8: Opc = X86::ADD8ri; break;
1591 case MVT::i16: Opc = X86::ADD16ri; break;
1592 case MVT::i32: Opc = X86::ADD32ri; break;
1593 }
1594 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001595 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001596 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1597 return Result;
1598 }
1599 }
1600
Chris Lattner88c8a232005-01-07 07:49:41 +00001601 switch (N.getValueType()) {
1602 default: assert(0 && "Cannot add this type!");
1603 case MVT::i8: Opc = X86::ADD8rr; break;
1604 case MVT::i16: Opc = X86::ADD16rr; break;
1605 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001606 case MVT::f32: Opc = X86::ADDSSrr; break;
1607 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001608 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001609
Chris Lattner62b22422005-01-11 21:19:59 +00001610 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1611 Tmp1 = SelectExpr(Op0);
1612 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001613 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001614 Tmp2 = SelectExpr(Op1);
1615 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001616 }
1617
Chris Lattner88c8a232005-01-07 07:49:41 +00001618 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1619 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00001620
Nate Begeman8a093362005-07-06 18:59:04 +00001621 case ISD::FSQRT:
1622 Tmp1 = SelectExpr(Node->getOperand(0));
1623 if (X86ScalarSSE) {
1624 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
1625 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1626 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001627 BuildMI(BB, X86::FpSQRT, 1, Result).addReg(Tmp1);
Nate Begeman8a093362005-07-06 18:59:04 +00001628 }
1629 return Result;
1630
1631 // FIXME:
1632 // Once we can spill 16 byte constants into the constant pool, we can
1633 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00001634 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00001635 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00001636 case ISD::FSIN:
1637 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00001638 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00001639 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00001640 switch (N.getOpcode()) {
1641 default: assert(0 && "Unreachable!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00001642 case ISD::FABS: BuildMI(BB, X86::FpABS, 1, Result).addReg(Tmp1); break;
1643 case ISD::FNEG: BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1); break;
1644 case ISD::FSIN: BuildMI(BB, X86::FpSIN, 1, Result).addReg(Tmp1); break;
1645 case ISD::FCOS: BuildMI(BB, X86::FpCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00001646 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00001647 return Result;
1648
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001649 case ISD::MULHU:
1650 switch (N.getValueType()) {
1651 default: assert(0 && "Unsupported VT!");
1652 case MVT::i8: Tmp2 = X86::MUL8r; break;
1653 case MVT::i16: Tmp2 = X86::MUL16r; break;
1654 case MVT::i32: Tmp2 = X86::MUL32r; break;
1655 }
1656 // FALL THROUGH
1657 case ISD::MULHS: {
1658 unsigned MovOpc, LowReg, HiReg;
1659 switch (N.getValueType()) {
1660 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00001661 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001662 MovOpc = X86::MOV8rr;
1663 LowReg = X86::AL;
1664 HiReg = X86::AH;
1665 Opc = X86::IMUL8r;
1666 break;
1667 case MVT::i16:
1668 MovOpc = X86::MOV16rr;
1669 LowReg = X86::AX;
1670 HiReg = X86::DX;
1671 Opc = X86::IMUL16r;
1672 break;
1673 case MVT::i32:
1674 MovOpc = X86::MOV32rr;
1675 LowReg = X86::EAX;
1676 HiReg = X86::EDX;
1677 Opc = X86::IMUL32r;
1678 break;
1679 }
1680 if (Node->getOpcode() != ISD::MULHS)
1681 Opc = Tmp2; // Get the MULHU opcode.
1682
1683 Op0 = Node->getOperand(0);
1684 Op1 = Node->getOperand(1);
1685 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1686 Tmp1 = SelectExpr(Op0);
1687 Tmp2 = SelectExpr(Op1);
1688 } else {
1689 Tmp2 = SelectExpr(Op1);
1690 Tmp1 = SelectExpr(Op0);
1691 }
1692
1693 // FIXME: Implement folding of loads into the memory operands here!
1694 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1695 BuildMI(BB, Opc, 1).addReg(Tmp2);
1696 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1697 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001698 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001699
Chris Lattner0815dcae2005-09-28 22:29:17 +00001700 case ISD::FSUB:
1701 case ISD::FMUL:
Chris Lattner88c8a232005-01-07 07:49:41 +00001702 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00001703 case ISD::MUL:
1704 case ISD::AND:
1705 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00001706 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00001707 static const unsigned SUBTab[] = {
1708 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001709 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FpSUB32m, X86::FpSUB64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001710 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1711 };
Nate Begeman8a093362005-07-06 18:59:04 +00001712 static const unsigned SSE_SUBTab[] = {
1713 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1714 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
1715 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
1716 };
Chris Lattner62b22422005-01-11 21:19:59 +00001717 static const unsigned MULTab[] = {
1718 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001719 0, X86::IMUL16rm , X86::IMUL32rm, X86::FpMUL32m, X86::FpMUL64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001720 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1721 };
Nate Begeman8a093362005-07-06 18:59:04 +00001722 static const unsigned SSE_MULTab[] = {
1723 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1724 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
1725 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
1726 };
Chris Lattner62b22422005-01-11 21:19:59 +00001727 static const unsigned ANDTab[] = {
1728 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1729 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001730 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00001731 };
1732 static const unsigned ORTab[] = {
1733 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1734 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1735 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1736 };
1737 static const unsigned XORTab[] = {
1738 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1739 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1740 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1741 };
1742
1743 Op0 = Node->getOperand(0);
1744 Op1 = Node->getOperand(1);
1745
Chris Lattner29f58192005-01-19 07:37:26 +00001746 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1747 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00001748 return Result;
1749
1750 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00001751 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1752 if (CN->isNullValue()) { // 0 - N -> neg N
1753 switch (N.getValueType()) {
1754 default: assert(0 && "Cannot sub this type!");
1755 case MVT::i1:
1756 case MVT::i8: Opc = X86::NEG8r; break;
1757 case MVT::i16: Opc = X86::NEG16r; break;
1758 case MVT::i32: Opc = X86::NEG32r; break;
1759 }
1760 Tmp1 = SelectExpr(N.getOperand(1));
1761 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1762 return Result;
1763 }
1764
Chris Lattner62b22422005-01-11 21:19:59 +00001765 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1766 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001767 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00001768 switch (N.getValueType()) {
1769 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001770 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00001771 case MVT::i8: Opc = X86::NOT8r; break;
1772 case MVT::i16: Opc = X86::NOT16r; break;
1773 case MVT::i32: Opc = X86::NOT32r; break;
1774 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001775 if (Opc) {
1776 Tmp1 = SelectExpr(Op0);
1777 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1778 return Result;
1779 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00001780 }
1781
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001782 // Fold common multiplies into LEA instructions.
1783 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1784 switch ((int)CN->getValue()) {
1785 default: break;
1786 case 3:
1787 case 5:
1788 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001789 // Remove N from exprmap so SelectAddress doesn't get confused.
1790 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001791 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001792 SelectAddress(N, AM);
1793 // Restore it to the map.
1794 ExprMap[N] = Result;
1795 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1796 return Result;
1797 }
1798 }
1799
Chris Lattner88c8a232005-01-07 07:49:41 +00001800 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00001801 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00001802 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00001803 case MVT::i8: Opc = 0; break;
1804 case MVT::i16: Opc = 1; break;
1805 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001806 }
Chris Lattner62b22422005-01-11 21:19:59 +00001807 switch (Node->getOpcode()) {
1808 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001809 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001810 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001811 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001812 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001813 case ISD::AND: Opc = ANDTab[Opc]; break;
1814 case ISD::OR: Opc = ORTab[Opc]; break;
1815 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001816 }
Chris Lattner62b22422005-01-11 21:19:59 +00001817 if (Opc) { // Can't fold MUL:i8 R, imm
1818 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1820 return Result;
1821 }
1822 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001823
Chris Lattner30607ec2005-01-25 20:03:11 +00001824 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner0815dcae2005-09-28 22:29:17 +00001825 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
Chris Lattner62b22422005-01-11 21:19:59 +00001826 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001827 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00001828 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00001829 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00001830 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001831 if (Op0.getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001832 Opc = X86::FpSUBR32m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001833 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00001834 Opc = X86::FpSUBR64m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001835
Chris Lattner62b22422005-01-11 21:19:59 +00001836 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001837 EmitFoldedLoad(Op0, AM);
1838 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00001839 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1840 return Result;
1841 }
1842 }
1843
Chris Lattner30607ec2005-01-25 20:03:11 +00001844 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001845 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00001846 switch (N.getValueType()) {
1847 default: assert(0 && "Cannot operate on this type!");
1848 case MVT::i1:
1849 case MVT::i8: Opc = 5; break;
1850 case MVT::i16: Opc = 6; break;
1851 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001852 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001853 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001854 case MVT::f64:
1855 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00001856 "SSE load should have been promoted");
1857 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001858 }
1859 switch (Node->getOpcode()) {
1860 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001861 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001862 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001863 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001864 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001865 case ISD::AND: Opc = ANDTab[Opc]; break;
1866 case ISD::OR: Opc = ORTab[Opc]; break;
1867 case ISD::XOR: Opc = XORTab[Opc]; break;
1868 }
1869
1870 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001871 EmitFoldedLoad(Op1, AM);
1872 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001873 if (Opc) {
1874 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1875 } else {
1876 assert(Node->getOpcode() == ISD::MUL &&
1877 N.getValueType() == MVT::i8 && "Unexpected situation!");
1878 // Must use the MUL instruction, which forces use of AL.
1879 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1880 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1881 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1882 }
1883 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001884 }
Chris Lattner62b22422005-01-11 21:19:59 +00001885
1886 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1887 Tmp1 = SelectExpr(Op0);
1888 Tmp2 = SelectExpr(Op1);
1889 } else {
1890 Tmp2 = SelectExpr(Op1);
1891 Tmp1 = SelectExpr(Op0);
1892 }
1893
Chris Lattner88c8a232005-01-07 07:49:41 +00001894 switch (N.getValueType()) {
1895 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00001896 case MVT::i1:
1897 case MVT::i8: Opc = 10; break;
1898 case MVT::i16: Opc = 11; break;
1899 case MVT::i32: Opc = 12; break;
1900 case MVT::f32: Opc = 13; break;
1901 case MVT::f64: Opc = 14; break;
1902 }
1903 switch (Node->getOpcode()) {
1904 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001905 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001906 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001907 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001908 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001909 case ISD::AND: Opc = ANDTab[Opc]; break;
1910 case ISD::OR: Opc = ORTab[Opc]; break;
1911 case ISD::XOR: Opc = XORTab[Opc]; break;
1912 }
1913 if (Opc) {
1914 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1915 } else {
1916 assert(Node->getOpcode() == ISD::MUL &&
1917 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00001918 // Must use the MUL instruction, which forces use of AL.
1919 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1920 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1921 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00001922 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001923 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00001924 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00001925 case ISD::ADD_PARTS:
1926 case ISD::SUB_PARTS: {
1927 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1928 "Not an i64 add/sub!");
1929 // Emit all of the operands.
1930 std::vector<unsigned> InVals;
1931 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1932 InVals.push_back(SelectExpr(N.getOperand(i)));
1933 if (N.getOpcode() == ISD::ADD_PARTS) {
1934 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1935 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1936 } else {
1937 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1938 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1939 }
1940 return Result+N.ResNo;
1941 }
1942
Chris Lattnera31d4c72005-04-02 04:01:14 +00001943 case ISD::SHL_PARTS:
1944 case ISD::SRA_PARTS:
1945 case ISD::SRL_PARTS: {
1946 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1947 "Not an i64 shift!");
1948 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1949 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1950 unsigned TmpReg = MakeReg(MVT::i32);
1951 if (N.getOpcode() == ISD::SRA_PARTS) {
1952 // If this is a SHR of a Long, then we need to do funny sign extension
1953 // stuff. TmpReg gets the value to use as the high-part if we are
1954 // shifting more than 32 bits.
1955 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
1956 } else {
1957 // Other shifts use a fixed zero value if the shift is more than 32 bits.
1958 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
1959 }
1960
1961 // Initialize CL with the shift amount.
1962 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
1963 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
1964
1965 unsigned TmpReg2 = MakeReg(MVT::i32);
1966 unsigned TmpReg3 = MakeReg(MVT::i32);
1967 if (N.getOpcode() == ISD::SHL_PARTS) {
1968 // TmpReg2 = shld inHi, inLo
1969 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
1970 .addReg(ShiftOpLo);
1971 // TmpReg3 = shl inLo, CL
1972 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001973
Chris Lattnera31d4c72005-04-02 04:01:14 +00001974 // Set the flags to indicate whether the shift was by more than 32 bits.
1975 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001976
Chris Lattnera31d4c72005-04-02 04:01:14 +00001977 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001978 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001979 Result+1).addReg(TmpReg2).addReg(TmpReg3);
1980 // DestLo = (>32) ? TmpReg : TmpReg3;
1981 BuildMI(BB, X86::CMOVNE32rr, 2,
1982 Result).addReg(TmpReg3).addReg(TmpReg);
1983 } else {
1984 // TmpReg2 = shrd inLo, inHi
1985 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
1986 .addReg(ShiftOpHi);
1987 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00001988 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00001989 : X86::SHR32rCL, 1, TmpReg3)
1990 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001991
Chris Lattnera31d4c72005-04-02 04:01:14 +00001992 // Set the flags to indicate whether the shift was by more than 32 bits.
1993 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001994
Chris Lattnera31d4c72005-04-02 04:01:14 +00001995 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001996 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001997 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001998
Chris Lattnera31d4c72005-04-02 04:01:14 +00001999 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002000 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002001 Result+1).addReg(TmpReg3).addReg(TmpReg);
2002 }
2003 return Result+N.ResNo;
2004 }
2005
Chris Lattner88c8a232005-01-07 07:49:41 +00002006 case ISD::SELECT:
Nate Begeman8d394eb2005-08-03 23:26:28 +00002007 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
2008 N.getValueType(), Result);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002009 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002010
Chris Lattner0815dcae2005-09-28 22:29:17 +00002011 case ISD::FDIV:
2012 case ISD::FREM:
Chris Lattner88c8a232005-01-07 07:49:41 +00002013 case ISD::SDIV:
2014 case ISD::UDIV:
2015 case ISD::SREM:
2016 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002017 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2018 "We don't support this operator!");
2019
Chris Lattner0815dcae2005-09-28 22:29:17 +00002020 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002021 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00002022 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00002023 // Check for reversed and unreversed DIV.
2024 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2025 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002026 Opc = X86::FpDIVR32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002027 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002028 Opc = X86::FpDIVR64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002029 X86AddressMode AM;
2030 EmitFoldedLoad(N.getOperand(0), AM);
2031 Tmp1 = SelectExpr(N.getOperand(1));
2032 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2033 return Result;
2034 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2035 N.getOperand(1).getOpcode() == ISD::LOAD) {
2036 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002037 Opc = X86::FpDIV32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002038 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002039 Opc = X86::FpDIV64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002040 X86AddressMode AM;
2041 EmitFoldedLoad(N.getOperand(1), AM);
2042 Tmp1 = SelectExpr(N.getOperand(0));
2043 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2044 return Result;
2045 }
2046 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002047 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002048
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002049 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2050 Tmp1 = SelectExpr(N.getOperand(0));
2051 Tmp2 = SelectExpr(N.getOperand(1));
2052 } else {
2053 Tmp2 = SelectExpr(N.getOperand(1));
2054 Tmp1 = SelectExpr(N.getOperand(0));
2055 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002056
2057 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2058 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2059 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2060 switch (N.getValueType()) {
2061 default: assert(0 && "Cannot sdiv this type!");
2062 case MVT::i8:
2063 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2064 LoReg = X86::AL;
2065 HiReg = X86::AH;
2066 MovOpcode = X86::MOV8rr;
2067 ClrOpcode = X86::MOV8ri;
2068 SExtOpcode = X86::CBW;
2069 break;
2070 case MVT::i16:
2071 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2072 LoReg = X86::AX;
2073 HiReg = X86::DX;
2074 MovOpcode = X86::MOV16rr;
2075 ClrOpcode = X86::MOV16ri;
2076 SExtOpcode = X86::CWD;
2077 break;
2078 case MVT::i32:
2079 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002080 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002081 HiReg = X86::EDX;
2082 MovOpcode = X86::MOV32rr;
2083 ClrOpcode = X86::MOV32ri;
2084 SExtOpcode = X86::CDQ;
2085 break;
Nate Begeman8a093362005-07-06 18:59:04 +00002086 case MVT::f32:
2087 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
2088 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002089 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002090 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
2091 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002092 return Result;
2093 }
2094
2095 // Set up the low part.
2096 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2097
2098 if (isSigned) {
2099 // Sign extend the low part into the high part.
2100 BuildMI(BB, SExtOpcode, 0);
2101 } else {
2102 // Zero out the high part, effectively zero extending the input.
2103 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2104 }
2105
2106 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002107 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002108
2109 // Get the result of the divide or rem.
2110 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2111 return Result;
2112 }
2113
2114 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002115 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002116 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2117 switch (N.getValueType()) {
2118 default: assert(0 && "Cannot shift this type!");
2119 case MVT::i8: Opc = X86::ADD8rr; break;
2120 case MVT::i16: Opc = X86::ADD16rr; break;
2121 case MVT::i32: Opc = X86::ADD32rr; break;
2122 }
2123 Tmp1 = SelectExpr(N.getOperand(0));
2124 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2125 return Result;
2126 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002127
Chris Lattner88c8a232005-01-07 07:49:41 +00002128 switch (N.getValueType()) {
2129 default: assert(0 && "Cannot shift this type!");
2130 case MVT::i8: Opc = X86::SHL8ri; break;
2131 case MVT::i16: Opc = X86::SHL16ri; break;
2132 case MVT::i32: Opc = X86::SHL32ri; break;
2133 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002134 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002135 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2136 return Result;
2137 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002138
2139 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2140 Tmp1 = SelectExpr(N.getOperand(0));
2141 Tmp2 = SelectExpr(N.getOperand(1));
2142 } else {
2143 Tmp2 = SelectExpr(N.getOperand(1));
2144 Tmp1 = SelectExpr(N.getOperand(0));
2145 }
2146
Chris Lattner88c8a232005-01-07 07:49:41 +00002147 switch (N.getValueType()) {
2148 default: assert(0 && "Cannot shift this type!");
2149 case MVT::i8 : Opc = X86::SHL8rCL; break;
2150 case MVT::i16: Opc = X86::SHL16rCL; break;
2151 case MVT::i32: Opc = X86::SHL32rCL; break;
2152 }
2153 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002154 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002155 return Result;
2156 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002157 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2158 switch (N.getValueType()) {
2159 default: assert(0 && "Cannot shift this type!");
2160 case MVT::i8: Opc = X86::SHR8ri; break;
2161 case MVT::i16: Opc = X86::SHR16ri; break;
2162 case MVT::i32: Opc = X86::SHR32ri; break;
2163 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002164 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002165 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2166 return Result;
2167 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002168
2169 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2170 Tmp1 = SelectExpr(N.getOperand(0));
2171 Tmp2 = SelectExpr(N.getOperand(1));
2172 } else {
2173 Tmp2 = SelectExpr(N.getOperand(1));
2174 Tmp1 = SelectExpr(N.getOperand(0));
2175 }
2176
Chris Lattner88c8a232005-01-07 07:49:41 +00002177 switch (N.getValueType()) {
2178 default: assert(0 && "Cannot shift this type!");
2179 case MVT::i8 : Opc = X86::SHR8rCL; break;
2180 case MVT::i16: Opc = X86::SHR16rCL; break;
2181 case MVT::i32: Opc = X86::SHR32rCL; break;
2182 }
2183 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002184 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002185 return Result;
2186 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002187 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2188 switch (N.getValueType()) {
2189 default: assert(0 && "Cannot shift this type!");
2190 case MVT::i8: Opc = X86::SAR8ri; break;
2191 case MVT::i16: Opc = X86::SAR16ri; break;
2192 case MVT::i32: Opc = X86::SAR32ri; break;
2193 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002194 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002195 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2196 return Result;
2197 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002198
2199 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2200 Tmp1 = SelectExpr(N.getOperand(0));
2201 Tmp2 = SelectExpr(N.getOperand(1));
2202 } else {
2203 Tmp2 = SelectExpr(N.getOperand(1));
2204 Tmp1 = SelectExpr(N.getOperand(0));
2205 }
2206
Chris Lattner88c8a232005-01-07 07:49:41 +00002207 switch (N.getValueType()) {
2208 default: assert(0 && "Cannot shift this type!");
2209 case MVT::i8 : Opc = X86::SAR8rCL; break;
2210 case MVT::i16: Opc = X86::SAR16rCL; break;
2211 case MVT::i32: Opc = X86::SAR32rCL; break;
2212 }
2213 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnera9d68f12005-08-19 00:31:37 +00002214 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002215 return Result;
2216
2217 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002218 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner6ec77452005-08-09 20:21:10 +00002219 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner88c8a232005-01-07 07:49:41 +00002220 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2221 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002222 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002223 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002224 if (Result != 1) { // Generate the token
2225 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2226 assert(0 && "Load already emitted!?");
2227 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002228 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2229
Chris Lattnerb52e0412005-01-08 19:28:19 +00002230 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002231 default: assert(0 && "Cannot load this type!");
2232 case MVT::i1:
2233 case MVT::i8: Opc = X86::MOV8rm; break;
2234 case MVT::i16: Opc = X86::MOV16rm; break;
2235 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002236 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002237 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002238 if (X86ScalarSSE) {
2239 Opc = X86::MOVSDrm;
2240 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00002241 Opc = X86::FpLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002242 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00002243 }
2244 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002245 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002246
Chris Lattner88c8a232005-01-07 07:49:41 +00002247 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattnerc30405e2005-08-26 17:15:30 +00002248 unsigned CPIdx = BB->getParent()->getConstantPool()->
2249 getConstantPoolIndex(CP->get());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002250 Select(N.getOperand(0));
Chris Lattnerc30405e2005-08-26 17:15:30 +00002251 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner88c8a232005-01-07 07:49:41 +00002252 } else {
2253 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002254
2255 SDOperand Chain = N.getOperand(0);
2256 SDOperand Address = N.getOperand(1);
2257 if (getRegPressure(Chain) > getRegPressure(Address)) {
2258 Select(Chain);
2259 SelectAddress(Address, AM);
2260 } else {
2261 SelectAddress(Address, AM);
2262 Select(Chain);
2263 }
2264
Chris Lattner88c8a232005-01-07 07:49:41 +00002265 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2266 }
2267 return Result;
Evan Cheng6305e502006-01-12 22:54:21 +00002268 case X86ISD::FILD:
Chris Lattnera36117b2005-05-14 06:52:07 +00002269 // Make sure we generate both values.
2270 assert(Result != 1 && N.getValueType() == MVT::f64);
2271 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2272 assert(0 && "Load already emitted!?");
2273
2274 {
2275 X86AddressMode AM;
2276
2277 SDOperand Chain = N.getOperand(0);
2278 SDOperand Address = N.getOperand(1);
2279 if (getRegPressure(Chain) > getRegPressure(Address)) {
2280 Select(Chain);
2281 SelectAddress(Address, AM);
2282 } else {
2283 SelectAddress(Address, AM);
2284 Select(Chain);
2285 }
Chris Lattner67756e22005-07-29 00:40:01 +00002286
Chris Lattnerf431ad42005-12-21 07:47:04 +00002287 addFullAddress(BuildMI(BB, X86::FpILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00002288 }
2289 return Result;
Jeff Cohen546fd592005-07-30 18:33:25 +00002290
Chris Lattnere18a4c42005-01-15 05:22:24 +00002291 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2292 case ISD::ZEXTLOAD: {
2293 // Make sure we generate both values.
2294 if (Result != 1)
2295 ExprMap[N.getValue(1)] = 1; // Generate the token
2296 else
2297 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2298
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002299 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2300 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00002301 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002302 "Bad EXTLOAD!");
Chris Lattnerc30405e2005-08-26 17:15:30 +00002303 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00002304 getConstantPoolIndex(CP->get());
Chris Lattnerc30405e2005-08-26 17:15:30 +00002305
Chris Lattnerf431ad42005-12-21 07:47:04 +00002306 addConstantPoolReference(BuildMI(BB, X86::FpLD32m, 4, Result), CPIdx);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002307 return Result;
2308 }
2309
Chris Lattnere18a4c42005-01-15 05:22:24 +00002310 X86AddressMode AM;
2311 if (getRegPressure(Node->getOperand(0)) >
2312 getRegPressure(Node->getOperand(1))) {
2313 Select(Node->getOperand(0)); // chain
2314 SelectAddress(Node->getOperand(1), AM);
2315 } else {
2316 SelectAddress(Node->getOperand(1), AM);
2317 Select(Node->getOperand(0)); // chain
2318 }
2319
2320 switch (Node->getValueType(0)) {
2321 default: assert(0 && "Unknown type to sign extend to.");
2322 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00002323 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002324 "Bad EXTLOAD!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00002325 addFullAddress(BuildMI(BB, X86::FpLD32m, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002326 break;
2327 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002328 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002329 default:
2330 assert(0 && "Bad zero extend!");
2331 case MVT::i1:
2332 case MVT::i8:
2333 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2334 break;
2335 case MVT::i16:
2336 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2337 break;
2338 }
2339 break;
2340 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002341 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002342 "Bad zero extend!");
Evan Cheng023aef22005-12-14 22:28:18 +00002343 addFullAddress(BuildMI(BB, X86::MOVZX16rm8, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002344 break;
2345 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00002346 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002347 "Bad zero extend!");
2348 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2349 break;
2350 }
2351 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002352 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002353 case ISD::SEXTLOAD: {
2354 // Make sure we generate both values.
2355 if (Result != 1)
2356 ExprMap[N.getValue(1)] = 1; // Generate the token
2357 else
2358 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2359
2360 X86AddressMode AM;
2361 if (getRegPressure(Node->getOperand(0)) >
2362 getRegPressure(Node->getOperand(1))) {
2363 Select(Node->getOperand(0)); // chain
2364 SelectAddress(Node->getOperand(1), AM);
2365 } else {
2366 SelectAddress(Node->getOperand(1), AM);
2367 Select(Node->getOperand(0)); // chain
2368 }
2369
2370 switch (Node->getValueType(0)) {
2371 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2372 default: assert(0 && "Unknown type to sign extend to.");
2373 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002374 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002375 default:
2376 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2377 case MVT::i8:
2378 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2379 break;
2380 case MVT::i16:
2381 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2382 break;
2383 }
2384 break;
2385 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002386 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002387 "Cannot sign extend from bool!");
2388 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2389 break;
2390 }
2391 return Result;
2392 }
2393
Chris Lattner1b3520c2005-05-14 08:48:15 +00002394 case X86ISD::TAILCALL:
2395 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00002396 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002397 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00002398
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002399 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
2400 isa<ExternalSymbolSDNode>(N.getOperand(1));
2401 unsigned Callee = 0;
2402 if (isDirect) {
2403 Select(N.getOperand(0));
2404 } else {
2405 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2406 Select(N.getOperand(0));
2407 Callee = SelectExpr(N.getOperand(1));
2408 } else {
2409 Callee = SelectExpr(N.getOperand(1));
2410 Select(N.getOperand(0));
2411 }
2412 }
2413
2414 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002415 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002416 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002417 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002418 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00002419 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002420
Chris Lattner1b3520c2005-05-14 08:48:15 +00002421 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002422 default: assert(0 && "Bad thing to pass in regs");
2423 case MVT::i1:
2424 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
2425 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
2426 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
2427 }
2428 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00002429 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002430 default: assert(0 && "Bad thing to pass in regs");
2431 case MVT::i1:
2432 case MVT::i8:
2433 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
2434 break;
2435 case MVT::i16:
2436 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
2437 break;
2438 case MVT::i32:
2439 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
2440 break;
2441 }
2442 }
2443
Chris Lattner88c8a232005-01-07 07:49:41 +00002444 if (GlobalAddressSDNode *GASD =
2445 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
2446 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2447 } else if (ExternalSymbolSDNode *ESSDN =
2448 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
2449 BuildMI(BB, X86::CALLpcrel32,
2450 1).addExternalSymbol(ESSDN->getSymbol(), true);
2451 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002452 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2453 Select(N.getOperand(0));
2454 Tmp1 = SelectExpr(N.getOperand(1));
2455 } else {
2456 Tmp1 = SelectExpr(N.getOperand(1));
2457 Select(N.getOperand(0));
2458 }
2459
Chris Lattner88c8a232005-01-07 07:49:41 +00002460 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2461 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002462
2463 // Get caller stack amount and amount the callee added to the stack pointer.
2464 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
2465 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
2466 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
2467
2468 if (Node->getNumValues() != 1)
2469 switch (Node->getValueType(1)) {
2470 default: assert(0 && "Unknown value type for call result!");
2471 case MVT::Other: return 1;
2472 case MVT::i1:
2473 case MVT::i8:
2474 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2475 break;
2476 case MVT::i16:
2477 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2478 break;
2479 case MVT::i32:
2480 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2481 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
2482 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2483 break;
2484 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00002485 if (X86ScalarSSE) {
2486 ContainsFPCode = true;
2487 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
2488
2489 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
2490 MachineFunction *F = BB->getParent();
2491 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerf431ad42005-12-21 07:47:04 +00002492 addFrameReference(BuildMI(BB, X86::FpST64m, 5), FrameIdx).addReg(X86::FP0);
Nate Begeman8a093362005-07-06 18:59:04 +00002493 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
2494 break;
2495 } else {
2496 ContainsFPCode = true;
2497 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2498 break;
2499 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002500 }
2501 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002502 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00002503 case ISD::READPORT:
2504 // First, determine that the size of the operand falls within the acceptable
2505 // range for this architecture.
2506 //
2507 if (Node->getOperand(1).getValueType() != MVT::i16) {
2508 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2509 exit(1);
2510 }
2511
2512 // Make sure we generate both values.
2513 if (Result != 1) { // Generate the token
2514 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2515 assert(0 && "readport already emitted!?");
2516 } else
2517 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002518
Chris Lattner70ea07c2005-05-09 21:17:38 +00002519 Select(Node->getOperand(0)); // Select the chain.
2520
2521 // If the port is a single-byte constant, use the immediate form.
2522 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2523 if ((Port->getValue() & 255) == Port->getValue()) {
2524 switch (Node->getValueType(0)) {
2525 case MVT::i8:
2526 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2527 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2528 return Result;
2529 case MVT::i16:
2530 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2531 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2532 return Result;
2533 case MVT::i32:
2534 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2535 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2536 return Result;
2537 default: break;
2538 }
2539 }
2540
2541 // Now, move the I/O port address into the DX register and use the IN
2542 // instruction to get the input data.
2543 //
2544 Tmp1 = SelectExpr(Node->getOperand(1));
2545 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2546 switch (Node->getValueType(0)) {
2547 case MVT::i8:
2548 BuildMI(BB, X86::IN8rr, 0);
2549 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2550 return Result;
2551 case MVT::i16:
2552 BuildMI(BB, X86::IN16rr, 0);
2553 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2554 return Result;
2555 case MVT::i32:
2556 BuildMI(BB, X86::IN32rr, 0);
2557 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2558 return Result;
2559 default:
2560 std::cerr << "Cannot do input on this data type";
2561 exit(1);
2562 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002563
Chris Lattner88c8a232005-01-07 07:49:41 +00002564 }
2565
2566 return 0;
2567}
2568
Chris Lattner96113fd2005-01-17 19:25:26 +00002569/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2570/// load/op/store instruction. If successful return true.
2571bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2572 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2573 SDOperand Chain = Node->getOperand(0);
2574 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00002575 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00002576
2577 // The chain has to be a load, the stored value must be an integer binary
2578 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00002579 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00002580 MVT::isFloatingPoint(StVal.getValueType()))
2581 return false;
2582
Chris Lattnere86c9332005-01-17 22:10:42 +00002583 // Token chain must either be a factor node or the load to fold.
2584 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2585 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00002586
Chris Lattnere86c9332005-01-17 22:10:42 +00002587 SDOperand TheLoad;
2588
2589 // Check to see if there is a load from the same pointer that we're storing
2590 // to in either operand of the binop.
2591 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2592 StVal.getOperand(0).getOperand(1) == StPtr)
2593 TheLoad = StVal.getOperand(0);
2594 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2595 StVal.getOperand(1).getOperand(1) == StPtr)
2596 TheLoad = StVal.getOperand(1);
2597 else
2598 return false; // No matching load operand.
2599
2600 // We can only fold the load if there are no intervening side-effecting
2601 // operations. This means that the store uses the load as its token chain, or
2602 // there are only token factor nodes in between the store and load.
2603 if (Chain != TheLoad.getValue(1)) {
2604 // Okay, the other option is that we have a store referring to (possibly
2605 // nested) token factor nodes. For now, just try peeking through one level
2606 // of token factors to see if this is the case.
2607 bool ChainOk = false;
2608 if (Chain.getOpcode() == ISD::TokenFactor) {
2609 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2610 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2611 ChainOk = true;
2612 break;
2613 }
2614 }
2615
2616 if (!ChainOk) return false;
2617 }
2618
2619 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00002620 return false;
2621
2622 // Make sure that one of the operands of the binop is the load, and that the
2623 // load folds into the binop.
2624 if (((StVal.getOperand(0) != TheLoad ||
2625 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2626 (StVal.getOperand(1) != TheLoad ||
2627 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2628 return false;
2629
2630 // Finally, check to see if this is one of the ops we can handle!
2631 static const unsigned ADDTAB[] = {
2632 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2633 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2634 };
2635 static const unsigned SUBTAB[] = {
2636 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2637 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2638 };
2639 static const unsigned ANDTAB[] = {
2640 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2641 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2642 };
2643 static const unsigned ORTAB[] = {
2644 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2645 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2646 };
2647 static const unsigned XORTAB[] = {
2648 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2649 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2650 };
2651 static const unsigned SHLTAB[] = {
2652 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2653 /*Have to put the reg in CL*/0, 0, 0,
2654 };
2655 static const unsigned SARTAB[] = {
2656 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2657 /*Have to put the reg in CL*/0, 0, 0,
2658 };
2659 static const unsigned SHRTAB[] = {
2660 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2661 /*Have to put the reg in CL*/0, 0, 0,
2662 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00002663
Chris Lattner96113fd2005-01-17 19:25:26 +00002664 const unsigned *TabPtr = 0;
2665 switch (StVal.getOpcode()) {
2666 default:
2667 std::cerr << "CANNOT [mem] op= val: ";
2668 StVal.Val->dump(); std::cerr << "\n";
Chris Lattner0815dcae2005-09-28 22:29:17 +00002669 case ISD::FMUL:
Chris Lattner96113fd2005-01-17 19:25:26 +00002670 case ISD::MUL:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002671 case ISD::FDIV:
Chris Lattner96113fd2005-01-17 19:25:26 +00002672 case ISD::SDIV:
2673 case ISD::UDIV:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002674 case ISD::FREM:
Chris Lattner96113fd2005-01-17 19:25:26 +00002675 case ISD::SREM:
2676 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002677
Chris Lattner96113fd2005-01-17 19:25:26 +00002678 case ISD::ADD: TabPtr = ADDTAB; break;
2679 case ISD::SUB: TabPtr = SUBTAB; break;
2680 case ISD::AND: TabPtr = ANDTAB; break;
2681 case ISD:: OR: TabPtr = ORTAB; break;
2682 case ISD::XOR: TabPtr = XORTAB; break;
2683 case ISD::SHL: TabPtr = SHLTAB; break;
2684 case ISD::SRA: TabPtr = SARTAB; break;
2685 case ISD::SRL: TabPtr = SHRTAB; break;
2686 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002687
Chris Lattner96113fd2005-01-17 19:25:26 +00002688 // Handle: [mem] op= CST
2689 SDOperand Op0 = StVal.getOperand(0);
2690 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00002691 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00002692 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2693 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2694 default: break;
2695 case MVT::i1:
2696 case MVT::i8: Opc = TabPtr[0]; break;
2697 case MVT::i16: Opc = TabPtr[1]; break;
2698 case MVT::i32: Opc = TabPtr[2]; break;
2699 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002700
Chris Lattner96113fd2005-01-17 19:25:26 +00002701 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00002702 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2703 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002704 Select(Chain);
2705
Chris Lattner96113fd2005-01-17 19:25:26 +00002706 X86AddressMode AM;
2707 if (getRegPressure(TheLoad.getOperand(0)) >
2708 getRegPressure(TheLoad.getOperand(1))) {
2709 Select(TheLoad.getOperand(0));
2710 SelectAddress(TheLoad.getOperand(1), AM);
2711 } else {
2712 SelectAddress(TheLoad.getOperand(1), AM);
2713 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00002714 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002715
2716 if (StVal.getOpcode() == ISD::ADD) {
2717 if (CN->getValue() == 1) {
2718 switch (Op0.getValueType()) {
2719 default: break;
2720 case MVT::i8:
2721 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2722 return true;
2723 case MVT::i16: Opc = TabPtr[1];
2724 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2725 return true;
2726 case MVT::i32: Opc = TabPtr[2];
2727 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2728 return true;
2729 }
2730 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2731 switch (Op0.getValueType()) {
2732 default: break;
2733 case MVT::i8:
2734 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2735 return true;
2736 case MVT::i16: Opc = TabPtr[1];
2737 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2738 return true;
2739 case MVT::i32: Opc = TabPtr[2];
2740 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2741 return true;
2742 }
2743 }
2744 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002745
Chris Lattner96113fd2005-01-17 19:25:26 +00002746 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2747 return true;
2748 }
2749 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002750
Chris Lattner96113fd2005-01-17 19:25:26 +00002751 // If we have [mem] = V op [mem], try to turn it into:
2752 // [mem] = [mem] op V.
Chris Lattner0815dcae2005-09-28 22:29:17 +00002753 if (Op1 == TheLoad &&
2754 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
Chris Lattner96113fd2005-01-17 19:25:26 +00002755 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2756 StVal.getOpcode() != ISD::SRL)
2757 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002758
Chris Lattner96113fd2005-01-17 19:25:26 +00002759 if (Op0 != TheLoad) return false;
2760
2761 switch (Op0.getValueType()) {
2762 default: return false;
2763 case MVT::i1:
2764 case MVT::i8: Opc = TabPtr[3]; break;
2765 case MVT::i16: Opc = TabPtr[4]; break;
2766 case MVT::i32: Opc = TabPtr[5]; break;
2767 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002768
Chris Lattner479c7112005-01-18 17:35:28 +00002769 // Table entry doesn't exist?
2770 if (Opc == 0) return false;
2771
Chris Lattner78d30282005-01-18 03:51:59 +00002772 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2773 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002774 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00002775 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00002776
Chris Lattner96113fd2005-01-17 19:25:26 +00002777 X86AddressMode AM;
2778 SelectAddress(TheLoad.getOperand(1), AM);
2779 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002780 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00002781 return true;
2782}
2783
Chris Lattnerdd66a412005-05-15 05:46:45 +00002784/// If node is a ret(tailcall) node, emit the specified tail call and return
2785/// true, otherwise return false.
2786///
2787/// FIXME: This whole thing should be a post-legalize optimization pass which
2788/// recognizes and transforms the dag. We don't want the selection phase doing
2789/// this stuff!!
2790///
2791bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
2792 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
2793
2794 SDOperand Chain = RetNode->getOperand(0);
2795
2796 // If this is a token factor node where one operand is a call, dig into it.
2797 SDOperand TokFactor;
2798 unsigned TokFactorOperand = 0;
2799 if (Chain.getOpcode() == ISD::TokenFactor) {
2800 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2801 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
2802 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
2803 TokFactorOperand = i;
2804 TokFactor = Chain;
2805 Chain = Chain.getOperand(i);
2806 break;
2807 }
2808 if (TokFactor.Val == 0) return false; // No call operand.
2809 }
2810
2811 // Skip the CALLSEQ_END node if present.
2812 if (Chain.getOpcode() == ISD::CALLSEQ_END)
2813 Chain = Chain.getOperand(0);
2814
2815 // Is a tailcall the last control operation that occurs before the return?
2816 if (Chain.getOpcode() != X86ISD::TAILCALL)
2817 return false;
2818
2819 // If we return a value, is it the value produced by the call?
2820 if (RetNode->getNumOperands() > 1) {
2821 // Not returning the ret val of the call?
2822 if (Chain.Val->getNumValues() == 1 ||
2823 RetNode->getOperand(1) != Chain.getValue(1))
2824 return false;
2825
2826 if (RetNode->getNumOperands() > 2) {
2827 if (Chain.Val->getNumValues() == 2 ||
2828 RetNode->getOperand(2) != Chain.getValue(2))
2829 return false;
2830 }
2831 assert(RetNode->getNumOperands() <= 3);
2832 }
2833
2834 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
2835 // For FastCC, this will always be > 0.
2836 unsigned CalleeCallArgAmt =
2837 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
2838
2839 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
2840 // callee. For FastCC this will always be > 0, for CCC this is always 0.
2841 unsigned CalleeCallArgPopAmt =
2842 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
2843
2844 // There are several cases we can handle here. First, if the caller and
2845 // callee are both CCC functions, we can tailcall if the callee takes <= the
2846 // number of argument bytes that the caller does.
2847 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
2848 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
2849 // Check to see if caller arg area size >= callee arg area size.
2850 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
2851 //std::cerr << "CCC TAILCALL UNIMP!\n";
2852 // If TokFactor is non-null, emit all operands.
2853
2854 //EmitCCCToCCCTailCall(Chain.Val);
2855 //return true;
2856 }
2857 return false;
2858 }
2859
2860 // Second, if both are FastCC functions, we can always perform the tail call.
2861 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
2862 // If TokFactor is non-null, emit all operands before the call.
2863 if (TokFactor.Val) {
2864 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
2865 if (i != TokFactorOperand)
2866 Select(TokFactor.getOperand(i));
2867 }
2868
2869 EmitFastCCToFastCCTailCall(Chain.Val);
2870 return true;
2871 }
2872
2873 // We don't support mixed calls, due to issues with alignment. We could in
2874 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
2875 // aligned (which depends on the number of arguments to the callee). TODO.
2876 return false;
2877}
2878
2879static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
2880 SelectionDAG &DAG) {
2881 MVT::ValueType StoreVT;
2882 switch (Chain.getOpcode()) {
Chris Lattnerc1469402005-08-25 00:05:15 +00002883 default: assert(0 && "Unexpected node!");
Chris Lattnerdd66a412005-05-15 05:46:45 +00002884 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00002885 // If we found the start of the call sequence, we're done. We actually
2886 // strip off the CALLSEQ_START node, to avoid generating the
2887 // ADJCALLSTACKDOWN marker for the tail call.
2888 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002889 case ISD::TokenFactor: {
2890 std::vector<SDOperand> Ops;
2891 Ops.reserve(Chain.getNumOperands());
2892 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2893 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
2894 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
2895 }
2896 case ISD::STORE: // Normal store
2897 StoreVT = Chain.getOperand(1).getValueType();
2898 break;
2899 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00002900 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00002901 break;
2902 }
2903
2904 SDOperand OrigDest = Chain.getOperand(2);
2905 unsigned OrigOffset;
2906
2907 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
2908 OrigOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +00002909 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattnerbc7226a2006-01-25 08:00:36 +00002910 } else if (OrigDest.getOpcode() == ISD::ADD &&
2911 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
2912 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
2913 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
2914 == X86::ESP) {
Chris Lattnerdd66a412005-05-15 05:46:45 +00002915 // We expect only (ESP+C)
Chris Lattnerbc7226a2006-01-25 08:00:36 +00002916 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2917 } else if (OrigDest.getOpcode() == ISD::Register) {
2918 // We expect only (ESP+C)
2919 OrigOffset = 0;
2920 } else {
Chris Lattnerdd66a412005-05-15 05:46:45 +00002921 assert(OrigDest.getOpcode() == ISD::ADD &&
2922 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
Chris Lattnerbc7226a2006-01-25 08:00:36 +00002923 OrigDest.getOperand(0).getOpcode() == ISD::Register &&
2924 cast<RegisterSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002925 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2926 }
2927
2928 // Compute the new offset from the incoming ESP value we wish to use.
2929 unsigned NewOffset = OrigOffset + Offset;
2930
2931 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
2932 MachineFunction &MF = DAG.getMachineFunction();
2933 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
2934 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
2935
2936 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
2937 DAG);
2938 if (Chain.getOpcode() == ISD::STORE)
2939 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
2940 FIN);
2941 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
2942 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00002943 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00002944}
2945
2946
2947/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
2948/// fastcc function from a fastcc function, emit the code to emit a 'proper'
2949/// tail call.
2950void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
2951 unsigned CalleeCallArgSize =
2952 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
2953 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
2954
2955 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
2956
2957 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
2958 // indexes that are relative to the incoming ESP. If the incoming and
2959 // outgoing arg sizes are the same we will store to [InESP] instead of
2960 // [CurESP] and the ESP referenced will be relative to the incoming function
2961 // ESP.
2962 int ESPOffset = CallerArgSize-CalleeCallArgSize;
2963 SDOperand AdjustedArgStores =
2964 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
2965
2966 // Copy the return address of the caller into a virtual register so we don't
2967 // clobber it.
Chris Lattnerefbb8da2006-01-06 17:56:38 +00002968 SDOperand RetVal(0, 0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002969 if (ESPOffset) {
2970 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
2971 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
2972 RetValAddr, TheDAG->getSrcValue(NULL));
2973 SelectExpr(RetVal);
2974 }
2975
2976 // Codegen all of the argument stores.
2977 Select(AdjustedArgStores);
2978
2979 if (RetVal.Val) {
2980 // Emit a store of the saved ret value to the new location.
2981 MachineFunction &MF = TheDAG->getMachineFunction();
2982 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
2983 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
2984 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
2985 RetVal, RetValAddr));
2986 }
2987
2988 // Get the destination value.
2989 SDOperand Callee = TailCallNode->getOperand(1);
2990 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
2991 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00002992 unsigned CalleeReg = 0;
Chris Lattner7e792922005-12-04 06:03:50 +00002993 if (!isDirect) {
2994 // If this is not a direct tail call, evaluate the callee's address.
2995 CalleeReg = SelectExpr(Callee);
2996 }
Chris Lattnerdd66a412005-05-15 05:46:45 +00002997
2998 unsigned RegOp1 = 0;
2999 unsigned RegOp2 = 0;
3000
3001 if (TailCallNode->getNumOperands() > 4) {
3002 // The first value is passed in (a part of) EAX, the second in EDX.
3003 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
3004 if (TailCallNode->getNumOperands() > 5)
3005 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003006
Chris Lattnerdd66a412005-05-15 05:46:45 +00003007 switch (TailCallNode->getOperand(4).getValueType()) {
3008 default: assert(0 && "Bad thing to pass in regs");
3009 case MVT::i1:
3010 case MVT::i8:
3011 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
3012 RegOp1 = X86::AL;
3013 break;
3014 case MVT::i16:
3015 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
3016 RegOp1 = X86::AX;
3017 break;
3018 case MVT::i32:
3019 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
3020 RegOp1 = X86::EAX;
3021 break;
3022 }
3023 if (RegOp2)
3024 switch (TailCallNode->getOperand(5).getValueType()) {
3025 default: assert(0 && "Bad thing to pass in regs");
3026 case MVT::i1:
3027 case MVT::i8:
3028 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
3029 RegOp2 = X86::DL;
3030 break;
3031 case MVT::i16:
3032 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3033 RegOp2 = X86::DX;
3034 break;
3035 case MVT::i32:
3036 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3037 RegOp2 = X86::EDX;
3038 break;
3039 }
3040 }
Chris Lattner7e792922005-12-04 06:03:50 +00003041
3042 // If this is not a direct tail call, put the callee's address into ECX.
3043 // The address has to be evaluated into a non-callee save register that is
3044 // not used for arguments. This means either ECX, as EAX and EDX may be
3045 // used for argument passing. We do this here to make sure that the
3046 // expressions for arguments and callee are all evaluated before the copies
3047 // into physical registers.
3048 if (!isDirect)
3049 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CalleeReg);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003050
3051 // Adjust ESP.
3052 if (ESPOffset)
3053 BuildMI(BB, X86::ADJSTACKPTRri, 2,
3054 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
3055
3056 // TODO: handle jmp [mem]
3057 if (!isDirect) {
Chris Lattner7e792922005-12-04 06:03:50 +00003058 BuildMI(BB, X86::TAILJMPr, 1).addReg(X86::ECX);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003059 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00003060 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003061 } else {
3062 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
3063 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
3064 }
3065 // ADD IMPLICIT USE RegOp1/RegOp2's
3066}
3067
Chris Lattner96113fd2005-01-17 19:25:26 +00003068
Chris Lattner88c8a232005-01-07 07:49:41 +00003069void ISel::Select(SDOperand N) {
Chris Lattner9982da22005-10-02 16:29:36 +00003070 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
Chris Lattner88c8a232005-01-07 07:49:41 +00003071
Nate Begeman95210522005-03-24 04:39:54 +00003072 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003073 return; // Already selected.
3074
Chris Lattner36f78482005-01-11 06:14:36 +00003075 SDNode *Node = N.Val;
3076
3077 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003078 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003079 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003080 assert(0 && "Node not handled yet!");
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00003081 case X86ISD::RDTSC_DAG:
3082 Select(Node->getOperand(0)); //Chain
3083 BuildMI(BB, X86::RDTSC, 0);
3084 return;
3085
Chris Lattner88c8a232005-01-07 07:49:41 +00003086 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003087 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003088 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003089 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003090 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3091 Select(Node->getOperand(OneFirst));
3092 Select(Node->getOperand(!OneFirst));
3093 } else {
3094 std::vector<std::pair<unsigned, unsigned> > OpsP;
3095 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3096 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3097 std::sort(OpsP.begin(), OpsP.end());
3098 std::reverse(OpsP.begin(), OpsP.end());
3099 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3100 Select(Node->getOperand(OpsP[i].second));
3101 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003102 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003103 case ISD::CopyToReg:
Chris Lattner7c762782005-08-16 21:56:37 +00003104 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattner2cfce682005-01-12 02:02:48 +00003105 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00003106 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003107 } else {
Chris Lattner7c762782005-08-16 21:56:37 +00003108 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003109 Select(N.getOperand(0));
3110 }
Chris Lattner7c762782005-08-16 21:56:37 +00003111 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003112
Chris Lattner88c8a232005-01-07 07:49:41 +00003113 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00003114 switch (N.getOperand(2).getValueType()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003115 default: assert(0 && "Invalid type for operation!");
3116 case MVT::i1:
3117 case MVT::i8: Opc = X86::MOV8rr; break;
3118 case MVT::i16: Opc = X86::MOV16rr; break;
3119 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman9d7008b2005-10-14 22:06:00 +00003120 case MVT::f32: Opc = X86::MOVSSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003121 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003122 if (X86ScalarSSE) {
Nate Begeman9d7008b2005-10-14 22:06:00 +00003123 Opc = X86::MOVSDrr;
Nate Begeman8a093362005-07-06 18:59:04 +00003124 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003125 Opc = X86::FpMOV;
3126 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003127 }
3128 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003129 }
3130 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3131 }
3132 return;
3133 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00003134 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
3135 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
3136 N.getOperand(0).getOpcode() == ISD::TokenFactor)
3137 if (EmitPotentialTailCall(Node))
3138 return;
3139
Chris Lattner88c8a232005-01-07 07:49:41 +00003140 switch (N.getNumOperands()) {
3141 default:
3142 assert(0 && "Unknown return instruction!");
3143 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003144 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003145 N.getOperand(2).getValueType() == MVT::i32 &&
3146 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003147 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3148 Tmp1 = SelectExpr(N.getOperand(1));
3149 Tmp2 = SelectExpr(N.getOperand(2));
3150 } else {
3151 Tmp2 = SelectExpr(N.getOperand(2));
3152 Tmp1 = SelectExpr(N.getOperand(1));
3153 }
3154 Select(N.getOperand(0));
3155
Chris Lattner88c8a232005-01-07 07:49:41 +00003156 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3157 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003158 break;
3159 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003160 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3161 Select(N.getOperand(0));
3162 Tmp1 = SelectExpr(N.getOperand(1));
3163 } else {
3164 Tmp1 = SelectExpr(N.getOperand(1));
3165 Select(N.getOperand(0));
3166 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003167 switch (N.getOperand(1).getValueType()) {
3168 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00003169 case MVT::f32:
3170 if (X86ScalarSSE) {
3171 // Spill the value to memory and reload it into top of stack.
3172 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
3173 MachineFunction *F = BB->getParent();
3174 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3175 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003176 addFrameReference(BuildMI(BB, X86::FpLD32m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003177 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003178 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003179 } else {
3180 assert(0 && "MVT::f32 only legal with scalar sse fp");
3181 abort();
3182 }
3183 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003184 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003185 if (X86ScalarSSE) {
3186 // Spill the value to memory and reload it into top of stack.
3187 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3188 MachineFunction *F = BB->getParent();
3189 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3190 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003191 addFrameReference(BuildMI(BB, X86::FpLD64m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003192 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003193 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003194 } else {
3195 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
3196 }
3197 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003198 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00003199 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3200 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003201 }
3202 break;
3203 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003204 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003205 break;
3206 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003207 if (X86Lowering.getBytesToPopOnReturn() == 0)
3208 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3209 else
3210 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00003211 return;
3212 case ISD::BR: {
3213 Select(N.getOperand(0));
3214 MachineBasicBlock *Dest =
3215 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3216 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3217 return;
3218 }
3219
3220 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003221 MachineBasicBlock *Dest =
3222 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003223
Chris Lattner88c8a232005-01-07 07:49:41 +00003224 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3225 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003226 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3227 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3228 Select(N.getOperand(0));
3229 Tmp1 = SelectExpr(N.getOperand(1));
3230 } else {
3231 Tmp1 = SelectExpr(N.getOperand(1));
3232 Select(N.getOperand(0));
3233 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003234 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3235 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3236 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003237
Chris Lattner88c8a232005-01-07 07:49:41 +00003238 return;
3239 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003240
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003241 case ISD::LOAD:
3242 // If this load could be folded into the only using instruction, and if it
3243 // is safe to emit the instruction here, try to do so now.
3244 if (Node->hasNUsesOfValue(1, 0)) {
3245 SDOperand TheVal = N.getValue(0);
3246 SDNode *User = 0;
3247 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3248 assert(UI != Node->use_end() && "Didn't find use!");
3249 SDNode *UN = *UI;
3250 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3251 if (UN->getOperand(i) == TheVal) {
3252 User = UN;
3253 goto FoundIt;
3254 }
3255 }
3256 FoundIt:
3257 // Only handle unary operators right now.
3258 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003259 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003260 SelectExpr(SDOperand(User, 0));
3261 return;
3262 }
3263 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003264 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003265 SelectExpr(N);
3266 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003267 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003268 case ISD::EXTLOAD:
3269 case ISD::SEXTLOAD:
3270 case ISD::ZEXTLOAD:
Chris Lattner1b3520c2005-05-14 08:48:15 +00003271 case X86ISD::TAILCALL:
3272 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00003273 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003274 SelectExpr(N);
3275 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003276 case ISD::CopyFromReg:
Evan Cheng6305e502006-01-12 22:54:21 +00003277 case X86ISD::FILD:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003278 ExprMap.erase(N);
3279 SelectExpr(N.getValue(0));
3280 return;
Jeff Cohen546fd592005-07-30 18:33:25 +00003281
Chris Lattner4738d1b2005-07-30 00:05:54 +00003282 case X86ISD::FP_TO_INT16_IN_MEM:
3283 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattner6dc60e82005-07-29 00:54:34 +00003284 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00003285 assert(N.getOperand(1).getValueType() == MVT::f64);
3286 X86AddressMode AM;
3287 Select(N.getOperand(0)); // Select the token chain
3288
3289 unsigned ValReg;
3290 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3291 ValReg = SelectExpr(N.getOperand(1));
3292 SelectAddress(N.getOperand(2), AM);
3293 } else {
3294 SelectAddress(N.getOperand(2), AM);
3295 ValReg = SelectExpr(N.getOperand(1));
3296 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003297
Chris Lattner6dc60e82005-07-29 00:54:34 +00003298 // Change the floating point control register to use "round towards zero"
3299 // mode when truncating to an integer value.
3300 //
3301 MachineFunction *F = BB->getParent();
3302 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3303 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003304
Chris Lattner6dc60e82005-07-29 00:54:34 +00003305 // Load the old value of the high byte of the control word...
Chris Lattneraeef51b2005-07-30 00:17:52 +00003306 unsigned OldCW = MakeReg(MVT::i16);
3307 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003308
Chris Lattner6dc60e82005-07-29 00:54:34 +00003309 // Set the high part to be round to zero...
Chris Lattner49134572005-07-30 00:43:00 +00003310 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohen546fd592005-07-30 18:33:25 +00003311
Chris Lattner6dc60e82005-07-29 00:54:34 +00003312 // Reload the modified control word now...
3313 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003314
Chris Lattner6dc60e82005-07-29 00:54:34 +00003315 // Restore the memory image of control word to original value
Chris Lattneraeef51b2005-07-30 00:17:52 +00003316 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner4738d1b2005-07-30 00:05:54 +00003317
3318 // Get the X86 opcode to use.
3319 switch (N.getOpcode()) {
Chris Lattnerf431ad42005-12-21 07:47:04 +00003320 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FpIST16m; break;
3321 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FpIST32m; break;
3322 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FpIST64m; break;
Chris Lattner4738d1b2005-07-30 00:05:54 +00003323 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003324
Chris Lattner4738d1b2005-07-30 00:05:54 +00003325 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohen546fd592005-07-30 18:33:25 +00003326
Chris Lattner6dc60e82005-07-29 00:54:34 +00003327 // Reload the original control word now.
3328 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00003329 return;
3330 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003331
Chris Lattner36db1ed2005-07-10 00:29:18 +00003332 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00003333 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00003334 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003335 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3336 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3337 && "Unsupported TRUNCSTORE for this target!");
3338
3339 if (StoredTy == MVT::i16) {
3340 // FIXME: This is here just to allow testing. X86 doesn't really have a
3341 // TRUNCSTORE i16 operation, but this is required for targets that do not
3342 // have 16-bit integer registers. We occasionally disable 16-bit integer
3343 // registers to test the promotion code.
3344 Select(N.getOperand(0));
3345 Tmp1 = SelectExpr(N.getOperand(1));
3346 SelectAddress(N.getOperand(2), AM);
3347
3348 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3349 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3350 return;
3351 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003352
3353 // Store of constant bool?
3354 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3355 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3356 Select(N.getOperand(0));
3357 SelectAddress(N.getOperand(2), AM);
3358 } else {
3359 SelectAddress(N.getOperand(2), AM);
3360 Select(N.getOperand(0));
3361 }
3362 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3363 return;
3364 }
3365
3366 switch (StoredTy) {
3367 default: assert(0 && "Cannot truncstore this type!");
3368 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003369 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003370 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Chris Lattnerf431ad42005-12-21 07:47:04 +00003371 Opc = X86::FpST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003372 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003373
Chris Lattnere18a4c42005-01-15 05:22:24 +00003374 std::vector<std::pair<unsigned, unsigned> > RP;
3375 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3376 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3377 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3378 std::sort(RP.begin(), RP.end());
3379
Chris Lattner80c5b972005-02-23 05:57:21 +00003380 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003381 for (unsigned i = 0; i != 3; ++i)
3382 switch (RP[2-i].second) {
3383 default: assert(0 && "Unknown operand number!");
3384 case 0: Select(N.getOperand(0)); break;
3385 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3386 case 2: SelectAddress(N.getOperand(2), AM); break;
3387 }
3388
3389 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3390 return;
3391 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003392 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003393 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003394
3395 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3396 Opc = 0;
3397 switch (CN->getValueType(0)) {
3398 default: assert(0 && "Invalid type for operation!");
3399 case MVT::i1:
3400 case MVT::i8: Opc = X86::MOV8mi; break;
3401 case MVT::i16: Opc = X86::MOV16mi; break;
3402 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003403 }
3404 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003405 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3406 Select(N.getOperand(0));
3407 SelectAddress(N.getOperand(2), AM);
3408 } else {
3409 SelectAddress(N.getOperand(2), AM);
3410 Select(N.getOperand(0));
3411 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003412 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3413 return;
3414 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003415 } else if (GlobalAddressSDNode *GA =
3416 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3417 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3418
3419 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3420 Select(N.getOperand(0));
3421 SelectAddress(N.getOperand(2), AM);
3422 } else {
3423 SelectAddress(N.getOperand(2), AM);
3424 Select(N.getOperand(0));
3425 }
Nate Begemana0b5e032005-07-15 00:38:55 +00003426 GlobalValue *GV = GA->getGlobal();
3427 // For Darwin, external and weak symbols are indirect, so we want to load
3428 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003429 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00003430 (GV->hasWeakLinkage() || GV->isExternal())) {
3431 Tmp1 = MakeReg(MVT::i32);
3432 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
3433 .addGlobalAddress(GV, false, 0);
3434 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
3435 } else {
3436 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
3437 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003438 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003439 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003440
3441 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003442 if (TryToFoldLoadOpStore(Node))
3443 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003444
Chris Lattner88c8a232005-01-07 07:49:41 +00003445 switch (N.getOperand(1).getValueType()) {
3446 default: assert(0 && "Cannot store this type!");
3447 case MVT::i1:
3448 case MVT::i8: Opc = X86::MOV8mr; break;
3449 case MVT::i16: Opc = X86::MOV16mr; break;
3450 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003451 case MVT::f32: Opc = X86::MOVSSmr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00003452 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FpST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003453 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003454
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003455 std::vector<std::pair<unsigned, unsigned> > RP;
3456 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3457 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3458 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3459 std::sort(RP.begin(), RP.end());
3460
Chris Lattner80c5b972005-02-23 05:57:21 +00003461 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003462 for (unsigned i = 0; i != 3; ++i)
3463 switch (RP[2-i].second) {
3464 default: assert(0 && "Unknown operand number!");
3465 case 0: Select(N.getOperand(0)); break;
3466 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003467 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003468 }
3469
Chris Lattner88c8a232005-01-07 07:49:41 +00003470 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3471 return;
3472 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003473 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003474 Select(N.getOperand(0));
3475 // Stack amount
3476 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3477 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3478 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00003479 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003480 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003481 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003482 case ISD::MEMSET: {
3483 Select(N.getOperand(0)); // Select the chain.
3484 unsigned Align =
3485 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3486 if (Align == 0) Align = 1;
3487
3488 // Turn the byte code into # iterations
3489 unsigned CountReg;
3490 unsigned Opcode;
3491 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3492 unsigned Val = ValC->getValue() & 255;
3493
3494 // If the value is a constant, then we can potentially use larger sets.
3495 switch (Align & 3) {
3496 case 2: // WORD aligned
3497 CountReg = MakeReg(MVT::i32);
3498 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3499 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3500 } else {
3501 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3502 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3503 }
3504 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3505 Opcode = X86::REP_STOSW;
3506 break;
3507 case 0: // DWORD aligned
3508 CountReg = MakeReg(MVT::i32);
3509 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3510 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3511 } else {
3512 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3513 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3514 }
3515 Val = (Val << 8) | Val;
3516 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3517 Opcode = X86::REP_STOSD;
3518 break;
3519 default: // BYTE aligned
3520 CountReg = SelectExpr(Node->getOperand(3));
3521 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3522 Opcode = X86::REP_STOSB;
3523 break;
3524 }
3525 } else {
3526 // If it's not a constant value we are storing, just fall back. We could
3527 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3528 unsigned ValReg = SelectExpr(Node->getOperand(2));
3529 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3530 CountReg = SelectExpr(Node->getOperand(3));
3531 Opcode = X86::REP_STOSB;
3532 }
3533
Evan Chengae986f12006-01-11 22:15:48 +00003534 // No matter what the alignment is, we put the destination in EDI, and the
3535 // count in ECX.
Chris Lattner36f78482005-01-11 06:14:36 +00003536 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3537 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3538 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3539 BuildMI(BB, Opcode, 0);
3540 return;
3541 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003542 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003543 Select(N.getOperand(0)); // Select the chain.
3544 unsigned Align =
3545 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3546 if (Align == 0) Align = 1;
3547
3548 // Turn the byte code into # iterations
3549 unsigned CountReg;
3550 unsigned Opcode;
3551 switch (Align & 3) {
3552 case 2: // WORD aligned
3553 CountReg = MakeReg(MVT::i32);
3554 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3555 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3556 } else {
3557 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3558 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3559 }
3560 Opcode = X86::REP_MOVSW;
3561 break;
3562 case 0: // DWORD aligned
3563 CountReg = MakeReg(MVT::i32);
3564 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3565 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3566 } else {
3567 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3568 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3569 }
3570 Opcode = X86::REP_MOVSD;
3571 break;
3572 default: // BYTE aligned
3573 CountReg = SelectExpr(Node->getOperand(3));
3574 Opcode = X86::REP_MOVSB;
3575 break;
3576 }
3577
3578 // No matter what the alignment is, we put the source in ESI, the
3579 // destination in EDI, and the count in ECX.
3580 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3581 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3582 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3583 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3584 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3585 BuildMI(BB, Opcode, 0);
3586 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003587 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003588 case ISD::WRITEPORT:
3589 if (Node->getOperand(2).getValueType() != MVT::i16) {
3590 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3591 exit(1);
3592 }
3593 Select(Node->getOperand(0)); // Emit the chain.
3594
3595 Tmp1 = SelectExpr(Node->getOperand(1));
3596 switch (Node->getOperand(1).getValueType()) {
3597 case MVT::i8:
3598 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3599 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3600 break;
3601 case MVT::i16:
3602 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3603 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3604 break;
3605 case MVT::i32:
3606 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3607 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3608 break;
3609 default:
3610 std::cerr << "llvm.writeport: invalid data type for X86 target";
3611 exit(1);
3612 }
3613
3614 // If the port is a single-byte constant, use the immediate form.
3615 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3616 if ((CN->getValue() & 255) == CN->getValue()) {
3617 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3618 return;
3619 }
3620
3621 // Otherwise, move the I/O port address into the DX register.
3622 unsigned Reg = SelectExpr(Node->getOperand(2));
3623 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3624 BuildMI(BB, Opc, 0);
3625 return;
3626 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003627 assert(0 && "Should not be reached!");
3628}
3629
3630
Chris Lattner76ac0682005-11-15 00:40:23 +00003631/// createX86ISelPattern - This pass converts an LLVM function
Chris Lattner88c8a232005-01-07 07:49:41 +00003632/// into a machine code representation using pattern matching and a machine
3633/// description file.
3634///
Chris Lattner76ac0682005-11-15 00:40:23 +00003635FunctionPass *llvm::createX86ISelPattern(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003636 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003637}