blob: 8b3f31cecfa568b2699553985ea450cce13b6722 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-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 Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-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"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000028#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000031#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// X86TargetLowering - X86 Implementation of the TargetLowering interface
36namespace {
37 class X86TargetLowering : public TargetLowering {
38 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000039 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000040 public:
41 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
42 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000043
44 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000047 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000048 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000049
50 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000051 addRegisterClass(MVT::i8, X86::R8RegisterClass);
52 addRegisterClass(MVT::i16, X86::R16RegisterClass);
53 addRegisterClass(MVT::i32, X86::R32RegisterClass);
54 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000055
Chris Lattner8acb1ba2005-01-07 07:49:41 +000056 // FIXME: Eliminate these two classes when legalize can handle promotions
57 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000058/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000059
Chris Lattnerda4d4692005-04-09 03:22:37 +000060 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000061 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
62 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000064 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
65 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
66 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000067
Chris Lattnerc5dcb532005-04-30 04:25:35 +000068 if (!UnsafeFPMath) {
69 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
70 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
71 }
72
Chris Lattnerda2ce112005-01-16 07:34:08 +000073 // These should be promoted to a larger select which is supported.
74/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
75 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000076
Chris Lattner8acb1ba2005-01-07 07:49:41 +000077 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000078
Chris Lattner8acb1ba2005-01-07 07:49:41 +000079 addLegalFPImmediate(+0.0); // FLD0
80 addLegalFPImmediate(+1.0); // FLD1
81 addLegalFPImmediate(-0.0); // FLD0/FCHS
82 addLegalFPImmediate(-1.0); // FLD1/FCHS
83 }
84
85 /// LowerArguments - This hook must be implemented to indicate how we should
86 /// lower the arguments for the specified function, into the specified DAG.
87 virtual std::vector<SDOperand>
88 LowerArguments(Function &F, SelectionDAG &DAG);
89
90 /// LowerCallTo - This hook lowers an abstract call to a function into an
91 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000092 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000093 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
94 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000095
96 virtual std::pair<SDOperand, SDOperand>
97 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
98
99 virtual std::pair<SDOperand,SDOperand>
100 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
101 const Type *ArgTy, SelectionDAG &DAG);
102
103 virtual std::pair<SDOperand, SDOperand>
104 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
105 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000106 };
107}
108
109
110std::vector<SDOperand>
111X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
112 std::vector<SDOperand> ArgValues;
113
114 // Add DAG nodes to load the arguments... On entry to a function on the X86,
115 // the stack frame looks like this:
116 //
117 // [ESP] -- return address
118 // [ESP + 4] -- first argument (leftmost lexically)
119 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000120 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000121 //
122 MachineFunction &MF = DAG.getMachineFunction();
123 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000124
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000125 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000126 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000127 MVT::ValueType ObjectVT = getValueType(I->getType());
128 unsigned ArgIncrement = 4;
129 unsigned ObjSize;
130 switch (ObjectVT) {
131 default: assert(0 && "Unhandled argument type!");
132 case MVT::i1:
133 case MVT::i8: ObjSize = 1; break;
134 case MVT::i16: ObjSize = 2; break;
135 case MVT::i32: ObjSize = 4; break;
136 case MVT::i64: ObjSize = ArgIncrement = 8; break;
137 case MVT::f32: ObjSize = 4; break;
138 case MVT::f64: ObjSize = ArgIncrement = 8; break;
139 }
140 // Create the frame index object for this incoming parameter...
141 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000142
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000143 // Create the SelectionDAG nodes corresponding to a load from this parameter
144 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
145
146 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
147 // dead loads.
148 SDOperand ArgValue;
149 if (!I->use_empty())
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000150 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000151 else {
152 if (MVT::isInteger(ObjectVT))
153 ArgValue = DAG.getConstant(0, ObjectVT);
154 else
155 ArgValue = DAG.getConstantFP(0, ObjectVT);
156 }
157 ArgValues.push_back(ArgValue);
158
159 ArgOffset += ArgIncrement; // Move on to the next argument...
160 }
161
162 // If the function takes variable number of arguments, make a frame index for
163 // the start of the first vararg value... for expansion of llvm.va_start.
164 if (F.isVarArg())
165 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000166 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000167
168 // Finally, inform the code generator which regs we return values in.
169 switch (getValueType(F.getReturnType())) {
170 default: assert(0 && "Unknown type!");
171 case MVT::isVoid: break;
172 case MVT::i1:
173 case MVT::i8:
174 case MVT::i16:
175 case MVT::i32:
176 MF.addLiveOut(X86::EAX);
177 break;
178 case MVT::i64:
179 MF.addLiveOut(X86::EAX);
180 MF.addLiveOut(X86::EDX);
181 break;
182 case MVT::f32:
183 case MVT::f64:
184 MF.addLiveOut(X86::ST0);
185 break;
186 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000187 return ArgValues;
188}
189
Chris Lattner5188ad72005-01-08 19:28:19 +0000190std::pair<SDOperand, SDOperand>
191X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000192 const Type *RetTy, bool isVarArg,
193 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194 // Count how many bytes are to be pushed on the stack.
195 unsigned NumBytes = 0;
196
197 if (Args.empty()) {
198 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000199 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
200 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000201 } else {
202 for (unsigned i = 0, e = Args.size(); i != e; ++i)
203 switch (getValueType(Args[i].second)) {
204 default: assert(0 && "Unknown value type!");
205 case MVT::i1:
206 case MVT::i8:
207 case MVT::i16:
208 case MVT::i32:
209 case MVT::f32:
210 NumBytes += 4;
211 break;
212 case MVT::i64:
213 case MVT::f64:
214 NumBytes += 8;
215 break;
216 }
217
Chris Lattner5188ad72005-01-08 19:28:19 +0000218 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
219 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000220
221 // Arguments go on the stack in reverse order, as specified by the ABI.
222 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000223 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
224 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000225 std::vector<SDOperand> Stores;
226
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000227 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
228 unsigned ArgReg;
229 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
230 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
231
232 switch (getValueType(Args[i].second)) {
233 default: assert(0 && "Unexpected ValueType for argument!");
234 case MVT::i1:
235 case MVT::i8:
236 case MVT::i16:
237 // Promote the integer to 32 bits. If the input type is signed use a
238 // sign extend, otherwise use a zero extend.
239 if (Args[i].second->isSigned())
240 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
241 else
242 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
243
244 // FALL THROUGH
245 case MVT::i32:
246 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000247 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000248 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000249 ArgOffset += 4;
250 break;
251 case MVT::i64:
252 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000253 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000254 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000255 ArgOffset += 8;
256 break;
257 }
258 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000259 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000260 }
261
262 std::vector<MVT::ValueType> RetVals;
263 MVT::ValueType RetTyVT = getValueType(RetTy);
264 if (RetTyVT != MVT::isVoid)
265 RetVals.push_back(RetTyVT);
266 RetVals.push_back(MVT::Other);
267
Chris Lattner5188ad72005-01-08 19:28:19 +0000268 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000269 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000270 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
271 DAG.getConstant(NumBytes, getPointerTy()));
272 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000273}
274
Chris Lattner14824582005-01-09 00:01:27 +0000275std::pair<SDOperand, SDOperand>
276X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
277 // vastart just returns the address of the VarArgsFrameIndex slot.
278 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
279}
280
281std::pair<SDOperand,SDOperand> X86TargetLowering::
282LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
283 const Type *ArgTy, SelectionDAG &DAG) {
284 MVT::ValueType ArgVT = getValueType(ArgTy);
285 SDOperand Result;
286 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000287 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000288 } else {
289 unsigned Amt;
290 if (ArgVT == MVT::i32)
291 Amt = 4;
292 else {
293 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
294 "Other types should have been promoted for varargs!");
295 Amt = 8;
296 }
297 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
298 DAG.getConstant(Amt, VAList.getValueType()));
299 }
300 return std::make_pair(Result, Chain);
301}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000302
Chris Lattner14824582005-01-09 00:01:27 +0000303
304std::pair<SDOperand, SDOperand> X86TargetLowering::
305LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
306 SelectionDAG &DAG) {
307 SDOperand Result;
308 if (Depth) // Depths > 0 not supported yet!
309 Result = DAG.getConstant(0, getPointerTy());
310 else {
311 if (ReturnAddrIndex == 0) {
312 // Set up a frame object for the return address.
313 MachineFunction &MF = DAG.getMachineFunction();
314 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
315 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000316
Chris Lattner14824582005-01-09 00:01:27 +0000317 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
318
319 if (!isFrameAddress)
320 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000321 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000322 else
323 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
324 DAG.getConstant(4, MVT::i32));
325 }
326 return std::make_pair(Result, Chain);
327}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000328
329
Chris Lattner98a8ba02005-01-18 01:06:26 +0000330namespace {
331 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
332 /// SDOperand's instead of register numbers for the leaves of the matched
333 /// tree.
334 struct X86ISelAddressMode {
335 enum {
336 RegBase,
337 FrameIndexBase,
338 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000339
Chris Lattner98a8ba02005-01-18 01:06:26 +0000340 struct { // This is really a union, discriminated by BaseType!
341 SDOperand Reg;
342 int FrameIndex;
343 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000344
Chris Lattner98a8ba02005-01-18 01:06:26 +0000345 unsigned Scale;
346 SDOperand IndexReg;
347 unsigned Disp;
348 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000349
Chris Lattner98a8ba02005-01-18 01:06:26 +0000350 X86ISelAddressMode()
351 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
352 }
353 };
354}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000355
356
357namespace {
358 Statistic<>
359 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
360
361 //===--------------------------------------------------------------------===//
362 /// ISel - X86 specific code to select X86 machine instructions for
363 /// SelectionDAG operations.
364 ///
365 class ISel : public SelectionDAGISel {
366 /// ContainsFPCode - Every instruction we select that uses or defines a FP
367 /// register should set this to true.
368 bool ContainsFPCode;
369
370 /// X86Lowering - This object fully describes how to lower LLVM code to an
371 /// X86-specific SelectionDAG.
372 X86TargetLowering X86Lowering;
373
Chris Lattner11333092005-01-11 03:11:44 +0000374 /// RegPressureMap - This keeps an approximate count of the number of
375 /// registers required to evaluate each node in the graph.
376 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000377
378 /// ExprMap - As shared expressions are codegen'd, we keep track of which
379 /// vreg the value is produced in, so we only emit one copy of each compiled
380 /// tree.
381 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000382
383 public:
384 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
385 }
386
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000387 virtual const char *getPassName() const {
388 return "X86 Pattern Instruction Selection";
389 }
390
Chris Lattner11333092005-01-11 03:11:44 +0000391 unsigned getRegPressure(SDOperand O) {
392 return RegPressureMap[O.Val];
393 }
394 unsigned ComputeRegPressure(SDOperand O);
395
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000396 /// InstructionSelectBasicBlock - This callback is invoked by
397 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000398 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000399
Chris Lattner44129b52005-01-25 20:03:11 +0000400 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
401 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000402 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000403 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000404
Chris Lattner30ea1e92005-01-19 07:37:26 +0000405 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000406 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000407 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000408 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
409 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000410 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000411
412 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
413 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
414 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000415 void Select(SDOperand N);
416 };
417}
418
Chris Lattner7dbcb752005-01-12 04:21:28 +0000419/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
420/// when it has created a SelectionDAG for us to codegen.
421void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
422 // While we're doing this, keep track of whether we see any FP code for
423 // FP_REG_KILL insertion.
424 ContainsFPCode = false;
425
426 // Scan the PHI nodes that already are inserted into this basic block. If any
427 // of them is a PHI of a floating point value, we need to insert an
428 // FP_REG_KILL.
429 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
430 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
431 I != E; ++I) {
432 assert(I->getOpcode() == X86::PHI &&
433 "Isn't just PHI nodes?");
434 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
435 X86::RFPRegisterClass) {
436 ContainsFPCode = true;
437 break;
438 }
439 }
440
441 // Compute the RegPressureMap, which is an approximation for the number of
442 // registers required to compute each node.
443 ComputeRegPressure(DAG.getRoot());
444
445 // Codegen the basic block.
446 Select(DAG.getRoot());
447
448 // Finally, look at all of the successors of this block. If any contain a PHI
449 // node of FP type, we need to insert an FP_REG_KILL in this block.
450 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
451 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
452 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
453 I != E && I->getOpcode() == X86::PHI; ++I) {
454 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
455 X86::RFPRegisterClass) {
456 ContainsFPCode = true;
457 break;
458 }
459 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000460
Chris Lattner7dbcb752005-01-12 04:21:28 +0000461 // Insert FP_REG_KILL instructions into basic blocks that need them. This
462 // only occurs due to the floating point stackifier not being aggressive
463 // enough to handle arbitrary global stackification.
464 //
465 // Currently we insert an FP_REG_KILL instruction into each block that uses or
466 // defines a floating point virtual register.
467 //
468 // When the global register allocators (like linear scan) finally update live
469 // variable analysis, we can keep floating point values in registers across
470 // basic blocks. This will be a huge win, but we are waiting on the global
471 // allocators before we can do this.
472 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000473 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000474 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
475 ++NumFPKill;
476 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000477
Chris Lattner7dbcb752005-01-12 04:21:28 +0000478 // Clear state used for selection.
479 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000480 RegPressureMap.clear();
481}
482
483
Chris Lattner11333092005-01-11 03:11:44 +0000484// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
485// for the number of registers required to compute each node. This is basically
486// computing a generalized form of the Sethi-Ullman number for each node.
487unsigned ISel::ComputeRegPressure(SDOperand O) {
488 SDNode *N = O.Val;
489 unsigned &Result = RegPressureMap[N];
490 if (Result) return Result;
491
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000492 // FIXME: Should operations like CALL (which clobber lots o regs) have a
493 // higher fixed cost??
494
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000495 if (N->getNumOperands() == 0) {
496 Result = 1;
497 } else {
498 unsigned MaxRegUse = 0;
499 unsigned NumExtraMaxRegUsers = 0;
500 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
501 unsigned Regs;
502 if (N->getOperand(i).getOpcode() == ISD::Constant)
503 Regs = 0;
504 else
505 Regs = ComputeRegPressure(N->getOperand(i));
506 if (Regs > MaxRegUse) {
507 MaxRegUse = Regs;
508 NumExtraMaxRegUsers = 0;
509 } else if (Regs == MaxRegUse &&
510 N->getOperand(i).getValueType() != MVT::Other) {
511 ++NumExtraMaxRegUsers;
512 }
Chris Lattner11333092005-01-11 03:11:44 +0000513 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000514
515 if (O.getOpcode() != ISD::TokenFactor)
516 Result = MaxRegUse+NumExtraMaxRegUsers;
517 else
Chris Lattner869e0432005-01-17 23:02:13 +0000518 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000519 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000520
Chris Lattner837caa72005-01-11 23:21:30 +0000521 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000522 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000523}
524
Chris Lattnerbf52d492005-01-20 16:50:16 +0000525/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
526/// The DAG cannot have cycles in it, by definition, so the visited set is not
527/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
528/// reuse, so it prevents exponential cases.
529///
530static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
531 std::set<SDNode*> &Visited) {
532 if (N == Op) return true; // Found it.
533 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000534 if (Node->getNumOperands() == 0 || // Leaf?
535 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000536 if (!Visited.insert(Node).second) return false; // Already visited?
537
538 // Recurse for the first N-1 operands.
539 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
540 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
541 return true;
542
543 // Tail recurse for the last operand.
544 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
545}
546
Chris Lattner98a8ba02005-01-18 01:06:26 +0000547X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
548 X86AddressMode Result;
549
550 // If we need to emit two register operands, emit the one with the highest
551 // register pressure first.
552 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
553 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000554 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000555 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000556 std::set<SDNode*> Visited;
557 EmitBaseThenIndex = true;
558 // If Base ends up pointing to Index, we must emit index first. This is
559 // because of the way we fold loads, we may end up doing bad things with
560 // the folded add.
561 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
562 EmitBaseThenIndex = false;
563 } else {
564 std::set<SDNode*> Visited;
565 EmitBaseThenIndex = false;
566 // If Base ends up pointing to Index, we must emit index first. This is
567 // because of the way we fold loads, we may end up doing bad things with
568 // the folded add.
569 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
570 EmitBaseThenIndex = true;
571 }
572
573 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000574 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
575 Result.IndexReg = SelectExpr(IAM.IndexReg);
576 } else {
577 Result.IndexReg = SelectExpr(IAM.IndexReg);
578 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
579 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000580
Chris Lattner98a8ba02005-01-18 01:06:26 +0000581 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
582 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
583 } else if (IAM.IndexReg.Val) {
584 Result.IndexReg = SelectExpr(IAM.IndexReg);
585 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000586
Chris Lattner98a8ba02005-01-18 01:06:26 +0000587 switch (IAM.BaseType) {
588 case X86ISelAddressMode::RegBase:
589 Result.BaseType = X86AddressMode::RegBase;
590 break;
591 case X86ISelAddressMode::FrameIndexBase:
592 Result.BaseType = X86AddressMode::FrameIndexBase;
593 Result.Base.FrameIndex = IAM.Base.FrameIndex;
594 break;
595 default:
596 assert(0 && "Unknown base type!");
597 break;
598 }
599 Result.Scale = IAM.Scale;
600 Result.Disp = IAM.Disp;
601 Result.GV = IAM.GV;
602 return Result;
603}
604
605/// SelectAddress - Pattern match the maximal addressing mode for this node and
606/// emit all of the leaf registers.
607void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
608 X86ISelAddressMode IAM;
609 MatchAddress(N, IAM);
610 AM = SelectAddrExprs(IAM);
611}
612
613/// MatchAddress - Add the specified node to the specified addressing mode,
614/// returning true if it cannot be done. This just pattern matches for the
615/// addressing mode, it does not cause any code to be emitted. For that, use
616/// SelectAddress.
617bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000618 switch (N.getOpcode()) {
619 default: break;
620 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000621 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
622 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000623 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
624 return false;
625 }
626 break;
627 case ISD::GlobalAddress:
628 if (AM.GV == 0) {
629 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
630 return false;
631 }
632 break;
633 case ISD::Constant:
634 AM.Disp += cast<ConstantSDNode>(N)->getValue();
635 return false;
636 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000637 // We might have folded the load into this shift, so don't regen the value
638 // if so.
639 if (ExprMap.count(N)) break;
640
Chris Lattner98a8ba02005-01-18 01:06:26 +0000641 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000642 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
643 unsigned Val = CN->getValue();
644 if (Val == 1 || Val == 2 || Val == 3) {
645 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000646 SDOperand ShVal = N.Val->getOperand(0);
647
648 // Okay, we know that we have a scale by now. However, if the scaled
649 // value is an add of something and a constant, we can fold the
650 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000651 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000652 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000653 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000654 ConstantSDNode *AddVal =
655 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
656 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000657 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000658 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000659 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000660 return false;
661 }
662 }
663 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000664 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000665 // We might have folded the load into this mul, so don't regen the value if
666 // so.
667 if (ExprMap.count(N)) break;
668
Chris Lattner947d5442005-01-11 19:37:02 +0000669 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000670 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
671 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000672 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
673 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
674 AM.Scale = unsigned(CN->getValue())-1;
675
676 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000677 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000678
679 // Okay, we know that we have a scale by now. However, if the scaled
680 // value is an add of something and a constant, we can fold the
681 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000682 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000683 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000684 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000685 ConstantSDNode *AddVal =
686 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
687 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000688 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000689 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000690 }
691
692 AM.IndexReg = AM.Base.Reg = Reg;
693 return false;
694 }
695 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000696
697 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000698 // We might have folded the load into this mul, so don't regen the value if
699 // so.
700 if (ExprMap.count(N)) break;
701
Chris Lattner98a8ba02005-01-18 01:06:26 +0000702 X86ISelAddressMode Backup = AM;
703 if (!MatchAddress(N.Val->getOperand(0), AM) &&
704 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000705 return false;
706 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000707 if (!MatchAddress(N.Val->getOperand(1), AM) &&
708 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000709 return false;
710 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000711 break;
712 }
713 }
714
Chris Lattnera95589b2005-01-11 04:40:19 +0000715 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000716 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000717 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000718 if (AM.IndexReg.Val == 0) {
719 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000720 AM.Scale = 1;
721 return false;
722 }
723
724 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000725 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000726 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000727
728 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000729 AM.BaseType = X86ISelAddressMode::RegBase;
730 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000731 return false;
732}
733
734/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
735/// assuming that the temporary registers are in the 8-bit register class.
736///
737/// Tmp1 = setcc1
738/// Tmp2 = setcc2
739/// DestReg = logicalop Tmp1, Tmp2
740///
741static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
742 unsigned SetCC2, unsigned LogicalOp,
743 unsigned DestReg) {
744 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
745 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
746 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
747 BuildMI(BB, SetCC1, 0, Tmp1);
748 BuildMI(BB, SetCC2, 0, Tmp2);
749 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
750}
751
752/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
753/// condition codes match the specified SetCCOpcode. Note that some conditions
754/// require multiple instructions to generate the correct value.
755static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
756 ISD::CondCode SetCCOpcode, bool isFP) {
757 unsigned Opc;
758 if (!isFP) {
759 switch (SetCCOpcode) {
760 default: assert(0 && "Illegal integer SetCC!");
761 case ISD::SETEQ: Opc = X86::SETEr; break;
762 case ISD::SETGT: Opc = X86::SETGr; break;
763 case ISD::SETGE: Opc = X86::SETGEr; break;
764 case ISD::SETLT: Opc = X86::SETLr; break;
765 case ISD::SETLE: Opc = X86::SETLEr; break;
766 case ISD::SETNE: Opc = X86::SETNEr; break;
767 case ISD::SETULT: Opc = X86::SETBr; break;
768 case ISD::SETUGT: Opc = X86::SETAr; break;
769 case ISD::SETULE: Opc = X86::SETBEr; break;
770 case ISD::SETUGE: Opc = X86::SETAEr; break;
771 }
772 } else {
773 // On a floating point condition, the flags are set as follows:
774 // ZF PF CF op
775 // 0 | 0 | 0 | X > Y
776 // 0 | 0 | 1 | X < Y
777 // 1 | 0 | 0 | X == Y
778 // 1 | 1 | 1 | unordered
779 //
780 switch (SetCCOpcode) {
781 default: assert(0 && "Invalid FP setcc!");
782 case ISD::SETUEQ:
783 case ISD::SETEQ:
784 Opc = X86::SETEr; // True if ZF = 1
785 break;
786 case ISD::SETOGT:
787 case ISD::SETGT:
788 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
789 break;
790 case ISD::SETOGE:
791 case ISD::SETGE:
792 Opc = X86::SETAEr; // True if CF = 0
793 break;
794 case ISD::SETULT:
795 case ISD::SETLT:
796 Opc = X86::SETBr; // True if CF = 1
797 break;
798 case ISD::SETULE:
799 case ISD::SETLE:
800 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
801 break;
802 case ISD::SETONE:
803 case ISD::SETNE:
804 Opc = X86::SETNEr; // True if ZF = 0
805 break;
806 case ISD::SETUO:
807 Opc = X86::SETPr; // True if PF = 1
808 break;
809 case ISD::SETO:
810 Opc = X86::SETNPr; // True if PF = 0
811 break;
812 case ISD::SETOEQ: // !PF & ZF
813 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
814 return;
815 case ISD::SETOLT: // !PF & CF
816 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
817 return;
818 case ISD::SETOLE: // !PF & (CF || ZF)
819 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
820 return;
821 case ISD::SETUGT: // PF | (!ZF & !CF)
822 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
823 return;
824 case ISD::SETUGE: // PF | !CF
825 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
826 return;
827 case ISD::SETUNE: // PF | !ZF
828 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
829 return;
830 }
831 }
832 BuildMI(BB, Opc, 0, DestReg);
833}
834
835
836/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
837/// the Dest block if the Cond condition is true. If we cannot fold this
838/// condition into the branch, return true.
839///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000840bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
841 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000842 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
843 // B) using two conditional branches instead of one condbr, two setcc's, and
844 // an or.
845 if ((Cond.getOpcode() == ISD::OR ||
846 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
847 // And and or set the flags for us, so there is no need to emit a TST of the
848 // result. It is only safe to do this if there is only a single use of the
849 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000850 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000851 SelectExpr(Cond);
852 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
853 return false;
854 }
855
856 // Codegen br not C -> JE.
857 if (Cond.getOpcode() == ISD::XOR)
858 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
859 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000860 unsigned CondR;
861 if (getRegPressure(Chain) > getRegPressure(Cond)) {
862 Select(Chain);
863 CondR = SelectExpr(Cond.Val->getOperand(0));
864 } else {
865 CondR = SelectExpr(Cond.Val->getOperand(0));
866 Select(Chain);
867 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000868 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
869 BuildMI(BB, X86::JE, 1).addMBB(Dest);
870 return false;
871 }
872
873 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
874 if (SetCC == 0)
875 return true; // Can only handle simple setcc's so far.
876
877 unsigned Opc;
878
879 // Handle integer conditions first.
880 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
881 switch (SetCC->getCondition()) {
882 default: assert(0 && "Illegal integer SetCC!");
883 case ISD::SETEQ: Opc = X86::JE; break;
884 case ISD::SETGT: Opc = X86::JG; break;
885 case ISD::SETGE: Opc = X86::JGE; break;
886 case ISD::SETLT: Opc = X86::JL; break;
887 case ISD::SETLE: Opc = X86::JLE; break;
888 case ISD::SETNE: Opc = X86::JNE; break;
889 case ISD::SETULT: Opc = X86::JB; break;
890 case ISD::SETUGT: Opc = X86::JA; break;
891 case ISD::SETULE: Opc = X86::JBE; break;
892 case ISD::SETUGE: Opc = X86::JAE; break;
893 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000894 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000895 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000896 BuildMI(BB, Opc, 1).addMBB(Dest);
897 return false;
898 }
899
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000900 unsigned Opc2 = 0; // Second branch if needed.
901
902 // On a floating point condition, the flags are set as follows:
903 // ZF PF CF op
904 // 0 | 0 | 0 | X > Y
905 // 0 | 0 | 1 | X < Y
906 // 1 | 0 | 0 | X == Y
907 // 1 | 1 | 1 | unordered
908 //
909 switch (SetCC->getCondition()) {
910 default: assert(0 && "Invalid FP setcc!");
911 case ISD::SETUEQ:
912 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
913 case ISD::SETOGT:
914 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
915 case ISD::SETOGE:
916 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
917 case ISD::SETULT:
918 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
919 case ISD::SETULE:
920 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
921 case ISD::SETONE:
922 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
923 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
924 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
925 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
926 Opc = X86::JA; // ZF = 0 & CF = 0
927 Opc2 = X86::JP; // PF = 1
928 break;
929 case ISD::SETUGE: // PF = 1 | CF = 0
930 Opc = X86::JAE; // CF = 0
931 Opc2 = X86::JP; // PF = 1
932 break;
933 case ISD::SETUNE: // PF = 1 | ZF = 0
934 Opc = X86::JNE; // ZF = 0
935 Opc2 = X86::JP; // PF = 1
936 break;
937 case ISD::SETOEQ: // PF = 0 & ZF = 1
938 //X86::JNP, X86::JE
939 //X86::AND8rr
940 return true; // FIXME: Emit more efficient code for this branch.
941 case ISD::SETOLT: // PF = 0 & CF = 1
942 //X86::JNP, X86::JB
943 //X86::AND8rr
944 return true; // FIXME: Emit more efficient code for this branch.
945 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
946 //X86::JNP, X86::JBE
947 //X86::AND8rr
948 return true; // FIXME: Emit more efficient code for this branch.
949 }
950
Chris Lattner6c07aee2005-01-11 04:06:27 +0000951 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000952 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000953 BuildMI(BB, Opc, 1).addMBB(Dest);
954 if (Opc2)
955 BuildMI(BB, Opc2, 1).addMBB(Dest);
956 return false;
957}
958
Chris Lattner24aad1b2005-01-10 22:10:13 +0000959/// EmitSelectCC - Emit code into BB that performs a select operation between
960/// the two registers RTrue and RFalse, generating a result into RDest. Return
961/// true if the fold cannot be performed.
962///
963void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
964 unsigned RTrue, unsigned RFalse, unsigned RDest) {
965 enum Condition {
966 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
967 NOT_SET
968 } CondCode = NOT_SET;
969
970 static const unsigned CMOVTAB16[] = {
971 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
972 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000973 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000974 };
975 static const unsigned CMOVTAB32[] = {
976 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
977 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000978 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000979 };
980 static const unsigned CMOVTABFP[] = {
981 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
982 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
983 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
984 };
985
986 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
987 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
988 switch (SetCC->getCondition()) {
989 default: assert(0 && "Unknown integer comparison!");
990 case ISD::SETEQ: CondCode = EQ; break;
991 case ISD::SETGT: CondCode = GT; break;
992 case ISD::SETGE: CondCode = GE; break;
993 case ISD::SETLT: CondCode = LT; break;
994 case ISD::SETLE: CondCode = LE; break;
995 case ISD::SETNE: CondCode = NE; break;
996 case ISD::SETULT: CondCode = B; break;
997 case ISD::SETUGT: CondCode = A; break;
998 case ISD::SETULE: CondCode = BE; break;
999 case ISD::SETUGE: CondCode = AE; break;
1000 }
1001 } else {
1002 // On a floating point condition, the flags are set as follows:
1003 // ZF PF CF op
1004 // 0 | 0 | 0 | X > Y
1005 // 0 | 0 | 1 | X < Y
1006 // 1 | 0 | 0 | X == Y
1007 // 1 | 1 | 1 | unordered
1008 //
1009 switch (SetCC->getCondition()) {
1010 default: assert(0 && "Unknown FP comparison!");
1011 case ISD::SETUEQ:
1012 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1013 case ISD::SETOGT:
1014 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1015 case ISD::SETOGE:
1016 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1017 case ISD::SETULT:
1018 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1019 case ISD::SETULE:
1020 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1021 case ISD::SETONE:
1022 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1023 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1024 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1025 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1026 case ISD::SETUGE: // PF = 1 | CF = 0
1027 case ISD::SETUNE: // PF = 1 | ZF = 0
1028 case ISD::SETOEQ: // PF = 0 & ZF = 1
1029 case ISD::SETOLT: // PF = 0 & CF = 1
1030 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1031 // We cannot emit this comparison as a single cmov.
1032 break;
1033 }
1034 }
1035 }
1036
1037 unsigned Opc = 0;
1038 if (CondCode != NOT_SET) {
1039 switch (SVT) {
1040 default: assert(0 && "Cannot select this type!");
1041 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1042 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001043 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001044 }
1045 }
1046
1047 // Finally, if we weren't able to fold this, just emit the condition and test
1048 // it.
1049 if (CondCode == NOT_SET || Opc == 0) {
1050 // Get the condition into the zero flag.
1051 unsigned CondReg = SelectExpr(Cond);
1052 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1053
1054 switch (SVT) {
1055 default: assert(0 && "Cannot select this type!");
1056 case MVT::i16: Opc = X86::CMOVE16rr; break;
1057 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001058 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001059 }
1060 } else {
1061 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001062 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001063 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001064 }
1065 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1066}
1067
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001068void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001069 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001070 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1071 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001072 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001073 switch (RHS.getValueType()) {
1074 default: break;
1075 case MVT::i1:
1076 case MVT::i8: Opc = X86::CMP8mi; break;
1077 case MVT::i16: Opc = X86::CMP16mi; break;
1078 case MVT::i32: Opc = X86::CMP32mi; break;
1079 }
1080 if (Opc) {
1081 X86AddressMode AM;
1082 EmitFoldedLoad(LHS, AM);
1083 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1084 return;
1085 }
1086 }
1087
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001088 switch (RHS.getValueType()) {
1089 default: break;
1090 case MVT::i1:
1091 case MVT::i8: Opc = X86::CMP8ri; break;
1092 case MVT::i16: Opc = X86::CMP16ri; break;
1093 case MVT::i32: Opc = X86::CMP32ri; break;
1094 }
1095 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001096 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001097 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1098 return;
1099 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001100 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1101 if (CN->isExactlyValue(+0.0) ||
1102 CN->isExactlyValue(-0.0)) {
1103 unsigned Reg = SelectExpr(LHS);
1104 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1105 BuildMI(BB, X86::FNSTSW8r, 0);
1106 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001107 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001108 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001109 }
1110
Chris Lattneref6806c2005-01-12 02:02:48 +00001111 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001112 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001113 switch (RHS.getValueType()) {
1114 default: break;
1115 case MVT::i1:
1116 case MVT::i8: Opc = X86::CMP8mr; break;
1117 case MVT::i16: Opc = X86::CMP16mr; break;
1118 case MVT::i32: Opc = X86::CMP32mr; break;
1119 }
1120 if (Opc) {
1121 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001122 EmitFoldedLoad(LHS, AM);
1123 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001124 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1125 return;
1126 }
1127 }
1128
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001129 switch (LHS.getValueType()) {
1130 default: assert(0 && "Cannot compare this value!");
1131 case MVT::i1:
1132 case MVT::i8: Opc = X86::CMP8rr; break;
1133 case MVT::i16: Opc = X86::CMP16rr; break;
1134 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001135 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001136 }
Chris Lattner11333092005-01-11 03:11:44 +00001137 unsigned Tmp1, Tmp2;
1138 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1139 Tmp1 = SelectExpr(LHS);
1140 Tmp2 = SelectExpr(RHS);
1141 } else {
1142 Tmp2 = SelectExpr(RHS);
1143 Tmp1 = SelectExpr(LHS);
1144 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001145 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1146}
1147
Chris Lattnera5ade062005-01-11 21:19:59 +00001148/// isFoldableLoad - Return true if this is a load instruction that can safely
1149/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001150bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1151 if (Op.getOpcode() == ISD::LOAD) {
1152 // FIXME: currently can't fold constant pool indexes.
1153 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1154 return false;
1155 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1156 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1157 // FIXME: currently can't fold constant pool indexes.
1158 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1159 return false;
1160 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001161 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001162 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001163
1164 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001165 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1166 if (ExprMap.count(Op.getValue(1))) return false;
1167 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001168 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001169
Chris Lattner4ff348b2005-01-17 06:26:58 +00001170 // If there is not just one use of its value, we cannot fold.
1171 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1172
1173 // Finally, we cannot fold the load into the operation if this would induce a
1174 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1175 // operand of the operation we are folding the load into) can possible use the
1176 // chain node defined by the load.
1177 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1178 std::set<SDNode*> Visited;
1179 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1180 return false;
1181 }
1182 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001183}
1184
Chris Lattner4ff348b2005-01-17 06:26:58 +00001185
Chris Lattnera5ade062005-01-11 21:19:59 +00001186/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1187/// and compute the address being loaded into AM.
1188void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1189 SDOperand Chain = Op.getOperand(0);
1190 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001191
Chris Lattnera5ade062005-01-11 21:19:59 +00001192 if (getRegPressure(Chain) > getRegPressure(Address)) {
1193 Select(Chain);
1194 SelectAddress(Address, AM);
1195 } else {
1196 SelectAddress(Address, AM);
1197 Select(Chain);
1198 }
1199
1200 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001201 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1202 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001203 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001204 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001205}
1206
Chris Lattner30ea1e92005-01-19 07:37:26 +00001207// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1208// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1209// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1210// return true.
1211bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001212 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1213 // good!
1214 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1215 std::swap(Op1, Op2); // Op1 is the SHL now.
1216 } else {
1217 return false; // No match
1218 }
1219
1220 SDOperand ShlVal = Op1.getOperand(0);
1221 SDOperand ShlAmt = Op1.getOperand(1);
1222 SDOperand ShrVal = Op2.getOperand(0);
1223 SDOperand ShrAmt = Op2.getOperand(1);
1224
Chris Lattner30ea1e92005-01-19 07:37:26 +00001225 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1226
Chris Lattner85716372005-01-19 06:18:43 +00001227 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1228 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1229 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001230 if (SubCST->getValue() == RegSize) {
1231 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001232 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001233 if (ShrVal == ShlVal) {
1234 unsigned Reg, ShAmt;
1235 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1236 Reg = SelectExpr(ShrVal);
1237 ShAmt = SelectExpr(ShrAmt);
1238 } else {
1239 ShAmt = SelectExpr(ShrAmt);
1240 Reg = SelectExpr(ShrVal);
1241 }
1242 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1243 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1244 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1245 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1246 return true;
1247 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001248 unsigned AReg, BReg;
1249 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001250 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001251 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001252 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001253 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001254 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001255 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001256 unsigned ShAmt = SelectExpr(ShrAmt);
1257 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1258 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1259 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001260 return true;
1261 }
1262 }
1263
Chris Lattner4053b1e2005-01-19 08:07:05 +00001264 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1265 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1266 if (SubCST->getValue() == RegSize) {
1267 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1268 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1269 if (ShrVal == ShlVal) {
1270 unsigned Reg, ShAmt;
1271 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1272 Reg = SelectExpr(ShrVal);
1273 ShAmt = SelectExpr(ShlAmt);
1274 } else {
1275 ShAmt = SelectExpr(ShlAmt);
1276 Reg = SelectExpr(ShrVal);
1277 }
1278 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1279 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1280 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1281 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1282 return true;
1283 } else if (RegSize != 8) {
1284 unsigned AReg, BReg;
1285 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001286 AReg = SelectExpr(ShlVal);
1287 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001288 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001289 BReg = SelectExpr(ShrVal);
1290 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001291 }
1292 unsigned ShAmt = SelectExpr(ShlAmt);
1293 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1294 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1295 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1296 return true;
1297 }
1298 }
Chris Lattner85716372005-01-19 06:18:43 +00001299
Chris Lattner4053b1e2005-01-19 08:07:05 +00001300 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1301 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1302 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1303 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1304 // (A >> 5) | (A << 27) --> ROR A, 5
1305 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1306 if (ShrVal == ShlVal) {
1307 unsigned Reg = SelectExpr(ShrVal);
1308 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1309 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1310 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1311 return true;
1312 } else if (RegSize != 8) {
1313 unsigned AReg, BReg;
1314 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001315 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001316 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001317 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001318 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001319 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001320 }
1321 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1322 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1323 .addImm(ShrCst->getValue());
1324 return true;
1325 }
1326 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001327
Chris Lattner85716372005-01-19 06:18:43 +00001328 return false;
1329}
1330
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001331unsigned ISel::SelectExpr(SDOperand N) {
1332 unsigned Result;
1333 unsigned Tmp1, Tmp2, Tmp3;
1334 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001335 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001336 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001337
Chris Lattner7f2afac2005-01-14 22:37:41 +00001338 if (Node->getOpcode() == ISD::CopyFromReg) {
1339 // FIXME: Handle copy from physregs!
1340
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001341 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001342 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001343 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001344
Chris Lattnera5ade062005-01-11 21:19:59 +00001345 unsigned &Reg = ExprMap[N];
1346 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001347
Chris Lattnerb38a7492005-04-02 04:01:14 +00001348 switch (N.getOpcode()) {
1349 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001350 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001351 MakeReg(N.getValueType()) : 1;
1352 break;
1353 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001354 // If this is a call instruction, make sure to prepare ALL of the result
1355 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001356 if (Node->getNumValues() == 1)
1357 Reg = Result = 1; // Void call, just a chain.
1358 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001359 Result = MakeReg(Node->getValueType(0));
1360 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001361 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001362 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001363 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001364 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001365 break;
1366 case ISD::ADD_PARTS:
1367 case ISD::SUB_PARTS:
1368 case ISD::SHL_PARTS:
1369 case ISD::SRL_PARTS:
1370 case ISD::SRA_PARTS:
1371 Result = MakeReg(Node->getValueType(0));
1372 ExprMap[N.getValue(0)] = Result;
1373 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1374 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1375 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001376 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001377
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001378 switch (N.getOpcode()) {
1379 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001380 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001381 assert(0 && "Node not handled!\n");
1382 case ISD::FrameIndex:
1383 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1384 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1385 return Result;
1386 case ISD::ConstantPool:
1387 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1388 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1389 return Result;
1390 case ISD::ConstantFP:
1391 ContainsFPCode = true;
1392 Tmp1 = Result; // Intermediate Register
1393 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1394 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1395 Tmp1 = MakeReg(MVT::f64);
1396
1397 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1398 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1399 BuildMI(BB, X86::FLD0, 0, Tmp1);
1400 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1401 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1402 BuildMI(BB, X86::FLD1, 0, Tmp1);
1403 else
1404 assert(0 && "Unexpected constant!");
1405 if (Tmp1 != Result)
1406 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1407 return Result;
1408 case ISD::Constant:
1409 switch (N.getValueType()) {
1410 default: assert(0 && "Cannot use constants of this type!");
1411 case MVT::i1:
1412 case MVT::i8: Opc = X86::MOV8ri; break;
1413 case MVT::i16: Opc = X86::MOV16ri; break;
1414 case MVT::i32: Opc = X86::MOV32ri; break;
1415 }
1416 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1417 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001418 case ISD::UNDEF:
1419 if (Node->getValueType(0) == MVT::f64) {
1420 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1421 BuildMI(BB, X86::FLD0, 0, Result);
1422 } else {
1423 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1424 }
1425 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001426 case ISD::GlobalAddress: {
1427 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1428 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1429 return Result;
1430 }
1431 case ISD::ExternalSymbol: {
1432 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1433 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1434 return Result;
1435 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001436 case ISD::ZERO_EXTEND: {
1437 int DestIs16 = N.getValueType() == MVT::i16;
1438 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001439
1440 // FIXME: This hack is here for zero extension casts from bool to i8. This
1441 // would not be needed if bools were promoted by Legalize.
1442 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001443 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001444 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1445 return Result;
1446 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001447
Chris Lattner4ff348b2005-01-17 06:26:58 +00001448 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001449 static const unsigned Opc[3] = {
1450 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1451 };
1452
1453 X86AddressMode AM;
1454 EmitFoldedLoad(N.getOperand(0), AM);
1455 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001456
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001457 return Result;
1458 }
1459
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001460 static const unsigned Opc[3] = {
1461 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1462 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001463 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001464 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1465 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001466 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001467 case ISD::SIGN_EXTEND: {
1468 int DestIs16 = N.getValueType() == MVT::i16;
1469 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1470
Chris Lattner590d8002005-01-09 18:52:44 +00001471 // FIXME: Legalize should promote bools to i8!
1472 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1473 "Sign extend from bool not implemented!");
1474
Chris Lattner4ff348b2005-01-17 06:26:58 +00001475 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001476 static const unsigned Opc[3] = {
1477 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1478 };
1479
1480 X86AddressMode AM;
1481 EmitFoldedLoad(N.getOperand(0), AM);
1482 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1483 return Result;
1484 }
1485
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001486 static const unsigned Opc[3] = {
1487 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1488 };
1489 Tmp1 = SelectExpr(N.getOperand(0));
1490 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1491 return Result;
1492 }
1493 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001494 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001495 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001496 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001497 switch (N.getValueType()) {
1498 default: assert(0 && "Unknown truncate!");
1499 case MVT::i1:
1500 case MVT::i8: Opc = X86::MOV8rm; break;
1501 case MVT::i16: Opc = X86::MOV16rm; break;
1502 }
1503 X86AddressMode AM;
1504 EmitFoldedLoad(N.getOperand(0), AM);
1505 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1506 return Result;
1507 }
1508
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001509 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1510 // a move out of AX or AL.
1511 switch (N.getOperand(0).getValueType()) {
1512 default: assert(0 && "Unknown truncate!");
1513 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1514 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1515 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1516 }
1517 Tmp1 = SelectExpr(N.getOperand(0));
1518 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1519
1520 switch (N.getValueType()) {
1521 default: assert(0 && "Unknown truncate!");
1522 case MVT::i1:
1523 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1524 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1525 }
1526 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1527 return Result;
1528
Chris Lattner590d8002005-01-09 18:52:44 +00001529 case ISD::SINT_TO_FP:
1530 case ISD::UINT_TO_FP: {
1531 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001532 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001533
1534 // Promote the integer to a type supported by FLD. We do this because there
1535 // are no unsigned FLD instructions, so we must promote an unsigned value to
1536 // a larger signed value, then use FLD on the larger value.
1537 //
1538 MVT::ValueType PromoteType = MVT::Other;
1539 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1540 unsigned PromoteOpcode = 0;
1541 unsigned RealDestReg = Result;
1542 switch (SrcTy) {
1543 case MVT::i1:
1544 case MVT::i8:
1545 // We don't have the facilities for directly loading byte sized data from
1546 // memory (even signed). Promote it to 16 bits.
1547 PromoteType = MVT::i16;
1548 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1549 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1550 break;
1551 case MVT::i16:
1552 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1553 PromoteType = MVT::i32;
1554 PromoteOpcode = X86::MOVZX32rr16;
1555 }
1556 break;
1557 default:
1558 // Don't fild into the real destination.
1559 if (Node->getOpcode() == ISD::UINT_TO_FP)
1560 Result = MakeReg(Node->getValueType(0));
1561 break;
1562 }
1563
1564 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001565
Chris Lattner590d8002005-01-09 18:52:44 +00001566 if (PromoteType != MVT::Other) {
1567 Tmp2 = MakeReg(PromoteType);
1568 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1569 SrcTy = PromoteType;
1570 Tmp1 = Tmp2;
1571 }
1572
1573 // Spill the integer to memory and reload it from there.
1574 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1575 MachineFunction *F = BB->getParent();
1576 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1577
1578 switch (SrcTy) {
1579 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001580 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001581 // FIXME: this won't work for cast [u]long to FP
1582 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1583 FrameIdx).addReg(Tmp1);
1584 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1585 FrameIdx, 4).addReg(Tmp1+1);
1586 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1587 break;
1588 case MVT::i32:
1589 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1590 FrameIdx).addReg(Tmp1);
1591 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1592 break;
1593 case MVT::i16:
1594 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1595 FrameIdx).addReg(Tmp1);
1596 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1597 break;
1598 default: break; // No promotion required.
1599 }
1600
Chris Lattner085c9952005-01-12 04:00:00 +00001601 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001602 // If this is a cast from uint -> double, we need to be careful when if
1603 // the "sign" bit is set. If so, we don't want to make a negative number,
1604 // we want to make a positive number. Emit code to add an offset if the
1605 // sign bit is set.
1606
1607 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1608 unsigned IsNeg = MakeReg(MVT::i32);
1609 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1610
1611 // Create a CP value that has the offset in one word and 0 in the other.
1612 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1613 0x4f80000000000000ULL);
1614 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1615 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1616 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1617
1618 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1619 // We need special handling for unsigned 64-bit integer sources. If the
1620 // input number has the "sign bit" set, then we loaded it incorrectly as a
1621 // negative 64-bit number. In this case, add an offset value.
1622
1623 // Emit a test instruction to see if the dynamic input value was signed.
1624 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1625
1626 // If the sign bit is set, get a pointer to an offset, otherwise get a
1627 // pointer to a zero.
1628 MachineConstantPool *CP = F->getConstantPool();
1629 unsigned Zero = MakeReg(MVT::i32);
1630 Constant *Null = Constant::getNullValue(Type::UIntTy);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001631 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
Chris Lattner590d8002005-01-09 18:52:44 +00001632 CP->getConstantPoolIndex(Null));
1633 unsigned Offset = MakeReg(MVT::i32);
1634 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001635
Chris Lattner590d8002005-01-09 18:52:44 +00001636 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1637 CP->getConstantPoolIndex(OffsetCst));
1638 unsigned Addr = MakeReg(MVT::i32);
1639 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1640
1641 // Load the constant for an add. FIXME: this could make an 'fadd' that
1642 // reads directly from memory, but we don't support these yet.
1643 unsigned ConstReg = MakeReg(MVT::f64);
1644 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1645
1646 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1647 }
1648 return RealDestReg;
1649 }
1650 case ISD::FP_TO_SINT:
1651 case ISD::FP_TO_UINT: {
1652 // FIXME: Most of this grunt work should be done by legalize!
1653 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1654
1655 // Change the floating point control register to use "round towards zero"
1656 // mode when truncating to an integer value.
1657 //
1658 MachineFunction *F = BB->getParent();
1659 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1660 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1661
1662 // Load the old value of the high byte of the control word...
1663 unsigned HighPartOfCW = MakeReg(MVT::i8);
1664 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1665 CWFrameIdx, 1);
1666
1667 // Set the high part to be round to zero...
1668 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1669 CWFrameIdx, 1).addImm(12);
1670
1671 // Reload the modified control word now...
1672 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001673
Chris Lattner590d8002005-01-09 18:52:44 +00001674 // Restore the memory image of control word to original value
1675 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1676 CWFrameIdx, 1).addReg(HighPartOfCW);
1677
1678 // We don't have the facilities for directly storing byte sized data to
1679 // memory. Promote it to 16 bits. We also must promote unsigned values to
1680 // larger classes because we only have signed FP stores.
1681 MVT::ValueType StoreClass = Node->getValueType(0);
1682 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1683 switch (StoreClass) {
1684 case MVT::i8: StoreClass = MVT::i16; break;
1685 case MVT::i16: StoreClass = MVT::i32; break;
1686 case MVT::i32: StoreClass = MVT::i64; break;
1687 // The following treatment of cLong may not be perfectly right,
1688 // but it survives chains of casts of the form
1689 // double->ulong->double.
1690 case MVT::i64: StoreClass = MVT::i64; break;
1691 default: assert(0 && "Unknown store class!");
1692 }
1693
1694 // Spill the integer to memory and reload it from there.
1695 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1696 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1697
1698 switch (StoreClass) {
1699 default: assert(0 && "Unknown store class!");
1700 case MVT::i16:
1701 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1702 break;
1703 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001704 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001705 break;
1706 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001707 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001708 break;
1709 }
1710
1711 switch (Node->getValueType(0)) {
1712 default:
1713 assert(0 && "Unknown integer type!");
1714 case MVT::i64:
1715 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001716 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001717 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1718 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1719 case MVT::i32:
1720 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1721 break;
1722 case MVT::i16:
1723 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1724 break;
1725 case MVT::i8:
1726 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1727 break;
1728 }
1729
1730 // Reload the original control word now.
1731 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1732 return Result;
1733 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001734 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001735 Op0 = N.getOperand(0);
1736 Op1 = N.getOperand(1);
1737
Chris Lattner44129b52005-01-25 20:03:11 +00001738 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001739 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001740 goto FoldAdd;
1741 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001742
Chris Lattner44129b52005-01-25 20:03:11 +00001743 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001744 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001745 switch (N.getValueType()) {
1746 default: assert(0 && "Cannot add this type!");
1747 case MVT::i1:
1748 case MVT::i8: Opc = X86::ADD8rm; break;
1749 case MVT::i16: Opc = X86::ADD16rm; break;
1750 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001751 case MVT::f64:
1752 // For F64, handle promoted load operations (from F32) as well!
1753 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1754 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001755 }
1756 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001757 EmitFoldedLoad(Op1, AM);
1758 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001759 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1760 return Result;
1761 }
1762
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001763 // See if we can codegen this as an LEA to fold operations together.
1764 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001765 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001766 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001767 MatchAddress(N, AM);
1768 ExprMap[N] = Result;
1769
1770 // If this is not just an add, emit the LEA. For a simple add (like
1771 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1772 // leave this as LEA, then peephole it to 'ADD' after two address elim
1773 // happens.
1774 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1775 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1776 X86AddressMode XAM = SelectAddrExprs(AM);
1777 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1778 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001779 }
1780 }
Chris Lattner11333092005-01-11 03:11:44 +00001781
Chris Lattnera5ade062005-01-11 21:19:59 +00001782 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783 Opc = 0;
1784 if (CN->getValue() == 1) { // add X, 1 -> inc X
1785 switch (N.getValueType()) {
1786 default: assert(0 && "Cannot integer add this type!");
1787 case MVT::i8: Opc = X86::INC8r; break;
1788 case MVT::i16: Opc = X86::INC16r; break;
1789 case MVT::i32: Opc = X86::INC32r; break;
1790 }
1791 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1792 switch (N.getValueType()) {
1793 default: assert(0 && "Cannot integer add this type!");
1794 case MVT::i8: Opc = X86::DEC8r; break;
1795 case MVT::i16: Opc = X86::DEC16r; break;
1796 case MVT::i32: Opc = X86::DEC32r; break;
1797 }
1798 }
1799
1800 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001801 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001802 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1803 return Result;
1804 }
1805
1806 switch (N.getValueType()) {
1807 default: assert(0 && "Cannot add this type!");
1808 case MVT::i8: Opc = X86::ADD8ri; break;
1809 case MVT::i16: Opc = X86::ADD16ri; break;
1810 case MVT::i32: Opc = X86::ADD32ri; break;
1811 }
1812 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001813 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001814 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1815 return Result;
1816 }
1817 }
1818
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001819 switch (N.getValueType()) {
1820 default: assert(0 && "Cannot add this type!");
1821 case MVT::i8: Opc = X86::ADD8rr; break;
1822 case MVT::i16: Opc = X86::ADD16rr; break;
1823 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001824 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001825 }
Chris Lattner11333092005-01-11 03:11:44 +00001826
Chris Lattnera5ade062005-01-11 21:19:59 +00001827 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1828 Tmp1 = SelectExpr(Op0);
1829 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001830 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001831 Tmp2 = SelectExpr(Op1);
1832 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001833 }
1834
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001835 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1836 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001837
1838 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001839 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001840 case ISD::FSIN:
1841 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001842 case ISD::FSQRT:
1843 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001844 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001845 switch (N.getOpcode()) {
1846 default: assert(0 && "Unreachable!");
1847 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1848 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1849 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001850 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1851 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001852 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001853 return Result;
1854
Chris Lattner8db0af12005-04-06 04:21:07 +00001855 case ISD::MULHU:
1856 switch (N.getValueType()) {
1857 default: assert(0 && "Unsupported VT!");
1858 case MVT::i8: Tmp2 = X86::MUL8r; break;
1859 case MVT::i16: Tmp2 = X86::MUL16r; break;
1860 case MVT::i32: Tmp2 = X86::MUL32r; break;
1861 }
1862 // FALL THROUGH
1863 case ISD::MULHS: {
1864 unsigned MovOpc, LowReg, HiReg;
1865 switch (N.getValueType()) {
1866 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001867 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001868 MovOpc = X86::MOV8rr;
1869 LowReg = X86::AL;
1870 HiReg = X86::AH;
1871 Opc = X86::IMUL8r;
1872 break;
1873 case MVT::i16:
1874 MovOpc = X86::MOV16rr;
1875 LowReg = X86::AX;
1876 HiReg = X86::DX;
1877 Opc = X86::IMUL16r;
1878 break;
1879 case MVT::i32:
1880 MovOpc = X86::MOV32rr;
1881 LowReg = X86::EAX;
1882 HiReg = X86::EDX;
1883 Opc = X86::IMUL32r;
1884 break;
1885 }
1886 if (Node->getOpcode() != ISD::MULHS)
1887 Opc = Tmp2; // Get the MULHU opcode.
1888
1889 Op0 = Node->getOperand(0);
1890 Op1 = Node->getOperand(1);
1891 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1892 Tmp1 = SelectExpr(Op0);
1893 Tmp2 = SelectExpr(Op1);
1894 } else {
1895 Tmp2 = SelectExpr(Op1);
1896 Tmp1 = SelectExpr(Op0);
1897 }
1898
1899 // FIXME: Implement folding of loads into the memory operands here!
1900 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1901 BuildMI(BB, Opc, 1).addReg(Tmp2);
1902 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1903 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001904 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001905
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001906 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001907 case ISD::MUL:
1908 case ISD::AND:
1909 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001910 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001911 static const unsigned SUBTab[] = {
1912 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1913 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1914 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1915 };
1916 static const unsigned MULTab[] = {
1917 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1918 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1919 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1920 };
1921 static const unsigned ANDTab[] = {
1922 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1923 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001924 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001925 };
1926 static const unsigned ORTab[] = {
1927 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1928 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1929 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1930 };
1931 static const unsigned XORTab[] = {
1932 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1933 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1934 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1935 };
1936
1937 Op0 = Node->getOperand(0);
1938 Op1 = Node->getOperand(1);
1939
Chris Lattner30ea1e92005-01-19 07:37:26 +00001940 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1941 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001942 return Result;
1943
1944 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001945 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1946 if (CN->isNullValue()) { // 0 - N -> neg N
1947 switch (N.getValueType()) {
1948 default: assert(0 && "Cannot sub this type!");
1949 case MVT::i1:
1950 case MVT::i8: Opc = X86::NEG8r; break;
1951 case MVT::i16: Opc = X86::NEG16r; break;
1952 case MVT::i32: Opc = X86::NEG32r; break;
1953 }
1954 Tmp1 = SelectExpr(N.getOperand(1));
1955 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1956 return Result;
1957 }
1958
Chris Lattnera5ade062005-01-11 21:19:59 +00001959 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1960 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001961 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001962 switch (N.getValueType()) {
1963 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001964 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001965 case MVT::i8: Opc = X86::NOT8r; break;
1966 case MVT::i16: Opc = X86::NOT16r; break;
1967 case MVT::i32: Opc = X86::NOT32r; break;
1968 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001969 if (Opc) {
1970 Tmp1 = SelectExpr(Op0);
1971 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1972 return Result;
1973 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001974 }
1975
Chris Lattner2a4e5082005-01-17 06:48:02 +00001976 // Fold common multiplies into LEA instructions.
1977 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1978 switch ((int)CN->getValue()) {
1979 default: break;
1980 case 3:
1981 case 5:
1982 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001983 // Remove N from exprmap so SelectAddress doesn't get confused.
1984 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001985 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001986 SelectAddress(N, AM);
1987 // Restore it to the map.
1988 ExprMap[N] = Result;
1989 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1990 return Result;
1991 }
1992 }
1993
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001994 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001995 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001996 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001997 case MVT::i8: Opc = 0; break;
1998 case MVT::i16: Opc = 1; break;
1999 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002001 switch (Node->getOpcode()) {
2002 default: assert(0 && "Unreachable!");
2003 case ISD::SUB: Opc = SUBTab[Opc]; break;
2004 case ISD::MUL: Opc = MULTab[Opc]; break;
2005 case ISD::AND: Opc = ANDTab[Opc]; break;
2006 case ISD::OR: Opc = ORTab[Opc]; break;
2007 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002008 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002009 if (Opc) { // Can't fold MUL:i8 R, imm
2010 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002011 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2012 return Result;
2013 }
2014 }
Chris Lattner11333092005-01-11 03:11:44 +00002015
Chris Lattner44129b52005-01-25 20:03:11 +00002016 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002017 if (Node->getOpcode() != ISD::SUB) {
2018 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002019 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002020 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002021 // For FP, emit 'reverse' subract, with a memory operand.
2022 if (N.getValueType() == MVT::f64) {
2023 if (Op0.getOpcode() == ISD::EXTLOAD)
2024 Opc = X86::FSUBR32m;
2025 else
2026 Opc = X86::FSUBR64m;
2027
Chris Lattnera5ade062005-01-11 21:19:59 +00002028 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002029 EmitFoldedLoad(Op0, AM);
2030 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002031 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2032 return Result;
2033 }
2034 }
2035
Chris Lattner44129b52005-01-25 20:03:11 +00002036 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002037 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002038 switch (N.getValueType()) {
2039 default: assert(0 && "Cannot operate on this type!");
2040 case MVT::i1:
2041 case MVT::i8: Opc = 5; break;
2042 case MVT::i16: Opc = 6; break;
2043 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002044 // For F64, handle promoted load operations (from F32) as well!
2045 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002046 }
2047 switch (Node->getOpcode()) {
2048 default: assert(0 && "Unreachable!");
2049 case ISD::SUB: Opc = SUBTab[Opc]; break;
2050 case ISD::MUL: Opc = MULTab[Opc]; break;
2051 case ISD::AND: Opc = ANDTab[Opc]; break;
2052 case ISD::OR: Opc = ORTab[Opc]; break;
2053 case ISD::XOR: Opc = XORTab[Opc]; break;
2054 }
2055
2056 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002057 EmitFoldedLoad(Op1, AM);
2058 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002059 if (Opc) {
2060 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2061 } else {
2062 assert(Node->getOpcode() == ISD::MUL &&
2063 N.getValueType() == MVT::i8 && "Unexpected situation!");
2064 // Must use the MUL instruction, which forces use of AL.
2065 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2066 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2067 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2068 }
2069 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002070 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002071
2072 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2073 Tmp1 = SelectExpr(Op0);
2074 Tmp2 = SelectExpr(Op1);
2075 } else {
2076 Tmp2 = SelectExpr(Op1);
2077 Tmp1 = SelectExpr(Op0);
2078 }
2079
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002080 switch (N.getValueType()) {
2081 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002082 case MVT::i1:
2083 case MVT::i8: Opc = 10; break;
2084 case MVT::i16: Opc = 11; break;
2085 case MVT::i32: Opc = 12; break;
2086 case MVT::f32: Opc = 13; break;
2087 case MVT::f64: Opc = 14; break;
2088 }
2089 switch (Node->getOpcode()) {
2090 default: assert(0 && "Unreachable!");
2091 case ISD::SUB: Opc = SUBTab[Opc]; break;
2092 case ISD::MUL: Opc = MULTab[Opc]; break;
2093 case ISD::AND: Opc = ANDTab[Opc]; break;
2094 case ISD::OR: Opc = ORTab[Opc]; break;
2095 case ISD::XOR: Opc = XORTab[Opc]; break;
2096 }
2097 if (Opc) {
2098 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2099 } else {
2100 assert(Node->getOpcode() == ISD::MUL &&
2101 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002102 // Must use the MUL instruction, which forces use of AL.
2103 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2104 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2105 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002106 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002107 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002108 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002109 case ISD::ADD_PARTS:
2110 case ISD::SUB_PARTS: {
2111 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2112 "Not an i64 add/sub!");
2113 // Emit all of the operands.
2114 std::vector<unsigned> InVals;
2115 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2116 InVals.push_back(SelectExpr(N.getOperand(i)));
2117 if (N.getOpcode() == ISD::ADD_PARTS) {
2118 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2119 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2120 } else {
2121 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2122 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2123 }
2124 return Result+N.ResNo;
2125 }
2126
Chris Lattnerb38a7492005-04-02 04:01:14 +00002127 case ISD::SHL_PARTS:
2128 case ISD::SRA_PARTS:
2129 case ISD::SRL_PARTS: {
2130 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2131 "Not an i64 shift!");
2132 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2133 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2134 unsigned TmpReg = MakeReg(MVT::i32);
2135 if (N.getOpcode() == ISD::SRA_PARTS) {
2136 // If this is a SHR of a Long, then we need to do funny sign extension
2137 // stuff. TmpReg gets the value to use as the high-part if we are
2138 // shifting more than 32 bits.
2139 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2140 } else {
2141 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2142 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2143 }
2144
2145 // Initialize CL with the shift amount.
2146 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2147 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2148
2149 unsigned TmpReg2 = MakeReg(MVT::i32);
2150 unsigned TmpReg3 = MakeReg(MVT::i32);
2151 if (N.getOpcode() == ISD::SHL_PARTS) {
2152 // TmpReg2 = shld inHi, inLo
2153 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2154 .addReg(ShiftOpLo);
2155 // TmpReg3 = shl inLo, CL
2156 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002157
Chris Lattnerb38a7492005-04-02 04:01:14 +00002158 // Set the flags to indicate whether the shift was by more than 32 bits.
2159 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002160
Chris Lattnerb38a7492005-04-02 04:01:14 +00002161 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002162 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002163 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2164 // DestLo = (>32) ? TmpReg : TmpReg3;
2165 BuildMI(BB, X86::CMOVNE32rr, 2,
2166 Result).addReg(TmpReg3).addReg(TmpReg);
2167 } else {
2168 // TmpReg2 = shrd inLo, inHi
2169 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2170 .addReg(ShiftOpHi);
2171 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002172 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002173 : X86::SHR32rCL, 1, TmpReg3)
2174 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002175
Chris Lattnerb38a7492005-04-02 04:01:14 +00002176 // Set the flags to indicate whether the shift was by more than 32 bits.
2177 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002178
Chris Lattnerb38a7492005-04-02 04:01:14 +00002179 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002180 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002181 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002182
Chris Lattnerb38a7492005-04-02 04:01:14 +00002183 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002184 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002185 Result+1).addReg(TmpReg3).addReg(TmpReg);
2186 }
2187 return Result+N.ResNo;
2188 }
2189
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002190 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002191 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2192 Tmp2 = SelectExpr(N.getOperand(1));
2193 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002194 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002195 Tmp3 = SelectExpr(N.getOperand(2));
2196 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002197 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002198 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2199 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002200
2201 case ISD::SDIV:
2202 case ISD::UDIV:
2203 case ISD::SREM:
2204 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002205 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2206 "We don't support this operator!");
2207
Chris Lattner5bf26862005-04-13 03:29:53 +00002208 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002209 // We can fold loads into FpDIVs, but not really into any others.
2210 if (N.getValueType() == MVT::f64) {
2211 // Check for reversed and unreversed DIV.
2212 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2213 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2214 Opc = X86::FDIVR32m;
2215 else
2216 Opc = X86::FDIVR64m;
2217 X86AddressMode AM;
2218 EmitFoldedLoad(N.getOperand(0), AM);
2219 Tmp1 = SelectExpr(N.getOperand(1));
2220 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2221 return Result;
2222 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2223 N.getOperand(1).getOpcode() == ISD::LOAD) {
2224 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2225 Opc = X86::FDIV32m;
2226 else
2227 Opc = X86::FDIV64m;
2228 X86AddressMode AM;
2229 EmitFoldedLoad(N.getOperand(1), AM);
2230 Tmp1 = SelectExpr(N.getOperand(0));
2231 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2232 return Result;
2233 }
2234 }
2235
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002236 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2237 // FIXME: These special cases should be handled by the lowering impl!
2238 unsigned RHS = CN->getValue();
2239 bool isNeg = false;
2240 if ((int)RHS < 0) {
2241 isNeg = true;
2242 RHS = -RHS;
2243 }
2244 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2245 unsigned Log = log2(RHS);
2246 unsigned TmpReg = MakeReg(N.getValueType());
2247 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2248 switch (N.getValueType()) {
2249 default: assert("Unknown type to signed divide!");
2250 case MVT::i8:
2251 SAROpc = X86::SAR8ri;
2252 SHROpc = X86::SHR8ri;
2253 ADDOpc = X86::ADD8rr;
2254 NEGOpc = X86::NEG8r;
2255 break;
2256 case MVT::i16:
2257 SAROpc = X86::SAR16ri;
2258 SHROpc = X86::SHR16ri;
2259 ADDOpc = X86::ADD16rr;
2260 NEGOpc = X86::NEG16r;
2261 break;
2262 case MVT::i32:
2263 SAROpc = X86::SAR32ri;
2264 SHROpc = X86::SHR32ri;
2265 ADDOpc = X86::ADD32rr;
2266 NEGOpc = X86::NEG32r;
2267 break;
2268 }
Chris Lattner11333092005-01-11 03:11:44 +00002269 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002270 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2271 unsigned TmpReg2 = MakeReg(N.getValueType());
2272 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2273 unsigned TmpReg3 = MakeReg(N.getValueType());
2274 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002275
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002276 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2277 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2278 if (isNeg)
2279 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2280 return Result;
2281 }
2282 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002283 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002284
Chris Lattner11333092005-01-11 03:11:44 +00002285 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2286 Tmp1 = SelectExpr(N.getOperand(0));
2287 Tmp2 = SelectExpr(N.getOperand(1));
2288 } else {
2289 Tmp2 = SelectExpr(N.getOperand(1));
2290 Tmp1 = SelectExpr(N.getOperand(0));
2291 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292
2293 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2294 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2295 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2296 switch (N.getValueType()) {
2297 default: assert(0 && "Cannot sdiv this type!");
2298 case MVT::i8:
2299 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2300 LoReg = X86::AL;
2301 HiReg = X86::AH;
2302 MovOpcode = X86::MOV8rr;
2303 ClrOpcode = X86::MOV8ri;
2304 SExtOpcode = X86::CBW;
2305 break;
2306 case MVT::i16:
2307 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2308 LoReg = X86::AX;
2309 HiReg = X86::DX;
2310 MovOpcode = X86::MOV16rr;
2311 ClrOpcode = X86::MOV16ri;
2312 SExtOpcode = X86::CWD;
2313 break;
2314 case MVT::i32:
2315 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002316 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002317 HiReg = X86::EDX;
2318 MovOpcode = X86::MOV32rr;
2319 ClrOpcode = X86::MOV32ri;
2320 SExtOpcode = X86::CDQ;
2321 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002322 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002323 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002324 return Result;
2325 }
2326
2327 // Set up the low part.
2328 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2329
2330 if (isSigned) {
2331 // Sign extend the low part into the high part.
2332 BuildMI(BB, SExtOpcode, 0);
2333 } else {
2334 // Zero out the high part, effectively zero extending the input.
2335 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2336 }
2337
2338 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002339 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002340
2341 // Get the result of the divide or rem.
2342 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2343 return Result;
2344 }
2345
2346 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002347 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002348 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2349 switch (N.getValueType()) {
2350 default: assert(0 && "Cannot shift this type!");
2351 case MVT::i8: Opc = X86::ADD8rr; break;
2352 case MVT::i16: Opc = X86::ADD16rr; break;
2353 case MVT::i32: Opc = X86::ADD32rr; break;
2354 }
2355 Tmp1 = SelectExpr(N.getOperand(0));
2356 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2357 return Result;
2358 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002359
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002360 switch (N.getValueType()) {
2361 default: assert(0 && "Cannot shift this type!");
2362 case MVT::i8: Opc = X86::SHL8ri; break;
2363 case MVT::i16: Opc = X86::SHL16ri; break;
2364 case MVT::i32: Opc = X86::SHL32ri; break;
2365 }
Chris Lattner11333092005-01-11 03:11:44 +00002366 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002367 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2368 return Result;
2369 }
Chris Lattner11333092005-01-11 03:11:44 +00002370
2371 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2372 Tmp1 = SelectExpr(N.getOperand(0));
2373 Tmp2 = SelectExpr(N.getOperand(1));
2374 } else {
2375 Tmp2 = SelectExpr(N.getOperand(1));
2376 Tmp1 = SelectExpr(N.getOperand(0));
2377 }
2378
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002379 switch (N.getValueType()) {
2380 default: assert(0 && "Cannot shift this type!");
2381 case MVT::i8 : Opc = X86::SHL8rCL; break;
2382 case MVT::i16: Opc = X86::SHL16rCL; break;
2383 case MVT::i32: Opc = X86::SHL32rCL; break;
2384 }
2385 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2386 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2387 return Result;
2388 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002389 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2390 switch (N.getValueType()) {
2391 default: assert(0 && "Cannot shift this type!");
2392 case MVT::i8: Opc = X86::SHR8ri; break;
2393 case MVT::i16: Opc = X86::SHR16ri; break;
2394 case MVT::i32: Opc = X86::SHR32ri; break;
2395 }
Chris Lattner11333092005-01-11 03:11:44 +00002396 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002397 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2398 return Result;
2399 }
Chris Lattner11333092005-01-11 03:11:44 +00002400
2401 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2402 Tmp1 = SelectExpr(N.getOperand(0));
2403 Tmp2 = SelectExpr(N.getOperand(1));
2404 } else {
2405 Tmp2 = SelectExpr(N.getOperand(1));
2406 Tmp1 = SelectExpr(N.getOperand(0));
2407 }
2408
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002409 switch (N.getValueType()) {
2410 default: assert(0 && "Cannot shift this type!");
2411 case MVT::i8 : Opc = X86::SHR8rCL; break;
2412 case MVT::i16: Opc = X86::SHR16rCL; break;
2413 case MVT::i32: Opc = X86::SHR32rCL; break;
2414 }
2415 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2416 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2417 return Result;
2418 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002419 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2420 switch (N.getValueType()) {
2421 default: assert(0 && "Cannot shift this type!");
2422 case MVT::i8: Opc = X86::SAR8ri; break;
2423 case MVT::i16: Opc = X86::SAR16ri; break;
2424 case MVT::i32: Opc = X86::SAR32ri; break;
2425 }
Chris Lattner11333092005-01-11 03:11:44 +00002426 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002427 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2428 return Result;
2429 }
Chris Lattner11333092005-01-11 03:11:44 +00002430
2431 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2432 Tmp1 = SelectExpr(N.getOperand(0));
2433 Tmp2 = SelectExpr(N.getOperand(1));
2434 } else {
2435 Tmp2 = SelectExpr(N.getOperand(1));
2436 Tmp1 = SelectExpr(N.getOperand(0));
2437 }
2438
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002439 switch (N.getValueType()) {
2440 default: assert(0 && "Cannot shift this type!");
2441 case MVT::i8 : Opc = X86::SAR8rCL; break;
2442 case MVT::i16: Opc = X86::SAR16rCL; break;
2443 case MVT::i32: Opc = X86::SAR32rCL; break;
2444 }
2445 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2446 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2447 return Result;
2448
2449 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002450 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002451 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2452 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2453 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002454 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002455 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002456 if (Result != 1) { // Generate the token
2457 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2458 assert(0 && "Load already emitted!?");
2459 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002460 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2461
Chris Lattner5188ad72005-01-08 19:28:19 +00002462 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002463 default: assert(0 && "Cannot load this type!");
2464 case MVT::i1:
2465 case MVT::i8: Opc = X86::MOV8rm; break;
2466 case MVT::i16: Opc = X86::MOV16rm; break;
2467 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002468 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2469 }
Chris Lattner11333092005-01-11 03:11:44 +00002470
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002471 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002472 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002473 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2474 } else {
2475 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002476
2477 SDOperand Chain = N.getOperand(0);
2478 SDOperand Address = N.getOperand(1);
2479 if (getRegPressure(Chain) > getRegPressure(Address)) {
2480 Select(Chain);
2481 SelectAddress(Address, AM);
2482 } else {
2483 SelectAddress(Address, AM);
2484 Select(Chain);
2485 }
2486
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002487 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2488 }
2489 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002490
2491 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2492 case ISD::ZEXTLOAD: {
2493 // Make sure we generate both values.
2494 if (Result != 1)
2495 ExprMap[N.getValue(1)] = 1; // Generate the token
2496 else
2497 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2498
Chris Lattnerda2ce112005-01-16 07:34:08 +00002499 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2500 if (Node->getValueType(0) == MVT::f64) {
2501 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2502 "Bad EXTLOAD!");
2503 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2504 CP->getIndex());
2505 return Result;
2506 }
2507
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002508 X86AddressMode AM;
2509 if (getRegPressure(Node->getOperand(0)) >
2510 getRegPressure(Node->getOperand(1))) {
2511 Select(Node->getOperand(0)); // chain
2512 SelectAddress(Node->getOperand(1), AM);
2513 } else {
2514 SelectAddress(Node->getOperand(1), AM);
2515 Select(Node->getOperand(0)); // chain
2516 }
2517
2518 switch (Node->getValueType(0)) {
2519 default: assert(0 && "Unknown type to sign extend to.");
2520 case MVT::f64:
2521 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2522 "Bad EXTLOAD!");
2523 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2524 break;
2525 case MVT::i32:
2526 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2527 default:
2528 assert(0 && "Bad zero extend!");
2529 case MVT::i1:
2530 case MVT::i8:
2531 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2532 break;
2533 case MVT::i16:
2534 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2535 break;
2536 }
2537 break;
2538 case MVT::i16:
2539 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2540 "Bad zero extend!");
2541 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2542 break;
2543 case MVT::i8:
2544 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2545 "Bad zero extend!");
2546 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2547 break;
2548 }
2549 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002550 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002551 case ISD::SEXTLOAD: {
2552 // Make sure we generate both values.
2553 if (Result != 1)
2554 ExprMap[N.getValue(1)] = 1; // Generate the token
2555 else
2556 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2557
2558 X86AddressMode AM;
2559 if (getRegPressure(Node->getOperand(0)) >
2560 getRegPressure(Node->getOperand(1))) {
2561 Select(Node->getOperand(0)); // chain
2562 SelectAddress(Node->getOperand(1), AM);
2563 } else {
2564 SelectAddress(Node->getOperand(1), AM);
2565 Select(Node->getOperand(0)); // chain
2566 }
2567
2568 switch (Node->getValueType(0)) {
2569 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2570 default: assert(0 && "Unknown type to sign extend to.");
2571 case MVT::i32:
2572 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2573 default:
2574 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2575 case MVT::i8:
2576 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2577 break;
2578 case MVT::i16:
2579 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2580 break;
2581 }
2582 break;
2583 case MVT::i16:
2584 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2585 "Cannot sign extend from bool!");
2586 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2587 break;
2588 }
2589 return Result;
2590 }
2591
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002592 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002593 // Generate both result values.
2594 if (Result != 1)
2595 ExprMap[N.getValue(1)] = 1; // Generate the token
2596 else
2597 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2598
2599 // FIXME: We are currently ignoring the requested alignment for handling
2600 // greater than the stack alignment. This will need to be revisited at some
2601 // point. Align = N.getOperand(2);
2602
2603 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2604 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2605 std::cerr << "Cannot allocate stack object with greater alignment than"
2606 << " the stack alignment yet!";
2607 abort();
2608 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002609
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002611 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002612 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2613 .addImm(CN->getValue());
2614 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002615 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2616 Select(N.getOperand(0));
2617 Tmp1 = SelectExpr(N.getOperand(1));
2618 } else {
2619 Tmp1 = SelectExpr(N.getOperand(1));
2620 Select(N.getOperand(0));
2621 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002622
2623 // Subtract size from stack pointer, thereby allocating some space.
2624 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2625 }
2626
2627 // Put a pointer to the space into the result register, by copying the stack
2628 // pointer.
2629 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2630 return Result;
2631
2632 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002633 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002634 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002635
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002636 if (GlobalAddressSDNode *GASD =
2637 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002638 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002639 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2640 } else if (ExternalSymbolSDNode *ESSDN =
2641 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002642 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002643 BuildMI(BB, X86::CALLpcrel32,
2644 1).addExternalSymbol(ESSDN->getSymbol(), true);
2645 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002646 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2647 Select(N.getOperand(0));
2648 Tmp1 = SelectExpr(N.getOperand(1));
2649 } else {
2650 Tmp1 = SelectExpr(N.getOperand(1));
2651 Select(N.getOperand(0));
2652 }
2653
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002654 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2655 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002656 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002657 default: assert(0 && "Unknown value type for call result!");
2658 case MVT::Other: return 1;
2659 case MVT::i1:
2660 case MVT::i8:
2661 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2662 break;
2663 case MVT::i16:
2664 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2665 break;
2666 case MVT::i32:
2667 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002668 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002669 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2670 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002671 case MVT::f64: // Floating-point return values live in %ST(0)
2672 ContainsFPCode = true;
2673 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2674 break;
2675 }
2676 return Result+N.ResNo;
2677 }
2678
2679 return 0;
2680}
2681
Chris Lattnere10269b2005-01-17 19:25:26 +00002682/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2683/// load/op/store instruction. If successful return true.
2684bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2685 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2686 SDOperand Chain = Node->getOperand(0);
2687 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002688 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002689
2690 // The chain has to be a load, the stored value must be an integer binary
2691 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002692 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002693 MVT::isFloatingPoint(StVal.getValueType()))
2694 return false;
2695
Chris Lattner5c659812005-01-17 22:10:42 +00002696 // Token chain must either be a factor node or the load to fold.
2697 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2698 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002699
Chris Lattner5c659812005-01-17 22:10:42 +00002700 SDOperand TheLoad;
2701
2702 // Check to see if there is a load from the same pointer that we're storing
2703 // to in either operand of the binop.
2704 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2705 StVal.getOperand(0).getOperand(1) == StPtr)
2706 TheLoad = StVal.getOperand(0);
2707 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2708 StVal.getOperand(1).getOperand(1) == StPtr)
2709 TheLoad = StVal.getOperand(1);
2710 else
2711 return false; // No matching load operand.
2712
2713 // We can only fold the load if there are no intervening side-effecting
2714 // operations. This means that the store uses the load as its token chain, or
2715 // there are only token factor nodes in between the store and load.
2716 if (Chain != TheLoad.getValue(1)) {
2717 // Okay, the other option is that we have a store referring to (possibly
2718 // nested) token factor nodes. For now, just try peeking through one level
2719 // of token factors to see if this is the case.
2720 bool ChainOk = false;
2721 if (Chain.getOpcode() == ISD::TokenFactor) {
2722 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2723 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2724 ChainOk = true;
2725 break;
2726 }
2727 }
2728
2729 if (!ChainOk) return false;
2730 }
2731
2732 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002733 return false;
2734
2735 // Make sure that one of the operands of the binop is the load, and that the
2736 // load folds into the binop.
2737 if (((StVal.getOperand(0) != TheLoad ||
2738 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2739 (StVal.getOperand(1) != TheLoad ||
2740 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2741 return false;
2742
2743 // Finally, check to see if this is one of the ops we can handle!
2744 static const unsigned ADDTAB[] = {
2745 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2746 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2747 };
2748 static const unsigned SUBTAB[] = {
2749 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2750 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2751 };
2752 static const unsigned ANDTAB[] = {
2753 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2754 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2755 };
2756 static const unsigned ORTAB[] = {
2757 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2758 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2759 };
2760 static const unsigned XORTAB[] = {
2761 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2762 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2763 };
2764 static const unsigned SHLTAB[] = {
2765 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2766 /*Have to put the reg in CL*/0, 0, 0,
2767 };
2768 static const unsigned SARTAB[] = {
2769 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2770 /*Have to put the reg in CL*/0, 0, 0,
2771 };
2772 static const unsigned SHRTAB[] = {
2773 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2774 /*Have to put the reg in CL*/0, 0, 0,
2775 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002776
Chris Lattnere10269b2005-01-17 19:25:26 +00002777 const unsigned *TabPtr = 0;
2778 switch (StVal.getOpcode()) {
2779 default:
2780 std::cerr << "CANNOT [mem] op= val: ";
2781 StVal.Val->dump(); std::cerr << "\n";
2782 case ISD::MUL:
2783 case ISD::SDIV:
2784 case ISD::UDIV:
2785 case ISD::SREM:
2786 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002787
Chris Lattnere10269b2005-01-17 19:25:26 +00002788 case ISD::ADD: TabPtr = ADDTAB; break;
2789 case ISD::SUB: TabPtr = SUBTAB; break;
2790 case ISD::AND: TabPtr = ANDTAB; break;
2791 case ISD:: OR: TabPtr = ORTAB; break;
2792 case ISD::XOR: TabPtr = XORTAB; break;
2793 case ISD::SHL: TabPtr = SHLTAB; break;
2794 case ISD::SRA: TabPtr = SARTAB; break;
2795 case ISD::SRL: TabPtr = SHRTAB; break;
2796 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002797
Chris Lattnere10269b2005-01-17 19:25:26 +00002798 // Handle: [mem] op= CST
2799 SDOperand Op0 = StVal.getOperand(0);
2800 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002801 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002802 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2803 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2804 default: break;
2805 case MVT::i1:
2806 case MVT::i8: Opc = TabPtr[0]; break;
2807 case MVT::i16: Opc = TabPtr[1]; break;
2808 case MVT::i32: Opc = TabPtr[2]; break;
2809 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002810
Chris Lattnere10269b2005-01-17 19:25:26 +00002811 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002812 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2813 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002814 Select(Chain);
2815
Chris Lattnere10269b2005-01-17 19:25:26 +00002816 X86AddressMode AM;
2817 if (getRegPressure(TheLoad.getOperand(0)) >
2818 getRegPressure(TheLoad.getOperand(1))) {
2819 Select(TheLoad.getOperand(0));
2820 SelectAddress(TheLoad.getOperand(1), AM);
2821 } else {
2822 SelectAddress(TheLoad.getOperand(1), AM);
2823 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002824 }
Chris Lattner5c659812005-01-17 22:10:42 +00002825
2826 if (StVal.getOpcode() == ISD::ADD) {
2827 if (CN->getValue() == 1) {
2828 switch (Op0.getValueType()) {
2829 default: break;
2830 case MVT::i8:
2831 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2832 return true;
2833 case MVT::i16: Opc = TabPtr[1];
2834 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2835 return true;
2836 case MVT::i32: Opc = TabPtr[2];
2837 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2838 return true;
2839 }
2840 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2841 switch (Op0.getValueType()) {
2842 default: break;
2843 case MVT::i8:
2844 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2845 return true;
2846 case MVT::i16: Opc = TabPtr[1];
2847 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2848 return true;
2849 case MVT::i32: Opc = TabPtr[2];
2850 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2851 return true;
2852 }
2853 }
2854 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002855
Chris Lattnere10269b2005-01-17 19:25:26 +00002856 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2857 return true;
2858 }
2859 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002860
Chris Lattnere10269b2005-01-17 19:25:26 +00002861 // If we have [mem] = V op [mem], try to turn it into:
2862 // [mem] = [mem] op V.
2863 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2864 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2865 StVal.getOpcode() != ISD::SRL)
2866 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002867
Chris Lattnere10269b2005-01-17 19:25:26 +00002868 if (Op0 != TheLoad) return false;
2869
2870 switch (Op0.getValueType()) {
2871 default: return false;
2872 case MVT::i1:
2873 case MVT::i8: Opc = TabPtr[3]; break;
2874 case MVT::i16: Opc = TabPtr[4]; break;
2875 case MVT::i32: Opc = TabPtr[5]; break;
2876 }
Chris Lattner5c659812005-01-17 22:10:42 +00002877
Chris Lattnerb422aea2005-01-18 17:35:28 +00002878 // Table entry doesn't exist?
2879 if (Opc == 0) return false;
2880
Chris Lattner4a108662005-01-18 03:51:59 +00002881 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2882 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002883 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002884 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002885
Chris Lattnere10269b2005-01-17 19:25:26 +00002886 X86AddressMode AM;
2887 SelectAddress(TheLoad.getOperand(1), AM);
2888 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002889 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002890 return true;
2891}
2892
2893
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002894void ISel::Select(SDOperand N) {
2895 unsigned Tmp1, Tmp2, Opc;
2896
Nate Begeman85fdeb22005-03-24 04:39:54 +00002897 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002898 return; // Already selected.
2899
Chris Lattner989de032005-01-11 06:14:36 +00002900 SDNode *Node = N.Val;
2901
2902 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002903 default:
Chris Lattner989de032005-01-11 06:14:36 +00002904 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002905 assert(0 && "Node not handled yet!");
2906 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002907 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002908 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002909 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002910 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2911 Select(Node->getOperand(OneFirst));
2912 Select(Node->getOperand(!OneFirst));
2913 } else {
2914 std::vector<std::pair<unsigned, unsigned> > OpsP;
2915 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2916 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2917 std::sort(OpsP.begin(), OpsP.end());
2918 std::reverse(OpsP.begin(), OpsP.end());
2919 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2920 Select(Node->getOperand(OpsP[i].second));
2921 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002922 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002923 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002924 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2925 Select(N.getOperand(0));
2926 Tmp1 = SelectExpr(N.getOperand(1));
2927 } else {
2928 Tmp1 = SelectExpr(N.getOperand(1));
2929 Select(N.getOperand(0));
2930 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002931 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002932
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002933 if (Tmp1 != Tmp2) {
2934 switch (N.getOperand(1).getValueType()) {
2935 default: assert(0 && "Invalid type for operation!");
2936 case MVT::i1:
2937 case MVT::i8: Opc = X86::MOV8rr; break;
2938 case MVT::i16: Opc = X86::MOV16rr; break;
2939 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002940 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002941 }
2942 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2943 }
2944 return;
2945 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002946 switch (N.getNumOperands()) {
2947 default:
2948 assert(0 && "Unknown return instruction!");
2949 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002950 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2951 N.getOperand(2).getValueType() == MVT::i32 &&
2952 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002953 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2954 Tmp1 = SelectExpr(N.getOperand(1));
2955 Tmp2 = SelectExpr(N.getOperand(2));
2956 } else {
2957 Tmp2 = SelectExpr(N.getOperand(2));
2958 Tmp1 = SelectExpr(N.getOperand(1));
2959 }
2960 Select(N.getOperand(0));
2961
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002962 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2963 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002964 break;
2965 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002966 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2967 Select(N.getOperand(0));
2968 Tmp1 = SelectExpr(N.getOperand(1));
2969 } else {
2970 Tmp1 = SelectExpr(N.getOperand(1));
2971 Select(N.getOperand(0));
2972 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002973 switch (N.getOperand(1).getValueType()) {
2974 default: assert(0 && "All other types should have been promoted!!");
2975 case MVT::f64:
2976 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002977 break;
2978 case MVT::i32:
2979 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002980 break;
2981 }
2982 break;
2983 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002984 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002985 break;
2986 }
2987 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2988 return;
2989 case ISD::BR: {
2990 Select(N.getOperand(0));
2991 MachineBasicBlock *Dest =
2992 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2993 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2994 return;
2995 }
2996
2997 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002998 MachineBasicBlock *Dest =
2999 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003000
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003001 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3002 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003003 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3004 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3005 Select(N.getOperand(0));
3006 Tmp1 = SelectExpr(N.getOperand(1));
3007 } else {
3008 Tmp1 = SelectExpr(N.getOperand(1));
3009 Select(N.getOperand(0));
3010 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003011 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3012 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3013 }
Chris Lattner11333092005-01-11 03:11:44 +00003014
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003015 return;
3016 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003017
Chris Lattner4df0de92005-01-17 00:00:33 +00003018 case ISD::LOAD:
3019 // If this load could be folded into the only using instruction, and if it
3020 // is safe to emit the instruction here, try to do so now.
3021 if (Node->hasNUsesOfValue(1, 0)) {
3022 SDOperand TheVal = N.getValue(0);
3023 SDNode *User = 0;
3024 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3025 assert(UI != Node->use_end() && "Didn't find use!");
3026 SDNode *UN = *UI;
3027 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3028 if (UN->getOperand(i) == TheVal) {
3029 User = UN;
3030 goto FoundIt;
3031 }
3032 }
3033 FoundIt:
3034 // Only handle unary operators right now.
3035 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003036 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003037 SelectExpr(SDOperand(User, 0));
3038 return;
3039 }
3040 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003041 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003042 SelectExpr(N);
3043 return;
3044
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003045 case ISD::EXTLOAD:
3046 case ISD::SEXTLOAD:
3047 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003048 case ISD::CALL:
3049 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003050 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003051 SelectExpr(N);
3052 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003053
3054 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3055 // On X86, we can represent all types except for Bool and Float natively.
3056 X86AddressMode AM;
3057 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003058 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3059 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3060 && "Unsupported TRUNCSTORE for this target!");
3061
3062 if (StoredTy == MVT::i16) {
3063 // FIXME: This is here just to allow testing. X86 doesn't really have a
3064 // TRUNCSTORE i16 operation, but this is required for targets that do not
3065 // have 16-bit integer registers. We occasionally disable 16-bit integer
3066 // registers to test the promotion code.
3067 Select(N.getOperand(0));
3068 Tmp1 = SelectExpr(N.getOperand(1));
3069 SelectAddress(N.getOperand(2), AM);
3070
3071 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3072 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3073 return;
3074 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003075
3076 // Store of constant bool?
3077 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3078 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3079 Select(N.getOperand(0));
3080 SelectAddress(N.getOperand(2), AM);
3081 } else {
3082 SelectAddress(N.getOperand(2), AM);
3083 Select(N.getOperand(0));
3084 }
3085 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3086 return;
3087 }
3088
3089 switch (StoredTy) {
3090 default: assert(0 && "Cannot truncstore this type!");
3091 case MVT::i1: Opc = X86::MOV8mr; break;
3092 case MVT::f32: Opc = X86::FST32m; break;
3093 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003094
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003095 std::vector<std::pair<unsigned, unsigned> > RP;
3096 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3097 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3098 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3099 std::sort(RP.begin(), RP.end());
3100
Chris Lattner572dd082005-02-23 05:57:21 +00003101 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003102 for (unsigned i = 0; i != 3; ++i)
3103 switch (RP[2-i].second) {
3104 default: assert(0 && "Unknown operand number!");
3105 case 0: Select(N.getOperand(0)); break;
3106 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3107 case 2: SelectAddress(N.getOperand(2), AM); break;
3108 }
3109
3110 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3111 return;
3112 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003113 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003114 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003115
3116 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3117 Opc = 0;
3118 switch (CN->getValueType(0)) {
3119 default: assert(0 && "Invalid type for operation!");
3120 case MVT::i1:
3121 case MVT::i8: Opc = X86::MOV8mi; break;
3122 case MVT::i16: Opc = X86::MOV16mi; break;
3123 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003124 case MVT::f64: break;
3125 }
3126 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003127 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3128 Select(N.getOperand(0));
3129 SelectAddress(N.getOperand(2), AM);
3130 } else {
3131 SelectAddress(N.getOperand(2), AM);
3132 Select(N.getOperand(0));
3133 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003134 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3135 return;
3136 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003137 } else if (GlobalAddressSDNode *GA =
3138 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3139 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3140
3141 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3142 Select(N.getOperand(0));
3143 SelectAddress(N.getOperand(2), AM);
3144 } else {
3145 SelectAddress(N.getOperand(2), AM);
3146 Select(N.getOperand(0));
3147 }
3148 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3149 AM).addGlobalAddress(GA->getGlobal());
3150 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003151 }
Chris Lattner837caa72005-01-11 23:21:30 +00003152
3153 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003154 if (TryToFoldLoadOpStore(Node))
3155 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003156
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003157 switch (N.getOperand(1).getValueType()) {
3158 default: assert(0 && "Cannot store this type!");
3159 case MVT::i1:
3160 case MVT::i8: Opc = X86::MOV8mr; break;
3161 case MVT::i16: Opc = X86::MOV16mr; break;
3162 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003163 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003164 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003165
Chris Lattner11333092005-01-11 03:11:44 +00003166 std::vector<std::pair<unsigned, unsigned> > RP;
3167 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3168 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3169 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3170 std::sort(RP.begin(), RP.end());
3171
Chris Lattner572dd082005-02-23 05:57:21 +00003172 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003173 for (unsigned i = 0; i != 3; ++i)
3174 switch (RP[2-i].second) {
3175 default: assert(0 && "Unknown operand number!");
3176 case 0: Select(N.getOperand(0)); break;
3177 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003178 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003179 }
3180
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003181 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3182 return;
3183 }
3184 case ISD::ADJCALLSTACKDOWN:
3185 case ISD::ADJCALLSTACKUP:
3186 Select(N.getOperand(0));
3187 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003188
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003189 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3190 X86::ADJCALLSTACKUP;
3191 BuildMI(BB, Opc, 1).addImm(Tmp1);
3192 return;
Chris Lattner989de032005-01-11 06:14:36 +00003193 case ISD::MEMSET: {
3194 Select(N.getOperand(0)); // Select the chain.
3195 unsigned Align =
3196 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3197 if (Align == 0) Align = 1;
3198
3199 // Turn the byte code into # iterations
3200 unsigned CountReg;
3201 unsigned Opcode;
3202 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3203 unsigned Val = ValC->getValue() & 255;
3204
3205 // If the value is a constant, then we can potentially use larger sets.
3206 switch (Align & 3) {
3207 case 2: // WORD aligned
3208 CountReg = MakeReg(MVT::i32);
3209 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3210 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3211 } else {
3212 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3213 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3214 }
3215 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3216 Opcode = X86::REP_STOSW;
3217 break;
3218 case 0: // DWORD aligned
3219 CountReg = MakeReg(MVT::i32);
3220 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3221 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3222 } else {
3223 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3224 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3225 }
3226 Val = (Val << 8) | Val;
3227 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3228 Opcode = X86::REP_STOSD;
3229 break;
3230 default: // BYTE aligned
3231 CountReg = SelectExpr(Node->getOperand(3));
3232 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3233 Opcode = X86::REP_STOSB;
3234 break;
3235 }
3236 } else {
3237 // If it's not a constant value we are storing, just fall back. We could
3238 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3239 unsigned ValReg = SelectExpr(Node->getOperand(2));
3240 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3241 CountReg = SelectExpr(Node->getOperand(3));
3242 Opcode = X86::REP_STOSB;
3243 }
3244
3245 // No matter what the alignment is, we put the source in ESI, the
3246 // destination in EDI, and the count in ECX.
3247 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3248 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3249 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3250 BuildMI(BB, Opcode, 0);
3251 return;
3252 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003253 case ISD::MEMCPY:
3254 Select(N.getOperand(0)); // Select the chain.
3255 unsigned Align =
3256 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3257 if (Align == 0) Align = 1;
3258
3259 // Turn the byte code into # iterations
3260 unsigned CountReg;
3261 unsigned Opcode;
3262 switch (Align & 3) {
3263 case 2: // WORD aligned
3264 CountReg = MakeReg(MVT::i32);
3265 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3266 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3267 } else {
3268 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3269 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3270 }
3271 Opcode = X86::REP_MOVSW;
3272 break;
3273 case 0: // DWORD aligned
3274 CountReg = MakeReg(MVT::i32);
3275 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3276 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3277 } else {
3278 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3279 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3280 }
3281 Opcode = X86::REP_MOVSD;
3282 break;
3283 default: // BYTE aligned
3284 CountReg = SelectExpr(Node->getOperand(3));
3285 Opcode = X86::REP_MOVSB;
3286 break;
3287 }
3288
3289 // No matter what the alignment is, we put the source in ESI, the
3290 // destination in EDI, and the count in ECX.
3291 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3292 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3293 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3294 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3295 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3296 BuildMI(BB, Opcode, 0);
3297 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003298 }
3299 assert(0 && "Should not be reached!");
3300}
3301
3302
3303/// createX86PatternInstructionSelector - This pass converts an LLVM function
3304/// into a machine code representation using pattern matching and a machine
3305/// description file.
3306///
3307FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003308 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003309}