blob: 209a9d051cb322de88bb70852fbd40bc295295ed [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 Lattnere3e0f272005-05-09 03:36:39 +000017#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000019#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000029#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000030#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000033#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// X86TargetLowering - X86 Implementation of the TargetLowering interface
38namespace {
39 class X86TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000041 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000042 public:
43 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000045
46 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000049 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000051
52 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000057
Chris Lattner8acb1ba2005-01-07 07:49:41 +000058 // FIXME: Eliminate these two classes when legalize can handle promotions
59 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000060/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000061
Chris Lattnerda4d4692005-04-09 03:22:37 +000062 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000065 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000066 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
67 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
68 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000069 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000071 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000072
Chris Lattnerc5dcb532005-04-30 04:25:35 +000073 if (!UnsafeFPMath) {
74 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
75 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
76 }
77
Chris Lattnerda2ce112005-01-16 07:34:08 +000078 // These should be promoted to a larger select which is supported.
79/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
80 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000081
Chris Lattner8acb1ba2005-01-07 07:49:41 +000082 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000083
Chris Lattner8acb1ba2005-01-07 07:49:41 +000084 addLegalFPImmediate(+0.0); // FLD0
85 addLegalFPImmediate(+1.0); // FLD1
86 addLegalFPImmediate(-0.0); // FLD0/FCHS
87 addLegalFPImmediate(-1.0); // FLD1/FCHS
88 }
89
90 /// LowerArguments - This hook must be implemented to indicate how we should
91 /// lower the arguments for the specified function, into the specified DAG.
92 virtual std::vector<SDOperand>
93 LowerArguments(Function &F, SelectionDAG &DAG);
94
95 /// LowerCallTo - This hook lowers an abstract call to a function into an
96 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000097 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000098 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
99 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000100
101 virtual std::pair<SDOperand, SDOperand>
102 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
103
104 virtual std::pair<SDOperand,SDOperand>
105 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
106 const Type *ArgTy, SelectionDAG &DAG);
107
108 virtual std::pair<SDOperand, SDOperand>
109 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
110 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000111 };
112}
113
114
115std::vector<SDOperand>
116X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
117 std::vector<SDOperand> ArgValues;
118
119 // Add DAG nodes to load the arguments... On entry to a function on the X86,
120 // the stack frame looks like this:
121 //
122 // [ESP] -- return address
123 // [ESP + 4] -- first argument (leftmost lexically)
124 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000125 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000126 //
127 MachineFunction &MF = DAG.getMachineFunction();
128 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000129
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000130 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000131 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000132 MVT::ValueType ObjectVT = getValueType(I->getType());
133 unsigned ArgIncrement = 4;
134 unsigned ObjSize;
135 switch (ObjectVT) {
136 default: assert(0 && "Unhandled argument type!");
137 case MVT::i1:
138 case MVT::i8: ObjSize = 1; break;
139 case MVT::i16: ObjSize = 2; break;
140 case MVT::i32: ObjSize = 4; break;
141 case MVT::i64: ObjSize = ArgIncrement = 8; break;
142 case MVT::f32: ObjSize = 4; break;
143 case MVT::f64: ObjSize = ArgIncrement = 8; break;
144 }
145 // Create the frame index object for this incoming parameter...
146 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000147
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000148 // Create the SelectionDAG nodes corresponding to a load from this parameter
149 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
150
151 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
152 // dead loads.
153 SDOperand ArgValue;
154 if (!I->use_empty())
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000155 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000156 else {
157 if (MVT::isInteger(ObjectVT))
158 ArgValue = DAG.getConstant(0, ObjectVT);
159 else
160 ArgValue = DAG.getConstantFP(0, ObjectVT);
161 }
162 ArgValues.push_back(ArgValue);
163
164 ArgOffset += ArgIncrement; // Move on to the next argument...
165 }
166
167 // If the function takes variable number of arguments, make a frame index for
168 // the start of the first vararg value... for expansion of llvm.va_start.
169 if (F.isVarArg())
170 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000171 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000172
173 // Finally, inform the code generator which regs we return values in.
174 switch (getValueType(F.getReturnType())) {
175 default: assert(0 && "Unknown type!");
176 case MVT::isVoid: break;
177 case MVT::i1:
178 case MVT::i8:
179 case MVT::i16:
180 case MVT::i32:
181 MF.addLiveOut(X86::EAX);
182 break;
183 case MVT::i64:
184 MF.addLiveOut(X86::EAX);
185 MF.addLiveOut(X86::EDX);
186 break;
187 case MVT::f32:
188 case MVT::f64:
189 MF.addLiveOut(X86::ST0);
190 break;
191 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000192 return ArgValues;
193}
194
Chris Lattner5188ad72005-01-08 19:28:19 +0000195std::pair<SDOperand, SDOperand>
196X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000197 const Type *RetTy, bool isVarArg,
198 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199 // Count how many bytes are to be pushed on the stack.
200 unsigned NumBytes = 0;
201
202 if (Args.empty()) {
203 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000204 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
205 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000206 } else {
207 for (unsigned i = 0, e = Args.size(); i != e; ++i)
208 switch (getValueType(Args[i].second)) {
209 default: assert(0 && "Unknown value type!");
210 case MVT::i1:
211 case MVT::i8:
212 case MVT::i16:
213 case MVT::i32:
214 case MVT::f32:
215 NumBytes += 4;
216 break;
217 case MVT::i64:
218 case MVT::f64:
219 NumBytes += 8;
220 break;
221 }
222
Chris Lattner5188ad72005-01-08 19:28:19 +0000223 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
224 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000225
226 // Arguments go on the stack in reverse order, as specified by the ABI.
227 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000228 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
229 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000230 std::vector<SDOperand> Stores;
231
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000232 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
233 unsigned ArgReg;
234 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
235 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
236
237 switch (getValueType(Args[i].second)) {
238 default: assert(0 && "Unexpected ValueType for argument!");
239 case MVT::i1:
240 case MVT::i8:
241 case MVT::i16:
242 // Promote the integer to 32 bits. If the input type is signed use a
243 // sign extend, otherwise use a zero extend.
244 if (Args[i].second->isSigned())
245 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
246 else
247 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
248
249 // FALL THROUGH
250 case MVT::i32:
251 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000252 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000253 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000254 ArgOffset += 4;
255 break;
256 case MVT::i64:
257 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000258 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000259 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000260 ArgOffset += 8;
261 break;
262 }
263 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000264 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000265 }
266
267 std::vector<MVT::ValueType> RetVals;
268 MVT::ValueType RetTyVT = getValueType(RetTy);
269 if (RetTyVT != MVT::isVoid)
270 RetVals.push_back(RetTyVT);
271 RetVals.push_back(MVT::Other);
272
Chris Lattner5188ad72005-01-08 19:28:19 +0000273 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000274 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000275 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
276 DAG.getConstant(NumBytes, getPointerTy()));
277 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000278}
279
Chris Lattner14824582005-01-09 00:01:27 +0000280std::pair<SDOperand, SDOperand>
281X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
282 // vastart just returns the address of the VarArgsFrameIndex slot.
283 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
284}
285
286std::pair<SDOperand,SDOperand> X86TargetLowering::
287LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
288 const Type *ArgTy, SelectionDAG &DAG) {
289 MVT::ValueType ArgVT = getValueType(ArgTy);
290 SDOperand Result;
291 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000292 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000293 } else {
294 unsigned Amt;
295 if (ArgVT == MVT::i32)
296 Amt = 4;
297 else {
298 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
299 "Other types should have been promoted for varargs!");
300 Amt = 8;
301 }
302 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
303 DAG.getConstant(Amt, VAList.getValueType()));
304 }
305 return std::make_pair(Result, Chain);
306}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000307
Chris Lattner14824582005-01-09 00:01:27 +0000308
309std::pair<SDOperand, SDOperand> X86TargetLowering::
310LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
311 SelectionDAG &DAG) {
312 SDOperand Result;
313 if (Depth) // Depths > 0 not supported yet!
314 Result = DAG.getConstant(0, getPointerTy());
315 else {
316 if (ReturnAddrIndex == 0) {
317 // Set up a frame object for the return address.
318 MachineFunction &MF = DAG.getMachineFunction();
319 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
320 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000321
Chris Lattner14824582005-01-09 00:01:27 +0000322 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
323
324 if (!isFrameAddress)
325 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000326 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000327 else
328 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
329 DAG.getConstant(4, MVT::i32));
330 }
331 return std::make_pair(Result, Chain);
332}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000333
334
Chris Lattner98a8ba02005-01-18 01:06:26 +0000335namespace {
336 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
337 /// SDOperand's instead of register numbers for the leaves of the matched
338 /// tree.
339 struct X86ISelAddressMode {
340 enum {
341 RegBase,
342 FrameIndexBase,
343 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000344
Chris Lattner98a8ba02005-01-18 01:06:26 +0000345 struct { // This is really a union, discriminated by BaseType!
346 SDOperand Reg;
347 int FrameIndex;
348 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000349
Chris Lattner98a8ba02005-01-18 01:06:26 +0000350 unsigned Scale;
351 SDOperand IndexReg;
352 unsigned Disp;
353 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000354
Chris Lattner98a8ba02005-01-18 01:06:26 +0000355 X86ISelAddressMode()
356 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
357 }
358 };
359}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000360
361
362namespace {
363 Statistic<>
364 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
365
366 //===--------------------------------------------------------------------===//
367 /// ISel - X86 specific code to select X86 machine instructions for
368 /// SelectionDAG operations.
369 ///
370 class ISel : public SelectionDAGISel {
371 /// ContainsFPCode - Every instruction we select that uses or defines a FP
372 /// register should set this to true.
373 bool ContainsFPCode;
374
375 /// X86Lowering - This object fully describes how to lower LLVM code to an
376 /// X86-specific SelectionDAG.
377 X86TargetLowering X86Lowering;
378
Chris Lattner11333092005-01-11 03:11:44 +0000379 /// RegPressureMap - This keeps an approximate count of the number of
380 /// registers required to evaluate each node in the graph.
381 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000382
383 /// ExprMap - As shared expressions are codegen'd, we keep track of which
384 /// vreg the value is produced in, so we only emit one copy of each compiled
385 /// tree.
386 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000387
388 public:
389 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
390 }
391
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000392 virtual const char *getPassName() const {
393 return "X86 Pattern Instruction Selection";
394 }
395
Chris Lattner11333092005-01-11 03:11:44 +0000396 unsigned getRegPressure(SDOperand O) {
397 return RegPressureMap[O.Val];
398 }
399 unsigned ComputeRegPressure(SDOperand O);
400
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000401 /// InstructionSelectBasicBlock - This callback is invoked by
402 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000403 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000404
Chris Lattner44129b52005-01-25 20:03:11 +0000405 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
406 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000407 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000408 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000409
Chris Lattner30ea1e92005-01-19 07:37:26 +0000410 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000411 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000412 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000413 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
414 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000415 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000416
417 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
418 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
419 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000420 void Select(SDOperand N);
421 };
422}
423
Chris Lattner7dbcb752005-01-12 04:21:28 +0000424/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
425/// when it has created a SelectionDAG for us to codegen.
426void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
427 // While we're doing this, keep track of whether we see any FP code for
428 // FP_REG_KILL insertion.
429 ContainsFPCode = false;
430
431 // Scan the PHI nodes that already are inserted into this basic block. If any
432 // of them is a PHI of a floating point value, we need to insert an
433 // FP_REG_KILL.
434 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
435 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
436 I != E; ++I) {
437 assert(I->getOpcode() == X86::PHI &&
438 "Isn't just PHI nodes?");
439 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
440 X86::RFPRegisterClass) {
441 ContainsFPCode = true;
442 break;
443 }
444 }
445
446 // Compute the RegPressureMap, which is an approximation for the number of
447 // registers required to compute each node.
448 ComputeRegPressure(DAG.getRoot());
449
450 // Codegen the basic block.
451 Select(DAG.getRoot());
452
453 // Finally, look at all of the successors of this block. If any contain a PHI
454 // node of FP type, we need to insert an FP_REG_KILL in this block.
455 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
456 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
457 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
458 I != E && I->getOpcode() == X86::PHI; ++I) {
459 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
460 X86::RFPRegisterClass) {
461 ContainsFPCode = true;
462 break;
463 }
464 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000465
Chris Lattnere3e0f272005-05-09 03:36:39 +0000466 // Final check, check LLVM BB's that are successors to the LLVM BB
467 // corresponding to BB for FP PHI nodes.
468 const BasicBlock *LLVMBB = BB->getBasicBlock();
469 const PHINode *PN;
470 if (!ContainsFPCode)
471 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
472 SI != E && !ContainsFPCode; ++SI)
473 for (BasicBlock::const_iterator II = SI->begin();
474 (PN = dyn_cast<PHINode>(II)); ++II)
475 if (PN->getType()->isFloatingPoint()) {
476 ContainsFPCode = true;
477 break;
478 }
479
480
Chris Lattner7dbcb752005-01-12 04:21:28 +0000481 // Insert FP_REG_KILL instructions into basic blocks that need them. This
482 // only occurs due to the floating point stackifier not being aggressive
483 // enough to handle arbitrary global stackification.
484 //
485 // Currently we insert an FP_REG_KILL instruction into each block that uses or
486 // defines a floating point virtual register.
487 //
488 // When the global register allocators (like linear scan) finally update live
489 // variable analysis, we can keep floating point values in registers across
490 // basic blocks. This will be a huge win, but we are waiting on the global
491 // allocators before we can do this.
492 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000493 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000494 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
495 ++NumFPKill;
496 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000497
Chris Lattner7dbcb752005-01-12 04:21:28 +0000498 // Clear state used for selection.
499 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000500 RegPressureMap.clear();
501}
502
503
Chris Lattner11333092005-01-11 03:11:44 +0000504// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
505// for the number of registers required to compute each node. This is basically
506// computing a generalized form of the Sethi-Ullman number for each node.
507unsigned ISel::ComputeRegPressure(SDOperand O) {
508 SDNode *N = O.Val;
509 unsigned &Result = RegPressureMap[N];
510 if (Result) return Result;
511
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000512 // FIXME: Should operations like CALL (which clobber lots o regs) have a
513 // higher fixed cost??
514
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000515 if (N->getNumOperands() == 0) {
516 Result = 1;
517 } else {
518 unsigned MaxRegUse = 0;
519 unsigned NumExtraMaxRegUsers = 0;
520 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
521 unsigned Regs;
522 if (N->getOperand(i).getOpcode() == ISD::Constant)
523 Regs = 0;
524 else
525 Regs = ComputeRegPressure(N->getOperand(i));
526 if (Regs > MaxRegUse) {
527 MaxRegUse = Regs;
528 NumExtraMaxRegUsers = 0;
529 } else if (Regs == MaxRegUse &&
530 N->getOperand(i).getValueType() != MVT::Other) {
531 ++NumExtraMaxRegUsers;
532 }
Chris Lattner11333092005-01-11 03:11:44 +0000533 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000534
535 if (O.getOpcode() != ISD::TokenFactor)
536 Result = MaxRegUse+NumExtraMaxRegUsers;
537 else
Chris Lattner869e0432005-01-17 23:02:13 +0000538 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000539 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000540
Chris Lattner837caa72005-01-11 23:21:30 +0000541 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000542 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000543}
544
Chris Lattnerbf52d492005-01-20 16:50:16 +0000545/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
546/// The DAG cannot have cycles in it, by definition, so the visited set is not
547/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
548/// reuse, so it prevents exponential cases.
549///
550static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
551 std::set<SDNode*> &Visited) {
552 if (N == Op) return true; // Found it.
553 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000554 if (Node->getNumOperands() == 0 || // Leaf?
555 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000556 if (!Visited.insert(Node).second) return false; // Already visited?
557
558 // Recurse for the first N-1 operands.
559 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
560 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
561 return true;
562
563 // Tail recurse for the last operand.
564 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
565}
566
Chris Lattner98a8ba02005-01-18 01:06:26 +0000567X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
568 X86AddressMode Result;
569
570 // If we need to emit two register operands, emit the one with the highest
571 // register pressure first.
572 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
573 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000574 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000575 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000576 std::set<SDNode*> Visited;
577 EmitBaseThenIndex = true;
578 // If Base ends up pointing to Index, we must emit index first. This is
579 // because of the way we fold loads, we may end up doing bad things with
580 // the folded add.
581 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
582 EmitBaseThenIndex = false;
583 } else {
584 std::set<SDNode*> Visited;
585 EmitBaseThenIndex = false;
586 // If Base ends up pointing to Index, we must emit index first. This is
587 // because of the way we fold loads, we may end up doing bad things with
588 // the folded add.
589 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
590 EmitBaseThenIndex = true;
591 }
592
593 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000594 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
595 Result.IndexReg = SelectExpr(IAM.IndexReg);
596 } else {
597 Result.IndexReg = SelectExpr(IAM.IndexReg);
598 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
599 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000600
Chris Lattner98a8ba02005-01-18 01:06:26 +0000601 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
602 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
603 } else if (IAM.IndexReg.Val) {
604 Result.IndexReg = SelectExpr(IAM.IndexReg);
605 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000606
Chris Lattner98a8ba02005-01-18 01:06:26 +0000607 switch (IAM.BaseType) {
608 case X86ISelAddressMode::RegBase:
609 Result.BaseType = X86AddressMode::RegBase;
610 break;
611 case X86ISelAddressMode::FrameIndexBase:
612 Result.BaseType = X86AddressMode::FrameIndexBase;
613 Result.Base.FrameIndex = IAM.Base.FrameIndex;
614 break;
615 default:
616 assert(0 && "Unknown base type!");
617 break;
618 }
619 Result.Scale = IAM.Scale;
620 Result.Disp = IAM.Disp;
621 Result.GV = IAM.GV;
622 return Result;
623}
624
625/// SelectAddress - Pattern match the maximal addressing mode for this node and
626/// emit all of the leaf registers.
627void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
628 X86ISelAddressMode IAM;
629 MatchAddress(N, IAM);
630 AM = SelectAddrExprs(IAM);
631}
632
633/// MatchAddress - Add the specified node to the specified addressing mode,
634/// returning true if it cannot be done. This just pattern matches for the
635/// addressing mode, it does not cause any code to be emitted. For that, use
636/// SelectAddress.
637bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000638 switch (N.getOpcode()) {
639 default: break;
640 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000641 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
642 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000643 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
644 return false;
645 }
646 break;
647 case ISD::GlobalAddress:
648 if (AM.GV == 0) {
649 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
650 return false;
651 }
652 break;
653 case ISD::Constant:
654 AM.Disp += cast<ConstantSDNode>(N)->getValue();
655 return false;
656 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000657 // We might have folded the load into this shift, so don't regen the value
658 // if so.
659 if (ExprMap.count(N)) break;
660
Chris Lattner98a8ba02005-01-18 01:06:26 +0000661 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000662 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
663 unsigned Val = CN->getValue();
664 if (Val == 1 || Val == 2 || Val == 3) {
665 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000666 SDOperand ShVal = N.Val->getOperand(0);
667
668 // Okay, we know that we have a scale by now. However, if the scaled
669 // value is an add of something and a constant, we can fold the
670 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000671 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000672 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000673 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000674 ConstantSDNode *AddVal =
675 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
676 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000677 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000678 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000679 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000680 return false;
681 }
682 }
683 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000684 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000685 // We might have folded the load into this mul, so don't regen the value if
686 // so.
687 if (ExprMap.count(N)) break;
688
Chris Lattner947d5442005-01-11 19:37:02 +0000689 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000690 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
691 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000692 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
693 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
694 AM.Scale = unsigned(CN->getValue())-1;
695
696 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000697 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000698
699 // Okay, we know that we have a scale by now. However, if the scaled
700 // value is an add of something and a constant, we can fold the
701 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000702 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000703 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000704 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000705 ConstantSDNode *AddVal =
706 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
707 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000708 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000709 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000710 }
711
712 AM.IndexReg = AM.Base.Reg = Reg;
713 return false;
714 }
715 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000716
717 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000718 // We might have folded the load into this mul, so don't regen the value if
719 // so.
720 if (ExprMap.count(N)) break;
721
Chris Lattner98a8ba02005-01-18 01:06:26 +0000722 X86ISelAddressMode Backup = AM;
723 if (!MatchAddress(N.Val->getOperand(0), AM) &&
724 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000725 return false;
726 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000727 if (!MatchAddress(N.Val->getOperand(1), AM) &&
728 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000729 return false;
730 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000731 break;
732 }
733 }
734
Chris Lattnera95589b2005-01-11 04:40:19 +0000735 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000736 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000737 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000738 if (AM.IndexReg.Val == 0) {
739 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000740 AM.Scale = 1;
741 return false;
742 }
743
744 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000745 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000746 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000747
748 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000749 AM.BaseType = X86ISelAddressMode::RegBase;
750 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000751 return false;
752}
753
754/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
755/// assuming that the temporary registers are in the 8-bit register class.
756///
757/// Tmp1 = setcc1
758/// Tmp2 = setcc2
759/// DestReg = logicalop Tmp1, Tmp2
760///
761static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
762 unsigned SetCC2, unsigned LogicalOp,
763 unsigned DestReg) {
764 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
765 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
766 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
767 BuildMI(BB, SetCC1, 0, Tmp1);
768 BuildMI(BB, SetCC2, 0, Tmp2);
769 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
770}
771
772/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
773/// condition codes match the specified SetCCOpcode. Note that some conditions
774/// require multiple instructions to generate the correct value.
775static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
776 ISD::CondCode SetCCOpcode, bool isFP) {
777 unsigned Opc;
778 if (!isFP) {
779 switch (SetCCOpcode) {
780 default: assert(0 && "Illegal integer SetCC!");
781 case ISD::SETEQ: Opc = X86::SETEr; break;
782 case ISD::SETGT: Opc = X86::SETGr; break;
783 case ISD::SETGE: Opc = X86::SETGEr; break;
784 case ISD::SETLT: Opc = X86::SETLr; break;
785 case ISD::SETLE: Opc = X86::SETLEr; break;
786 case ISD::SETNE: Opc = X86::SETNEr; break;
787 case ISD::SETULT: Opc = X86::SETBr; break;
788 case ISD::SETUGT: Opc = X86::SETAr; break;
789 case ISD::SETULE: Opc = X86::SETBEr; break;
790 case ISD::SETUGE: Opc = X86::SETAEr; break;
791 }
792 } else {
793 // On a floating point condition, the flags are set as follows:
794 // ZF PF CF op
795 // 0 | 0 | 0 | X > Y
796 // 0 | 0 | 1 | X < Y
797 // 1 | 0 | 0 | X == Y
798 // 1 | 1 | 1 | unordered
799 //
800 switch (SetCCOpcode) {
801 default: assert(0 && "Invalid FP setcc!");
802 case ISD::SETUEQ:
803 case ISD::SETEQ:
804 Opc = X86::SETEr; // True if ZF = 1
805 break;
806 case ISD::SETOGT:
807 case ISD::SETGT:
808 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
809 break;
810 case ISD::SETOGE:
811 case ISD::SETGE:
812 Opc = X86::SETAEr; // True if CF = 0
813 break;
814 case ISD::SETULT:
815 case ISD::SETLT:
816 Opc = X86::SETBr; // True if CF = 1
817 break;
818 case ISD::SETULE:
819 case ISD::SETLE:
820 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
821 break;
822 case ISD::SETONE:
823 case ISD::SETNE:
824 Opc = X86::SETNEr; // True if ZF = 0
825 break;
826 case ISD::SETUO:
827 Opc = X86::SETPr; // True if PF = 1
828 break;
829 case ISD::SETO:
830 Opc = X86::SETNPr; // True if PF = 0
831 break;
832 case ISD::SETOEQ: // !PF & ZF
833 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
834 return;
835 case ISD::SETOLT: // !PF & CF
836 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
837 return;
838 case ISD::SETOLE: // !PF & (CF || ZF)
839 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
840 return;
841 case ISD::SETUGT: // PF | (!ZF & !CF)
842 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
843 return;
844 case ISD::SETUGE: // PF | !CF
845 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
846 return;
847 case ISD::SETUNE: // PF | !ZF
848 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
849 return;
850 }
851 }
852 BuildMI(BB, Opc, 0, DestReg);
853}
854
855
856/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
857/// the Dest block if the Cond condition is true. If we cannot fold this
858/// condition into the branch, return true.
859///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000860bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
861 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000862 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
863 // B) using two conditional branches instead of one condbr, two setcc's, and
864 // an or.
865 if ((Cond.getOpcode() == ISD::OR ||
866 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
867 // And and or set the flags for us, so there is no need to emit a TST of the
868 // result. It is only safe to do this if there is only a single use of the
869 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000870 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000871 SelectExpr(Cond);
872 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
873 return false;
874 }
875
876 // Codegen br not C -> JE.
877 if (Cond.getOpcode() == ISD::XOR)
878 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
879 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000880 unsigned CondR;
881 if (getRegPressure(Chain) > getRegPressure(Cond)) {
882 Select(Chain);
883 CondR = SelectExpr(Cond.Val->getOperand(0));
884 } else {
885 CondR = SelectExpr(Cond.Val->getOperand(0));
886 Select(Chain);
887 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000888 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
889 BuildMI(BB, X86::JE, 1).addMBB(Dest);
890 return false;
891 }
892
893 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
894 if (SetCC == 0)
895 return true; // Can only handle simple setcc's so far.
896
897 unsigned Opc;
898
899 // Handle integer conditions first.
900 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
901 switch (SetCC->getCondition()) {
902 default: assert(0 && "Illegal integer SetCC!");
903 case ISD::SETEQ: Opc = X86::JE; break;
904 case ISD::SETGT: Opc = X86::JG; break;
905 case ISD::SETGE: Opc = X86::JGE; break;
906 case ISD::SETLT: Opc = X86::JL; break;
907 case ISD::SETLE: Opc = X86::JLE; break;
908 case ISD::SETNE: Opc = X86::JNE; break;
909 case ISD::SETULT: Opc = X86::JB; break;
910 case ISD::SETUGT: Opc = X86::JA; break;
911 case ISD::SETULE: Opc = X86::JBE; break;
912 case ISD::SETUGE: Opc = X86::JAE; break;
913 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000914 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000915 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000916 BuildMI(BB, Opc, 1).addMBB(Dest);
917 return false;
918 }
919
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000920 unsigned Opc2 = 0; // Second branch if needed.
921
922 // On a floating point condition, the flags are set as follows:
923 // ZF PF CF op
924 // 0 | 0 | 0 | X > Y
925 // 0 | 0 | 1 | X < Y
926 // 1 | 0 | 0 | X == Y
927 // 1 | 1 | 1 | unordered
928 //
929 switch (SetCC->getCondition()) {
930 default: assert(0 && "Invalid FP setcc!");
931 case ISD::SETUEQ:
932 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
933 case ISD::SETOGT:
934 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
935 case ISD::SETOGE:
936 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
937 case ISD::SETULT:
938 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
939 case ISD::SETULE:
940 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
941 case ISD::SETONE:
942 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
943 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
944 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
945 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
946 Opc = X86::JA; // ZF = 0 & CF = 0
947 Opc2 = X86::JP; // PF = 1
948 break;
949 case ISD::SETUGE: // PF = 1 | CF = 0
950 Opc = X86::JAE; // CF = 0
951 Opc2 = X86::JP; // PF = 1
952 break;
953 case ISD::SETUNE: // PF = 1 | ZF = 0
954 Opc = X86::JNE; // ZF = 0
955 Opc2 = X86::JP; // PF = 1
956 break;
957 case ISD::SETOEQ: // PF = 0 & ZF = 1
958 //X86::JNP, X86::JE
959 //X86::AND8rr
960 return true; // FIXME: Emit more efficient code for this branch.
961 case ISD::SETOLT: // PF = 0 & CF = 1
962 //X86::JNP, X86::JB
963 //X86::AND8rr
964 return true; // FIXME: Emit more efficient code for this branch.
965 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
966 //X86::JNP, X86::JBE
967 //X86::AND8rr
968 return true; // FIXME: Emit more efficient code for this branch.
969 }
970
Chris Lattner6c07aee2005-01-11 04:06:27 +0000971 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000972 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000973 BuildMI(BB, Opc, 1).addMBB(Dest);
974 if (Opc2)
975 BuildMI(BB, Opc2, 1).addMBB(Dest);
976 return false;
977}
978
Chris Lattner24aad1b2005-01-10 22:10:13 +0000979/// EmitSelectCC - Emit code into BB that performs a select operation between
980/// the two registers RTrue and RFalse, generating a result into RDest. Return
981/// true if the fold cannot be performed.
982///
983void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
984 unsigned RTrue, unsigned RFalse, unsigned RDest) {
985 enum Condition {
986 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
987 NOT_SET
988 } CondCode = NOT_SET;
989
990 static const unsigned CMOVTAB16[] = {
991 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
992 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000993 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000994 };
995 static const unsigned CMOVTAB32[] = {
996 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
997 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000998 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000999 };
1000 static const unsigned CMOVTABFP[] = {
1001 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1002 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1003 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1004 };
1005
1006 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1007 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1008 switch (SetCC->getCondition()) {
1009 default: assert(0 && "Unknown integer comparison!");
1010 case ISD::SETEQ: CondCode = EQ; break;
1011 case ISD::SETGT: CondCode = GT; break;
1012 case ISD::SETGE: CondCode = GE; break;
1013 case ISD::SETLT: CondCode = LT; break;
1014 case ISD::SETLE: CondCode = LE; break;
1015 case ISD::SETNE: CondCode = NE; break;
1016 case ISD::SETULT: CondCode = B; break;
1017 case ISD::SETUGT: CondCode = A; break;
1018 case ISD::SETULE: CondCode = BE; break;
1019 case ISD::SETUGE: CondCode = AE; break;
1020 }
1021 } else {
1022 // On a floating point condition, the flags are set as follows:
1023 // ZF PF CF op
1024 // 0 | 0 | 0 | X > Y
1025 // 0 | 0 | 1 | X < Y
1026 // 1 | 0 | 0 | X == Y
1027 // 1 | 1 | 1 | unordered
1028 //
1029 switch (SetCC->getCondition()) {
1030 default: assert(0 && "Unknown FP comparison!");
1031 case ISD::SETUEQ:
1032 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1033 case ISD::SETOGT:
1034 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1035 case ISD::SETOGE:
1036 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1037 case ISD::SETULT:
1038 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1039 case ISD::SETULE:
1040 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1041 case ISD::SETONE:
1042 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1043 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1044 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1045 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1046 case ISD::SETUGE: // PF = 1 | CF = 0
1047 case ISD::SETUNE: // PF = 1 | ZF = 0
1048 case ISD::SETOEQ: // PF = 0 & ZF = 1
1049 case ISD::SETOLT: // PF = 0 & CF = 1
1050 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1051 // We cannot emit this comparison as a single cmov.
1052 break;
1053 }
1054 }
1055 }
1056
1057 unsigned Opc = 0;
1058 if (CondCode != NOT_SET) {
1059 switch (SVT) {
1060 default: assert(0 && "Cannot select this type!");
1061 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1062 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001063 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001064 }
1065 }
1066
1067 // Finally, if we weren't able to fold this, just emit the condition and test
1068 // it.
1069 if (CondCode == NOT_SET || Opc == 0) {
1070 // Get the condition into the zero flag.
1071 unsigned CondReg = SelectExpr(Cond);
1072 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1073
1074 switch (SVT) {
1075 default: assert(0 && "Cannot select this type!");
1076 case MVT::i16: Opc = X86::CMOVE16rr; break;
1077 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001078 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001079 }
1080 } else {
1081 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001082 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001083 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001084 }
1085 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1086}
1087
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001088void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001089 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001090 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1091 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001092 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001093 switch (RHS.getValueType()) {
1094 default: break;
1095 case MVT::i1:
1096 case MVT::i8: Opc = X86::CMP8mi; break;
1097 case MVT::i16: Opc = X86::CMP16mi; break;
1098 case MVT::i32: Opc = X86::CMP32mi; break;
1099 }
1100 if (Opc) {
1101 X86AddressMode AM;
1102 EmitFoldedLoad(LHS, AM);
1103 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1104 return;
1105 }
1106 }
1107
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001108 switch (RHS.getValueType()) {
1109 default: break;
1110 case MVT::i1:
1111 case MVT::i8: Opc = X86::CMP8ri; break;
1112 case MVT::i16: Opc = X86::CMP16ri; break;
1113 case MVT::i32: Opc = X86::CMP32ri; break;
1114 }
1115 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001116 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001117 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1118 return;
1119 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001120 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1121 if (CN->isExactlyValue(+0.0) ||
1122 CN->isExactlyValue(-0.0)) {
1123 unsigned Reg = SelectExpr(LHS);
1124 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1125 BuildMI(BB, X86::FNSTSW8r, 0);
1126 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001127 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001128 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001129 }
1130
Chris Lattneref6806c2005-01-12 02:02:48 +00001131 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001132 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001133 switch (RHS.getValueType()) {
1134 default: break;
1135 case MVT::i1:
1136 case MVT::i8: Opc = X86::CMP8mr; break;
1137 case MVT::i16: Opc = X86::CMP16mr; break;
1138 case MVT::i32: Opc = X86::CMP32mr; break;
1139 }
1140 if (Opc) {
1141 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001142 EmitFoldedLoad(LHS, AM);
1143 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001144 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1145 return;
1146 }
1147 }
1148
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001149 switch (LHS.getValueType()) {
1150 default: assert(0 && "Cannot compare this value!");
1151 case MVT::i1:
1152 case MVT::i8: Opc = X86::CMP8rr; break;
1153 case MVT::i16: Opc = X86::CMP16rr; break;
1154 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001155 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001156 }
Chris Lattner11333092005-01-11 03:11:44 +00001157 unsigned Tmp1, Tmp2;
1158 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1159 Tmp1 = SelectExpr(LHS);
1160 Tmp2 = SelectExpr(RHS);
1161 } else {
1162 Tmp2 = SelectExpr(RHS);
1163 Tmp1 = SelectExpr(LHS);
1164 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001165 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1166}
1167
Chris Lattnera5ade062005-01-11 21:19:59 +00001168/// isFoldableLoad - Return true if this is a load instruction that can safely
1169/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001170bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1171 if (Op.getOpcode() == ISD::LOAD) {
1172 // FIXME: currently can't fold constant pool indexes.
1173 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1174 return false;
1175 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1176 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1177 // FIXME: currently can't fold constant pool indexes.
1178 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1179 return false;
1180 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001181 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001182 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001183
1184 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001185 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1186 if (ExprMap.count(Op.getValue(1))) return false;
1187 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001188 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001189
Chris Lattner4ff348b2005-01-17 06:26:58 +00001190 // If there is not just one use of its value, we cannot fold.
1191 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1192
1193 // Finally, we cannot fold the load into the operation if this would induce a
1194 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1195 // operand of the operation we are folding the load into) can possible use the
1196 // chain node defined by the load.
1197 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1198 std::set<SDNode*> Visited;
1199 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1200 return false;
1201 }
1202 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001203}
1204
Chris Lattner4ff348b2005-01-17 06:26:58 +00001205
Chris Lattnera5ade062005-01-11 21:19:59 +00001206/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1207/// and compute the address being loaded into AM.
1208void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1209 SDOperand Chain = Op.getOperand(0);
1210 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001211
Chris Lattnera5ade062005-01-11 21:19:59 +00001212 if (getRegPressure(Chain) > getRegPressure(Address)) {
1213 Select(Chain);
1214 SelectAddress(Address, AM);
1215 } else {
1216 SelectAddress(Address, AM);
1217 Select(Chain);
1218 }
1219
1220 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001221 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1222 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001223 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001224 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001225}
1226
Chris Lattner30ea1e92005-01-19 07:37:26 +00001227// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1228// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1229// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1230// return true.
1231bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001232 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1233 // good!
1234 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1235 std::swap(Op1, Op2); // Op1 is the SHL now.
1236 } else {
1237 return false; // No match
1238 }
1239
1240 SDOperand ShlVal = Op1.getOperand(0);
1241 SDOperand ShlAmt = Op1.getOperand(1);
1242 SDOperand ShrVal = Op2.getOperand(0);
1243 SDOperand ShrAmt = Op2.getOperand(1);
1244
Chris Lattner30ea1e92005-01-19 07:37:26 +00001245 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1246
Chris Lattner85716372005-01-19 06:18:43 +00001247 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1248 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1249 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001250 if (SubCST->getValue() == RegSize) {
1251 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001252 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001253 if (ShrVal == ShlVal) {
1254 unsigned Reg, ShAmt;
1255 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1256 Reg = SelectExpr(ShrVal);
1257 ShAmt = SelectExpr(ShrAmt);
1258 } else {
1259 ShAmt = SelectExpr(ShrAmt);
1260 Reg = SelectExpr(ShrVal);
1261 }
1262 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1263 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1264 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1265 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1266 return true;
1267 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001268 unsigned AReg, BReg;
1269 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001270 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001271 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001272 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001273 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001274 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001275 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001276 unsigned ShAmt = SelectExpr(ShrAmt);
1277 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1278 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1279 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001280 return true;
1281 }
1282 }
1283
Chris Lattner4053b1e2005-01-19 08:07:05 +00001284 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1285 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1286 if (SubCST->getValue() == RegSize) {
1287 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1288 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1289 if (ShrVal == ShlVal) {
1290 unsigned Reg, ShAmt;
1291 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1292 Reg = SelectExpr(ShrVal);
1293 ShAmt = SelectExpr(ShlAmt);
1294 } else {
1295 ShAmt = SelectExpr(ShlAmt);
1296 Reg = SelectExpr(ShrVal);
1297 }
1298 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1299 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1300 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1301 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1302 return true;
1303 } else if (RegSize != 8) {
1304 unsigned AReg, BReg;
1305 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001306 AReg = SelectExpr(ShlVal);
1307 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001308 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001309 BReg = SelectExpr(ShrVal);
1310 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001311 }
1312 unsigned ShAmt = SelectExpr(ShlAmt);
1313 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1314 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1315 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1316 return true;
1317 }
1318 }
Chris Lattner85716372005-01-19 06:18:43 +00001319
Chris Lattner4053b1e2005-01-19 08:07:05 +00001320 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1321 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1322 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1323 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1324 // (A >> 5) | (A << 27) --> ROR A, 5
1325 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1326 if (ShrVal == ShlVal) {
1327 unsigned Reg = SelectExpr(ShrVal);
1328 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1329 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1330 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1331 return true;
1332 } else if (RegSize != 8) {
1333 unsigned AReg, BReg;
1334 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001335 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001336 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001337 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001338 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001339 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001340 }
1341 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1342 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1343 .addImm(ShrCst->getValue());
1344 return true;
1345 }
1346 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001347
Chris Lattner85716372005-01-19 06:18:43 +00001348 return false;
1349}
1350
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001351unsigned ISel::SelectExpr(SDOperand N) {
1352 unsigned Result;
1353 unsigned Tmp1, Tmp2, Tmp3;
1354 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001355 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001356 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001357
Chris Lattner7f2afac2005-01-14 22:37:41 +00001358 if (Node->getOpcode() == ISD::CopyFromReg) {
1359 // FIXME: Handle copy from physregs!
1360
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001361 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001362 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001363 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001364
Chris Lattnera5ade062005-01-11 21:19:59 +00001365 unsigned &Reg = ExprMap[N];
1366 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001367
Chris Lattnerb38a7492005-04-02 04:01:14 +00001368 switch (N.getOpcode()) {
1369 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001370 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001371 MakeReg(N.getValueType()) : 1;
1372 break;
1373 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001374 // If this is a call instruction, make sure to prepare ALL of the result
1375 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001376 if (Node->getNumValues() == 1)
1377 Reg = Result = 1; // Void call, just a chain.
1378 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001379 Result = MakeReg(Node->getValueType(0));
1380 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001381 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001382 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001383 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001384 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001385 break;
1386 case ISD::ADD_PARTS:
1387 case ISD::SUB_PARTS:
1388 case ISD::SHL_PARTS:
1389 case ISD::SRL_PARTS:
1390 case ISD::SRA_PARTS:
1391 Result = MakeReg(Node->getValueType(0));
1392 ExprMap[N.getValue(0)] = Result;
1393 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1394 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1395 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001396 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001397
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001398 switch (N.getOpcode()) {
1399 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001400 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001401 assert(0 && "Node not handled!\n");
1402 case ISD::FrameIndex:
1403 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1404 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1405 return Result;
1406 case ISD::ConstantPool:
1407 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1408 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1409 return Result;
1410 case ISD::ConstantFP:
1411 ContainsFPCode = true;
1412 Tmp1 = Result; // Intermediate Register
1413 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1414 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1415 Tmp1 = MakeReg(MVT::f64);
1416
1417 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1418 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1419 BuildMI(BB, X86::FLD0, 0, Tmp1);
1420 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1421 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1422 BuildMI(BB, X86::FLD1, 0, Tmp1);
1423 else
1424 assert(0 && "Unexpected constant!");
1425 if (Tmp1 != Result)
1426 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1427 return Result;
1428 case ISD::Constant:
1429 switch (N.getValueType()) {
1430 default: assert(0 && "Cannot use constants of this type!");
1431 case MVT::i1:
1432 case MVT::i8: Opc = X86::MOV8ri; break;
1433 case MVT::i16: Opc = X86::MOV16ri; break;
1434 case MVT::i32: Opc = X86::MOV32ri; break;
1435 }
1436 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1437 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001438 case ISD::UNDEF:
1439 if (Node->getValueType(0) == MVT::f64) {
1440 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1441 BuildMI(BB, X86::FLD0, 0, Result);
1442 } else {
1443 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1444 }
1445 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001446 case ISD::GlobalAddress: {
1447 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1448 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1449 return Result;
1450 }
1451 case ISD::ExternalSymbol: {
1452 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1453 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1454 return Result;
1455 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001456 case ISD::ZERO_EXTEND: {
1457 int DestIs16 = N.getValueType() == MVT::i16;
1458 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001459
1460 // FIXME: This hack is here for zero extension casts from bool to i8. This
1461 // would not be needed if bools were promoted by Legalize.
1462 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001463 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001464 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1465 return Result;
1466 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001467
Chris Lattner4ff348b2005-01-17 06:26:58 +00001468 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001469 static const unsigned Opc[3] = {
1470 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1471 };
1472
1473 X86AddressMode AM;
1474 EmitFoldedLoad(N.getOperand(0), AM);
1475 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001476
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001477 return Result;
1478 }
1479
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001480 static const unsigned Opc[3] = {
1481 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1482 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001483 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001484 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1485 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001486 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001487 case ISD::SIGN_EXTEND: {
1488 int DestIs16 = N.getValueType() == MVT::i16;
1489 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1490
Chris Lattner590d8002005-01-09 18:52:44 +00001491 // FIXME: Legalize should promote bools to i8!
1492 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1493 "Sign extend from bool not implemented!");
1494
Chris Lattner4ff348b2005-01-17 06:26:58 +00001495 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001496 static const unsigned Opc[3] = {
1497 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1498 };
1499
1500 X86AddressMode AM;
1501 EmitFoldedLoad(N.getOperand(0), AM);
1502 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1503 return Result;
1504 }
1505
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001506 static const unsigned Opc[3] = {
1507 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1508 };
1509 Tmp1 = SelectExpr(N.getOperand(0));
1510 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1511 return Result;
1512 }
1513 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001514 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001515 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001516 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001517 switch (N.getValueType()) {
1518 default: assert(0 && "Unknown truncate!");
1519 case MVT::i1:
1520 case MVT::i8: Opc = X86::MOV8rm; break;
1521 case MVT::i16: Opc = X86::MOV16rm; break;
1522 }
1523 X86AddressMode AM;
1524 EmitFoldedLoad(N.getOperand(0), AM);
1525 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1526 return Result;
1527 }
1528
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001529 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1530 // a move out of AX or AL.
1531 switch (N.getOperand(0).getValueType()) {
1532 default: assert(0 && "Unknown truncate!");
1533 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1534 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1535 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1536 }
1537 Tmp1 = SelectExpr(N.getOperand(0));
1538 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1539
1540 switch (N.getValueType()) {
1541 default: assert(0 && "Unknown truncate!");
1542 case MVT::i1:
1543 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1544 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1545 }
1546 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1547 return Result;
1548
Chris Lattner590d8002005-01-09 18:52:44 +00001549 case ISD::SINT_TO_FP:
1550 case ISD::UINT_TO_FP: {
1551 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001552 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001553
1554 // Promote the integer to a type supported by FLD. We do this because there
1555 // are no unsigned FLD instructions, so we must promote an unsigned value to
1556 // a larger signed value, then use FLD on the larger value.
1557 //
1558 MVT::ValueType PromoteType = MVT::Other;
1559 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1560 unsigned PromoteOpcode = 0;
1561 unsigned RealDestReg = Result;
1562 switch (SrcTy) {
1563 case MVT::i1:
1564 case MVT::i8:
1565 // We don't have the facilities for directly loading byte sized data from
1566 // memory (even signed). Promote it to 16 bits.
1567 PromoteType = MVT::i16;
1568 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1569 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1570 break;
1571 case MVT::i16:
1572 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1573 PromoteType = MVT::i32;
1574 PromoteOpcode = X86::MOVZX32rr16;
1575 }
1576 break;
1577 default:
1578 // Don't fild into the real destination.
1579 if (Node->getOpcode() == ISD::UINT_TO_FP)
1580 Result = MakeReg(Node->getValueType(0));
1581 break;
1582 }
1583
1584 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001585
Chris Lattner590d8002005-01-09 18:52:44 +00001586 if (PromoteType != MVT::Other) {
1587 Tmp2 = MakeReg(PromoteType);
1588 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1589 SrcTy = PromoteType;
1590 Tmp1 = Tmp2;
1591 }
1592
1593 // Spill the integer to memory and reload it from there.
1594 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1595 MachineFunction *F = BB->getParent();
1596 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1597
1598 switch (SrcTy) {
1599 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001600 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001601 // FIXME: this won't work for cast [u]long to FP
1602 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1603 FrameIdx).addReg(Tmp1);
1604 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1605 FrameIdx, 4).addReg(Tmp1+1);
1606 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1607 break;
1608 case MVT::i32:
1609 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1610 FrameIdx).addReg(Tmp1);
1611 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1612 break;
1613 case MVT::i16:
1614 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1615 FrameIdx).addReg(Tmp1);
1616 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1617 break;
1618 default: break; // No promotion required.
1619 }
1620
Chris Lattner085c9952005-01-12 04:00:00 +00001621 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001622 // If this is a cast from uint -> double, we need to be careful when if
1623 // the "sign" bit is set. If so, we don't want to make a negative number,
1624 // we want to make a positive number. Emit code to add an offset if the
1625 // sign bit is set.
1626
1627 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1628 unsigned IsNeg = MakeReg(MVT::i32);
1629 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1630
1631 // Create a CP value that has the offset in one word and 0 in the other.
1632 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1633 0x4f80000000000000ULL);
1634 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1635 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1636 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1637
1638 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1639 // We need special handling for unsigned 64-bit integer sources. If the
1640 // input number has the "sign bit" set, then we loaded it incorrectly as a
1641 // negative 64-bit number. In this case, add an offset value.
1642
1643 // Emit a test instruction to see if the dynamic input value was signed.
1644 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1645
1646 // If the sign bit is set, get a pointer to an offset, otherwise get a
1647 // pointer to a zero.
1648 MachineConstantPool *CP = F->getConstantPool();
1649 unsigned Zero = MakeReg(MVT::i32);
1650 Constant *Null = Constant::getNullValue(Type::UIntTy);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001651 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
Chris Lattner590d8002005-01-09 18:52:44 +00001652 CP->getConstantPoolIndex(Null));
1653 unsigned Offset = MakeReg(MVT::i32);
1654 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001655
Chris Lattner590d8002005-01-09 18:52:44 +00001656 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1657 CP->getConstantPoolIndex(OffsetCst));
1658 unsigned Addr = MakeReg(MVT::i32);
1659 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1660
1661 // Load the constant for an add. FIXME: this could make an 'fadd' that
1662 // reads directly from memory, but we don't support these yet.
1663 unsigned ConstReg = MakeReg(MVT::f64);
1664 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1665
1666 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1667 }
1668 return RealDestReg;
1669 }
1670 case ISD::FP_TO_SINT:
1671 case ISD::FP_TO_UINT: {
1672 // FIXME: Most of this grunt work should be done by legalize!
1673 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1674
1675 // Change the floating point control register to use "round towards zero"
1676 // mode when truncating to an integer value.
1677 //
1678 MachineFunction *F = BB->getParent();
1679 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1680 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1681
1682 // Load the old value of the high byte of the control word...
1683 unsigned HighPartOfCW = MakeReg(MVT::i8);
1684 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1685 CWFrameIdx, 1);
1686
1687 // Set the high part to be round to zero...
1688 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1689 CWFrameIdx, 1).addImm(12);
1690
1691 // Reload the modified control word now...
1692 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001693
Chris Lattner590d8002005-01-09 18:52:44 +00001694 // Restore the memory image of control word to original value
1695 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1696 CWFrameIdx, 1).addReg(HighPartOfCW);
1697
1698 // We don't have the facilities for directly storing byte sized data to
1699 // memory. Promote it to 16 bits. We also must promote unsigned values to
1700 // larger classes because we only have signed FP stores.
1701 MVT::ValueType StoreClass = Node->getValueType(0);
1702 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1703 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00001704 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001705 case MVT::i8: StoreClass = MVT::i16; break;
1706 case MVT::i16: StoreClass = MVT::i32; break;
1707 case MVT::i32: StoreClass = MVT::i64; break;
1708 // The following treatment of cLong may not be perfectly right,
1709 // but it survives chains of casts of the form
1710 // double->ulong->double.
1711 case MVT::i64: StoreClass = MVT::i64; break;
1712 default: assert(0 && "Unknown store class!");
1713 }
1714
1715 // Spill the integer to memory and reload it from there.
1716 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1717 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1718
1719 switch (StoreClass) {
1720 default: assert(0 && "Unknown store class!");
1721 case MVT::i16:
1722 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1723 break;
1724 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001725 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001726 break;
1727 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001728 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001729 break;
1730 }
1731
1732 switch (Node->getValueType(0)) {
1733 default:
1734 assert(0 && "Unknown integer type!");
1735 case MVT::i64:
1736 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001737 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001738 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1739 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1740 case MVT::i32:
1741 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1742 break;
1743 case MVT::i16:
1744 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1745 break;
1746 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00001747 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001748 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1749 break;
1750 }
1751
1752 // Reload the original control word now.
1753 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1754 return Result;
1755 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001756 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001757 Op0 = N.getOperand(0);
1758 Op1 = N.getOperand(1);
1759
Chris Lattner44129b52005-01-25 20:03:11 +00001760 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001761 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001762 goto FoldAdd;
1763 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001764
Chris Lattner44129b52005-01-25 20:03:11 +00001765 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001766 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001767 switch (N.getValueType()) {
1768 default: assert(0 && "Cannot add this type!");
1769 case MVT::i1:
1770 case MVT::i8: Opc = X86::ADD8rm; break;
1771 case MVT::i16: Opc = X86::ADD16rm; break;
1772 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001773 case MVT::f64:
1774 // For F64, handle promoted load operations (from F32) as well!
1775 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1776 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001777 }
1778 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001779 EmitFoldedLoad(Op1, AM);
1780 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001781 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1782 return Result;
1783 }
1784
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001785 // See if we can codegen this as an LEA to fold operations together.
1786 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001787 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001788 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001789 MatchAddress(N, AM);
1790 ExprMap[N] = Result;
1791
1792 // If this is not just an add, emit the LEA. For a simple add (like
1793 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1794 // leave this as LEA, then peephole it to 'ADD' after two address elim
1795 // happens.
1796 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1797 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1798 X86AddressMode XAM = SelectAddrExprs(AM);
1799 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1800 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001801 }
1802 }
Chris Lattner11333092005-01-11 03:11:44 +00001803
Chris Lattnera5ade062005-01-11 21:19:59 +00001804 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001805 Opc = 0;
1806 if (CN->getValue() == 1) { // add X, 1 -> inc X
1807 switch (N.getValueType()) {
1808 default: assert(0 && "Cannot integer add this type!");
1809 case MVT::i8: Opc = X86::INC8r; break;
1810 case MVT::i16: Opc = X86::INC16r; break;
1811 case MVT::i32: Opc = X86::INC32r; break;
1812 }
1813 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1814 switch (N.getValueType()) {
1815 default: assert(0 && "Cannot integer add this type!");
1816 case MVT::i8: Opc = X86::DEC8r; break;
1817 case MVT::i16: Opc = X86::DEC16r; break;
1818 case MVT::i32: Opc = X86::DEC32r; break;
1819 }
1820 }
1821
1822 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001823 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001824 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1825 return Result;
1826 }
1827
1828 switch (N.getValueType()) {
1829 default: assert(0 && "Cannot add this type!");
1830 case MVT::i8: Opc = X86::ADD8ri; break;
1831 case MVT::i16: Opc = X86::ADD16ri; break;
1832 case MVT::i32: Opc = X86::ADD32ri; break;
1833 }
1834 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001835 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001836 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1837 return Result;
1838 }
1839 }
1840
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001841 switch (N.getValueType()) {
1842 default: assert(0 && "Cannot add this type!");
1843 case MVT::i8: Opc = X86::ADD8rr; break;
1844 case MVT::i16: Opc = X86::ADD16rr; break;
1845 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001846 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001847 }
Chris Lattner11333092005-01-11 03:11:44 +00001848
Chris Lattnera5ade062005-01-11 21:19:59 +00001849 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1850 Tmp1 = SelectExpr(Op0);
1851 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001852 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001853 Tmp2 = SelectExpr(Op1);
1854 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001855 }
1856
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1858 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001859
1860 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001861 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001862 case ISD::FSIN:
1863 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001864 case ISD::FSQRT:
1865 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001866 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001867 switch (N.getOpcode()) {
1868 default: assert(0 && "Unreachable!");
1869 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1870 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1871 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001872 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1873 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001874 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001875 return Result;
1876
Chris Lattner8db0af12005-04-06 04:21:07 +00001877 case ISD::MULHU:
1878 switch (N.getValueType()) {
1879 default: assert(0 && "Unsupported VT!");
1880 case MVT::i8: Tmp2 = X86::MUL8r; break;
1881 case MVT::i16: Tmp2 = X86::MUL16r; break;
1882 case MVT::i32: Tmp2 = X86::MUL32r; break;
1883 }
1884 // FALL THROUGH
1885 case ISD::MULHS: {
1886 unsigned MovOpc, LowReg, HiReg;
1887 switch (N.getValueType()) {
1888 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001889 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001890 MovOpc = X86::MOV8rr;
1891 LowReg = X86::AL;
1892 HiReg = X86::AH;
1893 Opc = X86::IMUL8r;
1894 break;
1895 case MVT::i16:
1896 MovOpc = X86::MOV16rr;
1897 LowReg = X86::AX;
1898 HiReg = X86::DX;
1899 Opc = X86::IMUL16r;
1900 break;
1901 case MVT::i32:
1902 MovOpc = X86::MOV32rr;
1903 LowReg = X86::EAX;
1904 HiReg = X86::EDX;
1905 Opc = X86::IMUL32r;
1906 break;
1907 }
1908 if (Node->getOpcode() != ISD::MULHS)
1909 Opc = Tmp2; // Get the MULHU opcode.
1910
1911 Op0 = Node->getOperand(0);
1912 Op1 = Node->getOperand(1);
1913 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1914 Tmp1 = SelectExpr(Op0);
1915 Tmp2 = SelectExpr(Op1);
1916 } else {
1917 Tmp2 = SelectExpr(Op1);
1918 Tmp1 = SelectExpr(Op0);
1919 }
1920
1921 // FIXME: Implement folding of loads into the memory operands here!
1922 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1923 BuildMI(BB, Opc, 1).addReg(Tmp2);
1924 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1925 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001926 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001927
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001928 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001929 case ISD::MUL:
1930 case ISD::AND:
1931 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001932 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001933 static const unsigned SUBTab[] = {
1934 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1935 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1936 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1937 };
1938 static const unsigned MULTab[] = {
1939 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1940 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1941 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1942 };
1943 static const unsigned ANDTab[] = {
1944 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1945 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001946 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001947 };
1948 static const unsigned ORTab[] = {
1949 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1950 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1951 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1952 };
1953 static const unsigned XORTab[] = {
1954 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1955 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1956 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1957 };
1958
1959 Op0 = Node->getOperand(0);
1960 Op1 = Node->getOperand(1);
1961
Chris Lattner30ea1e92005-01-19 07:37:26 +00001962 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1963 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001964 return Result;
1965
1966 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001967 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1968 if (CN->isNullValue()) { // 0 - N -> neg N
1969 switch (N.getValueType()) {
1970 default: assert(0 && "Cannot sub this type!");
1971 case MVT::i1:
1972 case MVT::i8: Opc = X86::NEG8r; break;
1973 case MVT::i16: Opc = X86::NEG16r; break;
1974 case MVT::i32: Opc = X86::NEG32r; break;
1975 }
1976 Tmp1 = SelectExpr(N.getOperand(1));
1977 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1978 return Result;
1979 }
1980
Chris Lattnera5ade062005-01-11 21:19:59 +00001981 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1982 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001983 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001984 switch (N.getValueType()) {
1985 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001986 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001987 case MVT::i8: Opc = X86::NOT8r; break;
1988 case MVT::i16: Opc = X86::NOT16r; break;
1989 case MVT::i32: Opc = X86::NOT32r; break;
1990 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001991 if (Opc) {
1992 Tmp1 = SelectExpr(Op0);
1993 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1994 return Result;
1995 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001996 }
1997
Chris Lattner2a4e5082005-01-17 06:48:02 +00001998 // Fold common multiplies into LEA instructions.
1999 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2000 switch ((int)CN->getValue()) {
2001 default: break;
2002 case 3:
2003 case 5:
2004 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002005 // Remove N from exprmap so SelectAddress doesn't get confused.
2006 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002007 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002008 SelectAddress(N, AM);
2009 // Restore it to the map.
2010 ExprMap[N] = Result;
2011 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2012 return Result;
2013 }
2014 }
2015
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002017 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002018 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002019 case MVT::i8: Opc = 0; break;
2020 case MVT::i16: Opc = 1; break;
2021 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002022 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002023 switch (Node->getOpcode()) {
2024 default: assert(0 && "Unreachable!");
2025 case ISD::SUB: Opc = SUBTab[Opc]; break;
2026 case ISD::MUL: Opc = MULTab[Opc]; break;
2027 case ISD::AND: Opc = ANDTab[Opc]; break;
2028 case ISD::OR: Opc = ORTab[Opc]; break;
2029 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002030 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002031 if (Opc) { // Can't fold MUL:i8 R, imm
2032 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002033 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2034 return Result;
2035 }
2036 }
Chris Lattner11333092005-01-11 03:11:44 +00002037
Chris Lattner44129b52005-01-25 20:03:11 +00002038 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002039 if (Node->getOpcode() != ISD::SUB) {
2040 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002041 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002042 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002043 // For FP, emit 'reverse' subract, with a memory operand.
2044 if (N.getValueType() == MVT::f64) {
2045 if (Op0.getOpcode() == ISD::EXTLOAD)
2046 Opc = X86::FSUBR32m;
2047 else
2048 Opc = X86::FSUBR64m;
2049
Chris Lattnera5ade062005-01-11 21:19:59 +00002050 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002051 EmitFoldedLoad(Op0, AM);
2052 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002053 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2054 return Result;
2055 }
2056 }
2057
Chris Lattner44129b52005-01-25 20:03:11 +00002058 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002059 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002060 switch (N.getValueType()) {
2061 default: assert(0 && "Cannot operate on this type!");
2062 case MVT::i1:
2063 case MVT::i8: Opc = 5; break;
2064 case MVT::i16: Opc = 6; break;
2065 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002066 // For F64, handle promoted load operations (from F32) as well!
2067 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002068 }
2069 switch (Node->getOpcode()) {
2070 default: assert(0 && "Unreachable!");
2071 case ISD::SUB: Opc = SUBTab[Opc]; break;
2072 case ISD::MUL: Opc = MULTab[Opc]; break;
2073 case ISD::AND: Opc = ANDTab[Opc]; break;
2074 case ISD::OR: Opc = ORTab[Opc]; break;
2075 case ISD::XOR: Opc = XORTab[Opc]; break;
2076 }
2077
2078 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002079 EmitFoldedLoad(Op1, AM);
2080 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002081 if (Opc) {
2082 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2083 } else {
2084 assert(Node->getOpcode() == ISD::MUL &&
2085 N.getValueType() == MVT::i8 && "Unexpected situation!");
2086 // Must use the MUL instruction, which forces use of AL.
2087 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2088 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2089 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2090 }
2091 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002092 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002093
2094 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2095 Tmp1 = SelectExpr(Op0);
2096 Tmp2 = SelectExpr(Op1);
2097 } else {
2098 Tmp2 = SelectExpr(Op1);
2099 Tmp1 = SelectExpr(Op0);
2100 }
2101
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002102 switch (N.getValueType()) {
2103 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002104 case MVT::i1:
2105 case MVT::i8: Opc = 10; break;
2106 case MVT::i16: Opc = 11; break;
2107 case MVT::i32: Opc = 12; break;
2108 case MVT::f32: Opc = 13; break;
2109 case MVT::f64: Opc = 14; break;
2110 }
2111 switch (Node->getOpcode()) {
2112 default: assert(0 && "Unreachable!");
2113 case ISD::SUB: Opc = SUBTab[Opc]; break;
2114 case ISD::MUL: Opc = MULTab[Opc]; break;
2115 case ISD::AND: Opc = ANDTab[Opc]; break;
2116 case ISD::OR: Opc = ORTab[Opc]; break;
2117 case ISD::XOR: Opc = XORTab[Opc]; break;
2118 }
2119 if (Opc) {
2120 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2121 } else {
2122 assert(Node->getOpcode() == ISD::MUL &&
2123 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002124 // Must use the MUL instruction, which forces use of AL.
2125 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2126 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2127 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002128 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002129 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002130 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002131 case ISD::ADD_PARTS:
2132 case ISD::SUB_PARTS: {
2133 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2134 "Not an i64 add/sub!");
2135 // Emit all of the operands.
2136 std::vector<unsigned> InVals;
2137 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2138 InVals.push_back(SelectExpr(N.getOperand(i)));
2139 if (N.getOpcode() == ISD::ADD_PARTS) {
2140 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2141 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2142 } else {
2143 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2144 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2145 }
2146 return Result+N.ResNo;
2147 }
2148
Chris Lattnerb38a7492005-04-02 04:01:14 +00002149 case ISD::SHL_PARTS:
2150 case ISD::SRA_PARTS:
2151 case ISD::SRL_PARTS: {
2152 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2153 "Not an i64 shift!");
2154 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2155 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2156 unsigned TmpReg = MakeReg(MVT::i32);
2157 if (N.getOpcode() == ISD::SRA_PARTS) {
2158 // If this is a SHR of a Long, then we need to do funny sign extension
2159 // stuff. TmpReg gets the value to use as the high-part if we are
2160 // shifting more than 32 bits.
2161 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2162 } else {
2163 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2164 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2165 }
2166
2167 // Initialize CL with the shift amount.
2168 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2169 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2170
2171 unsigned TmpReg2 = MakeReg(MVT::i32);
2172 unsigned TmpReg3 = MakeReg(MVT::i32);
2173 if (N.getOpcode() == ISD::SHL_PARTS) {
2174 // TmpReg2 = shld inHi, inLo
2175 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2176 .addReg(ShiftOpLo);
2177 // TmpReg3 = shl inLo, CL
2178 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002179
Chris Lattnerb38a7492005-04-02 04:01:14 +00002180 // Set the flags to indicate whether the shift was by more than 32 bits.
2181 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002182
Chris Lattnerb38a7492005-04-02 04:01:14 +00002183 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002184 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002185 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2186 // DestLo = (>32) ? TmpReg : TmpReg3;
2187 BuildMI(BB, X86::CMOVNE32rr, 2,
2188 Result).addReg(TmpReg3).addReg(TmpReg);
2189 } else {
2190 // TmpReg2 = shrd inLo, inHi
2191 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2192 .addReg(ShiftOpHi);
2193 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002194 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002195 : X86::SHR32rCL, 1, TmpReg3)
2196 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002197
Chris Lattnerb38a7492005-04-02 04:01:14 +00002198 // Set the flags to indicate whether the shift was by more than 32 bits.
2199 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002200
Chris Lattnerb38a7492005-04-02 04:01:14 +00002201 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002202 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002203 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002204
Chris Lattnerb38a7492005-04-02 04:01:14 +00002205 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002206 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002207 Result+1).addReg(TmpReg3).addReg(TmpReg);
2208 }
2209 return Result+N.ResNo;
2210 }
2211
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002212 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002213 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2214 Tmp2 = SelectExpr(N.getOperand(1));
2215 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002216 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002217 Tmp3 = SelectExpr(N.getOperand(2));
2218 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002219 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002220 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2221 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002222
2223 case ISD::SDIV:
2224 case ISD::UDIV:
2225 case ISD::SREM:
2226 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002227 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2228 "We don't support this operator!");
2229
Chris Lattner5bf26862005-04-13 03:29:53 +00002230 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002231 // We can fold loads into FpDIVs, but not really into any others.
2232 if (N.getValueType() == MVT::f64) {
2233 // Check for reversed and unreversed DIV.
2234 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2235 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2236 Opc = X86::FDIVR32m;
2237 else
2238 Opc = X86::FDIVR64m;
2239 X86AddressMode AM;
2240 EmitFoldedLoad(N.getOperand(0), AM);
2241 Tmp1 = SelectExpr(N.getOperand(1));
2242 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2243 return Result;
2244 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2245 N.getOperand(1).getOpcode() == ISD::LOAD) {
2246 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2247 Opc = X86::FDIV32m;
2248 else
2249 Opc = X86::FDIV64m;
2250 X86AddressMode AM;
2251 EmitFoldedLoad(N.getOperand(1), AM);
2252 Tmp1 = SelectExpr(N.getOperand(0));
2253 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2254 return Result;
2255 }
2256 }
2257
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002258 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2259 // FIXME: These special cases should be handled by the lowering impl!
2260 unsigned RHS = CN->getValue();
2261 bool isNeg = false;
2262 if ((int)RHS < 0) {
2263 isNeg = true;
2264 RHS = -RHS;
2265 }
2266 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2267 unsigned Log = log2(RHS);
2268 unsigned TmpReg = MakeReg(N.getValueType());
2269 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2270 switch (N.getValueType()) {
2271 default: assert("Unknown type to signed divide!");
2272 case MVT::i8:
2273 SAROpc = X86::SAR8ri;
2274 SHROpc = X86::SHR8ri;
2275 ADDOpc = X86::ADD8rr;
2276 NEGOpc = X86::NEG8r;
2277 break;
2278 case MVT::i16:
2279 SAROpc = X86::SAR16ri;
2280 SHROpc = X86::SHR16ri;
2281 ADDOpc = X86::ADD16rr;
2282 NEGOpc = X86::NEG16r;
2283 break;
2284 case MVT::i32:
2285 SAROpc = X86::SAR32ri;
2286 SHROpc = X86::SHR32ri;
2287 ADDOpc = X86::ADD32rr;
2288 NEGOpc = X86::NEG32r;
2289 break;
2290 }
Chris Lattner11333092005-01-11 03:11:44 +00002291 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2293 unsigned TmpReg2 = MakeReg(N.getValueType());
2294 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2295 unsigned TmpReg3 = MakeReg(N.getValueType());
2296 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002297
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002298 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2299 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2300 if (isNeg)
2301 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2302 return Result;
2303 }
2304 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002305 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002306
Chris Lattner11333092005-01-11 03:11:44 +00002307 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2308 Tmp1 = SelectExpr(N.getOperand(0));
2309 Tmp2 = SelectExpr(N.getOperand(1));
2310 } else {
2311 Tmp2 = SelectExpr(N.getOperand(1));
2312 Tmp1 = SelectExpr(N.getOperand(0));
2313 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002314
2315 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2316 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2317 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2318 switch (N.getValueType()) {
2319 default: assert(0 && "Cannot sdiv this type!");
2320 case MVT::i8:
2321 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2322 LoReg = X86::AL;
2323 HiReg = X86::AH;
2324 MovOpcode = X86::MOV8rr;
2325 ClrOpcode = X86::MOV8ri;
2326 SExtOpcode = X86::CBW;
2327 break;
2328 case MVT::i16:
2329 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2330 LoReg = X86::AX;
2331 HiReg = X86::DX;
2332 MovOpcode = X86::MOV16rr;
2333 ClrOpcode = X86::MOV16ri;
2334 SExtOpcode = X86::CWD;
2335 break;
2336 case MVT::i32:
2337 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002338 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002339 HiReg = X86::EDX;
2340 MovOpcode = X86::MOV32rr;
2341 ClrOpcode = X86::MOV32ri;
2342 SExtOpcode = X86::CDQ;
2343 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002344 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002345 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002346 return Result;
2347 }
2348
2349 // Set up the low part.
2350 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2351
2352 if (isSigned) {
2353 // Sign extend the low part into the high part.
2354 BuildMI(BB, SExtOpcode, 0);
2355 } else {
2356 // Zero out the high part, effectively zero extending the input.
2357 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2358 }
2359
2360 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002361 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002362
2363 // Get the result of the divide or rem.
2364 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2365 return Result;
2366 }
2367
2368 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002369 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002370 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2371 switch (N.getValueType()) {
2372 default: assert(0 && "Cannot shift this type!");
2373 case MVT::i8: Opc = X86::ADD8rr; break;
2374 case MVT::i16: Opc = X86::ADD16rr; break;
2375 case MVT::i32: Opc = X86::ADD32rr; break;
2376 }
2377 Tmp1 = SelectExpr(N.getOperand(0));
2378 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2379 return Result;
2380 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002381
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002382 switch (N.getValueType()) {
2383 default: assert(0 && "Cannot shift this type!");
2384 case MVT::i8: Opc = X86::SHL8ri; break;
2385 case MVT::i16: Opc = X86::SHL16ri; break;
2386 case MVT::i32: Opc = X86::SHL32ri; break;
2387 }
Chris Lattner11333092005-01-11 03:11:44 +00002388 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002389 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2390 return Result;
2391 }
Chris Lattner11333092005-01-11 03:11:44 +00002392
2393 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2394 Tmp1 = SelectExpr(N.getOperand(0));
2395 Tmp2 = SelectExpr(N.getOperand(1));
2396 } else {
2397 Tmp2 = SelectExpr(N.getOperand(1));
2398 Tmp1 = SelectExpr(N.getOperand(0));
2399 }
2400
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002401 switch (N.getValueType()) {
2402 default: assert(0 && "Cannot shift this type!");
2403 case MVT::i8 : Opc = X86::SHL8rCL; break;
2404 case MVT::i16: Opc = X86::SHL16rCL; break;
2405 case MVT::i32: Opc = X86::SHL32rCL; break;
2406 }
2407 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2408 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2409 return Result;
2410 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002411 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2412 switch (N.getValueType()) {
2413 default: assert(0 && "Cannot shift this type!");
2414 case MVT::i8: Opc = X86::SHR8ri; break;
2415 case MVT::i16: Opc = X86::SHR16ri; break;
2416 case MVT::i32: Opc = X86::SHR32ri; break;
2417 }
Chris Lattner11333092005-01-11 03:11:44 +00002418 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002419 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2420 return Result;
2421 }
Chris Lattner11333092005-01-11 03:11:44 +00002422
2423 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2424 Tmp1 = SelectExpr(N.getOperand(0));
2425 Tmp2 = SelectExpr(N.getOperand(1));
2426 } else {
2427 Tmp2 = SelectExpr(N.getOperand(1));
2428 Tmp1 = SelectExpr(N.getOperand(0));
2429 }
2430
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002431 switch (N.getValueType()) {
2432 default: assert(0 && "Cannot shift this type!");
2433 case MVT::i8 : Opc = X86::SHR8rCL; break;
2434 case MVT::i16: Opc = X86::SHR16rCL; break;
2435 case MVT::i32: Opc = X86::SHR32rCL; break;
2436 }
2437 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2438 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2439 return Result;
2440 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002441 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2442 switch (N.getValueType()) {
2443 default: assert(0 && "Cannot shift this type!");
2444 case MVT::i8: Opc = X86::SAR8ri; break;
2445 case MVT::i16: Opc = X86::SAR16ri; break;
2446 case MVT::i32: Opc = X86::SAR32ri; break;
2447 }
Chris Lattner11333092005-01-11 03:11:44 +00002448 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002449 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2450 return Result;
2451 }
Chris Lattner11333092005-01-11 03:11:44 +00002452
2453 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2454 Tmp1 = SelectExpr(N.getOperand(0));
2455 Tmp2 = SelectExpr(N.getOperand(1));
2456 } else {
2457 Tmp2 = SelectExpr(N.getOperand(1));
2458 Tmp1 = SelectExpr(N.getOperand(0));
2459 }
2460
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002461 switch (N.getValueType()) {
2462 default: assert(0 && "Cannot shift this type!");
2463 case MVT::i8 : Opc = X86::SAR8rCL; break;
2464 case MVT::i16: Opc = X86::SAR16rCL; break;
2465 case MVT::i32: Opc = X86::SAR32rCL; break;
2466 }
2467 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2468 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2469 return Result;
2470
2471 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002472 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002473 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2474 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2475 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002476 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002477 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002478 if (Result != 1) { // Generate the token
2479 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2480 assert(0 && "Load already emitted!?");
2481 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002482 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2483
Chris Lattner5188ad72005-01-08 19:28:19 +00002484 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002485 default: assert(0 && "Cannot load this type!");
2486 case MVT::i1:
2487 case MVT::i8: Opc = X86::MOV8rm; break;
2488 case MVT::i16: Opc = X86::MOV16rm; break;
2489 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002490 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2491 }
Chris Lattner11333092005-01-11 03:11:44 +00002492
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002493 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002494 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002495 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2496 } else {
2497 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002498
2499 SDOperand Chain = N.getOperand(0);
2500 SDOperand Address = N.getOperand(1);
2501 if (getRegPressure(Chain) > getRegPressure(Address)) {
2502 Select(Chain);
2503 SelectAddress(Address, AM);
2504 } else {
2505 SelectAddress(Address, AM);
2506 Select(Chain);
2507 }
2508
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002509 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2510 }
2511 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002512
2513 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2514 case ISD::ZEXTLOAD: {
2515 // Make sure we generate both values.
2516 if (Result != 1)
2517 ExprMap[N.getValue(1)] = 1; // Generate the token
2518 else
2519 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2520
Chris Lattnerda2ce112005-01-16 07:34:08 +00002521 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2522 if (Node->getValueType(0) == MVT::f64) {
2523 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2524 "Bad EXTLOAD!");
2525 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2526 CP->getIndex());
2527 return Result;
2528 }
2529
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002530 X86AddressMode AM;
2531 if (getRegPressure(Node->getOperand(0)) >
2532 getRegPressure(Node->getOperand(1))) {
2533 Select(Node->getOperand(0)); // chain
2534 SelectAddress(Node->getOperand(1), AM);
2535 } else {
2536 SelectAddress(Node->getOperand(1), AM);
2537 Select(Node->getOperand(0)); // chain
2538 }
2539
2540 switch (Node->getValueType(0)) {
2541 default: assert(0 && "Unknown type to sign extend to.");
2542 case MVT::f64:
2543 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2544 "Bad EXTLOAD!");
2545 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2546 break;
2547 case MVT::i32:
2548 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2549 default:
2550 assert(0 && "Bad zero extend!");
2551 case MVT::i1:
2552 case MVT::i8:
2553 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2554 break;
2555 case MVT::i16:
2556 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2557 break;
2558 }
2559 break;
2560 case MVT::i16:
2561 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2562 "Bad zero extend!");
2563 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2564 break;
2565 case MVT::i8:
2566 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2567 "Bad zero extend!");
2568 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2569 break;
2570 }
2571 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002572 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002573 case ISD::SEXTLOAD: {
2574 // Make sure we generate both values.
2575 if (Result != 1)
2576 ExprMap[N.getValue(1)] = 1; // Generate the token
2577 else
2578 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2579
2580 X86AddressMode AM;
2581 if (getRegPressure(Node->getOperand(0)) >
2582 getRegPressure(Node->getOperand(1))) {
2583 Select(Node->getOperand(0)); // chain
2584 SelectAddress(Node->getOperand(1), AM);
2585 } else {
2586 SelectAddress(Node->getOperand(1), AM);
2587 Select(Node->getOperand(0)); // chain
2588 }
2589
2590 switch (Node->getValueType(0)) {
2591 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2592 default: assert(0 && "Unknown type to sign extend to.");
2593 case MVT::i32:
2594 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2595 default:
2596 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2597 case MVT::i8:
2598 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2599 break;
2600 case MVT::i16:
2601 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2602 break;
2603 }
2604 break;
2605 case MVT::i16:
2606 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2607 "Cannot sign extend from bool!");
2608 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2609 break;
2610 }
2611 return Result;
2612 }
2613
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002614 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002615 // Generate both result values.
2616 if (Result != 1)
2617 ExprMap[N.getValue(1)] = 1; // Generate the token
2618 else
2619 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2620
2621 // FIXME: We are currently ignoring the requested alignment for handling
2622 // greater than the stack alignment. This will need to be revisited at some
2623 // point. Align = N.getOperand(2);
2624
2625 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2626 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2627 std::cerr << "Cannot allocate stack object with greater alignment than"
2628 << " the stack alignment yet!";
2629 abort();
2630 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002631
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002632 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002633 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002634 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2635 .addImm(CN->getValue());
2636 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002637 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2638 Select(N.getOperand(0));
2639 Tmp1 = SelectExpr(N.getOperand(1));
2640 } else {
2641 Tmp1 = SelectExpr(N.getOperand(1));
2642 Select(N.getOperand(0));
2643 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002644
2645 // Subtract size from stack pointer, thereby allocating some space.
2646 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2647 }
2648
2649 // Put a pointer to the space into the result register, by copying the stack
2650 // pointer.
2651 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2652 return Result;
2653
2654 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002655 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002656 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002657
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002658 if (GlobalAddressSDNode *GASD =
2659 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002660 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002661 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2662 } else if (ExternalSymbolSDNode *ESSDN =
2663 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002664 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002665 BuildMI(BB, X86::CALLpcrel32,
2666 1).addExternalSymbol(ESSDN->getSymbol(), true);
2667 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002668 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2669 Select(N.getOperand(0));
2670 Tmp1 = SelectExpr(N.getOperand(1));
2671 } else {
2672 Tmp1 = SelectExpr(N.getOperand(1));
2673 Select(N.getOperand(0));
2674 }
2675
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002676 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2677 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002678 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002679 default: assert(0 && "Unknown value type for call result!");
2680 case MVT::Other: return 1;
2681 case MVT::i1:
2682 case MVT::i8:
2683 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2684 break;
2685 case MVT::i16:
2686 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2687 break;
2688 case MVT::i32:
2689 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002690 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002691 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2692 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002693 case MVT::f64: // Floating-point return values live in %ST(0)
2694 ContainsFPCode = true;
2695 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2696 break;
2697 }
2698 return Result+N.ResNo;
2699 }
2700
2701 return 0;
2702}
2703
Chris Lattnere10269b2005-01-17 19:25:26 +00002704/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2705/// load/op/store instruction. If successful return true.
2706bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2707 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2708 SDOperand Chain = Node->getOperand(0);
2709 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002710 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002711
2712 // The chain has to be a load, the stored value must be an integer binary
2713 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002714 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002715 MVT::isFloatingPoint(StVal.getValueType()))
2716 return false;
2717
Chris Lattner5c659812005-01-17 22:10:42 +00002718 // Token chain must either be a factor node or the load to fold.
2719 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2720 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002721
Chris Lattner5c659812005-01-17 22:10:42 +00002722 SDOperand TheLoad;
2723
2724 // Check to see if there is a load from the same pointer that we're storing
2725 // to in either operand of the binop.
2726 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2727 StVal.getOperand(0).getOperand(1) == StPtr)
2728 TheLoad = StVal.getOperand(0);
2729 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2730 StVal.getOperand(1).getOperand(1) == StPtr)
2731 TheLoad = StVal.getOperand(1);
2732 else
2733 return false; // No matching load operand.
2734
2735 // We can only fold the load if there are no intervening side-effecting
2736 // operations. This means that the store uses the load as its token chain, or
2737 // there are only token factor nodes in between the store and load.
2738 if (Chain != TheLoad.getValue(1)) {
2739 // Okay, the other option is that we have a store referring to (possibly
2740 // nested) token factor nodes. For now, just try peeking through one level
2741 // of token factors to see if this is the case.
2742 bool ChainOk = false;
2743 if (Chain.getOpcode() == ISD::TokenFactor) {
2744 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2745 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2746 ChainOk = true;
2747 break;
2748 }
2749 }
2750
2751 if (!ChainOk) return false;
2752 }
2753
2754 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002755 return false;
2756
2757 // Make sure that one of the operands of the binop is the load, and that the
2758 // load folds into the binop.
2759 if (((StVal.getOperand(0) != TheLoad ||
2760 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2761 (StVal.getOperand(1) != TheLoad ||
2762 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2763 return false;
2764
2765 // Finally, check to see if this is one of the ops we can handle!
2766 static const unsigned ADDTAB[] = {
2767 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2768 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2769 };
2770 static const unsigned SUBTAB[] = {
2771 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2772 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2773 };
2774 static const unsigned ANDTAB[] = {
2775 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2776 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2777 };
2778 static const unsigned ORTAB[] = {
2779 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2780 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2781 };
2782 static const unsigned XORTAB[] = {
2783 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2784 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2785 };
2786 static const unsigned SHLTAB[] = {
2787 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2788 /*Have to put the reg in CL*/0, 0, 0,
2789 };
2790 static const unsigned SARTAB[] = {
2791 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2792 /*Have to put the reg in CL*/0, 0, 0,
2793 };
2794 static const unsigned SHRTAB[] = {
2795 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2796 /*Have to put the reg in CL*/0, 0, 0,
2797 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002798
Chris Lattnere10269b2005-01-17 19:25:26 +00002799 const unsigned *TabPtr = 0;
2800 switch (StVal.getOpcode()) {
2801 default:
2802 std::cerr << "CANNOT [mem] op= val: ";
2803 StVal.Val->dump(); std::cerr << "\n";
2804 case ISD::MUL:
2805 case ISD::SDIV:
2806 case ISD::UDIV:
2807 case ISD::SREM:
2808 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002809
Chris Lattnere10269b2005-01-17 19:25:26 +00002810 case ISD::ADD: TabPtr = ADDTAB; break;
2811 case ISD::SUB: TabPtr = SUBTAB; break;
2812 case ISD::AND: TabPtr = ANDTAB; break;
2813 case ISD:: OR: TabPtr = ORTAB; break;
2814 case ISD::XOR: TabPtr = XORTAB; break;
2815 case ISD::SHL: TabPtr = SHLTAB; break;
2816 case ISD::SRA: TabPtr = SARTAB; break;
2817 case ISD::SRL: TabPtr = SHRTAB; break;
2818 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002819
Chris Lattnere10269b2005-01-17 19:25:26 +00002820 // Handle: [mem] op= CST
2821 SDOperand Op0 = StVal.getOperand(0);
2822 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002823 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002824 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2825 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2826 default: break;
2827 case MVT::i1:
2828 case MVT::i8: Opc = TabPtr[0]; break;
2829 case MVT::i16: Opc = TabPtr[1]; break;
2830 case MVT::i32: Opc = TabPtr[2]; break;
2831 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002832
Chris Lattnere10269b2005-01-17 19:25:26 +00002833 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002834 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2835 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002836 Select(Chain);
2837
Chris Lattnere10269b2005-01-17 19:25:26 +00002838 X86AddressMode AM;
2839 if (getRegPressure(TheLoad.getOperand(0)) >
2840 getRegPressure(TheLoad.getOperand(1))) {
2841 Select(TheLoad.getOperand(0));
2842 SelectAddress(TheLoad.getOperand(1), AM);
2843 } else {
2844 SelectAddress(TheLoad.getOperand(1), AM);
2845 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002846 }
Chris Lattner5c659812005-01-17 22:10:42 +00002847
2848 if (StVal.getOpcode() == ISD::ADD) {
2849 if (CN->getValue() == 1) {
2850 switch (Op0.getValueType()) {
2851 default: break;
2852 case MVT::i8:
2853 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2854 return true;
2855 case MVT::i16: Opc = TabPtr[1];
2856 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2857 return true;
2858 case MVT::i32: Opc = TabPtr[2];
2859 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2860 return true;
2861 }
2862 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2863 switch (Op0.getValueType()) {
2864 default: break;
2865 case MVT::i8:
2866 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2867 return true;
2868 case MVT::i16: Opc = TabPtr[1];
2869 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2870 return true;
2871 case MVT::i32: Opc = TabPtr[2];
2872 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2873 return true;
2874 }
2875 }
2876 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002877
Chris Lattnere10269b2005-01-17 19:25:26 +00002878 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2879 return true;
2880 }
2881 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002882
Chris Lattnere10269b2005-01-17 19:25:26 +00002883 // If we have [mem] = V op [mem], try to turn it into:
2884 // [mem] = [mem] op V.
2885 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2886 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2887 StVal.getOpcode() != ISD::SRL)
2888 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002889
Chris Lattnere10269b2005-01-17 19:25:26 +00002890 if (Op0 != TheLoad) return false;
2891
2892 switch (Op0.getValueType()) {
2893 default: return false;
2894 case MVT::i1:
2895 case MVT::i8: Opc = TabPtr[3]; break;
2896 case MVT::i16: Opc = TabPtr[4]; break;
2897 case MVT::i32: Opc = TabPtr[5]; break;
2898 }
Chris Lattner5c659812005-01-17 22:10:42 +00002899
Chris Lattnerb422aea2005-01-18 17:35:28 +00002900 // Table entry doesn't exist?
2901 if (Opc == 0) return false;
2902
Chris Lattner4a108662005-01-18 03:51:59 +00002903 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2904 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002905 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002906 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002907
Chris Lattnere10269b2005-01-17 19:25:26 +00002908 X86AddressMode AM;
2909 SelectAddress(TheLoad.getOperand(1), AM);
2910 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002911 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002912 return true;
2913}
2914
2915
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002916void ISel::Select(SDOperand N) {
2917 unsigned Tmp1, Tmp2, Opc;
2918
Nate Begeman85fdeb22005-03-24 04:39:54 +00002919 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002920 return; // Already selected.
2921
Chris Lattner989de032005-01-11 06:14:36 +00002922 SDNode *Node = N.Val;
2923
2924 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002925 default:
Chris Lattner989de032005-01-11 06:14:36 +00002926 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002927 assert(0 && "Node not handled yet!");
2928 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002929 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002930 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002931 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002932 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2933 Select(Node->getOperand(OneFirst));
2934 Select(Node->getOperand(!OneFirst));
2935 } else {
2936 std::vector<std::pair<unsigned, unsigned> > OpsP;
2937 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2938 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2939 std::sort(OpsP.begin(), OpsP.end());
2940 std::reverse(OpsP.begin(), OpsP.end());
2941 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2942 Select(Node->getOperand(OpsP[i].second));
2943 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002944 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002945 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002946 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2947 Select(N.getOperand(0));
2948 Tmp1 = SelectExpr(N.getOperand(1));
2949 } else {
2950 Tmp1 = SelectExpr(N.getOperand(1));
2951 Select(N.getOperand(0));
2952 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002953 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002954
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002955 if (Tmp1 != Tmp2) {
2956 switch (N.getOperand(1).getValueType()) {
2957 default: assert(0 && "Invalid type for operation!");
2958 case MVT::i1:
2959 case MVT::i8: Opc = X86::MOV8rr; break;
2960 case MVT::i16: Opc = X86::MOV16rr; break;
2961 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002962 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002963 }
2964 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2965 }
2966 return;
2967 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002968 switch (N.getNumOperands()) {
2969 default:
2970 assert(0 && "Unknown return instruction!");
2971 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002972 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2973 N.getOperand(2).getValueType() == MVT::i32 &&
2974 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002975 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2976 Tmp1 = SelectExpr(N.getOperand(1));
2977 Tmp2 = SelectExpr(N.getOperand(2));
2978 } else {
2979 Tmp2 = SelectExpr(N.getOperand(2));
2980 Tmp1 = SelectExpr(N.getOperand(1));
2981 }
2982 Select(N.getOperand(0));
2983
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002984 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2985 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002986 break;
2987 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002988 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2989 Select(N.getOperand(0));
2990 Tmp1 = SelectExpr(N.getOperand(1));
2991 } else {
2992 Tmp1 = SelectExpr(N.getOperand(1));
2993 Select(N.getOperand(0));
2994 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002995 switch (N.getOperand(1).getValueType()) {
2996 default: assert(0 && "All other types should have been promoted!!");
2997 case MVT::f64:
2998 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002999 break;
3000 case MVT::i32:
3001 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003002 break;
3003 }
3004 break;
3005 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00003006 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003007 break;
3008 }
3009 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3010 return;
3011 case ISD::BR: {
3012 Select(N.getOperand(0));
3013 MachineBasicBlock *Dest =
3014 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3015 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3016 return;
3017 }
3018
3019 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003020 MachineBasicBlock *Dest =
3021 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003022
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003023 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3024 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003025 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3026 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3027 Select(N.getOperand(0));
3028 Tmp1 = SelectExpr(N.getOperand(1));
3029 } else {
3030 Tmp1 = SelectExpr(N.getOperand(1));
3031 Select(N.getOperand(0));
3032 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003033 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3034 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3035 }
Chris Lattner11333092005-01-11 03:11:44 +00003036
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003037 return;
3038 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003039
Chris Lattner4df0de92005-01-17 00:00:33 +00003040 case ISD::LOAD:
3041 // If this load could be folded into the only using instruction, and if it
3042 // is safe to emit the instruction here, try to do so now.
3043 if (Node->hasNUsesOfValue(1, 0)) {
3044 SDOperand TheVal = N.getValue(0);
3045 SDNode *User = 0;
3046 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3047 assert(UI != Node->use_end() && "Didn't find use!");
3048 SDNode *UN = *UI;
3049 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3050 if (UN->getOperand(i) == TheVal) {
3051 User = UN;
3052 goto FoundIt;
3053 }
3054 }
3055 FoundIt:
3056 // Only handle unary operators right now.
3057 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003058 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003059 SelectExpr(SDOperand(User, 0));
3060 return;
3061 }
3062 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003063 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003064 SelectExpr(N);
3065 return;
3066
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003067 case ISD::EXTLOAD:
3068 case ISD::SEXTLOAD:
3069 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003070 case ISD::CALL:
3071 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003072 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003073 SelectExpr(N);
3074 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003075
3076 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3077 // On X86, we can represent all types except for Bool and Float natively.
3078 X86AddressMode AM;
3079 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003080 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3081 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3082 && "Unsupported TRUNCSTORE for this target!");
3083
3084 if (StoredTy == MVT::i16) {
3085 // FIXME: This is here just to allow testing. X86 doesn't really have a
3086 // TRUNCSTORE i16 operation, but this is required for targets that do not
3087 // have 16-bit integer registers. We occasionally disable 16-bit integer
3088 // registers to test the promotion code.
3089 Select(N.getOperand(0));
3090 Tmp1 = SelectExpr(N.getOperand(1));
3091 SelectAddress(N.getOperand(2), AM);
3092
3093 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3094 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3095 return;
3096 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003097
3098 // Store of constant bool?
3099 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3100 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3101 Select(N.getOperand(0));
3102 SelectAddress(N.getOperand(2), AM);
3103 } else {
3104 SelectAddress(N.getOperand(2), AM);
3105 Select(N.getOperand(0));
3106 }
3107 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3108 return;
3109 }
3110
3111 switch (StoredTy) {
3112 default: assert(0 && "Cannot truncstore this type!");
3113 case MVT::i1: Opc = X86::MOV8mr; break;
3114 case MVT::f32: Opc = X86::FST32m; break;
3115 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003116
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003117 std::vector<std::pair<unsigned, unsigned> > RP;
3118 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3119 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3120 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3121 std::sort(RP.begin(), RP.end());
3122
Chris Lattner572dd082005-02-23 05:57:21 +00003123 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003124 for (unsigned i = 0; i != 3; ++i)
3125 switch (RP[2-i].second) {
3126 default: assert(0 && "Unknown operand number!");
3127 case 0: Select(N.getOperand(0)); break;
3128 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3129 case 2: SelectAddress(N.getOperand(2), AM); break;
3130 }
3131
3132 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3133 return;
3134 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003135 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003136 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003137
3138 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3139 Opc = 0;
3140 switch (CN->getValueType(0)) {
3141 default: assert(0 && "Invalid type for operation!");
3142 case MVT::i1:
3143 case MVT::i8: Opc = X86::MOV8mi; break;
3144 case MVT::i16: Opc = X86::MOV16mi; break;
3145 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003146 case MVT::f64: break;
3147 }
3148 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003149 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3150 Select(N.getOperand(0));
3151 SelectAddress(N.getOperand(2), AM);
3152 } else {
3153 SelectAddress(N.getOperand(2), AM);
3154 Select(N.getOperand(0));
3155 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003156 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3157 return;
3158 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003159 } else if (GlobalAddressSDNode *GA =
3160 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3161 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3162
3163 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3164 Select(N.getOperand(0));
3165 SelectAddress(N.getOperand(2), AM);
3166 } else {
3167 SelectAddress(N.getOperand(2), AM);
3168 Select(N.getOperand(0));
3169 }
3170 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3171 AM).addGlobalAddress(GA->getGlobal());
3172 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003173 }
Chris Lattner837caa72005-01-11 23:21:30 +00003174
3175 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003176 if (TryToFoldLoadOpStore(Node))
3177 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003178
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003179 switch (N.getOperand(1).getValueType()) {
3180 default: assert(0 && "Cannot store this type!");
3181 case MVT::i1:
3182 case MVT::i8: Opc = X86::MOV8mr; break;
3183 case MVT::i16: Opc = X86::MOV16mr; break;
3184 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003185 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003186 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003187
Chris Lattner11333092005-01-11 03:11:44 +00003188 std::vector<std::pair<unsigned, unsigned> > RP;
3189 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3190 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3191 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3192 std::sort(RP.begin(), RP.end());
3193
Chris Lattner572dd082005-02-23 05:57:21 +00003194 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003195 for (unsigned i = 0; i != 3; ++i)
3196 switch (RP[2-i].second) {
3197 default: assert(0 && "Unknown operand number!");
3198 case 0: Select(N.getOperand(0)); break;
3199 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003200 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003201 }
3202
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003203 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3204 return;
3205 }
3206 case ISD::ADJCALLSTACKDOWN:
3207 case ISD::ADJCALLSTACKUP:
3208 Select(N.getOperand(0));
3209 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003210
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003211 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3212 X86::ADJCALLSTACKUP;
3213 BuildMI(BB, Opc, 1).addImm(Tmp1);
3214 return;
Chris Lattner989de032005-01-11 06:14:36 +00003215 case ISD::MEMSET: {
3216 Select(N.getOperand(0)); // Select the chain.
3217 unsigned Align =
3218 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3219 if (Align == 0) Align = 1;
3220
3221 // Turn the byte code into # iterations
3222 unsigned CountReg;
3223 unsigned Opcode;
3224 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3225 unsigned Val = ValC->getValue() & 255;
3226
3227 // If the value is a constant, then we can potentially use larger sets.
3228 switch (Align & 3) {
3229 case 2: // WORD aligned
3230 CountReg = MakeReg(MVT::i32);
3231 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3232 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3233 } else {
3234 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3235 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3236 }
3237 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3238 Opcode = X86::REP_STOSW;
3239 break;
3240 case 0: // DWORD aligned
3241 CountReg = MakeReg(MVT::i32);
3242 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3243 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3244 } else {
3245 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3246 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3247 }
3248 Val = (Val << 8) | Val;
3249 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3250 Opcode = X86::REP_STOSD;
3251 break;
3252 default: // BYTE aligned
3253 CountReg = SelectExpr(Node->getOperand(3));
3254 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3255 Opcode = X86::REP_STOSB;
3256 break;
3257 }
3258 } else {
3259 // If it's not a constant value we are storing, just fall back. We could
3260 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3261 unsigned ValReg = SelectExpr(Node->getOperand(2));
3262 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3263 CountReg = SelectExpr(Node->getOperand(3));
3264 Opcode = X86::REP_STOSB;
3265 }
3266
3267 // No matter what the alignment is, we put the source in ESI, the
3268 // destination in EDI, and the count in ECX.
3269 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3270 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3271 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3272 BuildMI(BB, Opcode, 0);
3273 return;
3274 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003275 case ISD::MEMCPY:
3276 Select(N.getOperand(0)); // Select the chain.
3277 unsigned Align =
3278 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3279 if (Align == 0) Align = 1;
3280
3281 // Turn the byte code into # iterations
3282 unsigned CountReg;
3283 unsigned Opcode;
3284 switch (Align & 3) {
3285 case 2: // WORD aligned
3286 CountReg = MakeReg(MVT::i32);
3287 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3288 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3289 } else {
3290 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3291 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3292 }
3293 Opcode = X86::REP_MOVSW;
3294 break;
3295 case 0: // DWORD aligned
3296 CountReg = MakeReg(MVT::i32);
3297 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3298 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3299 } else {
3300 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3301 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3302 }
3303 Opcode = X86::REP_MOVSD;
3304 break;
3305 default: // BYTE aligned
3306 CountReg = SelectExpr(Node->getOperand(3));
3307 Opcode = X86::REP_MOVSB;
3308 break;
3309 }
3310
3311 // No matter what the alignment is, we put the source in ESI, the
3312 // destination in EDI, and the count in ECX.
3313 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3314 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3315 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3316 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3317 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3318 BuildMI(BB, Opcode, 0);
3319 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003320 }
3321 assert(0 && "Should not be reached!");
3322}
3323
3324
3325/// createX86PatternInstructionSelector - This pass converts an LLVM function
3326/// into a machine code representation using pattern matching and a machine
3327/// description file.
3328///
3329FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003330 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003331}