blob: 61acb2e9d7cdc6df64974b23f6ad21da86e53d1b [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) {
1704 case MVT::i8: StoreClass = MVT::i16; break;
1705 case MVT::i16: StoreClass = MVT::i32; break;
1706 case MVT::i32: StoreClass = MVT::i64; break;
1707 // The following treatment of cLong may not be perfectly right,
1708 // but it survives chains of casts of the form
1709 // double->ulong->double.
1710 case MVT::i64: StoreClass = MVT::i64; break;
1711 default: assert(0 && "Unknown store class!");
1712 }
1713
1714 // Spill the integer to memory and reload it from there.
1715 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1716 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1717
1718 switch (StoreClass) {
1719 default: assert(0 && "Unknown store class!");
1720 case MVT::i16:
1721 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1722 break;
1723 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001724 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001725 break;
1726 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001727 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001728 break;
1729 }
1730
1731 switch (Node->getValueType(0)) {
1732 default:
1733 assert(0 && "Unknown integer type!");
1734 case MVT::i64:
1735 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001736 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001737 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1738 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1739 case MVT::i32:
1740 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1741 break;
1742 case MVT::i16:
1743 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1744 break;
1745 case MVT::i8:
1746 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1747 break;
1748 }
1749
1750 // Reload the original control word now.
1751 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1752 return Result;
1753 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001754 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001755 Op0 = N.getOperand(0);
1756 Op1 = N.getOperand(1);
1757
Chris Lattner44129b52005-01-25 20:03:11 +00001758 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001759 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001760 goto FoldAdd;
1761 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001762
Chris Lattner44129b52005-01-25 20:03:11 +00001763 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001764 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001765 switch (N.getValueType()) {
1766 default: assert(0 && "Cannot add this type!");
1767 case MVT::i1:
1768 case MVT::i8: Opc = X86::ADD8rm; break;
1769 case MVT::i16: Opc = X86::ADD16rm; break;
1770 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001771 case MVT::f64:
1772 // For F64, handle promoted load operations (from F32) as well!
1773 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1774 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001775 }
1776 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001777 EmitFoldedLoad(Op1, AM);
1778 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001779 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1780 return Result;
1781 }
1782
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783 // See if we can codegen this as an LEA to fold operations together.
1784 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001785 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001786 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001787 MatchAddress(N, AM);
1788 ExprMap[N] = Result;
1789
1790 // If this is not just an add, emit the LEA. For a simple add (like
1791 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1792 // leave this as LEA, then peephole it to 'ADD' after two address elim
1793 // happens.
1794 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1795 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1796 X86AddressMode XAM = SelectAddrExprs(AM);
1797 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1798 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001799 }
1800 }
Chris Lattner11333092005-01-11 03:11:44 +00001801
Chris Lattnera5ade062005-01-11 21:19:59 +00001802 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001803 Opc = 0;
1804 if (CN->getValue() == 1) { // add X, 1 -> inc X
1805 switch (N.getValueType()) {
1806 default: assert(0 && "Cannot integer add this type!");
1807 case MVT::i8: Opc = X86::INC8r; break;
1808 case MVT::i16: Opc = X86::INC16r; break;
1809 case MVT::i32: Opc = X86::INC32r; break;
1810 }
1811 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1812 switch (N.getValueType()) {
1813 default: assert(0 && "Cannot integer add this type!");
1814 case MVT::i8: Opc = X86::DEC8r; break;
1815 case MVT::i16: Opc = X86::DEC16r; break;
1816 case MVT::i32: Opc = X86::DEC32r; break;
1817 }
1818 }
1819
1820 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001821 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001822 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1823 return Result;
1824 }
1825
1826 switch (N.getValueType()) {
1827 default: assert(0 && "Cannot add this type!");
1828 case MVT::i8: Opc = X86::ADD8ri; break;
1829 case MVT::i16: Opc = X86::ADD16ri; break;
1830 case MVT::i32: Opc = X86::ADD32ri; break;
1831 }
1832 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001833 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001834 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1835 return Result;
1836 }
1837 }
1838
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001839 switch (N.getValueType()) {
1840 default: assert(0 && "Cannot add this type!");
1841 case MVT::i8: Opc = X86::ADD8rr; break;
1842 case MVT::i16: Opc = X86::ADD16rr; break;
1843 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001844 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001845 }
Chris Lattner11333092005-01-11 03:11:44 +00001846
Chris Lattnera5ade062005-01-11 21:19:59 +00001847 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1848 Tmp1 = SelectExpr(Op0);
1849 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001850 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001851 Tmp2 = SelectExpr(Op1);
1852 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001853 }
1854
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001855 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1856 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001857
1858 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001859 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001860 case ISD::FSIN:
1861 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001862 case ISD::FSQRT:
1863 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001864 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001865 switch (N.getOpcode()) {
1866 default: assert(0 && "Unreachable!");
1867 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1868 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1869 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001870 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1871 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001872 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001873 return Result;
1874
Chris Lattner8db0af12005-04-06 04:21:07 +00001875 case ISD::MULHU:
1876 switch (N.getValueType()) {
1877 default: assert(0 && "Unsupported VT!");
1878 case MVT::i8: Tmp2 = X86::MUL8r; break;
1879 case MVT::i16: Tmp2 = X86::MUL16r; break;
1880 case MVT::i32: Tmp2 = X86::MUL32r; break;
1881 }
1882 // FALL THROUGH
1883 case ISD::MULHS: {
1884 unsigned MovOpc, LowReg, HiReg;
1885 switch (N.getValueType()) {
1886 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001887 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001888 MovOpc = X86::MOV8rr;
1889 LowReg = X86::AL;
1890 HiReg = X86::AH;
1891 Opc = X86::IMUL8r;
1892 break;
1893 case MVT::i16:
1894 MovOpc = X86::MOV16rr;
1895 LowReg = X86::AX;
1896 HiReg = X86::DX;
1897 Opc = X86::IMUL16r;
1898 break;
1899 case MVT::i32:
1900 MovOpc = X86::MOV32rr;
1901 LowReg = X86::EAX;
1902 HiReg = X86::EDX;
1903 Opc = X86::IMUL32r;
1904 break;
1905 }
1906 if (Node->getOpcode() != ISD::MULHS)
1907 Opc = Tmp2; // Get the MULHU opcode.
1908
1909 Op0 = Node->getOperand(0);
1910 Op1 = Node->getOperand(1);
1911 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1912 Tmp1 = SelectExpr(Op0);
1913 Tmp2 = SelectExpr(Op1);
1914 } else {
1915 Tmp2 = SelectExpr(Op1);
1916 Tmp1 = SelectExpr(Op0);
1917 }
1918
1919 // FIXME: Implement folding of loads into the memory operands here!
1920 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1921 BuildMI(BB, Opc, 1).addReg(Tmp2);
1922 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1923 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001924 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001925
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001926 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001927 case ISD::MUL:
1928 case ISD::AND:
1929 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001930 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001931 static const unsigned SUBTab[] = {
1932 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1933 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1934 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1935 };
1936 static const unsigned MULTab[] = {
1937 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1938 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1939 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1940 };
1941 static const unsigned ANDTab[] = {
1942 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1943 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001944 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001945 };
1946 static const unsigned ORTab[] = {
1947 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1948 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1949 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1950 };
1951 static const unsigned XORTab[] = {
1952 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1953 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1954 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1955 };
1956
1957 Op0 = Node->getOperand(0);
1958 Op1 = Node->getOperand(1);
1959
Chris Lattner30ea1e92005-01-19 07:37:26 +00001960 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1961 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001962 return Result;
1963
1964 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001965 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1966 if (CN->isNullValue()) { // 0 - N -> neg N
1967 switch (N.getValueType()) {
1968 default: assert(0 && "Cannot sub this type!");
1969 case MVT::i1:
1970 case MVT::i8: Opc = X86::NEG8r; break;
1971 case MVT::i16: Opc = X86::NEG16r; break;
1972 case MVT::i32: Opc = X86::NEG32r; break;
1973 }
1974 Tmp1 = SelectExpr(N.getOperand(1));
1975 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1976 return Result;
1977 }
1978
Chris Lattnera5ade062005-01-11 21:19:59 +00001979 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1980 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001981 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001982 switch (N.getValueType()) {
1983 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001984 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001985 case MVT::i8: Opc = X86::NOT8r; break;
1986 case MVT::i16: Opc = X86::NOT16r; break;
1987 case MVT::i32: Opc = X86::NOT32r; break;
1988 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001989 if (Opc) {
1990 Tmp1 = SelectExpr(Op0);
1991 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1992 return Result;
1993 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001994 }
1995
Chris Lattner2a4e5082005-01-17 06:48:02 +00001996 // Fold common multiplies into LEA instructions.
1997 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1998 switch ((int)CN->getValue()) {
1999 default: break;
2000 case 3:
2001 case 5:
2002 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002003 // Remove N from exprmap so SelectAddress doesn't get confused.
2004 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002005 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002006 SelectAddress(N, AM);
2007 // Restore it to the map.
2008 ExprMap[N] = Result;
2009 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2010 return Result;
2011 }
2012 }
2013
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002014 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002015 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002017 case MVT::i8: Opc = 0; break;
2018 case MVT::i16: Opc = 1; break;
2019 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002020 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002021 switch (Node->getOpcode()) {
2022 default: assert(0 && "Unreachable!");
2023 case ISD::SUB: Opc = SUBTab[Opc]; break;
2024 case ISD::MUL: Opc = MULTab[Opc]; break;
2025 case ISD::AND: Opc = ANDTab[Opc]; break;
2026 case ISD::OR: Opc = ORTab[Opc]; break;
2027 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002028 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002029 if (Opc) { // Can't fold MUL:i8 R, imm
2030 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002031 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2032 return Result;
2033 }
2034 }
Chris Lattner11333092005-01-11 03:11:44 +00002035
Chris Lattner44129b52005-01-25 20:03:11 +00002036 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002037 if (Node->getOpcode() != ISD::SUB) {
2038 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002039 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002040 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002041 // For FP, emit 'reverse' subract, with a memory operand.
2042 if (N.getValueType() == MVT::f64) {
2043 if (Op0.getOpcode() == ISD::EXTLOAD)
2044 Opc = X86::FSUBR32m;
2045 else
2046 Opc = X86::FSUBR64m;
2047
Chris Lattnera5ade062005-01-11 21:19:59 +00002048 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002049 EmitFoldedLoad(Op0, AM);
2050 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002051 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2052 return Result;
2053 }
2054 }
2055
Chris Lattner44129b52005-01-25 20:03:11 +00002056 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002057 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002058 switch (N.getValueType()) {
2059 default: assert(0 && "Cannot operate on this type!");
2060 case MVT::i1:
2061 case MVT::i8: Opc = 5; break;
2062 case MVT::i16: Opc = 6; break;
2063 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002064 // For F64, handle promoted load operations (from F32) as well!
2065 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002066 }
2067 switch (Node->getOpcode()) {
2068 default: assert(0 && "Unreachable!");
2069 case ISD::SUB: Opc = SUBTab[Opc]; break;
2070 case ISD::MUL: Opc = MULTab[Opc]; break;
2071 case ISD::AND: Opc = ANDTab[Opc]; break;
2072 case ISD::OR: Opc = ORTab[Opc]; break;
2073 case ISD::XOR: Opc = XORTab[Opc]; break;
2074 }
2075
2076 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002077 EmitFoldedLoad(Op1, AM);
2078 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002079 if (Opc) {
2080 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2081 } else {
2082 assert(Node->getOpcode() == ISD::MUL &&
2083 N.getValueType() == MVT::i8 && "Unexpected situation!");
2084 // Must use the MUL instruction, which forces use of AL.
2085 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2086 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2087 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2088 }
2089 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002090 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002091
2092 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2093 Tmp1 = SelectExpr(Op0);
2094 Tmp2 = SelectExpr(Op1);
2095 } else {
2096 Tmp2 = SelectExpr(Op1);
2097 Tmp1 = SelectExpr(Op0);
2098 }
2099
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002100 switch (N.getValueType()) {
2101 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002102 case MVT::i1:
2103 case MVT::i8: Opc = 10; break;
2104 case MVT::i16: Opc = 11; break;
2105 case MVT::i32: Opc = 12; break;
2106 case MVT::f32: Opc = 13; break;
2107 case MVT::f64: Opc = 14; break;
2108 }
2109 switch (Node->getOpcode()) {
2110 default: assert(0 && "Unreachable!");
2111 case ISD::SUB: Opc = SUBTab[Opc]; break;
2112 case ISD::MUL: Opc = MULTab[Opc]; break;
2113 case ISD::AND: Opc = ANDTab[Opc]; break;
2114 case ISD::OR: Opc = ORTab[Opc]; break;
2115 case ISD::XOR: Opc = XORTab[Opc]; break;
2116 }
2117 if (Opc) {
2118 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2119 } else {
2120 assert(Node->getOpcode() == ISD::MUL &&
2121 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002122 // Must use the MUL instruction, which forces use of AL.
2123 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2124 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2125 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002126 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002127 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002128 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002129 case ISD::ADD_PARTS:
2130 case ISD::SUB_PARTS: {
2131 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2132 "Not an i64 add/sub!");
2133 // Emit all of the operands.
2134 std::vector<unsigned> InVals;
2135 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2136 InVals.push_back(SelectExpr(N.getOperand(i)));
2137 if (N.getOpcode() == ISD::ADD_PARTS) {
2138 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2139 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2140 } else {
2141 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2142 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2143 }
2144 return Result+N.ResNo;
2145 }
2146
Chris Lattnerb38a7492005-04-02 04:01:14 +00002147 case ISD::SHL_PARTS:
2148 case ISD::SRA_PARTS:
2149 case ISD::SRL_PARTS: {
2150 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2151 "Not an i64 shift!");
2152 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2153 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2154 unsigned TmpReg = MakeReg(MVT::i32);
2155 if (N.getOpcode() == ISD::SRA_PARTS) {
2156 // If this is a SHR of a Long, then we need to do funny sign extension
2157 // stuff. TmpReg gets the value to use as the high-part if we are
2158 // shifting more than 32 bits.
2159 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2160 } else {
2161 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2162 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2163 }
2164
2165 // Initialize CL with the shift amount.
2166 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2167 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2168
2169 unsigned TmpReg2 = MakeReg(MVT::i32);
2170 unsigned TmpReg3 = MakeReg(MVT::i32);
2171 if (N.getOpcode() == ISD::SHL_PARTS) {
2172 // TmpReg2 = shld inHi, inLo
2173 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2174 .addReg(ShiftOpLo);
2175 // TmpReg3 = shl inLo, CL
2176 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002177
Chris Lattnerb38a7492005-04-02 04:01:14 +00002178 // Set the flags to indicate whether the shift was by more than 32 bits.
2179 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002180
Chris Lattnerb38a7492005-04-02 04:01:14 +00002181 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002182 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002183 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2184 // DestLo = (>32) ? TmpReg : TmpReg3;
2185 BuildMI(BB, X86::CMOVNE32rr, 2,
2186 Result).addReg(TmpReg3).addReg(TmpReg);
2187 } else {
2188 // TmpReg2 = shrd inLo, inHi
2189 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2190 .addReg(ShiftOpHi);
2191 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002192 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002193 : X86::SHR32rCL, 1, TmpReg3)
2194 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002195
Chris Lattnerb38a7492005-04-02 04:01:14 +00002196 // Set the flags to indicate whether the shift was by more than 32 bits.
2197 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002198
Chris Lattnerb38a7492005-04-02 04:01:14 +00002199 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002200 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002201 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002202
Chris Lattnerb38a7492005-04-02 04:01:14 +00002203 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002204 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002205 Result+1).addReg(TmpReg3).addReg(TmpReg);
2206 }
2207 return Result+N.ResNo;
2208 }
2209
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002210 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002211 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2212 Tmp2 = SelectExpr(N.getOperand(1));
2213 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002214 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002215 Tmp3 = SelectExpr(N.getOperand(2));
2216 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002217 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002218 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2219 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002220
2221 case ISD::SDIV:
2222 case ISD::UDIV:
2223 case ISD::SREM:
2224 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002225 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2226 "We don't support this operator!");
2227
Chris Lattner5bf26862005-04-13 03:29:53 +00002228 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002229 // We can fold loads into FpDIVs, but not really into any others.
2230 if (N.getValueType() == MVT::f64) {
2231 // Check for reversed and unreversed DIV.
2232 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2233 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2234 Opc = X86::FDIVR32m;
2235 else
2236 Opc = X86::FDIVR64m;
2237 X86AddressMode AM;
2238 EmitFoldedLoad(N.getOperand(0), AM);
2239 Tmp1 = SelectExpr(N.getOperand(1));
2240 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2241 return Result;
2242 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2243 N.getOperand(1).getOpcode() == ISD::LOAD) {
2244 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2245 Opc = X86::FDIV32m;
2246 else
2247 Opc = X86::FDIV64m;
2248 X86AddressMode AM;
2249 EmitFoldedLoad(N.getOperand(1), AM);
2250 Tmp1 = SelectExpr(N.getOperand(0));
2251 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2252 return Result;
2253 }
2254 }
2255
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2257 // FIXME: These special cases should be handled by the lowering impl!
2258 unsigned RHS = CN->getValue();
2259 bool isNeg = false;
2260 if ((int)RHS < 0) {
2261 isNeg = true;
2262 RHS = -RHS;
2263 }
2264 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2265 unsigned Log = log2(RHS);
2266 unsigned TmpReg = MakeReg(N.getValueType());
2267 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2268 switch (N.getValueType()) {
2269 default: assert("Unknown type to signed divide!");
2270 case MVT::i8:
2271 SAROpc = X86::SAR8ri;
2272 SHROpc = X86::SHR8ri;
2273 ADDOpc = X86::ADD8rr;
2274 NEGOpc = X86::NEG8r;
2275 break;
2276 case MVT::i16:
2277 SAROpc = X86::SAR16ri;
2278 SHROpc = X86::SHR16ri;
2279 ADDOpc = X86::ADD16rr;
2280 NEGOpc = X86::NEG16r;
2281 break;
2282 case MVT::i32:
2283 SAROpc = X86::SAR32ri;
2284 SHROpc = X86::SHR32ri;
2285 ADDOpc = X86::ADD32rr;
2286 NEGOpc = X86::NEG32r;
2287 break;
2288 }
Chris Lattner11333092005-01-11 03:11:44 +00002289 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002290 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2291 unsigned TmpReg2 = MakeReg(N.getValueType());
2292 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2293 unsigned TmpReg3 = MakeReg(N.getValueType());
2294 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002295
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002296 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2297 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2298 if (isNeg)
2299 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2300 return Result;
2301 }
2302 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002303 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002304
Chris Lattner11333092005-01-11 03:11:44 +00002305 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2306 Tmp1 = SelectExpr(N.getOperand(0));
2307 Tmp2 = SelectExpr(N.getOperand(1));
2308 } else {
2309 Tmp2 = SelectExpr(N.getOperand(1));
2310 Tmp1 = SelectExpr(N.getOperand(0));
2311 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002312
2313 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2314 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2315 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2316 switch (N.getValueType()) {
2317 default: assert(0 && "Cannot sdiv this type!");
2318 case MVT::i8:
2319 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2320 LoReg = X86::AL;
2321 HiReg = X86::AH;
2322 MovOpcode = X86::MOV8rr;
2323 ClrOpcode = X86::MOV8ri;
2324 SExtOpcode = X86::CBW;
2325 break;
2326 case MVT::i16:
2327 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2328 LoReg = X86::AX;
2329 HiReg = X86::DX;
2330 MovOpcode = X86::MOV16rr;
2331 ClrOpcode = X86::MOV16ri;
2332 SExtOpcode = X86::CWD;
2333 break;
2334 case MVT::i32:
2335 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002336 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002337 HiReg = X86::EDX;
2338 MovOpcode = X86::MOV32rr;
2339 ClrOpcode = X86::MOV32ri;
2340 SExtOpcode = X86::CDQ;
2341 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002342 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002343 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002344 return Result;
2345 }
2346
2347 // Set up the low part.
2348 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2349
2350 if (isSigned) {
2351 // Sign extend the low part into the high part.
2352 BuildMI(BB, SExtOpcode, 0);
2353 } else {
2354 // Zero out the high part, effectively zero extending the input.
2355 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2356 }
2357
2358 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002359 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002360
2361 // Get the result of the divide or rem.
2362 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2363 return Result;
2364 }
2365
2366 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002367 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002368 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2369 switch (N.getValueType()) {
2370 default: assert(0 && "Cannot shift this type!");
2371 case MVT::i8: Opc = X86::ADD8rr; break;
2372 case MVT::i16: Opc = X86::ADD16rr; break;
2373 case MVT::i32: Opc = X86::ADD32rr; break;
2374 }
2375 Tmp1 = SelectExpr(N.getOperand(0));
2376 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2377 return Result;
2378 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002379
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002380 switch (N.getValueType()) {
2381 default: assert(0 && "Cannot shift this type!");
2382 case MVT::i8: Opc = X86::SHL8ri; break;
2383 case MVT::i16: Opc = X86::SHL16ri; break;
2384 case MVT::i32: Opc = X86::SHL32ri; break;
2385 }
Chris Lattner11333092005-01-11 03:11:44 +00002386 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002387 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2388 return Result;
2389 }
Chris Lattner11333092005-01-11 03:11:44 +00002390
2391 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2392 Tmp1 = SelectExpr(N.getOperand(0));
2393 Tmp2 = SelectExpr(N.getOperand(1));
2394 } else {
2395 Tmp2 = SelectExpr(N.getOperand(1));
2396 Tmp1 = SelectExpr(N.getOperand(0));
2397 }
2398
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002399 switch (N.getValueType()) {
2400 default: assert(0 && "Cannot shift this type!");
2401 case MVT::i8 : Opc = X86::SHL8rCL; break;
2402 case MVT::i16: Opc = X86::SHL16rCL; break;
2403 case MVT::i32: Opc = X86::SHL32rCL; break;
2404 }
2405 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2406 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2407 return Result;
2408 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002409 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2410 switch (N.getValueType()) {
2411 default: assert(0 && "Cannot shift this type!");
2412 case MVT::i8: Opc = X86::SHR8ri; break;
2413 case MVT::i16: Opc = X86::SHR16ri; break;
2414 case MVT::i32: Opc = X86::SHR32ri; break;
2415 }
Chris Lattner11333092005-01-11 03:11:44 +00002416 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002417 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2418 return Result;
2419 }
Chris Lattner11333092005-01-11 03:11:44 +00002420
2421 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2422 Tmp1 = SelectExpr(N.getOperand(0));
2423 Tmp2 = SelectExpr(N.getOperand(1));
2424 } else {
2425 Tmp2 = SelectExpr(N.getOperand(1));
2426 Tmp1 = SelectExpr(N.getOperand(0));
2427 }
2428
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002429 switch (N.getValueType()) {
2430 default: assert(0 && "Cannot shift this type!");
2431 case MVT::i8 : Opc = X86::SHR8rCL; break;
2432 case MVT::i16: Opc = X86::SHR16rCL; break;
2433 case MVT::i32: Opc = X86::SHR32rCL; break;
2434 }
2435 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2436 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2437 return Result;
2438 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002439 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2440 switch (N.getValueType()) {
2441 default: assert(0 && "Cannot shift this type!");
2442 case MVT::i8: Opc = X86::SAR8ri; break;
2443 case MVT::i16: Opc = X86::SAR16ri; break;
2444 case MVT::i32: Opc = X86::SAR32ri; break;
2445 }
Chris Lattner11333092005-01-11 03:11:44 +00002446 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002447 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2448 return Result;
2449 }
Chris Lattner11333092005-01-11 03:11:44 +00002450
2451 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2452 Tmp1 = SelectExpr(N.getOperand(0));
2453 Tmp2 = SelectExpr(N.getOperand(1));
2454 } else {
2455 Tmp2 = SelectExpr(N.getOperand(1));
2456 Tmp1 = SelectExpr(N.getOperand(0));
2457 }
2458
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459 switch (N.getValueType()) {
2460 default: assert(0 && "Cannot shift this type!");
2461 case MVT::i8 : Opc = X86::SAR8rCL; break;
2462 case MVT::i16: Opc = X86::SAR16rCL; break;
2463 case MVT::i32: Opc = X86::SAR32rCL; break;
2464 }
2465 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2466 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2467 return Result;
2468
2469 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002470 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002471 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2472 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2473 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002474 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002475 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002476 if (Result != 1) { // Generate the token
2477 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2478 assert(0 && "Load already emitted!?");
2479 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002480 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2481
Chris Lattner5188ad72005-01-08 19:28:19 +00002482 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002483 default: assert(0 && "Cannot load this type!");
2484 case MVT::i1:
2485 case MVT::i8: Opc = X86::MOV8rm; break;
2486 case MVT::i16: Opc = X86::MOV16rm; break;
2487 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002488 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2489 }
Chris Lattner11333092005-01-11 03:11:44 +00002490
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002491 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002492 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002493 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2494 } else {
2495 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002496
2497 SDOperand Chain = N.getOperand(0);
2498 SDOperand Address = N.getOperand(1);
2499 if (getRegPressure(Chain) > getRegPressure(Address)) {
2500 Select(Chain);
2501 SelectAddress(Address, AM);
2502 } else {
2503 SelectAddress(Address, AM);
2504 Select(Chain);
2505 }
2506
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002507 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2508 }
2509 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002510
2511 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2512 case ISD::ZEXTLOAD: {
2513 // Make sure we generate both values.
2514 if (Result != 1)
2515 ExprMap[N.getValue(1)] = 1; // Generate the token
2516 else
2517 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2518
Chris Lattnerda2ce112005-01-16 07:34:08 +00002519 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2520 if (Node->getValueType(0) == MVT::f64) {
2521 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2522 "Bad EXTLOAD!");
2523 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2524 CP->getIndex());
2525 return Result;
2526 }
2527
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002528 X86AddressMode AM;
2529 if (getRegPressure(Node->getOperand(0)) >
2530 getRegPressure(Node->getOperand(1))) {
2531 Select(Node->getOperand(0)); // chain
2532 SelectAddress(Node->getOperand(1), AM);
2533 } else {
2534 SelectAddress(Node->getOperand(1), AM);
2535 Select(Node->getOperand(0)); // chain
2536 }
2537
2538 switch (Node->getValueType(0)) {
2539 default: assert(0 && "Unknown type to sign extend to.");
2540 case MVT::f64:
2541 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2542 "Bad EXTLOAD!");
2543 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2544 break;
2545 case MVT::i32:
2546 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2547 default:
2548 assert(0 && "Bad zero extend!");
2549 case MVT::i1:
2550 case MVT::i8:
2551 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2552 break;
2553 case MVT::i16:
2554 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2555 break;
2556 }
2557 break;
2558 case MVT::i16:
2559 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2560 "Bad zero extend!");
2561 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2562 break;
2563 case MVT::i8:
2564 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2565 "Bad zero extend!");
2566 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2567 break;
2568 }
2569 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002570 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002571 case ISD::SEXTLOAD: {
2572 // Make sure we generate both values.
2573 if (Result != 1)
2574 ExprMap[N.getValue(1)] = 1; // Generate the token
2575 else
2576 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2577
2578 X86AddressMode AM;
2579 if (getRegPressure(Node->getOperand(0)) >
2580 getRegPressure(Node->getOperand(1))) {
2581 Select(Node->getOperand(0)); // chain
2582 SelectAddress(Node->getOperand(1), AM);
2583 } else {
2584 SelectAddress(Node->getOperand(1), AM);
2585 Select(Node->getOperand(0)); // chain
2586 }
2587
2588 switch (Node->getValueType(0)) {
2589 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2590 default: assert(0 && "Unknown type to sign extend to.");
2591 case MVT::i32:
2592 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2593 default:
2594 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2595 case MVT::i8:
2596 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2597 break;
2598 case MVT::i16:
2599 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2600 break;
2601 }
2602 break;
2603 case MVT::i16:
2604 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2605 "Cannot sign extend from bool!");
2606 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2607 break;
2608 }
2609 return Result;
2610 }
2611
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002612 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002613 // Generate both result values.
2614 if (Result != 1)
2615 ExprMap[N.getValue(1)] = 1; // Generate the token
2616 else
2617 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2618
2619 // FIXME: We are currently ignoring the requested alignment for handling
2620 // greater than the stack alignment. This will need to be revisited at some
2621 // point. Align = N.getOperand(2);
2622
2623 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2624 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2625 std::cerr << "Cannot allocate stack object with greater alignment than"
2626 << " the stack alignment yet!";
2627 abort();
2628 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002629
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002630 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002631 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002632 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2633 .addImm(CN->getValue());
2634 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002635 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2636 Select(N.getOperand(0));
2637 Tmp1 = SelectExpr(N.getOperand(1));
2638 } else {
2639 Tmp1 = SelectExpr(N.getOperand(1));
2640 Select(N.getOperand(0));
2641 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002642
2643 // Subtract size from stack pointer, thereby allocating some space.
2644 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2645 }
2646
2647 // Put a pointer to the space into the result register, by copying the stack
2648 // pointer.
2649 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2650 return Result;
2651
2652 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002653 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002654 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002655
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002656 if (GlobalAddressSDNode *GASD =
2657 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002658 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002659 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2660 } else if (ExternalSymbolSDNode *ESSDN =
2661 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002662 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002663 BuildMI(BB, X86::CALLpcrel32,
2664 1).addExternalSymbol(ESSDN->getSymbol(), true);
2665 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002666 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2667 Select(N.getOperand(0));
2668 Tmp1 = SelectExpr(N.getOperand(1));
2669 } else {
2670 Tmp1 = SelectExpr(N.getOperand(1));
2671 Select(N.getOperand(0));
2672 }
2673
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002674 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2675 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002676 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002677 default: assert(0 && "Unknown value type for call result!");
2678 case MVT::Other: return 1;
2679 case MVT::i1:
2680 case MVT::i8:
2681 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2682 break;
2683 case MVT::i16:
2684 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2685 break;
2686 case MVT::i32:
2687 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002688 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002689 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2690 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002691 case MVT::f64: // Floating-point return values live in %ST(0)
2692 ContainsFPCode = true;
2693 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2694 break;
2695 }
2696 return Result+N.ResNo;
2697 }
2698
2699 return 0;
2700}
2701
Chris Lattnere10269b2005-01-17 19:25:26 +00002702/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2703/// load/op/store instruction. If successful return true.
2704bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2705 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2706 SDOperand Chain = Node->getOperand(0);
2707 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002708 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002709
2710 // The chain has to be a load, the stored value must be an integer binary
2711 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002712 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002713 MVT::isFloatingPoint(StVal.getValueType()))
2714 return false;
2715
Chris Lattner5c659812005-01-17 22:10:42 +00002716 // Token chain must either be a factor node or the load to fold.
2717 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2718 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002719
Chris Lattner5c659812005-01-17 22:10:42 +00002720 SDOperand TheLoad;
2721
2722 // Check to see if there is a load from the same pointer that we're storing
2723 // to in either operand of the binop.
2724 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2725 StVal.getOperand(0).getOperand(1) == StPtr)
2726 TheLoad = StVal.getOperand(0);
2727 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2728 StVal.getOperand(1).getOperand(1) == StPtr)
2729 TheLoad = StVal.getOperand(1);
2730 else
2731 return false; // No matching load operand.
2732
2733 // We can only fold the load if there are no intervening side-effecting
2734 // operations. This means that the store uses the load as its token chain, or
2735 // there are only token factor nodes in between the store and load.
2736 if (Chain != TheLoad.getValue(1)) {
2737 // Okay, the other option is that we have a store referring to (possibly
2738 // nested) token factor nodes. For now, just try peeking through one level
2739 // of token factors to see if this is the case.
2740 bool ChainOk = false;
2741 if (Chain.getOpcode() == ISD::TokenFactor) {
2742 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2743 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2744 ChainOk = true;
2745 break;
2746 }
2747 }
2748
2749 if (!ChainOk) return false;
2750 }
2751
2752 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002753 return false;
2754
2755 // Make sure that one of the operands of the binop is the load, and that the
2756 // load folds into the binop.
2757 if (((StVal.getOperand(0) != TheLoad ||
2758 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2759 (StVal.getOperand(1) != TheLoad ||
2760 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2761 return false;
2762
2763 // Finally, check to see if this is one of the ops we can handle!
2764 static const unsigned ADDTAB[] = {
2765 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2766 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2767 };
2768 static const unsigned SUBTAB[] = {
2769 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2770 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2771 };
2772 static const unsigned ANDTAB[] = {
2773 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2774 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2775 };
2776 static const unsigned ORTAB[] = {
2777 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2778 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2779 };
2780 static const unsigned XORTAB[] = {
2781 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2782 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2783 };
2784 static const unsigned SHLTAB[] = {
2785 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2786 /*Have to put the reg in CL*/0, 0, 0,
2787 };
2788 static const unsigned SARTAB[] = {
2789 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2790 /*Have to put the reg in CL*/0, 0, 0,
2791 };
2792 static const unsigned SHRTAB[] = {
2793 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2794 /*Have to put the reg in CL*/0, 0, 0,
2795 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002796
Chris Lattnere10269b2005-01-17 19:25:26 +00002797 const unsigned *TabPtr = 0;
2798 switch (StVal.getOpcode()) {
2799 default:
2800 std::cerr << "CANNOT [mem] op= val: ";
2801 StVal.Val->dump(); std::cerr << "\n";
2802 case ISD::MUL:
2803 case ISD::SDIV:
2804 case ISD::UDIV:
2805 case ISD::SREM:
2806 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002807
Chris Lattnere10269b2005-01-17 19:25:26 +00002808 case ISD::ADD: TabPtr = ADDTAB; break;
2809 case ISD::SUB: TabPtr = SUBTAB; break;
2810 case ISD::AND: TabPtr = ANDTAB; break;
2811 case ISD:: OR: TabPtr = ORTAB; break;
2812 case ISD::XOR: TabPtr = XORTAB; break;
2813 case ISD::SHL: TabPtr = SHLTAB; break;
2814 case ISD::SRA: TabPtr = SARTAB; break;
2815 case ISD::SRL: TabPtr = SHRTAB; break;
2816 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002817
Chris Lattnere10269b2005-01-17 19:25:26 +00002818 // Handle: [mem] op= CST
2819 SDOperand Op0 = StVal.getOperand(0);
2820 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002821 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002822 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2823 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2824 default: break;
2825 case MVT::i1:
2826 case MVT::i8: Opc = TabPtr[0]; break;
2827 case MVT::i16: Opc = TabPtr[1]; break;
2828 case MVT::i32: Opc = TabPtr[2]; break;
2829 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002830
Chris Lattnere10269b2005-01-17 19:25:26 +00002831 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002832 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2833 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002834 Select(Chain);
2835
Chris Lattnere10269b2005-01-17 19:25:26 +00002836 X86AddressMode AM;
2837 if (getRegPressure(TheLoad.getOperand(0)) >
2838 getRegPressure(TheLoad.getOperand(1))) {
2839 Select(TheLoad.getOperand(0));
2840 SelectAddress(TheLoad.getOperand(1), AM);
2841 } else {
2842 SelectAddress(TheLoad.getOperand(1), AM);
2843 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002844 }
Chris Lattner5c659812005-01-17 22:10:42 +00002845
2846 if (StVal.getOpcode() == ISD::ADD) {
2847 if (CN->getValue() == 1) {
2848 switch (Op0.getValueType()) {
2849 default: break;
2850 case MVT::i8:
2851 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2852 return true;
2853 case MVT::i16: Opc = TabPtr[1];
2854 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2855 return true;
2856 case MVT::i32: Opc = TabPtr[2];
2857 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2858 return true;
2859 }
2860 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2861 switch (Op0.getValueType()) {
2862 default: break;
2863 case MVT::i8:
2864 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2865 return true;
2866 case MVT::i16: Opc = TabPtr[1];
2867 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2868 return true;
2869 case MVT::i32: Opc = TabPtr[2];
2870 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2871 return true;
2872 }
2873 }
2874 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002875
Chris Lattnere10269b2005-01-17 19:25:26 +00002876 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2877 return true;
2878 }
2879 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002880
Chris Lattnere10269b2005-01-17 19:25:26 +00002881 // If we have [mem] = V op [mem], try to turn it into:
2882 // [mem] = [mem] op V.
2883 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2884 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2885 StVal.getOpcode() != ISD::SRL)
2886 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002887
Chris Lattnere10269b2005-01-17 19:25:26 +00002888 if (Op0 != TheLoad) return false;
2889
2890 switch (Op0.getValueType()) {
2891 default: return false;
2892 case MVT::i1:
2893 case MVT::i8: Opc = TabPtr[3]; break;
2894 case MVT::i16: Opc = TabPtr[4]; break;
2895 case MVT::i32: Opc = TabPtr[5]; break;
2896 }
Chris Lattner5c659812005-01-17 22:10:42 +00002897
Chris Lattnerb422aea2005-01-18 17:35:28 +00002898 // Table entry doesn't exist?
2899 if (Opc == 0) return false;
2900
Chris Lattner4a108662005-01-18 03:51:59 +00002901 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2902 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002903 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002904 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002905
Chris Lattnere10269b2005-01-17 19:25:26 +00002906 X86AddressMode AM;
2907 SelectAddress(TheLoad.getOperand(1), AM);
2908 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002909 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002910 return true;
2911}
2912
2913
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002914void ISel::Select(SDOperand N) {
2915 unsigned Tmp1, Tmp2, Opc;
2916
Nate Begeman85fdeb22005-03-24 04:39:54 +00002917 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002918 return; // Already selected.
2919
Chris Lattner989de032005-01-11 06:14:36 +00002920 SDNode *Node = N.Val;
2921
2922 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002923 default:
Chris Lattner989de032005-01-11 06:14:36 +00002924 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002925 assert(0 && "Node not handled yet!");
2926 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002927 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002928 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002929 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002930 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2931 Select(Node->getOperand(OneFirst));
2932 Select(Node->getOperand(!OneFirst));
2933 } else {
2934 std::vector<std::pair<unsigned, unsigned> > OpsP;
2935 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2936 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2937 std::sort(OpsP.begin(), OpsP.end());
2938 std::reverse(OpsP.begin(), OpsP.end());
2939 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2940 Select(Node->getOperand(OpsP[i].second));
2941 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002942 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002943 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002944 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2945 Select(N.getOperand(0));
2946 Tmp1 = SelectExpr(N.getOperand(1));
2947 } else {
2948 Tmp1 = SelectExpr(N.getOperand(1));
2949 Select(N.getOperand(0));
2950 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002951 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002952
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002953 if (Tmp1 != Tmp2) {
2954 switch (N.getOperand(1).getValueType()) {
2955 default: assert(0 && "Invalid type for operation!");
2956 case MVT::i1:
2957 case MVT::i8: Opc = X86::MOV8rr; break;
2958 case MVT::i16: Opc = X86::MOV16rr; break;
2959 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002960 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002961 }
2962 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2963 }
2964 return;
2965 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002966 switch (N.getNumOperands()) {
2967 default:
2968 assert(0 && "Unknown return instruction!");
2969 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002970 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2971 N.getOperand(2).getValueType() == MVT::i32 &&
2972 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002973 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2974 Tmp1 = SelectExpr(N.getOperand(1));
2975 Tmp2 = SelectExpr(N.getOperand(2));
2976 } else {
2977 Tmp2 = SelectExpr(N.getOperand(2));
2978 Tmp1 = SelectExpr(N.getOperand(1));
2979 }
2980 Select(N.getOperand(0));
2981
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002982 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2983 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002984 break;
2985 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002986 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2987 Select(N.getOperand(0));
2988 Tmp1 = SelectExpr(N.getOperand(1));
2989 } else {
2990 Tmp1 = SelectExpr(N.getOperand(1));
2991 Select(N.getOperand(0));
2992 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002993 switch (N.getOperand(1).getValueType()) {
2994 default: assert(0 && "All other types should have been promoted!!");
2995 case MVT::f64:
2996 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002997 break;
2998 case MVT::i32:
2999 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003000 break;
3001 }
3002 break;
3003 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00003004 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003005 break;
3006 }
3007 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3008 return;
3009 case ISD::BR: {
3010 Select(N.getOperand(0));
3011 MachineBasicBlock *Dest =
3012 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3013 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3014 return;
3015 }
3016
3017 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003018 MachineBasicBlock *Dest =
3019 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003020
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003021 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3022 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003023 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3024 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3025 Select(N.getOperand(0));
3026 Tmp1 = SelectExpr(N.getOperand(1));
3027 } else {
3028 Tmp1 = SelectExpr(N.getOperand(1));
3029 Select(N.getOperand(0));
3030 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003031 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3032 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3033 }
Chris Lattner11333092005-01-11 03:11:44 +00003034
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003035 return;
3036 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003037
Chris Lattner4df0de92005-01-17 00:00:33 +00003038 case ISD::LOAD:
3039 // If this load could be folded into the only using instruction, and if it
3040 // is safe to emit the instruction here, try to do so now.
3041 if (Node->hasNUsesOfValue(1, 0)) {
3042 SDOperand TheVal = N.getValue(0);
3043 SDNode *User = 0;
3044 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3045 assert(UI != Node->use_end() && "Didn't find use!");
3046 SDNode *UN = *UI;
3047 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3048 if (UN->getOperand(i) == TheVal) {
3049 User = UN;
3050 goto FoundIt;
3051 }
3052 }
3053 FoundIt:
3054 // Only handle unary operators right now.
3055 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003056 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003057 SelectExpr(SDOperand(User, 0));
3058 return;
3059 }
3060 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003061 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003062 SelectExpr(N);
3063 return;
3064
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003065 case ISD::EXTLOAD:
3066 case ISD::SEXTLOAD:
3067 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003068 case ISD::CALL:
3069 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003070 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003071 SelectExpr(N);
3072 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003073
3074 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3075 // On X86, we can represent all types except for Bool and Float natively.
3076 X86AddressMode AM;
3077 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003078 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3079 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3080 && "Unsupported TRUNCSTORE for this target!");
3081
3082 if (StoredTy == MVT::i16) {
3083 // FIXME: This is here just to allow testing. X86 doesn't really have a
3084 // TRUNCSTORE i16 operation, but this is required for targets that do not
3085 // have 16-bit integer registers. We occasionally disable 16-bit integer
3086 // registers to test the promotion code.
3087 Select(N.getOperand(0));
3088 Tmp1 = SelectExpr(N.getOperand(1));
3089 SelectAddress(N.getOperand(2), AM);
3090
3091 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3092 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3093 return;
3094 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003095
3096 // Store of constant bool?
3097 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3098 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3099 Select(N.getOperand(0));
3100 SelectAddress(N.getOperand(2), AM);
3101 } else {
3102 SelectAddress(N.getOperand(2), AM);
3103 Select(N.getOperand(0));
3104 }
3105 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3106 return;
3107 }
3108
3109 switch (StoredTy) {
3110 default: assert(0 && "Cannot truncstore this type!");
3111 case MVT::i1: Opc = X86::MOV8mr; break;
3112 case MVT::f32: Opc = X86::FST32m; break;
3113 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003114
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003115 std::vector<std::pair<unsigned, unsigned> > RP;
3116 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3117 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3118 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3119 std::sort(RP.begin(), RP.end());
3120
Chris Lattner572dd082005-02-23 05:57:21 +00003121 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003122 for (unsigned i = 0; i != 3; ++i)
3123 switch (RP[2-i].second) {
3124 default: assert(0 && "Unknown operand number!");
3125 case 0: Select(N.getOperand(0)); break;
3126 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3127 case 2: SelectAddress(N.getOperand(2), AM); break;
3128 }
3129
3130 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3131 return;
3132 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003133 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003134 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003135
3136 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3137 Opc = 0;
3138 switch (CN->getValueType(0)) {
3139 default: assert(0 && "Invalid type for operation!");
3140 case MVT::i1:
3141 case MVT::i8: Opc = X86::MOV8mi; break;
3142 case MVT::i16: Opc = X86::MOV16mi; break;
3143 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003144 case MVT::f64: break;
3145 }
3146 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003147 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3148 Select(N.getOperand(0));
3149 SelectAddress(N.getOperand(2), AM);
3150 } else {
3151 SelectAddress(N.getOperand(2), AM);
3152 Select(N.getOperand(0));
3153 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003154 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3155 return;
3156 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003157 } else if (GlobalAddressSDNode *GA =
3158 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3159 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3160
3161 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3162 Select(N.getOperand(0));
3163 SelectAddress(N.getOperand(2), AM);
3164 } else {
3165 SelectAddress(N.getOperand(2), AM);
3166 Select(N.getOperand(0));
3167 }
3168 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3169 AM).addGlobalAddress(GA->getGlobal());
3170 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003171 }
Chris Lattner837caa72005-01-11 23:21:30 +00003172
3173 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003174 if (TryToFoldLoadOpStore(Node))
3175 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003176
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003177 switch (N.getOperand(1).getValueType()) {
3178 default: assert(0 && "Cannot store this type!");
3179 case MVT::i1:
3180 case MVT::i8: Opc = X86::MOV8mr; break;
3181 case MVT::i16: Opc = X86::MOV16mr; break;
3182 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003183 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003184 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003185
Chris Lattner11333092005-01-11 03:11:44 +00003186 std::vector<std::pair<unsigned, unsigned> > RP;
3187 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3188 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3189 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3190 std::sort(RP.begin(), RP.end());
3191
Chris Lattner572dd082005-02-23 05:57:21 +00003192 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003193 for (unsigned i = 0; i != 3; ++i)
3194 switch (RP[2-i].second) {
3195 default: assert(0 && "Unknown operand number!");
3196 case 0: Select(N.getOperand(0)); break;
3197 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003198 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003199 }
3200
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003201 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3202 return;
3203 }
3204 case ISD::ADJCALLSTACKDOWN:
3205 case ISD::ADJCALLSTACKUP:
3206 Select(N.getOperand(0));
3207 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003208
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003209 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3210 X86::ADJCALLSTACKUP;
3211 BuildMI(BB, Opc, 1).addImm(Tmp1);
3212 return;
Chris Lattner989de032005-01-11 06:14:36 +00003213 case ISD::MEMSET: {
3214 Select(N.getOperand(0)); // Select the chain.
3215 unsigned Align =
3216 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3217 if (Align == 0) Align = 1;
3218
3219 // Turn the byte code into # iterations
3220 unsigned CountReg;
3221 unsigned Opcode;
3222 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3223 unsigned Val = ValC->getValue() & 255;
3224
3225 // If the value is a constant, then we can potentially use larger sets.
3226 switch (Align & 3) {
3227 case 2: // WORD aligned
3228 CountReg = MakeReg(MVT::i32);
3229 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3230 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3231 } else {
3232 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3233 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3234 }
3235 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3236 Opcode = X86::REP_STOSW;
3237 break;
3238 case 0: // DWORD aligned
3239 CountReg = MakeReg(MVT::i32);
3240 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3241 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3242 } else {
3243 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3244 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3245 }
3246 Val = (Val << 8) | Val;
3247 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3248 Opcode = X86::REP_STOSD;
3249 break;
3250 default: // BYTE aligned
3251 CountReg = SelectExpr(Node->getOperand(3));
3252 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3253 Opcode = X86::REP_STOSB;
3254 break;
3255 }
3256 } else {
3257 // If it's not a constant value we are storing, just fall back. We could
3258 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3259 unsigned ValReg = SelectExpr(Node->getOperand(2));
3260 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3261 CountReg = SelectExpr(Node->getOperand(3));
3262 Opcode = X86::REP_STOSB;
3263 }
3264
3265 // No matter what the alignment is, we put the source in ESI, the
3266 // destination in EDI, and the count in ECX.
3267 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3268 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3269 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3270 BuildMI(BB, Opcode, 0);
3271 return;
3272 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003273 case ISD::MEMCPY:
3274 Select(N.getOperand(0)); // Select the chain.
3275 unsigned Align =
3276 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3277 if (Align == 0) Align = 1;
3278
3279 // Turn the byte code into # iterations
3280 unsigned CountReg;
3281 unsigned Opcode;
3282 switch (Align & 3) {
3283 case 2: // WORD aligned
3284 CountReg = MakeReg(MVT::i32);
3285 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3286 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3287 } else {
3288 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3289 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3290 }
3291 Opcode = X86::REP_MOVSW;
3292 break;
3293 case 0: // DWORD aligned
3294 CountReg = MakeReg(MVT::i32);
3295 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3296 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3297 } else {
3298 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3299 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3300 }
3301 Opcode = X86::REP_MOVSD;
3302 break;
3303 default: // BYTE aligned
3304 CountReg = SelectExpr(Node->getOperand(3));
3305 Opcode = X86::REP_MOVSB;
3306 break;
3307 }
3308
3309 // No matter what the alignment is, we put the source in ESI, the
3310 // destination in EDI, and the count in ECX.
3311 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3312 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3313 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3314 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3315 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3316 BuildMI(BB, Opcode, 0);
3317 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003318 }
3319 assert(0 && "Should not be reached!");
3320}
3321
3322
3323/// createX86PatternInstructionSelector - This pass converts an LLVM function
3324/// into a machine code representation using pattern matching and a machine
3325/// description file.
3326///
3327FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003328 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003329}