blob: 77863e177cc309eda3c70b4d4a3710dd0beb6a86 [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:
Chris Lattner35d20a42006-01-29 06:45:31 +00001356 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001357 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001358 case ISD::GlobalAddress: {
1359 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00001360 // For Darwin, external and weak symbols are indirect, so we want to load
1361 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001362 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001363 (GV->hasWeakLinkage() || GV->isExternal())) {
1364 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
1365 .addGlobalAddress(GV, false, 0);
1366 } else {
1367 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1368 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001369 return Result;
1370 }
1371 case ISD::ExternalSymbol: {
1372 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1373 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1374 return Result;
1375 }
Chris Lattner210975c2005-09-02 00:16:09 +00001376 case ISD::ANY_EXTEND: // treat any extend like zext
Chris Lattner88c8a232005-01-07 07:49:41 +00001377 case ISD::ZERO_EXTEND: {
1378 int DestIs16 = N.getValueType() == MVT::i16;
1379 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001380
1381 // FIXME: This hack is here for zero extension casts from bool to i8. This
1382 // would not be needed if bools were promoted by Legalize.
1383 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001384 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001385 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1386 return Result;
1387 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001388
Chris Lattnera56d29d2005-01-17 06:26:58 +00001389 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001390 static const unsigned Opc[3] = {
1391 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1392 };
1393
1394 X86AddressMode AM;
1395 EmitFoldedLoad(N.getOperand(0), AM);
1396 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001397
Chris Lattnerb0eef822005-01-11 23:33:00 +00001398 return Result;
1399 }
1400
Chris Lattner88c8a232005-01-07 07:49:41 +00001401 static const unsigned Opc[3] = {
1402 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1403 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001404 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001405 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1406 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001407 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001408 case ISD::SIGN_EXTEND: {
1409 int DestIs16 = N.getValueType() == MVT::i16;
1410 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1411
Chris Lattner282781c2005-01-09 18:52:44 +00001412 // FIXME: Legalize should promote bools to i8!
1413 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1414 "Sign extend from bool not implemented!");
1415
Chris Lattnera56d29d2005-01-17 06:26:58 +00001416 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001417 static const unsigned Opc[3] = {
1418 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1419 };
1420
1421 X86AddressMode AM;
1422 EmitFoldedLoad(N.getOperand(0), AM);
1423 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1424 return Result;
1425 }
1426
Chris Lattner88c8a232005-01-07 07:49:41 +00001427 static const unsigned Opc[3] = {
1428 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1429 };
1430 Tmp1 = SelectExpr(N.getOperand(0));
1431 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1432 return Result;
1433 }
1434 case ISD::TRUNCATE:
1435 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1436 // a move out of AX or AL.
1437 switch (N.getOperand(0).getValueType()) {
1438 default: assert(0 && "Unknown truncate!");
1439 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1440 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1441 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1442 }
1443 Tmp1 = SelectExpr(N.getOperand(0));
1444 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1445
1446 switch (N.getValueType()) {
1447 default: assert(0 && "Unknown truncate!");
1448 case MVT::i1:
1449 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1450 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1451 }
1452 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1453 return Result;
1454
Chris Lattner507a2752005-07-16 00:28:20 +00001455 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00001456 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1457 unsigned PromoteOpcode = 0;
1458
Nate Begeman7e74c832005-07-16 02:02:34 +00001459 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00001460 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00001461 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00001462 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1463 return Result;
1464 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001465
Chris Lattnere44e6d12005-01-11 03:50:45 +00001466 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00001467
Chris Lattner282781c2005-01-09 18:52:44 +00001468 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00001469 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00001470 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1471 MachineFunction *F = BB->getParent();
1472 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1473
1474 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00001475 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00001476 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001477 addFrameReference(BuildMI(BB, X86::FpILD32m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001478 break;
1479 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00001480 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00001481 addFrameReference(BuildMI(BB, X86::FpILD16m, 5, Result), FrameIdx);
Chris Lattner282781c2005-01-09 18:52:44 +00001482 break;
1483 default: break; // No promotion required.
1484 }
Chris Lattner507a2752005-07-16 00:28:20 +00001485 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00001486 }
Chris Lattner4738d1b2005-07-30 00:05:54 +00001487 case ISD::FP_TO_SINT:
Chris Lattner282781c2005-01-09 18:52:44 +00001488 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1489
Nate Begeman8a093362005-07-06 18:59:04 +00001490 // If the target supports SSE2 and is performing FP operations in SSE regs
1491 // instead of the FP stack, then we can use the efficient CVTSS2SI and
1492 // CVTSD2SI instructions.
Chris Lattner4738d1b2005-07-30 00:05:54 +00001493 assert(X86ScalarSSE);
1494 if (MVT::f32 == N.getOperand(0).getValueType()) {
1495 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
1496 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
1497 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
1498 } else {
1499 assert(0 && "Not an f32 or f64?");
1500 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001501 }
Chris Lattner282781c2005-01-09 18:52:44 +00001502 return Result;
Chris Lattner4738d1b2005-07-30 00:05:54 +00001503
Chris Lattner0815dcae2005-09-28 22:29:17 +00001504 case ISD::FADD:
Chris Lattner88c8a232005-01-07 07:49:41 +00001505 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00001506 Op0 = N.getOperand(0);
1507 Op1 = N.getOperand(1);
1508
Chris Lattner30607ec2005-01-25 20:03:11 +00001509 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00001510 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001511 goto FoldAdd;
1512 }
Chris Lattner62b22422005-01-11 21:19:59 +00001513
Chris Lattner30607ec2005-01-25 20:03:11 +00001514 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001515 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00001516 switch (N.getValueType()) {
1517 default: assert(0 && "Cannot add this type!");
1518 case MVT::i1:
1519 case MVT::i8: Opc = X86::ADD8rm; break;
1520 case MVT::i16: Opc = X86::ADD16rm; break;
1521 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001522 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001523 case MVT::f64:
1524 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00001525 if (X86ScalarSSE) {
1526 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
1527 Opc = X86::ADDSDrm;
1528 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001529 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FpADD64m : X86::FpADD32m;
Nate Begeman8a093362005-07-06 18:59:04 +00001530 }
Chris Lattner30607ec2005-01-25 20:03:11 +00001531 break;
Chris Lattner62b22422005-01-11 21:19:59 +00001532 }
1533 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001534 EmitFoldedLoad(Op1, AM);
1535 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001536 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1537 return Result;
1538 }
1539
Chris Lattner88c8a232005-01-07 07:49:41 +00001540 // See if we can codegen this as an LEA to fold operations together.
1541 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00001542 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001543 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00001544 MatchAddress(N, AM);
1545 ExprMap[N] = Result;
1546
1547 // If this is not just an add, emit the LEA. For a simple add (like
1548 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1549 // leave this as LEA, then peephole it to 'ADD' after two address elim
1550 // happens.
1551 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1552 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1553 X86AddressMode XAM = SelectAddrExprs(AM);
1554 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1555 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001556 }
1557 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001558
Chris Lattner62b22422005-01-11 21:19:59 +00001559 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001560 Opc = 0;
1561 if (CN->getValue() == 1) { // add X, 1 -> inc X
1562 switch (N.getValueType()) {
1563 default: assert(0 && "Cannot integer add this type!");
1564 case MVT::i8: Opc = X86::INC8r; break;
1565 case MVT::i16: Opc = X86::INC16r; break;
1566 case MVT::i32: Opc = X86::INC32r; break;
1567 }
1568 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1569 switch (N.getValueType()) {
1570 default: assert(0 && "Cannot integer add this type!");
1571 case MVT::i8: Opc = X86::DEC8r; break;
1572 case MVT::i16: Opc = X86::DEC16r; break;
1573 case MVT::i32: Opc = X86::DEC32r; break;
1574 }
1575 }
1576
1577 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001578 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001579 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1580 return Result;
1581 }
1582
1583 switch (N.getValueType()) {
1584 default: assert(0 && "Cannot add this type!");
1585 case MVT::i8: Opc = X86::ADD8ri; break;
1586 case MVT::i16: Opc = X86::ADD16ri; break;
1587 case MVT::i32: Opc = X86::ADD32ri; break;
1588 }
1589 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00001590 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001591 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1592 return Result;
1593 }
1594 }
1595
Chris Lattner88c8a232005-01-07 07:49:41 +00001596 switch (N.getValueType()) {
1597 default: assert(0 && "Cannot add this type!");
1598 case MVT::i8: Opc = X86::ADD8rr; break;
1599 case MVT::i16: Opc = X86::ADD16rr; break;
1600 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001601 case MVT::f32: Opc = X86::ADDSSrr; break;
1602 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001603 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001604
Chris Lattner62b22422005-01-11 21:19:59 +00001605 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1606 Tmp1 = SelectExpr(Op0);
1607 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001608 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001609 Tmp2 = SelectExpr(Op1);
1610 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001611 }
1612
Chris Lattner88c8a232005-01-07 07:49:41 +00001613 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1614 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00001615
Nate Begeman8a093362005-07-06 18:59:04 +00001616 case ISD::FSQRT:
1617 Tmp1 = SelectExpr(Node->getOperand(0));
1618 if (X86ScalarSSE) {
1619 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
1620 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1621 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00001622 BuildMI(BB, X86::FpSQRT, 1, Result).addReg(Tmp1);
Nate Begeman8a093362005-07-06 18:59:04 +00001623 }
1624 return Result;
1625
1626 // FIXME:
1627 // Once we can spill 16 byte constants into the constant pool, we can
1628 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00001629 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00001630 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00001631 case ISD::FSIN:
1632 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00001633 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00001634 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00001635 switch (N.getOpcode()) {
1636 default: assert(0 && "Unreachable!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00001637 case ISD::FABS: BuildMI(BB, X86::FpABS, 1, Result).addReg(Tmp1); break;
1638 case ISD::FNEG: BuildMI(BB, X86::FpCHS, 1, Result).addReg(Tmp1); break;
1639 case ISD::FSIN: BuildMI(BB, X86::FpSIN, 1, Result).addReg(Tmp1); break;
1640 case ISD::FCOS: BuildMI(BB, X86::FpCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00001641 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00001642 return Result;
1643
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001644 case ISD::MULHU:
1645 switch (N.getValueType()) {
1646 default: assert(0 && "Unsupported VT!");
1647 case MVT::i8: Tmp2 = X86::MUL8r; break;
1648 case MVT::i16: Tmp2 = X86::MUL16r; break;
1649 case MVT::i32: Tmp2 = X86::MUL32r; break;
1650 }
1651 // FALL THROUGH
1652 case ISD::MULHS: {
1653 unsigned MovOpc, LowReg, HiReg;
1654 switch (N.getValueType()) {
1655 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00001656 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001657 MovOpc = X86::MOV8rr;
1658 LowReg = X86::AL;
1659 HiReg = X86::AH;
1660 Opc = X86::IMUL8r;
1661 break;
1662 case MVT::i16:
1663 MovOpc = X86::MOV16rr;
1664 LowReg = X86::AX;
1665 HiReg = X86::DX;
1666 Opc = X86::IMUL16r;
1667 break;
1668 case MVT::i32:
1669 MovOpc = X86::MOV32rr;
1670 LowReg = X86::EAX;
1671 HiReg = X86::EDX;
1672 Opc = X86::IMUL32r;
1673 break;
1674 }
1675 if (Node->getOpcode() != ISD::MULHS)
1676 Opc = Tmp2; // Get the MULHU opcode.
1677
1678 Op0 = Node->getOperand(0);
1679 Op1 = Node->getOperand(1);
1680 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1681 Tmp1 = SelectExpr(Op0);
1682 Tmp2 = SelectExpr(Op1);
1683 } else {
1684 Tmp2 = SelectExpr(Op1);
1685 Tmp1 = SelectExpr(Op0);
1686 }
1687
1688 // FIXME: Implement folding of loads into the memory operands here!
1689 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1690 BuildMI(BB, Opc, 1).addReg(Tmp2);
1691 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1692 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001693 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00001694
Chris Lattner0815dcae2005-09-28 22:29:17 +00001695 case ISD::FSUB:
1696 case ISD::FMUL:
Chris Lattner88c8a232005-01-07 07:49:41 +00001697 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00001698 case ISD::MUL:
1699 case ISD::AND:
1700 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00001701 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00001702 static const unsigned SUBTab[] = {
1703 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001704 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FpSUB32m, X86::FpSUB64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001705 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1706 };
Nate Begeman8a093362005-07-06 18:59:04 +00001707 static const unsigned SSE_SUBTab[] = {
1708 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1709 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
1710 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
1711 };
Chris Lattner62b22422005-01-11 21:19:59 +00001712 static const unsigned MULTab[] = {
1713 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
Chris Lattnerf431ad42005-12-21 07:47:04 +00001714 0, X86::IMUL16rm , X86::IMUL32rm, X86::FpMUL32m, X86::FpMUL64m,
Chris Lattner62b22422005-01-11 21:19:59 +00001715 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1716 };
Nate Begeman8a093362005-07-06 18:59:04 +00001717 static const unsigned SSE_MULTab[] = {
1718 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1719 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
1720 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
1721 };
Chris Lattner62b22422005-01-11 21:19:59 +00001722 static const unsigned ANDTab[] = {
1723 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1724 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001725 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00001726 };
1727 static const unsigned ORTab[] = {
1728 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1729 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1730 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1731 };
1732 static const unsigned XORTab[] = {
1733 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1734 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1735 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1736 };
1737
1738 Op0 = Node->getOperand(0);
1739 Op1 = Node->getOperand(1);
1740
Chris Lattner29f58192005-01-19 07:37:26 +00001741 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1742 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00001743 return Result;
1744
1745 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00001746 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1747 if (CN->isNullValue()) { // 0 - N -> neg N
1748 switch (N.getValueType()) {
1749 default: assert(0 && "Cannot sub this type!");
1750 case MVT::i1:
1751 case MVT::i8: Opc = X86::NEG8r; break;
1752 case MVT::i16: Opc = X86::NEG16r; break;
1753 case MVT::i32: Opc = X86::NEG32r; break;
1754 }
1755 Tmp1 = SelectExpr(N.getOperand(1));
1756 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1757 return Result;
1758 }
1759
Chris Lattner62b22422005-01-11 21:19:59 +00001760 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1761 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001762 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00001763 switch (N.getValueType()) {
1764 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001765 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00001766 case MVT::i8: Opc = X86::NOT8r; break;
1767 case MVT::i16: Opc = X86::NOT16r; break;
1768 case MVT::i32: Opc = X86::NOT32r; break;
1769 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00001770 if (Opc) {
1771 Tmp1 = SelectExpr(Op0);
1772 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1773 return Result;
1774 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00001775 }
1776
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001777 // Fold common multiplies into LEA instructions.
1778 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1779 switch ((int)CN->getValue()) {
1780 default: break;
1781 case 3:
1782 case 5:
1783 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001784 // Remove N from exprmap so SelectAddress doesn't get confused.
1785 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001786 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00001787 SelectAddress(N, AM);
1788 // Restore it to the map.
1789 ExprMap[N] = Result;
1790 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1791 return Result;
1792 }
1793 }
1794
Chris Lattner88c8a232005-01-07 07:49:41 +00001795 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00001796 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00001797 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00001798 case MVT::i8: Opc = 0; break;
1799 case MVT::i16: Opc = 1; break;
1800 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001801 }
Chris Lattner62b22422005-01-11 21:19:59 +00001802 switch (Node->getOpcode()) {
1803 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001804 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001805 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001806 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001807 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001808 case ISD::AND: Opc = ANDTab[Opc]; break;
1809 case ISD::OR: Opc = ORTab[Opc]; break;
1810 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001811 }
Chris Lattner62b22422005-01-11 21:19:59 +00001812 if (Opc) { // Can't fold MUL:i8 R, imm
1813 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00001814 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1815 return Result;
1816 }
1817 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001818
Chris Lattner30607ec2005-01-25 20:03:11 +00001819 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner0815dcae2005-09-28 22:29:17 +00001820 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
Chris Lattner62b22422005-01-11 21:19:59 +00001821 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00001822 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00001823 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00001824 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00001825 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00001826 if (Op0.getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00001827 Opc = X86::FpSUBR32m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001828 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00001829 Opc = X86::FpSUBR64m;
Chris Lattner30607ec2005-01-25 20:03:11 +00001830
Chris Lattner62b22422005-01-11 21:19:59 +00001831 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001832 EmitFoldedLoad(Op0, AM);
1833 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00001834 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1835 return Result;
1836 }
1837 }
1838
Chris Lattner30607ec2005-01-25 20:03:11 +00001839 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00001840 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00001841 switch (N.getValueType()) {
1842 default: assert(0 && "Cannot operate on this type!");
1843 case MVT::i1:
1844 case MVT::i8: Opc = 5; break;
1845 case MVT::i16: Opc = 6; break;
1846 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00001847 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00001848 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001849 case MVT::f64:
1850 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00001851 "SSE load should have been promoted");
1852 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001853 }
1854 switch (Node->getOpcode()) {
1855 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001856 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001857 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001858 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001859 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001860 case ISD::AND: Opc = ANDTab[Opc]; break;
1861 case ISD::OR: Opc = ORTab[Opc]; break;
1862 case ISD::XOR: Opc = XORTab[Opc]; break;
1863 }
1864
1865 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001866 EmitFoldedLoad(Op1, AM);
1867 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00001868 if (Opc) {
1869 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1870 } else {
1871 assert(Node->getOpcode() == ISD::MUL &&
1872 N.getValueType() == MVT::i8 && "Unexpected situation!");
1873 // Must use the MUL instruction, which forces use of AL.
1874 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1875 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1876 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1877 }
1878 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001879 }
Chris Lattner62b22422005-01-11 21:19:59 +00001880
1881 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1882 Tmp1 = SelectExpr(Op0);
1883 Tmp2 = SelectExpr(Op1);
1884 } else {
1885 Tmp2 = SelectExpr(Op1);
1886 Tmp1 = SelectExpr(Op0);
1887 }
1888
Chris Lattner88c8a232005-01-07 07:49:41 +00001889 switch (N.getValueType()) {
1890 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00001891 case MVT::i1:
1892 case MVT::i8: Opc = 10; break;
1893 case MVT::i16: Opc = 11; break;
1894 case MVT::i32: Opc = 12; break;
1895 case MVT::f32: Opc = 13; break;
1896 case MVT::f64: Opc = 14; break;
1897 }
1898 switch (Node->getOpcode()) {
1899 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00001900 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00001901 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00001902 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00001903 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00001904 case ISD::AND: Opc = ANDTab[Opc]; break;
1905 case ISD::OR: Opc = ORTab[Opc]; break;
1906 case ISD::XOR: Opc = XORTab[Opc]; break;
1907 }
1908 if (Opc) {
1909 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1910 } else {
1911 assert(Node->getOpcode() == ISD::MUL &&
1912 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00001913 // Must use the MUL instruction, which forces use of AL.
1914 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1915 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1916 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00001917 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001918 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00001919 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00001920 case ISD::ADD_PARTS:
1921 case ISD::SUB_PARTS: {
1922 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1923 "Not an i64 add/sub!");
1924 // Emit all of the operands.
1925 std::vector<unsigned> InVals;
1926 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1927 InVals.push_back(SelectExpr(N.getOperand(i)));
1928 if (N.getOpcode() == ISD::ADD_PARTS) {
1929 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1930 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1931 } else {
1932 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1933 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
1934 }
1935 return Result+N.ResNo;
1936 }
1937
Chris Lattnera31d4c72005-04-02 04:01:14 +00001938 case ISD::SHL_PARTS:
1939 case ISD::SRA_PARTS:
1940 case ISD::SRL_PARTS: {
1941 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1942 "Not an i64 shift!");
1943 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1944 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1945 unsigned TmpReg = MakeReg(MVT::i32);
1946 if (N.getOpcode() == ISD::SRA_PARTS) {
1947 // If this is a SHR of a Long, then we need to do funny sign extension
1948 // stuff. TmpReg gets the value to use as the high-part if we are
1949 // shifting more than 32 bits.
1950 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
1951 } else {
1952 // Other shifts use a fixed zero value if the shift is more than 32 bits.
1953 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
1954 }
1955
1956 // Initialize CL with the shift amount.
1957 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
1958 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
1959
1960 unsigned TmpReg2 = MakeReg(MVT::i32);
1961 unsigned TmpReg3 = MakeReg(MVT::i32);
1962 if (N.getOpcode() == ISD::SHL_PARTS) {
1963 // TmpReg2 = shld inHi, inLo
1964 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
1965 .addReg(ShiftOpLo);
1966 // TmpReg3 = shl inLo, CL
1967 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001968
Chris Lattnera31d4c72005-04-02 04:01:14 +00001969 // Set the flags to indicate whether the shift was by more than 32 bits.
1970 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001971
Chris Lattnera31d4c72005-04-02 04:01:14 +00001972 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001973 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001974 Result+1).addReg(TmpReg2).addReg(TmpReg3);
1975 // DestLo = (>32) ? TmpReg : TmpReg3;
1976 BuildMI(BB, X86::CMOVNE32rr, 2,
1977 Result).addReg(TmpReg3).addReg(TmpReg);
1978 } else {
1979 // TmpReg2 = shrd inLo, inHi
1980 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
1981 .addReg(ShiftOpHi);
1982 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00001983 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00001984 : X86::SHR32rCL, 1, TmpReg3)
1985 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001986
Chris Lattnera31d4c72005-04-02 04:01:14 +00001987 // Set the flags to indicate whether the shift was by more than 32 bits.
1988 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001989
Chris Lattnera31d4c72005-04-02 04:01:14 +00001990 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001991 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001992 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001993
Chris Lattnera31d4c72005-04-02 04:01:14 +00001994 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001995 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00001996 Result+1).addReg(TmpReg3).addReg(TmpReg);
1997 }
1998 return Result+N.ResNo;
1999 }
2000
Chris Lattner88c8a232005-01-07 07:49:41 +00002001 case ISD::SELECT:
Nate Begeman8d394eb2005-08-03 23:26:28 +00002002 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
2003 N.getValueType(), Result);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002004 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002005
Chris Lattner0815dcae2005-09-28 22:29:17 +00002006 case ISD::FDIV:
2007 case ISD::FREM:
Chris Lattner88c8a232005-01-07 07:49:41 +00002008 case ISD::SDIV:
2009 case ISD::UDIV:
2010 case ISD::SREM:
2011 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002012 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2013 "We don't support this operator!");
2014
Chris Lattner0815dcae2005-09-28 22:29:17 +00002015 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002016 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00002017 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00002018 // Check for reversed and unreversed DIV.
2019 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2020 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002021 Opc = X86::FpDIVR32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002022 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002023 Opc = X86::FpDIVR64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002024 X86AddressMode AM;
2025 EmitFoldedLoad(N.getOperand(0), AM);
2026 Tmp1 = SelectExpr(N.getOperand(1));
2027 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2028 return Result;
2029 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2030 N.getOperand(1).getOpcode() == ISD::LOAD) {
2031 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
Chris Lattnerf431ad42005-12-21 07:47:04 +00002032 Opc = X86::FpDIV32m;
Chris Lattner1b206152005-01-25 20:35:10 +00002033 else
Chris Lattnerf431ad42005-12-21 07:47:04 +00002034 Opc = X86::FpDIV64m;
Chris Lattner1b206152005-01-25 20:35:10 +00002035 X86AddressMode AM;
2036 EmitFoldedLoad(N.getOperand(1), AM);
2037 Tmp1 = SelectExpr(N.getOperand(0));
2038 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2039 return Result;
2040 }
2041 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002042 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002043
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002044 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2045 Tmp1 = SelectExpr(N.getOperand(0));
2046 Tmp2 = SelectExpr(N.getOperand(1));
2047 } else {
2048 Tmp2 = SelectExpr(N.getOperand(1));
2049 Tmp1 = SelectExpr(N.getOperand(0));
2050 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002051
2052 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2053 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2054 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2055 switch (N.getValueType()) {
2056 default: assert(0 && "Cannot sdiv this type!");
2057 case MVT::i8:
2058 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2059 LoReg = X86::AL;
2060 HiReg = X86::AH;
2061 MovOpcode = X86::MOV8rr;
2062 ClrOpcode = X86::MOV8ri;
2063 SExtOpcode = X86::CBW;
2064 break;
2065 case MVT::i16:
2066 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2067 LoReg = X86::AX;
2068 HiReg = X86::DX;
2069 MovOpcode = X86::MOV16rr;
2070 ClrOpcode = X86::MOV16ri;
2071 SExtOpcode = X86::CWD;
2072 break;
2073 case MVT::i32:
2074 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002075 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002076 HiReg = X86::EDX;
2077 MovOpcode = X86::MOV32rr;
2078 ClrOpcode = X86::MOV32ri;
2079 SExtOpcode = X86::CDQ;
2080 break;
Nate Begeman8a093362005-07-06 18:59:04 +00002081 case MVT::f32:
2082 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
2083 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002084 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002085 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
2086 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002087 return Result;
2088 }
2089
2090 // Set up the low part.
2091 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2092
2093 if (isSigned) {
2094 // Sign extend the low part into the high part.
2095 BuildMI(BB, SExtOpcode, 0);
2096 } else {
2097 // Zero out the high part, effectively zero extending the input.
2098 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2099 }
2100
2101 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002102 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002103
2104 // Get the result of the divide or rem.
2105 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2106 return Result;
2107 }
2108
2109 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002110 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002111 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2112 switch (N.getValueType()) {
2113 default: assert(0 && "Cannot shift this type!");
2114 case MVT::i8: Opc = X86::ADD8rr; break;
2115 case MVT::i16: Opc = X86::ADD16rr; break;
2116 case MVT::i32: Opc = X86::ADD32rr; break;
2117 }
2118 Tmp1 = SelectExpr(N.getOperand(0));
2119 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2120 return Result;
2121 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002122
Chris Lattner88c8a232005-01-07 07:49:41 +00002123 switch (N.getValueType()) {
2124 default: assert(0 && "Cannot shift this type!");
2125 case MVT::i8: Opc = X86::SHL8ri; break;
2126 case MVT::i16: Opc = X86::SHL16ri; break;
2127 case MVT::i32: Opc = X86::SHL32ri; break;
2128 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002129 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002130 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2131 return Result;
2132 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002133
2134 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2135 Tmp1 = SelectExpr(N.getOperand(0));
2136 Tmp2 = SelectExpr(N.getOperand(1));
2137 } else {
2138 Tmp2 = SelectExpr(N.getOperand(1));
2139 Tmp1 = SelectExpr(N.getOperand(0));
2140 }
2141
Chris Lattner88c8a232005-01-07 07:49:41 +00002142 switch (N.getValueType()) {
2143 default: assert(0 && "Cannot shift this type!");
2144 case MVT::i8 : Opc = X86::SHL8rCL; break;
2145 case MVT::i16: Opc = X86::SHL16rCL; break;
2146 case MVT::i32: Opc = X86::SHL32rCL; break;
2147 }
2148 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002149 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002150 return Result;
2151 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002152 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2153 switch (N.getValueType()) {
2154 default: assert(0 && "Cannot shift this type!");
2155 case MVT::i8: Opc = X86::SHR8ri; break;
2156 case MVT::i16: Opc = X86::SHR16ri; break;
2157 case MVT::i32: Opc = X86::SHR32ri; break;
2158 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002159 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002160 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2161 return Result;
2162 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002163
2164 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2165 Tmp1 = SelectExpr(N.getOperand(0));
2166 Tmp2 = SelectExpr(N.getOperand(1));
2167 } else {
2168 Tmp2 = SelectExpr(N.getOperand(1));
2169 Tmp1 = SelectExpr(N.getOperand(0));
2170 }
2171
Chris Lattner88c8a232005-01-07 07:49:41 +00002172 switch (N.getValueType()) {
2173 default: assert(0 && "Cannot shift this type!");
2174 case MVT::i8 : Opc = X86::SHR8rCL; break;
2175 case MVT::i16: Opc = X86::SHR16rCL; break;
2176 case MVT::i32: Opc = X86::SHR32rCL; break;
2177 }
2178 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00002179 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002180 return Result;
2181 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002182 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2183 switch (N.getValueType()) {
2184 default: assert(0 && "Cannot shift this type!");
2185 case MVT::i8: Opc = X86::SAR8ri; break;
2186 case MVT::i16: Opc = X86::SAR16ri; break;
2187 case MVT::i32: Opc = X86::SAR32ri; break;
2188 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002189 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002190 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2191 return Result;
2192 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002193
2194 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2195 Tmp1 = SelectExpr(N.getOperand(0));
2196 Tmp2 = SelectExpr(N.getOperand(1));
2197 } else {
2198 Tmp2 = SelectExpr(N.getOperand(1));
2199 Tmp1 = SelectExpr(N.getOperand(0));
2200 }
2201
Chris Lattner88c8a232005-01-07 07:49:41 +00002202 switch (N.getValueType()) {
2203 default: assert(0 && "Cannot shift this type!");
2204 case MVT::i8 : Opc = X86::SAR8rCL; break;
2205 case MVT::i16: Opc = X86::SAR16rCL; break;
2206 case MVT::i32: Opc = X86::SAR32rCL; break;
2207 }
2208 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnera9d68f12005-08-19 00:31:37 +00002209 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00002210 return Result;
2211
2212 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002213 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner6ec77452005-08-09 20:21:10 +00002214 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner88c8a232005-01-07 07:49:41 +00002215 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2216 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002217 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002218 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002219 if (Result != 1) { // Generate the token
2220 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2221 assert(0 && "Load already emitted!?");
2222 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002223 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2224
Chris Lattnerb52e0412005-01-08 19:28:19 +00002225 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002226 default: assert(0 && "Cannot load this type!");
2227 case MVT::i1:
2228 case MVT::i8: Opc = X86::MOV8rm; break;
2229 case MVT::i16: Opc = X86::MOV16rm; break;
2230 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002231 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002232 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00002233 if (X86ScalarSSE) {
2234 Opc = X86::MOVSDrm;
2235 } else {
Chris Lattnerf431ad42005-12-21 07:47:04 +00002236 Opc = X86::FpLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002237 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00002238 }
2239 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002240 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002241
Chris Lattner88c8a232005-01-07 07:49:41 +00002242 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattnerc30405e2005-08-26 17:15:30 +00002243 unsigned CPIdx = BB->getParent()->getConstantPool()->
2244 getConstantPoolIndex(CP->get());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002245 Select(N.getOperand(0));
Chris Lattnerc30405e2005-08-26 17:15:30 +00002246 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner88c8a232005-01-07 07:49:41 +00002247 } else {
2248 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002249
2250 SDOperand Chain = N.getOperand(0);
2251 SDOperand Address = N.getOperand(1);
2252 if (getRegPressure(Chain) > getRegPressure(Address)) {
2253 Select(Chain);
2254 SelectAddress(Address, AM);
2255 } else {
2256 SelectAddress(Address, AM);
2257 Select(Chain);
2258 }
2259
Chris Lattner88c8a232005-01-07 07:49:41 +00002260 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2261 }
2262 return Result;
Evan Cheng6305e502006-01-12 22:54:21 +00002263 case X86ISD::FILD:
Chris Lattnera36117b2005-05-14 06:52:07 +00002264 // Make sure we generate both values.
2265 assert(Result != 1 && N.getValueType() == MVT::f64);
2266 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2267 assert(0 && "Load already emitted!?");
2268
2269 {
2270 X86AddressMode AM;
2271
2272 SDOperand Chain = N.getOperand(0);
2273 SDOperand Address = N.getOperand(1);
2274 if (getRegPressure(Chain) > getRegPressure(Address)) {
2275 Select(Chain);
2276 SelectAddress(Address, AM);
2277 } else {
2278 SelectAddress(Address, AM);
2279 Select(Chain);
2280 }
Chris Lattner67756e22005-07-29 00:40:01 +00002281
Chris Lattnerf431ad42005-12-21 07:47:04 +00002282 addFullAddress(BuildMI(BB, X86::FpILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00002283 }
2284 return Result;
Jeff Cohen546fd592005-07-30 18:33:25 +00002285
Chris Lattnere18a4c42005-01-15 05:22:24 +00002286 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2287 case ISD::ZEXTLOAD: {
2288 // Make sure we generate both values.
2289 if (Result != 1)
2290 ExprMap[N.getValue(1)] = 1; // Generate the token
2291 else
2292 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2293
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002294 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2295 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00002296 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002297 "Bad EXTLOAD!");
Chris Lattnerc30405e2005-08-26 17:15:30 +00002298 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00002299 getConstantPoolIndex(CP->get());
Chris Lattnerc30405e2005-08-26 17:15:30 +00002300
Chris Lattnerf431ad42005-12-21 07:47:04 +00002301 addConstantPoolReference(BuildMI(BB, X86::FpLD32m, 4, Result), CPIdx);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002302 return Result;
2303 }
2304
Chris Lattnere18a4c42005-01-15 05:22:24 +00002305 X86AddressMode AM;
2306 if (getRegPressure(Node->getOperand(0)) >
2307 getRegPressure(Node->getOperand(1))) {
2308 Select(Node->getOperand(0)); // chain
2309 SelectAddress(Node->getOperand(1), AM);
2310 } else {
2311 SelectAddress(Node->getOperand(1), AM);
2312 Select(Node->getOperand(0)); // chain
2313 }
2314
2315 switch (Node->getValueType(0)) {
2316 default: assert(0 && "Unknown type to sign extend to.");
2317 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00002318 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002319 "Bad EXTLOAD!");
Chris Lattnerf431ad42005-12-21 07:47:04 +00002320 addFullAddress(BuildMI(BB, X86::FpLD32m, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002321 break;
2322 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002323 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002324 default:
2325 assert(0 && "Bad zero extend!");
2326 case MVT::i1:
2327 case MVT::i8:
2328 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2329 break;
2330 case MVT::i16:
2331 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2332 break;
2333 }
2334 break;
2335 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002336 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002337 "Bad zero extend!");
Evan Cheng023aef22005-12-14 22:28:18 +00002338 addFullAddress(BuildMI(BB, X86::MOVZX16rm8, 5, Result), AM);
Chris Lattnere18a4c42005-01-15 05:22:24 +00002339 break;
2340 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00002341 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002342 "Bad zero extend!");
2343 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2344 break;
2345 }
2346 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002347 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002348 case ISD::SEXTLOAD: {
2349 // Make sure we generate both values.
2350 if (Result != 1)
2351 ExprMap[N.getValue(1)] = 1; // Generate the token
2352 else
2353 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2354
2355 X86AddressMode AM;
2356 if (getRegPressure(Node->getOperand(0)) >
2357 getRegPressure(Node->getOperand(1))) {
2358 Select(Node->getOperand(0)); // chain
2359 SelectAddress(Node->getOperand(1), AM);
2360 } else {
2361 SelectAddress(Node->getOperand(1), AM);
2362 Select(Node->getOperand(0)); // chain
2363 }
2364
2365 switch (Node->getValueType(0)) {
2366 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2367 default: assert(0 && "Unknown type to sign extend to.");
2368 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00002369 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00002370 default:
2371 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2372 case MVT::i8:
2373 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2374 break;
2375 case MVT::i16:
2376 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2377 break;
2378 }
2379 break;
2380 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00002381 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00002382 "Cannot sign extend from bool!");
2383 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2384 break;
2385 }
2386 return Result;
2387 }
2388
Chris Lattner1b3520c2005-05-14 08:48:15 +00002389 case X86ISD::TAILCALL:
2390 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00002391 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002392 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00002393
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002394 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
2395 isa<ExternalSymbolSDNode>(N.getOperand(1));
2396 unsigned Callee = 0;
2397 if (isDirect) {
2398 Select(N.getOperand(0));
2399 } else {
2400 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2401 Select(N.getOperand(0));
2402 Callee = SelectExpr(N.getOperand(1));
2403 } else {
2404 Callee = SelectExpr(N.getOperand(1));
2405 Select(N.getOperand(0));
2406 }
2407 }
2408
2409 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002410 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002411 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002412 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002413 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00002414 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002415
Chris Lattner1b3520c2005-05-14 08:48:15 +00002416 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002417 default: assert(0 && "Bad thing to pass in regs");
2418 case MVT::i1:
2419 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
2420 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
2421 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
2422 }
2423 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00002424 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002425 default: assert(0 && "Bad thing to pass in regs");
2426 case MVT::i1:
2427 case MVT::i8:
2428 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
2429 break;
2430 case MVT::i16:
2431 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
2432 break;
2433 case MVT::i32:
2434 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
2435 break;
2436 }
2437 }
2438
Chris Lattner88c8a232005-01-07 07:49:41 +00002439 if (GlobalAddressSDNode *GASD =
2440 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
2441 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2442 } else if (ExternalSymbolSDNode *ESSDN =
2443 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
2444 BuildMI(BB, X86::CALLpcrel32,
2445 1).addExternalSymbol(ESSDN->getSymbol(), true);
2446 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002447 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2448 Select(N.getOperand(0));
2449 Tmp1 = SelectExpr(N.getOperand(1));
2450 } else {
2451 Tmp1 = SelectExpr(N.getOperand(1));
2452 Select(N.getOperand(0));
2453 }
2454
Chris Lattner88c8a232005-01-07 07:49:41 +00002455 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2456 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002457
2458 // Get caller stack amount and amount the callee added to the stack pointer.
2459 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
2460 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
2461 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
2462
2463 if (Node->getNumValues() != 1)
2464 switch (Node->getValueType(1)) {
2465 default: assert(0 && "Unknown value type for call result!");
2466 case MVT::Other: return 1;
2467 case MVT::i1:
2468 case MVT::i8:
2469 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2470 break;
2471 case MVT::i16:
2472 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2473 break;
2474 case MVT::i32:
2475 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2476 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
2477 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2478 break;
2479 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00002480 if (X86ScalarSSE) {
2481 ContainsFPCode = true;
2482 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
2483
2484 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
2485 MachineFunction *F = BB->getParent();
2486 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerf431ad42005-12-21 07:47:04 +00002487 addFrameReference(BuildMI(BB, X86::FpST64m, 5), FrameIdx).addReg(X86::FP0);
Nate Begeman8a093362005-07-06 18:59:04 +00002488 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
2489 break;
2490 } else {
2491 ContainsFPCode = true;
2492 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2493 break;
2494 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00002495 }
2496 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002497 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00002498 case ISD::READPORT:
2499 // First, determine that the size of the operand falls within the acceptable
2500 // range for this architecture.
2501 //
2502 if (Node->getOperand(1).getValueType() != MVT::i16) {
2503 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2504 exit(1);
2505 }
2506
2507 // Make sure we generate both values.
2508 if (Result != 1) { // Generate the token
2509 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2510 assert(0 && "readport already emitted!?");
2511 } else
2512 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002513
Chris Lattner70ea07c2005-05-09 21:17:38 +00002514 Select(Node->getOperand(0)); // Select the chain.
2515
2516 // If the port is a single-byte constant, use the immediate form.
2517 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2518 if ((Port->getValue() & 255) == Port->getValue()) {
2519 switch (Node->getValueType(0)) {
2520 case MVT::i8:
2521 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2522 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2523 return Result;
2524 case MVT::i16:
2525 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2526 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2527 return Result;
2528 case MVT::i32:
2529 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2530 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2531 return Result;
2532 default: break;
2533 }
2534 }
2535
2536 // Now, move the I/O port address into the DX register and use the IN
2537 // instruction to get the input data.
2538 //
2539 Tmp1 = SelectExpr(Node->getOperand(1));
2540 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2541 switch (Node->getValueType(0)) {
2542 case MVT::i8:
2543 BuildMI(BB, X86::IN8rr, 0);
2544 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2545 return Result;
2546 case MVT::i16:
2547 BuildMI(BB, X86::IN16rr, 0);
2548 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2549 return Result;
2550 case MVT::i32:
2551 BuildMI(BB, X86::IN32rr, 0);
2552 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2553 return Result;
2554 default:
2555 std::cerr << "Cannot do input on this data type";
2556 exit(1);
2557 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002558
Chris Lattner88c8a232005-01-07 07:49:41 +00002559 }
2560
2561 return 0;
2562}
2563
Chris Lattner96113fd2005-01-17 19:25:26 +00002564/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2565/// load/op/store instruction. If successful return true.
2566bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2567 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2568 SDOperand Chain = Node->getOperand(0);
2569 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00002570 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00002571
2572 // The chain has to be a load, the stored value must be an integer binary
2573 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00002574 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00002575 MVT::isFloatingPoint(StVal.getValueType()))
2576 return false;
2577
Chris Lattnere86c9332005-01-17 22:10:42 +00002578 // Token chain must either be a factor node or the load to fold.
2579 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2580 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00002581
Chris Lattnere86c9332005-01-17 22:10:42 +00002582 SDOperand TheLoad;
2583
2584 // Check to see if there is a load from the same pointer that we're storing
2585 // to in either operand of the binop.
2586 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2587 StVal.getOperand(0).getOperand(1) == StPtr)
2588 TheLoad = StVal.getOperand(0);
2589 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2590 StVal.getOperand(1).getOperand(1) == StPtr)
2591 TheLoad = StVal.getOperand(1);
2592 else
2593 return false; // No matching load operand.
2594
2595 // We can only fold the load if there are no intervening side-effecting
2596 // operations. This means that the store uses the load as its token chain, or
2597 // there are only token factor nodes in between the store and load.
2598 if (Chain != TheLoad.getValue(1)) {
2599 // Okay, the other option is that we have a store referring to (possibly
2600 // nested) token factor nodes. For now, just try peeking through one level
2601 // of token factors to see if this is the case.
2602 bool ChainOk = false;
2603 if (Chain.getOpcode() == ISD::TokenFactor) {
2604 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2605 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2606 ChainOk = true;
2607 break;
2608 }
2609 }
2610
2611 if (!ChainOk) return false;
2612 }
2613
2614 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00002615 return false;
2616
2617 // Make sure that one of the operands of the binop is the load, and that the
2618 // load folds into the binop.
2619 if (((StVal.getOperand(0) != TheLoad ||
2620 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2621 (StVal.getOperand(1) != TheLoad ||
2622 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2623 return false;
2624
2625 // Finally, check to see if this is one of the ops we can handle!
2626 static const unsigned ADDTAB[] = {
2627 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2628 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2629 };
2630 static const unsigned SUBTAB[] = {
2631 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2632 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2633 };
2634 static const unsigned ANDTAB[] = {
2635 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2636 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2637 };
2638 static const unsigned ORTAB[] = {
2639 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2640 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2641 };
2642 static const unsigned XORTAB[] = {
2643 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2644 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2645 };
2646 static const unsigned SHLTAB[] = {
2647 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2648 /*Have to put the reg in CL*/0, 0, 0,
2649 };
2650 static const unsigned SARTAB[] = {
2651 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2652 /*Have to put the reg in CL*/0, 0, 0,
2653 };
2654 static const unsigned SHRTAB[] = {
2655 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2656 /*Have to put the reg in CL*/0, 0, 0,
2657 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00002658
Chris Lattner96113fd2005-01-17 19:25:26 +00002659 const unsigned *TabPtr = 0;
2660 switch (StVal.getOpcode()) {
2661 default:
2662 std::cerr << "CANNOT [mem] op= val: ";
2663 StVal.Val->dump(); std::cerr << "\n";
Chris Lattner0815dcae2005-09-28 22:29:17 +00002664 case ISD::FMUL:
Chris Lattner96113fd2005-01-17 19:25:26 +00002665 case ISD::MUL:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002666 case ISD::FDIV:
Chris Lattner96113fd2005-01-17 19:25:26 +00002667 case ISD::SDIV:
2668 case ISD::UDIV:
Chris Lattner0815dcae2005-09-28 22:29:17 +00002669 case ISD::FREM:
Chris Lattner96113fd2005-01-17 19:25:26 +00002670 case ISD::SREM:
2671 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002672
Chris Lattner96113fd2005-01-17 19:25:26 +00002673 case ISD::ADD: TabPtr = ADDTAB; break;
2674 case ISD::SUB: TabPtr = SUBTAB; break;
2675 case ISD::AND: TabPtr = ANDTAB; break;
2676 case ISD:: OR: TabPtr = ORTAB; break;
2677 case ISD::XOR: TabPtr = XORTAB; break;
2678 case ISD::SHL: TabPtr = SHLTAB; break;
2679 case ISD::SRA: TabPtr = SARTAB; break;
2680 case ISD::SRL: TabPtr = SHRTAB; break;
2681 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002682
Chris Lattner96113fd2005-01-17 19:25:26 +00002683 // Handle: [mem] op= CST
2684 SDOperand Op0 = StVal.getOperand(0);
2685 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00002686 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00002687 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2688 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2689 default: break;
2690 case MVT::i1:
2691 case MVT::i8: Opc = TabPtr[0]; break;
2692 case MVT::i16: Opc = TabPtr[1]; break;
2693 case MVT::i32: Opc = TabPtr[2]; break;
2694 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002695
Chris Lattner96113fd2005-01-17 19:25:26 +00002696 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00002697 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2698 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002699 Select(Chain);
2700
Chris Lattner96113fd2005-01-17 19:25:26 +00002701 X86AddressMode AM;
2702 if (getRegPressure(TheLoad.getOperand(0)) >
2703 getRegPressure(TheLoad.getOperand(1))) {
2704 Select(TheLoad.getOperand(0));
2705 SelectAddress(TheLoad.getOperand(1), AM);
2706 } else {
2707 SelectAddress(TheLoad.getOperand(1), AM);
2708 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00002709 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002710
2711 if (StVal.getOpcode() == ISD::ADD) {
2712 if (CN->getValue() == 1) {
2713 switch (Op0.getValueType()) {
2714 default: break;
2715 case MVT::i8:
2716 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2717 return true;
2718 case MVT::i16: Opc = TabPtr[1];
2719 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2720 return true;
2721 case MVT::i32: Opc = TabPtr[2];
2722 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2723 return true;
2724 }
2725 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2726 switch (Op0.getValueType()) {
2727 default: break;
2728 case MVT::i8:
2729 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2730 return true;
2731 case MVT::i16: Opc = TabPtr[1];
2732 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2733 return true;
2734 case MVT::i32: Opc = TabPtr[2];
2735 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2736 return true;
2737 }
2738 }
2739 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002740
Chris Lattner96113fd2005-01-17 19:25:26 +00002741 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2742 return true;
2743 }
2744 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002745
Chris Lattner96113fd2005-01-17 19:25:26 +00002746 // If we have [mem] = V op [mem], try to turn it into:
2747 // [mem] = [mem] op V.
Chris Lattner0815dcae2005-09-28 22:29:17 +00002748 if (Op1 == TheLoad &&
2749 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
Chris Lattner96113fd2005-01-17 19:25:26 +00002750 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2751 StVal.getOpcode() != ISD::SRL)
2752 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002753
Chris Lattner96113fd2005-01-17 19:25:26 +00002754 if (Op0 != TheLoad) return false;
2755
2756 switch (Op0.getValueType()) {
2757 default: return false;
2758 case MVT::i1:
2759 case MVT::i8: Opc = TabPtr[3]; break;
2760 case MVT::i16: Opc = TabPtr[4]; break;
2761 case MVT::i32: Opc = TabPtr[5]; break;
2762 }
Chris Lattnere86c9332005-01-17 22:10:42 +00002763
Chris Lattner479c7112005-01-18 17:35:28 +00002764 // Table entry doesn't exist?
2765 if (Opc == 0) return false;
2766
Chris Lattner78d30282005-01-18 03:51:59 +00002767 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2768 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00002769 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00002770 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00002771
Chris Lattner96113fd2005-01-17 19:25:26 +00002772 X86AddressMode AM;
2773 SelectAddress(TheLoad.getOperand(1), AM);
2774 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002775 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00002776 return true;
2777}
2778
Chris Lattnerdd66a412005-05-15 05:46:45 +00002779/// If node is a ret(tailcall) node, emit the specified tail call and return
2780/// true, otherwise return false.
2781///
2782/// FIXME: This whole thing should be a post-legalize optimization pass which
2783/// recognizes and transforms the dag. We don't want the selection phase doing
2784/// this stuff!!
2785///
2786bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
2787 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
2788
2789 SDOperand Chain = RetNode->getOperand(0);
2790
2791 // If this is a token factor node where one operand is a call, dig into it.
2792 SDOperand TokFactor;
2793 unsigned TokFactorOperand = 0;
2794 if (Chain.getOpcode() == ISD::TokenFactor) {
2795 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2796 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
2797 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
2798 TokFactorOperand = i;
2799 TokFactor = Chain;
2800 Chain = Chain.getOperand(i);
2801 break;
2802 }
2803 if (TokFactor.Val == 0) return false; // No call operand.
2804 }
2805
2806 // Skip the CALLSEQ_END node if present.
2807 if (Chain.getOpcode() == ISD::CALLSEQ_END)
2808 Chain = Chain.getOperand(0);
2809
2810 // Is a tailcall the last control operation that occurs before the return?
2811 if (Chain.getOpcode() != X86ISD::TAILCALL)
2812 return false;
2813
2814 // If we return a value, is it the value produced by the call?
2815 if (RetNode->getNumOperands() > 1) {
2816 // Not returning the ret val of the call?
2817 if (Chain.Val->getNumValues() == 1 ||
2818 RetNode->getOperand(1) != Chain.getValue(1))
2819 return false;
2820
2821 if (RetNode->getNumOperands() > 2) {
2822 if (Chain.Val->getNumValues() == 2 ||
2823 RetNode->getOperand(2) != Chain.getValue(2))
2824 return false;
2825 }
2826 assert(RetNode->getNumOperands() <= 3);
2827 }
2828
2829 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
2830 // For FastCC, this will always be > 0.
2831 unsigned CalleeCallArgAmt =
2832 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
2833
2834 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
2835 // callee. For FastCC this will always be > 0, for CCC this is always 0.
2836 unsigned CalleeCallArgPopAmt =
2837 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
2838
2839 // There are several cases we can handle here. First, if the caller and
2840 // callee are both CCC functions, we can tailcall if the callee takes <= the
2841 // number of argument bytes that the caller does.
2842 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
2843 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
2844 // Check to see if caller arg area size >= callee arg area size.
2845 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
2846 //std::cerr << "CCC TAILCALL UNIMP!\n";
2847 // If TokFactor is non-null, emit all operands.
2848
2849 //EmitCCCToCCCTailCall(Chain.Val);
2850 //return true;
2851 }
2852 return false;
2853 }
2854
2855 // Second, if both are FastCC functions, we can always perform the tail call.
2856 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
2857 // If TokFactor is non-null, emit all operands before the call.
2858 if (TokFactor.Val) {
2859 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
2860 if (i != TokFactorOperand)
2861 Select(TokFactor.getOperand(i));
2862 }
2863
2864 EmitFastCCToFastCCTailCall(Chain.Val);
2865 return true;
2866 }
2867
2868 // We don't support mixed calls, due to issues with alignment. We could in
2869 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
2870 // aligned (which depends on the number of arguments to the callee). TODO.
2871 return false;
2872}
2873
2874static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
2875 SelectionDAG &DAG) {
2876 MVT::ValueType StoreVT;
2877 switch (Chain.getOpcode()) {
Chris Lattnerc1469402005-08-25 00:05:15 +00002878 default: assert(0 && "Unexpected node!");
Chris Lattnerdd66a412005-05-15 05:46:45 +00002879 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00002880 // If we found the start of the call sequence, we're done. We actually
2881 // strip off the CALLSEQ_START node, to avoid generating the
2882 // ADJCALLSTACKDOWN marker for the tail call.
2883 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002884 case ISD::TokenFactor: {
2885 std::vector<SDOperand> Ops;
2886 Ops.reserve(Chain.getNumOperands());
2887 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2888 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
2889 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
2890 }
2891 case ISD::STORE: // Normal store
2892 StoreVT = Chain.getOperand(1).getValueType();
2893 break;
2894 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00002895 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00002896 break;
2897 }
2898
2899 SDOperand OrigDest = Chain.getOperand(2);
2900 unsigned OrigOffset;
2901
2902 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
2903 OrigOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +00002904 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattnerbc7226a2006-01-25 08:00:36 +00002905 } else if (OrigDest.getOpcode() == ISD::ADD &&
2906 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
2907 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
2908 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
2909 == X86::ESP) {
Chris Lattnerdd66a412005-05-15 05:46:45 +00002910 // We expect only (ESP+C)
Chris Lattnerbc7226a2006-01-25 08:00:36 +00002911 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2912 } else if (OrigDest.getOpcode() == ISD::Register) {
2913 // We expect only (ESP+C)
2914 OrigOffset = 0;
2915 } else {
Chris Lattnerdd66a412005-05-15 05:46:45 +00002916 assert(OrigDest.getOpcode() == ISD::ADD &&
2917 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
Chris Lattnerbc7226a2006-01-25 08:00:36 +00002918 OrigDest.getOperand(0).getOpcode() == ISD::Register &&
2919 cast<RegisterSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002920 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
2921 }
2922
2923 // Compute the new offset from the incoming ESP value we wish to use.
2924 unsigned NewOffset = OrigOffset + Offset;
2925
2926 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
2927 MachineFunction &MF = DAG.getMachineFunction();
2928 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
2929 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
2930
2931 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
2932 DAG);
2933 if (Chain.getOpcode() == ISD::STORE)
2934 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
2935 FIN);
2936 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
2937 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00002938 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00002939}
2940
2941
2942/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
2943/// fastcc function from a fastcc function, emit the code to emit a 'proper'
2944/// tail call.
2945void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
2946 unsigned CalleeCallArgSize =
2947 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
2948 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
2949
2950 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
2951
2952 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
2953 // indexes that are relative to the incoming ESP. If the incoming and
2954 // outgoing arg sizes are the same we will store to [InESP] instead of
2955 // [CurESP] and the ESP referenced will be relative to the incoming function
2956 // ESP.
2957 int ESPOffset = CallerArgSize-CalleeCallArgSize;
2958 SDOperand AdjustedArgStores =
2959 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
2960
2961 // Copy the return address of the caller into a virtual register so we don't
2962 // clobber it.
Chris Lattnerefbb8da2006-01-06 17:56:38 +00002963 SDOperand RetVal(0, 0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00002964 if (ESPOffset) {
2965 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
2966 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
2967 RetValAddr, TheDAG->getSrcValue(NULL));
2968 SelectExpr(RetVal);
2969 }
2970
2971 // Codegen all of the argument stores.
2972 Select(AdjustedArgStores);
2973
2974 if (RetVal.Val) {
2975 // Emit a store of the saved ret value to the new location.
2976 MachineFunction &MF = TheDAG->getMachineFunction();
2977 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
2978 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
2979 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
2980 RetVal, RetValAddr));
2981 }
2982
2983 // Get the destination value.
2984 SDOperand Callee = TailCallNode->getOperand(1);
2985 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
2986 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00002987 unsigned CalleeReg = 0;
Chris Lattner7e792922005-12-04 06:03:50 +00002988 if (!isDirect) {
2989 // If this is not a direct tail call, evaluate the callee's address.
2990 CalleeReg = SelectExpr(Callee);
2991 }
Chris Lattnerdd66a412005-05-15 05:46:45 +00002992
2993 unsigned RegOp1 = 0;
2994 unsigned RegOp2 = 0;
2995
2996 if (TailCallNode->getNumOperands() > 4) {
2997 // The first value is passed in (a part of) EAX, the second in EDX.
2998 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
2999 if (TailCallNode->getNumOperands() > 5)
3000 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003001
Chris Lattnerdd66a412005-05-15 05:46:45 +00003002 switch (TailCallNode->getOperand(4).getValueType()) {
3003 default: assert(0 && "Bad thing to pass in regs");
3004 case MVT::i1:
3005 case MVT::i8:
3006 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
3007 RegOp1 = X86::AL;
3008 break;
3009 case MVT::i16:
3010 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
3011 RegOp1 = X86::AX;
3012 break;
3013 case MVT::i32:
3014 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
3015 RegOp1 = X86::EAX;
3016 break;
3017 }
3018 if (RegOp2)
3019 switch (TailCallNode->getOperand(5).getValueType()) {
3020 default: assert(0 && "Bad thing to pass in regs");
3021 case MVT::i1:
3022 case MVT::i8:
3023 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
3024 RegOp2 = X86::DL;
3025 break;
3026 case MVT::i16:
3027 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3028 RegOp2 = X86::DX;
3029 break;
3030 case MVT::i32:
3031 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3032 RegOp2 = X86::EDX;
3033 break;
3034 }
3035 }
Chris Lattner7e792922005-12-04 06:03:50 +00003036
3037 // If this is not a direct tail call, put the callee's address into ECX.
3038 // The address has to be evaluated into a non-callee save register that is
3039 // not used for arguments. This means either ECX, as EAX and EDX may be
3040 // used for argument passing. We do this here to make sure that the
3041 // expressions for arguments and callee are all evaluated before the copies
3042 // into physical registers.
3043 if (!isDirect)
3044 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CalleeReg);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003045
3046 // Adjust ESP.
3047 if (ESPOffset)
3048 BuildMI(BB, X86::ADJSTACKPTRri, 2,
3049 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
3050
3051 // TODO: handle jmp [mem]
3052 if (!isDirect) {
Chris Lattner7e792922005-12-04 06:03:50 +00003053 BuildMI(BB, X86::TAILJMPr, 1).addReg(X86::ECX);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003054 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00003055 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003056 } else {
3057 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
3058 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
3059 }
3060 // ADD IMPLICIT USE RegOp1/RegOp2's
3061}
3062
Chris Lattner96113fd2005-01-17 19:25:26 +00003063
Chris Lattner88c8a232005-01-07 07:49:41 +00003064void ISel::Select(SDOperand N) {
Chris Lattner9982da22005-10-02 16:29:36 +00003065 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
Chris Lattner88c8a232005-01-07 07:49:41 +00003066
Nate Begeman95210522005-03-24 04:39:54 +00003067 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003068 return; // Already selected.
3069
Chris Lattner36f78482005-01-11 06:14:36 +00003070 SDNode *Node = N.Val;
3071
3072 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003073 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003074 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003075 assert(0 && "Node not handled yet!");
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00003076 case X86ISD::RDTSC_DAG:
3077 Select(Node->getOperand(0)); //Chain
3078 BuildMI(BB, X86::RDTSC, 0);
3079 return;
3080
Chris Lattner88c8a232005-01-07 07:49:41 +00003081 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003082 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003083 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003084 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003085 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3086 Select(Node->getOperand(OneFirst));
3087 Select(Node->getOperand(!OneFirst));
3088 } else {
3089 std::vector<std::pair<unsigned, unsigned> > OpsP;
3090 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3091 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3092 std::sort(OpsP.begin(), OpsP.end());
3093 std::reverse(OpsP.begin(), OpsP.end());
3094 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3095 Select(Node->getOperand(OpsP[i].second));
3096 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003097 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003098 case ISD::CopyToReg:
Chris Lattner7c762782005-08-16 21:56:37 +00003099 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattner2cfce682005-01-12 02:02:48 +00003100 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00003101 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003102 } else {
Chris Lattner7c762782005-08-16 21:56:37 +00003103 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00003104 Select(N.getOperand(0));
3105 }
Chris Lattner7c762782005-08-16 21:56:37 +00003106 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003107
Chris Lattner88c8a232005-01-07 07:49:41 +00003108 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00003109 switch (N.getOperand(2).getValueType()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003110 default: assert(0 && "Invalid type for operation!");
3111 case MVT::i1:
3112 case MVT::i8: Opc = X86::MOV8rr; break;
3113 case MVT::i16: Opc = X86::MOV16rr; break;
3114 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman9d7008b2005-10-14 22:06:00 +00003115 case MVT::f32: Opc = X86::MOVSSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003116 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003117 if (X86ScalarSSE) {
Nate Begeman9d7008b2005-10-14 22:06:00 +00003118 Opc = X86::MOVSDrr;
Nate Begeman8a093362005-07-06 18:59:04 +00003119 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003120 Opc = X86::FpMOV;
3121 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003122 }
3123 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003124 }
3125 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3126 }
3127 return;
3128 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00003129 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
3130 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
3131 N.getOperand(0).getOpcode() == ISD::TokenFactor)
3132 if (EmitPotentialTailCall(Node))
3133 return;
3134
Chris Lattner88c8a232005-01-07 07:49:41 +00003135 switch (N.getNumOperands()) {
3136 default:
3137 assert(0 && "Unknown return instruction!");
3138 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003139 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003140 N.getOperand(2).getValueType() == MVT::i32 &&
3141 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003142 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3143 Tmp1 = SelectExpr(N.getOperand(1));
3144 Tmp2 = SelectExpr(N.getOperand(2));
3145 } else {
3146 Tmp2 = SelectExpr(N.getOperand(2));
3147 Tmp1 = SelectExpr(N.getOperand(1));
3148 }
3149 Select(N.getOperand(0));
3150
Chris Lattner88c8a232005-01-07 07:49:41 +00003151 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3152 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003153 break;
3154 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003155 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3156 Select(N.getOperand(0));
3157 Tmp1 = SelectExpr(N.getOperand(1));
3158 } else {
3159 Tmp1 = SelectExpr(N.getOperand(1));
3160 Select(N.getOperand(0));
3161 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003162 switch (N.getOperand(1).getValueType()) {
3163 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00003164 case MVT::f32:
3165 if (X86ScalarSSE) {
3166 // Spill the value to memory and reload it into top of stack.
3167 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
3168 MachineFunction *F = BB->getParent();
3169 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3170 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003171 addFrameReference(BuildMI(BB, X86::FpLD32m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003172 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003173 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003174 } else {
3175 assert(0 && "MVT::f32 only legal with scalar sse fp");
3176 abort();
3177 }
3178 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003179 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003180 if (X86ScalarSSE) {
3181 // Spill the value to memory and reload it into top of stack.
3182 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3183 MachineFunction *F = BB->getParent();
3184 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3185 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
Chris Lattnerf431ad42005-12-21 07:47:04 +00003186 addFrameReference(BuildMI(BB, X86::FpLD64m, 4, X86::FP0), FrameIdx);
Nate Begeman8a093362005-07-06 18:59:04 +00003187 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003188 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003189 } else {
3190 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
3191 }
3192 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003193 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00003194 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3195 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003196 }
3197 break;
3198 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003199 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003200 break;
3201 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003202 if (X86Lowering.getBytesToPopOnReturn() == 0)
3203 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3204 else
3205 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00003206 return;
3207 case ISD::BR: {
3208 Select(N.getOperand(0));
3209 MachineBasicBlock *Dest =
3210 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3211 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3212 return;
3213 }
3214
3215 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003216 MachineBasicBlock *Dest =
3217 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003218
Chris Lattner88c8a232005-01-07 07:49:41 +00003219 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3220 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003221 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3222 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3223 Select(N.getOperand(0));
3224 Tmp1 = SelectExpr(N.getOperand(1));
3225 } else {
3226 Tmp1 = SelectExpr(N.getOperand(1));
3227 Select(N.getOperand(0));
3228 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003229 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3230 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3231 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003232
Chris Lattner88c8a232005-01-07 07:49:41 +00003233 return;
3234 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003235
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003236 case ISD::LOAD:
3237 // If this load could be folded into the only using instruction, and if it
3238 // is safe to emit the instruction here, try to do so now.
3239 if (Node->hasNUsesOfValue(1, 0)) {
3240 SDOperand TheVal = N.getValue(0);
3241 SDNode *User = 0;
3242 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3243 assert(UI != Node->use_end() && "Didn't find use!");
3244 SDNode *UN = *UI;
3245 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3246 if (UN->getOperand(i) == TheVal) {
3247 User = UN;
3248 goto FoundIt;
3249 }
3250 }
3251 FoundIt:
3252 // Only handle unary operators right now.
3253 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003254 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003255 SelectExpr(SDOperand(User, 0));
3256 return;
3257 }
3258 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003259 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003260 SelectExpr(N);
3261 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003262 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003263 case ISD::EXTLOAD:
3264 case ISD::SEXTLOAD:
3265 case ISD::ZEXTLOAD:
Chris Lattner1b3520c2005-05-14 08:48:15 +00003266 case X86ISD::TAILCALL:
3267 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00003268 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003269 SelectExpr(N);
3270 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003271 case ISD::CopyFromReg:
Evan Cheng6305e502006-01-12 22:54:21 +00003272 case X86ISD::FILD:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003273 ExprMap.erase(N);
3274 SelectExpr(N.getValue(0));
3275 return;
Jeff Cohen546fd592005-07-30 18:33:25 +00003276
Chris Lattner4738d1b2005-07-30 00:05:54 +00003277 case X86ISD::FP_TO_INT16_IN_MEM:
3278 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattner6dc60e82005-07-29 00:54:34 +00003279 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00003280 assert(N.getOperand(1).getValueType() == MVT::f64);
3281 X86AddressMode AM;
3282 Select(N.getOperand(0)); // Select the token chain
3283
3284 unsigned ValReg;
3285 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3286 ValReg = SelectExpr(N.getOperand(1));
3287 SelectAddress(N.getOperand(2), AM);
3288 } else {
3289 SelectAddress(N.getOperand(2), AM);
3290 ValReg = SelectExpr(N.getOperand(1));
3291 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003292
Chris Lattner6dc60e82005-07-29 00:54:34 +00003293 // Change the floating point control register to use "round towards zero"
3294 // mode when truncating to an integer value.
3295 //
3296 MachineFunction *F = BB->getParent();
3297 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
3298 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003299
Chris Lattner6dc60e82005-07-29 00:54:34 +00003300 // Load the old value of the high byte of the control word...
Chris Lattneraeef51b2005-07-30 00:17:52 +00003301 unsigned OldCW = MakeReg(MVT::i16);
3302 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003303
Chris Lattner6dc60e82005-07-29 00:54:34 +00003304 // Set the high part to be round to zero...
Chris Lattner49134572005-07-30 00:43:00 +00003305 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohen546fd592005-07-30 18:33:25 +00003306
Chris Lattner6dc60e82005-07-29 00:54:34 +00003307 // Reload the modified control word now...
3308 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00003309
Chris Lattner6dc60e82005-07-29 00:54:34 +00003310 // Restore the memory image of control word to original value
Chris Lattneraeef51b2005-07-30 00:17:52 +00003311 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner4738d1b2005-07-30 00:05:54 +00003312
3313 // Get the X86 opcode to use.
3314 switch (N.getOpcode()) {
Chris Lattnerf431ad42005-12-21 07:47:04 +00003315 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FpIST16m; break;
3316 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FpIST32m; break;
3317 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FpIST64m; break;
Chris Lattner4738d1b2005-07-30 00:05:54 +00003318 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003319
Chris Lattner4738d1b2005-07-30 00:05:54 +00003320 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohen546fd592005-07-30 18:33:25 +00003321
Chris Lattner6dc60e82005-07-29 00:54:34 +00003322 // Reload the original control word now.
3323 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00003324 return;
3325 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003326
Chris Lattner36db1ed2005-07-10 00:29:18 +00003327 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00003328 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00003329 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003330 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3331 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3332 && "Unsupported TRUNCSTORE for this target!");
3333
3334 if (StoredTy == MVT::i16) {
3335 // FIXME: This is here just to allow testing. X86 doesn't really have a
3336 // TRUNCSTORE i16 operation, but this is required for targets that do not
3337 // have 16-bit integer registers. We occasionally disable 16-bit integer
3338 // registers to test the promotion code.
3339 Select(N.getOperand(0));
3340 Tmp1 = SelectExpr(N.getOperand(1));
3341 SelectAddress(N.getOperand(2), AM);
3342
3343 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3344 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3345 return;
3346 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003347
3348 // Store of constant bool?
3349 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3350 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3351 Select(N.getOperand(0));
3352 SelectAddress(N.getOperand(2), AM);
3353 } else {
3354 SelectAddress(N.getOperand(2), AM);
3355 Select(N.getOperand(0));
3356 }
3357 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3358 return;
3359 }
3360
3361 switch (StoredTy) {
3362 default: assert(0 && "Cannot truncstore this type!");
3363 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003364 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003365 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Chris Lattnerf431ad42005-12-21 07:47:04 +00003366 Opc = X86::FpST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003367 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003368
Chris Lattnere18a4c42005-01-15 05:22:24 +00003369 std::vector<std::pair<unsigned, unsigned> > RP;
3370 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3371 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3372 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3373 std::sort(RP.begin(), RP.end());
3374
Chris Lattner80c5b972005-02-23 05:57:21 +00003375 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003376 for (unsigned i = 0; i != 3; ++i)
3377 switch (RP[2-i].second) {
3378 default: assert(0 && "Unknown operand number!");
3379 case 0: Select(N.getOperand(0)); break;
3380 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3381 case 2: SelectAddress(N.getOperand(2), AM); break;
3382 }
3383
3384 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3385 return;
3386 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003387 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003388 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003389
3390 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3391 Opc = 0;
3392 switch (CN->getValueType(0)) {
3393 default: assert(0 && "Invalid type for operation!");
3394 case MVT::i1:
3395 case MVT::i8: Opc = X86::MOV8mi; break;
3396 case MVT::i16: Opc = X86::MOV16mi; break;
3397 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003398 }
3399 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003400 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3401 Select(N.getOperand(0));
3402 SelectAddress(N.getOperand(2), AM);
3403 } else {
3404 SelectAddress(N.getOperand(2), AM);
3405 Select(N.getOperand(0));
3406 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003407 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3408 return;
3409 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003410 } else if (GlobalAddressSDNode *GA =
3411 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3412 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3413
3414 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3415 Select(N.getOperand(0));
3416 SelectAddress(N.getOperand(2), AM);
3417 } else {
3418 SelectAddress(N.getOperand(2), AM);
3419 Select(N.getOperand(0));
3420 }
Nate Begemana0b5e032005-07-15 00:38:55 +00003421 GlobalValue *GV = GA->getGlobal();
3422 // For Darwin, external and weak symbols are indirect, so we want to load
3423 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003424 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00003425 (GV->hasWeakLinkage() || GV->isExternal())) {
3426 Tmp1 = MakeReg(MVT::i32);
3427 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
3428 .addGlobalAddress(GV, false, 0);
3429 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
3430 } else {
3431 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
3432 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003433 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003434 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003435
3436 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003437 if (TryToFoldLoadOpStore(Node))
3438 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003439
Chris Lattner88c8a232005-01-07 07:49:41 +00003440 switch (N.getOperand(1).getValueType()) {
3441 default: assert(0 && "Cannot store this type!");
3442 case MVT::i1:
3443 case MVT::i8: Opc = X86::MOV8mr; break;
3444 case MVT::i16: Opc = X86::MOV16mr; break;
3445 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003446 case MVT::f32: Opc = X86::MOVSSmr; break;
Chris Lattnerf431ad42005-12-21 07:47:04 +00003447 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FpST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003448 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003449
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003450 std::vector<std::pair<unsigned, unsigned> > RP;
3451 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3452 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3453 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3454 std::sort(RP.begin(), RP.end());
3455
Chris Lattner80c5b972005-02-23 05:57:21 +00003456 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003457 for (unsigned i = 0; i != 3; ++i)
3458 switch (RP[2-i].second) {
3459 default: assert(0 && "Unknown operand number!");
3460 case 0: Select(N.getOperand(0)); break;
3461 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003462 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003463 }
3464
Chris Lattner88c8a232005-01-07 07:49:41 +00003465 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3466 return;
3467 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003468 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00003469 Select(N.getOperand(0));
3470 // Stack amount
3471 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3472 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3473 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00003474 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003475 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003476 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003477 case ISD::MEMSET: {
3478 Select(N.getOperand(0)); // Select the chain.
3479 unsigned Align =
3480 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3481 if (Align == 0) Align = 1;
3482
3483 // Turn the byte code into # iterations
3484 unsigned CountReg;
3485 unsigned Opcode;
3486 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3487 unsigned Val = ValC->getValue() & 255;
3488
3489 // If the value is a constant, then we can potentially use larger sets.
3490 switch (Align & 3) {
3491 case 2: // WORD aligned
3492 CountReg = MakeReg(MVT::i32);
3493 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3494 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3495 } else {
3496 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3497 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3498 }
3499 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3500 Opcode = X86::REP_STOSW;
3501 break;
3502 case 0: // DWORD aligned
3503 CountReg = MakeReg(MVT::i32);
3504 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3505 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3506 } else {
3507 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3508 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3509 }
3510 Val = (Val << 8) | Val;
3511 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3512 Opcode = X86::REP_STOSD;
3513 break;
3514 default: // BYTE aligned
3515 CountReg = SelectExpr(Node->getOperand(3));
3516 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3517 Opcode = X86::REP_STOSB;
3518 break;
3519 }
3520 } else {
3521 // If it's not a constant value we are storing, just fall back. We could
3522 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3523 unsigned ValReg = SelectExpr(Node->getOperand(2));
3524 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3525 CountReg = SelectExpr(Node->getOperand(3));
3526 Opcode = X86::REP_STOSB;
3527 }
3528
Evan Chengae986f12006-01-11 22:15:48 +00003529 // No matter what the alignment is, we put the destination in EDI, and the
3530 // count in ECX.
Chris Lattner36f78482005-01-11 06:14:36 +00003531 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3532 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3533 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3534 BuildMI(BB, Opcode, 0);
3535 return;
3536 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003537 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003538 Select(N.getOperand(0)); // Select the chain.
3539 unsigned Align =
3540 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3541 if (Align == 0) Align = 1;
3542
3543 // Turn the byte code into # iterations
3544 unsigned CountReg;
3545 unsigned Opcode;
3546 switch (Align & 3) {
3547 case 2: // WORD aligned
3548 CountReg = MakeReg(MVT::i32);
3549 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3550 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3551 } else {
3552 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3553 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3554 }
3555 Opcode = X86::REP_MOVSW;
3556 break;
3557 case 0: // DWORD aligned
3558 CountReg = MakeReg(MVT::i32);
3559 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3560 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3561 } else {
3562 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3563 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3564 }
3565 Opcode = X86::REP_MOVSD;
3566 break;
3567 default: // BYTE aligned
3568 CountReg = SelectExpr(Node->getOperand(3));
3569 Opcode = X86::REP_MOVSB;
3570 break;
3571 }
3572
3573 // No matter what the alignment is, we put the source in ESI, the
3574 // destination in EDI, and the count in ECX.
3575 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3576 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3577 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3578 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3579 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3580 BuildMI(BB, Opcode, 0);
3581 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003582 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003583 case ISD::WRITEPORT:
3584 if (Node->getOperand(2).getValueType() != MVT::i16) {
3585 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3586 exit(1);
3587 }
3588 Select(Node->getOperand(0)); // Emit the chain.
3589
3590 Tmp1 = SelectExpr(Node->getOperand(1));
3591 switch (Node->getOperand(1).getValueType()) {
3592 case MVT::i8:
3593 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3594 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3595 break;
3596 case MVT::i16:
3597 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3598 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3599 break;
3600 case MVT::i32:
3601 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3602 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3603 break;
3604 default:
3605 std::cerr << "llvm.writeport: invalid data type for X86 target";
3606 exit(1);
3607 }
3608
3609 // If the port is a single-byte constant, use the immediate form.
3610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3611 if ((CN->getValue() & 255) == CN->getValue()) {
3612 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3613 return;
3614 }
3615
3616 // Otherwise, move the I/O port address into the DX register.
3617 unsigned Reg = SelectExpr(Node->getOperand(2));
3618 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3619 BuildMI(BB, Opc, 0);
3620 return;
3621 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003622 assert(0 && "Should not be reached!");
3623}
3624
3625
Chris Lattner76ac0682005-11-15 00:40:23 +00003626/// createX86ISelPattern - This pass converts an LLVM function
Chris Lattner88c8a232005-01-07 07:49:41 +00003627/// into a machine code representation using pattern matching and a machine
3628/// description file.
3629///
Chris Lattner76ac0682005-11-15 00:40:23 +00003630FunctionPass *llvm::createX86ISelPattern(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003631 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003632}