blob: 600e06895b44fb0d9a7b4650280c603e90ecae0a [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())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000155 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
156 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000157 else {
158 if (MVT::isInteger(ObjectVT))
159 ArgValue = DAG.getConstant(0, ObjectVT);
160 else
161 ArgValue = DAG.getConstantFP(0, ObjectVT);
162 }
163 ArgValues.push_back(ArgValue);
164
165 ArgOffset += ArgIncrement; // Move on to the next argument...
166 }
167
168 // If the function takes variable number of arguments, make a frame index for
169 // the start of the first vararg value... for expansion of llvm.va_start.
170 if (F.isVarArg())
171 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000172 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000173
174 // Finally, inform the code generator which regs we return values in.
175 switch (getValueType(F.getReturnType())) {
176 default: assert(0 && "Unknown type!");
177 case MVT::isVoid: break;
178 case MVT::i1:
179 case MVT::i8:
180 case MVT::i16:
181 case MVT::i32:
182 MF.addLiveOut(X86::EAX);
183 break;
184 case MVT::i64:
185 MF.addLiveOut(X86::EAX);
186 MF.addLiveOut(X86::EDX);
187 break;
188 case MVT::f32:
189 case MVT::f64:
190 MF.addLiveOut(X86::ST0);
191 break;
192 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000193 return ArgValues;
194}
195
Chris Lattner5188ad72005-01-08 19:28:19 +0000196std::pair<SDOperand, SDOperand>
197X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000198 const Type *RetTy, bool isVarArg,
199 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000200 // Count how many bytes are to be pushed on the stack.
201 unsigned NumBytes = 0;
202
203 if (Args.empty()) {
204 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000205 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
206 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 } else {
208 for (unsigned i = 0, e = Args.size(); i != e; ++i)
209 switch (getValueType(Args[i].second)) {
210 default: assert(0 && "Unknown value type!");
211 case MVT::i1:
212 case MVT::i8:
213 case MVT::i16:
214 case MVT::i32:
215 case MVT::f32:
216 NumBytes += 4;
217 break;
218 case MVT::i64:
219 case MVT::f64:
220 NumBytes += 8;
221 break;
222 }
223
Chris Lattner5188ad72005-01-08 19:28:19 +0000224 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
225 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000226
227 // Arguments go on the stack in reverse order, as specified by the ABI.
228 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000229 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
230 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000231 std::vector<SDOperand> Stores;
232
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000233 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
234 unsigned ArgReg;
235 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
236 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
237
238 switch (getValueType(Args[i].second)) {
239 default: assert(0 && "Unexpected ValueType for argument!");
240 case MVT::i1:
241 case MVT::i8:
242 case MVT::i16:
243 // Promote the integer to 32 bits. If the input type is signed use a
244 // sign extend, otherwise use a zero extend.
245 if (Args[i].second->isSigned())
246 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
247 else
248 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
249
250 // FALL THROUGH
251 case MVT::i32:
252 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000253 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000254 Args[i].first, PtrOff,
255 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000256 ArgOffset += 4;
257 break;
258 case MVT::i64:
259 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000260 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000261 Args[i].first, PtrOff,
262 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000263 ArgOffset += 8;
264 break;
265 }
266 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000267 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000268 }
269
270 std::vector<MVT::ValueType> RetVals;
271 MVT::ValueType RetTyVT = getValueType(RetTy);
272 if (RetTyVT != MVT::isVoid)
273 RetVals.push_back(RetTyVT);
274 RetVals.push_back(MVT::Other);
275
Chris Lattner5188ad72005-01-08 19:28:19 +0000276 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000277 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000278 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
279 DAG.getConstant(NumBytes, getPointerTy()));
280 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000281}
282
Chris Lattner14824582005-01-09 00:01:27 +0000283std::pair<SDOperand, SDOperand>
284X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
285 // vastart just returns the address of the VarArgsFrameIndex slot.
286 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
287}
288
289std::pair<SDOperand,SDOperand> X86TargetLowering::
290LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
291 const Type *ArgTy, SelectionDAG &DAG) {
292 MVT::ValueType ArgVT = getValueType(ArgTy);
293 SDOperand Result;
294 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000295 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
296 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000297 } else {
298 unsigned Amt;
299 if (ArgVT == MVT::i32)
300 Amt = 4;
301 else {
302 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
303 "Other types should have been promoted for varargs!");
304 Amt = 8;
305 }
306 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
307 DAG.getConstant(Amt, VAList.getValueType()));
308 }
309 return std::make_pair(Result, Chain);
310}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000311
Chris Lattner14824582005-01-09 00:01:27 +0000312
313std::pair<SDOperand, SDOperand> X86TargetLowering::
314LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
315 SelectionDAG &DAG) {
316 SDOperand Result;
317 if (Depth) // Depths > 0 not supported yet!
318 Result = DAG.getConstant(0, getPointerTy());
319 else {
320 if (ReturnAddrIndex == 0) {
321 // Set up a frame object for the return address.
322 MachineFunction &MF = DAG.getMachineFunction();
323 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
324 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000325
Chris Lattner14824582005-01-09 00:01:27 +0000326 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
327
328 if (!isFrameAddress)
329 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000330 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000331 else
332 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
333 DAG.getConstant(4, MVT::i32));
334 }
335 return std::make_pair(Result, Chain);
336}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000337
338
Chris Lattner98a8ba02005-01-18 01:06:26 +0000339namespace {
340 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
341 /// SDOperand's instead of register numbers for the leaves of the matched
342 /// tree.
343 struct X86ISelAddressMode {
344 enum {
345 RegBase,
346 FrameIndexBase,
347 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000348
Chris Lattner98a8ba02005-01-18 01:06:26 +0000349 struct { // This is really a union, discriminated by BaseType!
350 SDOperand Reg;
351 int FrameIndex;
352 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000353
Chris Lattner98a8ba02005-01-18 01:06:26 +0000354 unsigned Scale;
355 SDOperand IndexReg;
356 unsigned Disp;
357 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000358
Chris Lattner98a8ba02005-01-18 01:06:26 +0000359 X86ISelAddressMode()
360 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
361 }
362 };
363}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000364
365
366namespace {
367 Statistic<>
368 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
369
370 //===--------------------------------------------------------------------===//
371 /// ISel - X86 specific code to select X86 machine instructions for
372 /// SelectionDAG operations.
373 ///
374 class ISel : public SelectionDAGISel {
375 /// ContainsFPCode - Every instruction we select that uses or defines a FP
376 /// register should set this to true.
377 bool ContainsFPCode;
378
379 /// X86Lowering - This object fully describes how to lower LLVM code to an
380 /// X86-specific SelectionDAG.
381 X86TargetLowering X86Lowering;
382
Chris Lattner11333092005-01-11 03:11:44 +0000383 /// RegPressureMap - This keeps an approximate count of the number of
384 /// registers required to evaluate each node in the graph.
385 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000386
387 /// ExprMap - As shared expressions are codegen'd, we keep track of which
388 /// vreg the value is produced in, so we only emit one copy of each compiled
389 /// tree.
390 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000391
392 public:
393 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
394 }
395
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000396 virtual const char *getPassName() const {
397 return "X86 Pattern Instruction Selection";
398 }
399
Chris Lattner11333092005-01-11 03:11:44 +0000400 unsigned getRegPressure(SDOperand O) {
401 return RegPressureMap[O.Val];
402 }
403 unsigned ComputeRegPressure(SDOperand O);
404
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000405 /// InstructionSelectBasicBlock - This callback is invoked by
406 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000407 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000408
Chris Lattner44129b52005-01-25 20:03:11 +0000409 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
410 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000411 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000412 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000413
Chris Lattner30ea1e92005-01-19 07:37:26 +0000414 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000415 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000416 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000417 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
418 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000419 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000420
421 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
422 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
423 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000424 void Select(SDOperand N);
425 };
426}
427
Chris Lattner7dbcb752005-01-12 04:21:28 +0000428/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
429/// when it has created a SelectionDAG for us to codegen.
430void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
431 // While we're doing this, keep track of whether we see any FP code for
432 // FP_REG_KILL insertion.
433 ContainsFPCode = false;
434
435 // Scan the PHI nodes that already are inserted into this basic block. If any
436 // of them is a PHI of a floating point value, we need to insert an
437 // FP_REG_KILL.
438 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
439 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
440 I != E; ++I) {
441 assert(I->getOpcode() == X86::PHI &&
442 "Isn't just PHI nodes?");
443 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
444 X86::RFPRegisterClass) {
445 ContainsFPCode = true;
446 break;
447 }
448 }
449
450 // Compute the RegPressureMap, which is an approximation for the number of
451 // registers required to compute each node.
452 ComputeRegPressure(DAG.getRoot());
453
454 // Codegen the basic block.
455 Select(DAG.getRoot());
456
457 // Finally, look at all of the successors of this block. If any contain a PHI
458 // node of FP type, we need to insert an FP_REG_KILL in this block.
459 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
460 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
461 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
462 I != E && I->getOpcode() == X86::PHI; ++I) {
463 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
464 X86::RFPRegisterClass) {
465 ContainsFPCode = true;
466 break;
467 }
468 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000469
Chris Lattnere3e0f272005-05-09 03:36:39 +0000470 // Final check, check LLVM BB's that are successors to the LLVM BB
471 // corresponding to BB for FP PHI nodes.
472 const BasicBlock *LLVMBB = BB->getBasicBlock();
473 const PHINode *PN;
474 if (!ContainsFPCode)
475 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
476 SI != E && !ContainsFPCode; ++SI)
477 for (BasicBlock::const_iterator II = SI->begin();
478 (PN = dyn_cast<PHINode>(II)); ++II)
479 if (PN->getType()->isFloatingPoint()) {
480 ContainsFPCode = true;
481 break;
482 }
483
484
Chris Lattner7dbcb752005-01-12 04:21:28 +0000485 // Insert FP_REG_KILL instructions into basic blocks that need them. This
486 // only occurs due to the floating point stackifier not being aggressive
487 // enough to handle arbitrary global stackification.
488 //
489 // Currently we insert an FP_REG_KILL instruction into each block that uses or
490 // defines a floating point virtual register.
491 //
492 // When the global register allocators (like linear scan) finally update live
493 // variable analysis, we can keep floating point values in registers across
494 // basic blocks. This will be a huge win, but we are waiting on the global
495 // allocators before we can do this.
496 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000497 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000498 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
499 ++NumFPKill;
500 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000501
Chris Lattner7dbcb752005-01-12 04:21:28 +0000502 // Clear state used for selection.
503 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000504 RegPressureMap.clear();
505}
506
507
Chris Lattner11333092005-01-11 03:11:44 +0000508// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
509// for the number of registers required to compute each node. This is basically
510// computing a generalized form of the Sethi-Ullman number for each node.
511unsigned ISel::ComputeRegPressure(SDOperand O) {
512 SDNode *N = O.Val;
513 unsigned &Result = RegPressureMap[N];
514 if (Result) return Result;
515
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000516 // FIXME: Should operations like CALL (which clobber lots o regs) have a
517 // higher fixed cost??
518
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000519 if (N->getNumOperands() == 0) {
520 Result = 1;
521 } else {
522 unsigned MaxRegUse = 0;
523 unsigned NumExtraMaxRegUsers = 0;
524 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
525 unsigned Regs;
526 if (N->getOperand(i).getOpcode() == ISD::Constant)
527 Regs = 0;
528 else
529 Regs = ComputeRegPressure(N->getOperand(i));
530 if (Regs > MaxRegUse) {
531 MaxRegUse = Regs;
532 NumExtraMaxRegUsers = 0;
533 } else if (Regs == MaxRegUse &&
534 N->getOperand(i).getValueType() != MVT::Other) {
535 ++NumExtraMaxRegUsers;
536 }
Chris Lattner11333092005-01-11 03:11:44 +0000537 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000538
539 if (O.getOpcode() != ISD::TokenFactor)
540 Result = MaxRegUse+NumExtraMaxRegUsers;
541 else
Chris Lattner869e0432005-01-17 23:02:13 +0000542 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000543 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000544
Chris Lattner837caa72005-01-11 23:21:30 +0000545 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000546 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000547}
548
Chris Lattnerbf52d492005-01-20 16:50:16 +0000549/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
550/// The DAG cannot have cycles in it, by definition, so the visited set is not
551/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
552/// reuse, so it prevents exponential cases.
553///
554static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
555 std::set<SDNode*> &Visited) {
556 if (N == Op) return true; // Found it.
557 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000558 if (Node->getNumOperands() == 0 || // Leaf?
559 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000560 if (!Visited.insert(Node).second) return false; // Already visited?
561
562 // Recurse for the first N-1 operands.
563 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
564 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
565 return true;
566
567 // Tail recurse for the last operand.
568 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
569}
570
Chris Lattner98a8ba02005-01-18 01:06:26 +0000571X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
572 X86AddressMode Result;
573
574 // If we need to emit two register operands, emit the one with the highest
575 // register pressure first.
576 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
577 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000578 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000579 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000580 std::set<SDNode*> Visited;
581 EmitBaseThenIndex = true;
582 // If Base ends up pointing to Index, we must emit index first. This is
583 // because of the way we fold loads, we may end up doing bad things with
584 // the folded add.
585 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
586 EmitBaseThenIndex = false;
587 } else {
588 std::set<SDNode*> Visited;
589 EmitBaseThenIndex = false;
590 // If Base ends up pointing to Index, we must emit index first. This is
591 // because of the way we fold loads, we may end up doing bad things with
592 // the folded add.
593 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
594 EmitBaseThenIndex = true;
595 }
596
597 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000598 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
599 Result.IndexReg = SelectExpr(IAM.IndexReg);
600 } else {
601 Result.IndexReg = SelectExpr(IAM.IndexReg);
602 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
603 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000604
Chris Lattner98a8ba02005-01-18 01:06:26 +0000605 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
606 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
607 } else if (IAM.IndexReg.Val) {
608 Result.IndexReg = SelectExpr(IAM.IndexReg);
609 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000610
Chris Lattner98a8ba02005-01-18 01:06:26 +0000611 switch (IAM.BaseType) {
612 case X86ISelAddressMode::RegBase:
613 Result.BaseType = X86AddressMode::RegBase;
614 break;
615 case X86ISelAddressMode::FrameIndexBase:
616 Result.BaseType = X86AddressMode::FrameIndexBase;
617 Result.Base.FrameIndex = IAM.Base.FrameIndex;
618 break;
619 default:
620 assert(0 && "Unknown base type!");
621 break;
622 }
623 Result.Scale = IAM.Scale;
624 Result.Disp = IAM.Disp;
625 Result.GV = IAM.GV;
626 return Result;
627}
628
629/// SelectAddress - Pattern match the maximal addressing mode for this node and
630/// emit all of the leaf registers.
631void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
632 X86ISelAddressMode IAM;
633 MatchAddress(N, IAM);
634 AM = SelectAddrExprs(IAM);
635}
636
637/// MatchAddress - Add the specified node to the specified addressing mode,
638/// returning true if it cannot be done. This just pattern matches for the
639/// addressing mode, it does not cause any code to be emitted. For that, use
640/// SelectAddress.
641bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000642 switch (N.getOpcode()) {
643 default: break;
644 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000645 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
646 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000647 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
648 return false;
649 }
650 break;
651 case ISD::GlobalAddress:
652 if (AM.GV == 0) {
653 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
654 return false;
655 }
656 break;
657 case ISD::Constant:
658 AM.Disp += cast<ConstantSDNode>(N)->getValue();
659 return false;
660 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000661 // We might have folded the load into this shift, so don't regen the value
662 // if so.
663 if (ExprMap.count(N)) break;
664
Chris Lattner98a8ba02005-01-18 01:06:26 +0000665 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000666 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
667 unsigned Val = CN->getValue();
668 if (Val == 1 || Val == 2 || Val == 3) {
669 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000670 SDOperand ShVal = N.Val->getOperand(0);
671
672 // Okay, we know that we have a scale by now. However, if the scaled
673 // value is an add of something and a constant, we can fold the
674 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000675 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000676 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000677 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000678 ConstantSDNode *AddVal =
679 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
680 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000681 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000682 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000683 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000684 return false;
685 }
686 }
687 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000688 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000689 // We might have folded the load into this mul, so don't regen the value if
690 // so.
691 if (ExprMap.count(N)) break;
692
Chris Lattner947d5442005-01-11 19:37:02 +0000693 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000694 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
695 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000696 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
697 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
698 AM.Scale = unsigned(CN->getValue())-1;
699
700 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000701 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000702
703 // Okay, we know that we have a scale by now. However, if the scaled
704 // value is an add of something and a constant, we can fold the
705 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000706 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000707 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000708 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000709 ConstantSDNode *AddVal =
710 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
711 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000712 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000713 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000714 }
715
716 AM.IndexReg = AM.Base.Reg = Reg;
717 return false;
718 }
719 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000720
721 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000722 // We might have folded the load into this mul, so don't regen the value if
723 // so.
724 if (ExprMap.count(N)) break;
725
Chris Lattner98a8ba02005-01-18 01:06:26 +0000726 X86ISelAddressMode Backup = AM;
727 if (!MatchAddress(N.Val->getOperand(0), AM) &&
728 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000729 return false;
730 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000731 if (!MatchAddress(N.Val->getOperand(1), AM) &&
732 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000733 return false;
734 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000735 break;
736 }
737 }
738
Chris Lattnera95589b2005-01-11 04:40:19 +0000739 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000740 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000741 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000742 if (AM.IndexReg.Val == 0) {
743 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000744 AM.Scale = 1;
745 return false;
746 }
747
748 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000749 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000750 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000751
752 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000753 AM.BaseType = X86ISelAddressMode::RegBase;
754 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000755 return false;
756}
757
758/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
759/// assuming that the temporary registers are in the 8-bit register class.
760///
761/// Tmp1 = setcc1
762/// Tmp2 = setcc2
763/// DestReg = logicalop Tmp1, Tmp2
764///
765static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
766 unsigned SetCC2, unsigned LogicalOp,
767 unsigned DestReg) {
768 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
769 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
770 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
771 BuildMI(BB, SetCC1, 0, Tmp1);
772 BuildMI(BB, SetCC2, 0, Tmp2);
773 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
774}
775
776/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
777/// condition codes match the specified SetCCOpcode. Note that some conditions
778/// require multiple instructions to generate the correct value.
779static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
780 ISD::CondCode SetCCOpcode, bool isFP) {
781 unsigned Opc;
782 if (!isFP) {
783 switch (SetCCOpcode) {
784 default: assert(0 && "Illegal integer SetCC!");
785 case ISD::SETEQ: Opc = X86::SETEr; break;
786 case ISD::SETGT: Opc = X86::SETGr; break;
787 case ISD::SETGE: Opc = X86::SETGEr; break;
788 case ISD::SETLT: Opc = X86::SETLr; break;
789 case ISD::SETLE: Opc = X86::SETLEr; break;
790 case ISD::SETNE: Opc = X86::SETNEr; break;
791 case ISD::SETULT: Opc = X86::SETBr; break;
792 case ISD::SETUGT: Opc = X86::SETAr; break;
793 case ISD::SETULE: Opc = X86::SETBEr; break;
794 case ISD::SETUGE: Opc = X86::SETAEr; break;
795 }
796 } else {
797 // On a floating point condition, the flags are set as follows:
798 // ZF PF CF op
799 // 0 | 0 | 0 | X > Y
800 // 0 | 0 | 1 | X < Y
801 // 1 | 0 | 0 | X == Y
802 // 1 | 1 | 1 | unordered
803 //
804 switch (SetCCOpcode) {
805 default: assert(0 && "Invalid FP setcc!");
806 case ISD::SETUEQ:
807 case ISD::SETEQ:
808 Opc = X86::SETEr; // True if ZF = 1
809 break;
810 case ISD::SETOGT:
811 case ISD::SETGT:
812 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
813 break;
814 case ISD::SETOGE:
815 case ISD::SETGE:
816 Opc = X86::SETAEr; // True if CF = 0
817 break;
818 case ISD::SETULT:
819 case ISD::SETLT:
820 Opc = X86::SETBr; // True if CF = 1
821 break;
822 case ISD::SETULE:
823 case ISD::SETLE:
824 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
825 break;
826 case ISD::SETONE:
827 case ISD::SETNE:
828 Opc = X86::SETNEr; // True if ZF = 0
829 break;
830 case ISD::SETUO:
831 Opc = X86::SETPr; // True if PF = 1
832 break;
833 case ISD::SETO:
834 Opc = X86::SETNPr; // True if PF = 0
835 break;
836 case ISD::SETOEQ: // !PF & ZF
837 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
838 return;
839 case ISD::SETOLT: // !PF & CF
840 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
841 return;
842 case ISD::SETOLE: // !PF & (CF || ZF)
843 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
844 return;
845 case ISD::SETUGT: // PF | (!ZF & !CF)
846 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
847 return;
848 case ISD::SETUGE: // PF | !CF
849 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
850 return;
851 case ISD::SETUNE: // PF | !ZF
852 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
853 return;
854 }
855 }
856 BuildMI(BB, Opc, 0, DestReg);
857}
858
859
860/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
861/// the Dest block if the Cond condition is true. If we cannot fold this
862/// condition into the branch, return true.
863///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000864bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
865 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000866 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
867 // B) using two conditional branches instead of one condbr, two setcc's, and
868 // an or.
869 if ((Cond.getOpcode() == ISD::OR ||
870 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
871 // And and or set the flags for us, so there is no need to emit a TST of the
872 // result. It is only safe to do this if there is only a single use of the
873 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000874 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000875 SelectExpr(Cond);
876 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
877 return false;
878 }
879
880 // Codegen br not C -> JE.
881 if (Cond.getOpcode() == ISD::XOR)
882 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
883 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000884 unsigned CondR;
885 if (getRegPressure(Chain) > getRegPressure(Cond)) {
886 Select(Chain);
887 CondR = SelectExpr(Cond.Val->getOperand(0));
888 } else {
889 CondR = SelectExpr(Cond.Val->getOperand(0));
890 Select(Chain);
891 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000892 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
893 BuildMI(BB, X86::JE, 1).addMBB(Dest);
894 return false;
895 }
896
897 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
898 if (SetCC == 0)
899 return true; // Can only handle simple setcc's so far.
900
901 unsigned Opc;
902
903 // Handle integer conditions first.
904 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
905 switch (SetCC->getCondition()) {
906 default: assert(0 && "Illegal integer SetCC!");
907 case ISD::SETEQ: Opc = X86::JE; break;
908 case ISD::SETGT: Opc = X86::JG; break;
909 case ISD::SETGE: Opc = X86::JGE; break;
910 case ISD::SETLT: Opc = X86::JL; break;
911 case ISD::SETLE: Opc = X86::JLE; break;
912 case ISD::SETNE: Opc = X86::JNE; break;
913 case ISD::SETULT: Opc = X86::JB; break;
914 case ISD::SETUGT: Opc = X86::JA; break;
915 case ISD::SETULE: Opc = X86::JBE; break;
916 case ISD::SETUGE: Opc = X86::JAE; break;
917 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000918 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000919 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000920 BuildMI(BB, Opc, 1).addMBB(Dest);
921 return false;
922 }
923
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000924 unsigned Opc2 = 0; // Second branch if needed.
925
926 // On a floating point condition, the flags are set as follows:
927 // ZF PF CF op
928 // 0 | 0 | 0 | X > Y
929 // 0 | 0 | 1 | X < Y
930 // 1 | 0 | 0 | X == Y
931 // 1 | 1 | 1 | unordered
932 //
933 switch (SetCC->getCondition()) {
934 default: assert(0 && "Invalid FP setcc!");
935 case ISD::SETUEQ:
936 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
937 case ISD::SETOGT:
938 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
939 case ISD::SETOGE:
940 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
941 case ISD::SETULT:
942 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
943 case ISD::SETULE:
944 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
945 case ISD::SETONE:
946 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
947 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
948 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
949 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
950 Opc = X86::JA; // ZF = 0 & CF = 0
951 Opc2 = X86::JP; // PF = 1
952 break;
953 case ISD::SETUGE: // PF = 1 | CF = 0
954 Opc = X86::JAE; // CF = 0
955 Opc2 = X86::JP; // PF = 1
956 break;
957 case ISD::SETUNE: // PF = 1 | ZF = 0
958 Opc = X86::JNE; // ZF = 0
959 Opc2 = X86::JP; // PF = 1
960 break;
961 case ISD::SETOEQ: // PF = 0 & ZF = 1
962 //X86::JNP, X86::JE
963 //X86::AND8rr
964 return true; // FIXME: Emit more efficient code for this branch.
965 case ISD::SETOLT: // PF = 0 & CF = 1
966 //X86::JNP, X86::JB
967 //X86::AND8rr
968 return true; // FIXME: Emit more efficient code for this branch.
969 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
970 //X86::JNP, X86::JBE
971 //X86::AND8rr
972 return true; // FIXME: Emit more efficient code for this branch.
973 }
974
Chris Lattner6c07aee2005-01-11 04:06:27 +0000975 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000976 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000977 BuildMI(BB, Opc, 1).addMBB(Dest);
978 if (Opc2)
979 BuildMI(BB, Opc2, 1).addMBB(Dest);
980 return false;
981}
982
Chris Lattner24aad1b2005-01-10 22:10:13 +0000983/// EmitSelectCC - Emit code into BB that performs a select operation between
984/// the two registers RTrue and RFalse, generating a result into RDest. Return
985/// true if the fold cannot be performed.
986///
987void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
988 unsigned RTrue, unsigned RFalse, unsigned RDest) {
989 enum Condition {
990 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
991 NOT_SET
992 } CondCode = NOT_SET;
993
994 static const unsigned CMOVTAB16[] = {
995 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
996 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000997 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000998 };
999 static const unsigned CMOVTAB32[] = {
1000 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1001 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001002 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001003 };
1004 static const unsigned CMOVTABFP[] = {
1005 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1006 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1007 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1008 };
1009
1010 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1011 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1012 switch (SetCC->getCondition()) {
1013 default: assert(0 && "Unknown integer comparison!");
1014 case ISD::SETEQ: CondCode = EQ; break;
1015 case ISD::SETGT: CondCode = GT; break;
1016 case ISD::SETGE: CondCode = GE; break;
1017 case ISD::SETLT: CondCode = LT; break;
1018 case ISD::SETLE: CondCode = LE; break;
1019 case ISD::SETNE: CondCode = NE; break;
1020 case ISD::SETULT: CondCode = B; break;
1021 case ISD::SETUGT: CondCode = A; break;
1022 case ISD::SETULE: CondCode = BE; break;
1023 case ISD::SETUGE: CondCode = AE; break;
1024 }
1025 } else {
1026 // On a floating point condition, the flags are set as follows:
1027 // ZF PF CF op
1028 // 0 | 0 | 0 | X > Y
1029 // 0 | 0 | 1 | X < Y
1030 // 1 | 0 | 0 | X == Y
1031 // 1 | 1 | 1 | unordered
1032 //
1033 switch (SetCC->getCondition()) {
1034 default: assert(0 && "Unknown FP comparison!");
1035 case ISD::SETUEQ:
1036 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1037 case ISD::SETOGT:
1038 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1039 case ISD::SETOGE:
1040 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1041 case ISD::SETULT:
1042 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1043 case ISD::SETULE:
1044 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1045 case ISD::SETONE:
1046 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1047 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1048 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1049 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1050 case ISD::SETUGE: // PF = 1 | CF = 0
1051 case ISD::SETUNE: // PF = 1 | ZF = 0
1052 case ISD::SETOEQ: // PF = 0 & ZF = 1
1053 case ISD::SETOLT: // PF = 0 & CF = 1
1054 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1055 // We cannot emit this comparison as a single cmov.
1056 break;
1057 }
1058 }
1059 }
1060
1061 unsigned Opc = 0;
1062 if (CondCode != NOT_SET) {
1063 switch (SVT) {
1064 default: assert(0 && "Cannot select this type!");
1065 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1066 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001067 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001068 }
1069 }
1070
1071 // Finally, if we weren't able to fold this, just emit the condition and test
1072 // it.
1073 if (CondCode == NOT_SET || Opc == 0) {
1074 // Get the condition into the zero flag.
1075 unsigned CondReg = SelectExpr(Cond);
1076 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1077
1078 switch (SVT) {
1079 default: assert(0 && "Cannot select this type!");
1080 case MVT::i16: Opc = X86::CMOVE16rr; break;
1081 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001082 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001083 }
1084 } else {
1085 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001086 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001087 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001088 }
1089 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1090}
1091
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001092void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001093 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001094 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1095 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001096 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001097 switch (RHS.getValueType()) {
1098 default: break;
1099 case MVT::i1:
1100 case MVT::i8: Opc = X86::CMP8mi; break;
1101 case MVT::i16: Opc = X86::CMP16mi; break;
1102 case MVT::i32: Opc = X86::CMP32mi; break;
1103 }
1104 if (Opc) {
1105 X86AddressMode AM;
1106 EmitFoldedLoad(LHS, AM);
1107 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1108 return;
1109 }
1110 }
1111
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001112 switch (RHS.getValueType()) {
1113 default: break;
1114 case MVT::i1:
1115 case MVT::i8: Opc = X86::CMP8ri; break;
1116 case MVT::i16: Opc = X86::CMP16ri; break;
1117 case MVT::i32: Opc = X86::CMP32ri; break;
1118 }
1119 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001120 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001121 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1122 return;
1123 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001124 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1125 if (CN->isExactlyValue(+0.0) ||
1126 CN->isExactlyValue(-0.0)) {
1127 unsigned Reg = SelectExpr(LHS);
1128 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1129 BuildMI(BB, X86::FNSTSW8r, 0);
1130 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001131 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001132 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001133 }
1134
Chris Lattneref6806c2005-01-12 02:02:48 +00001135 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001136 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001137 switch (RHS.getValueType()) {
1138 default: break;
1139 case MVT::i1:
1140 case MVT::i8: Opc = X86::CMP8mr; break;
1141 case MVT::i16: Opc = X86::CMP16mr; break;
1142 case MVT::i32: Opc = X86::CMP32mr; break;
1143 }
1144 if (Opc) {
1145 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001146 EmitFoldedLoad(LHS, AM);
1147 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001148 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1149 return;
1150 }
1151 }
1152
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001153 switch (LHS.getValueType()) {
1154 default: assert(0 && "Cannot compare this value!");
1155 case MVT::i1:
1156 case MVT::i8: Opc = X86::CMP8rr; break;
1157 case MVT::i16: Opc = X86::CMP16rr; break;
1158 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001159 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001160 }
Chris Lattner11333092005-01-11 03:11:44 +00001161 unsigned Tmp1, Tmp2;
1162 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1163 Tmp1 = SelectExpr(LHS);
1164 Tmp2 = SelectExpr(RHS);
1165 } else {
1166 Tmp2 = SelectExpr(RHS);
1167 Tmp1 = SelectExpr(LHS);
1168 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001169 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1170}
1171
Chris Lattnera5ade062005-01-11 21:19:59 +00001172/// isFoldableLoad - Return true if this is a load instruction that can safely
1173/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001174bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1175 if (Op.getOpcode() == ISD::LOAD) {
1176 // FIXME: currently can't fold constant pool indexes.
1177 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1178 return false;
1179 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1180 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1181 // FIXME: currently can't fold constant pool indexes.
1182 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1183 return false;
1184 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001185 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001186 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001187
1188 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001189 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1190 if (ExprMap.count(Op.getValue(1))) return false;
1191 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001192 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001193
Chris Lattner4ff348b2005-01-17 06:26:58 +00001194 // If there is not just one use of its value, we cannot fold.
1195 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1196
1197 // Finally, we cannot fold the load into the operation if this would induce a
1198 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1199 // operand of the operation we are folding the load into) can possible use the
1200 // chain node defined by the load.
1201 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1202 std::set<SDNode*> Visited;
1203 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1204 return false;
1205 }
1206 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001207}
1208
Chris Lattner4ff348b2005-01-17 06:26:58 +00001209
Chris Lattnera5ade062005-01-11 21:19:59 +00001210/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1211/// and compute the address being loaded into AM.
1212void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1213 SDOperand Chain = Op.getOperand(0);
1214 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001215
Chris Lattnera5ade062005-01-11 21:19:59 +00001216 if (getRegPressure(Chain) > getRegPressure(Address)) {
1217 Select(Chain);
1218 SelectAddress(Address, AM);
1219 } else {
1220 SelectAddress(Address, AM);
1221 Select(Chain);
1222 }
1223
1224 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001225 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1226 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001227 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001228 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001229}
1230
Chris Lattner30ea1e92005-01-19 07:37:26 +00001231// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1232// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1233// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1234// return true.
1235bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001236 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1237 // good!
1238 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1239 std::swap(Op1, Op2); // Op1 is the SHL now.
1240 } else {
1241 return false; // No match
1242 }
1243
1244 SDOperand ShlVal = Op1.getOperand(0);
1245 SDOperand ShlAmt = Op1.getOperand(1);
1246 SDOperand ShrVal = Op2.getOperand(0);
1247 SDOperand ShrAmt = Op2.getOperand(1);
1248
Chris Lattner30ea1e92005-01-19 07:37:26 +00001249 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1250
Chris Lattner85716372005-01-19 06:18:43 +00001251 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1252 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1253 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001254 if (SubCST->getValue() == RegSize) {
1255 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001256 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001257 if (ShrVal == ShlVal) {
1258 unsigned Reg, ShAmt;
1259 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1260 Reg = SelectExpr(ShrVal);
1261 ShAmt = SelectExpr(ShrAmt);
1262 } else {
1263 ShAmt = SelectExpr(ShrAmt);
1264 Reg = SelectExpr(ShrVal);
1265 }
1266 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1267 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1268 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1269 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1270 return true;
1271 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001272 unsigned AReg, BReg;
1273 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001274 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001275 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001276 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001277 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001278 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001279 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001280 unsigned ShAmt = SelectExpr(ShrAmt);
1281 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1282 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1283 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001284 return true;
1285 }
1286 }
1287
Chris Lattner4053b1e2005-01-19 08:07:05 +00001288 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1289 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1290 if (SubCST->getValue() == RegSize) {
1291 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1292 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1293 if (ShrVal == ShlVal) {
1294 unsigned Reg, ShAmt;
1295 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1296 Reg = SelectExpr(ShrVal);
1297 ShAmt = SelectExpr(ShlAmt);
1298 } else {
1299 ShAmt = SelectExpr(ShlAmt);
1300 Reg = SelectExpr(ShrVal);
1301 }
1302 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1303 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1304 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1305 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1306 return true;
1307 } else if (RegSize != 8) {
1308 unsigned AReg, BReg;
1309 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001310 AReg = SelectExpr(ShlVal);
1311 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001312 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001313 BReg = SelectExpr(ShrVal);
1314 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001315 }
1316 unsigned ShAmt = SelectExpr(ShlAmt);
1317 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1318 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1319 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1320 return true;
1321 }
1322 }
Chris Lattner85716372005-01-19 06:18:43 +00001323
Chris Lattner4053b1e2005-01-19 08:07:05 +00001324 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1325 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1326 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1327 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1328 // (A >> 5) | (A << 27) --> ROR A, 5
1329 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1330 if (ShrVal == ShlVal) {
1331 unsigned Reg = SelectExpr(ShrVal);
1332 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1333 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1334 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1335 return true;
1336 } else if (RegSize != 8) {
1337 unsigned AReg, BReg;
1338 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001339 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001340 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001341 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001342 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001343 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001344 }
1345 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1346 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1347 .addImm(ShrCst->getValue());
1348 return true;
1349 }
1350 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001351
Chris Lattner85716372005-01-19 06:18:43 +00001352 return false;
1353}
1354
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001355unsigned ISel::SelectExpr(SDOperand N) {
1356 unsigned Result;
1357 unsigned Tmp1, Tmp2, Tmp3;
1358 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001359 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001360 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001361
Chris Lattner7f2afac2005-01-14 22:37:41 +00001362 if (Node->getOpcode() == ISD::CopyFromReg) {
1363 // FIXME: Handle copy from physregs!
1364
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001365 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001366 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001367 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001368
Chris Lattnera5ade062005-01-11 21:19:59 +00001369 unsigned &Reg = ExprMap[N];
1370 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001371
Chris Lattnerb38a7492005-04-02 04:01:14 +00001372 switch (N.getOpcode()) {
1373 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001374 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001375 MakeReg(N.getValueType()) : 1;
1376 break;
1377 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001378 // If this is a call instruction, make sure to prepare ALL of the result
1379 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001380 if (Node->getNumValues() == 1)
1381 Reg = Result = 1; // Void call, just a chain.
1382 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001383 Result = MakeReg(Node->getValueType(0));
1384 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001385 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001386 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001387 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001388 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001389 break;
1390 case ISD::ADD_PARTS:
1391 case ISD::SUB_PARTS:
1392 case ISD::SHL_PARTS:
1393 case ISD::SRL_PARTS:
1394 case ISD::SRA_PARTS:
1395 Result = MakeReg(Node->getValueType(0));
1396 ExprMap[N.getValue(0)] = Result;
1397 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1398 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1399 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001400 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001401
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001402 switch (N.getOpcode()) {
1403 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001404 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001405 assert(0 && "Node not handled!\n");
1406 case ISD::FrameIndex:
1407 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1408 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1409 return Result;
1410 case ISD::ConstantPool:
1411 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1412 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1413 return Result;
1414 case ISD::ConstantFP:
1415 ContainsFPCode = true;
1416 Tmp1 = Result; // Intermediate Register
1417 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1418 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1419 Tmp1 = MakeReg(MVT::f64);
1420
1421 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1422 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1423 BuildMI(BB, X86::FLD0, 0, Tmp1);
1424 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1425 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1426 BuildMI(BB, X86::FLD1, 0, Tmp1);
1427 else
1428 assert(0 && "Unexpected constant!");
1429 if (Tmp1 != Result)
1430 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1431 return Result;
1432 case ISD::Constant:
1433 switch (N.getValueType()) {
1434 default: assert(0 && "Cannot use constants of this type!");
1435 case MVT::i1:
1436 case MVT::i8: Opc = X86::MOV8ri; break;
1437 case MVT::i16: Opc = X86::MOV16ri; break;
1438 case MVT::i32: Opc = X86::MOV32ri; break;
1439 }
1440 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1441 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001442 case ISD::UNDEF:
1443 if (Node->getValueType(0) == MVT::f64) {
1444 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1445 BuildMI(BB, X86::FLD0, 0, Result);
1446 } else {
1447 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1448 }
1449 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001450 case ISD::GlobalAddress: {
1451 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1452 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1453 return Result;
1454 }
1455 case ISD::ExternalSymbol: {
1456 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1457 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1458 return Result;
1459 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001460 case ISD::ZERO_EXTEND: {
1461 int DestIs16 = N.getValueType() == MVT::i16;
1462 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001463
1464 // FIXME: This hack is here for zero extension casts from bool to i8. This
1465 // would not be needed if bools were promoted by Legalize.
1466 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001467 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001468 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1469 return Result;
1470 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001471
Chris Lattner4ff348b2005-01-17 06:26:58 +00001472 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001473 static const unsigned Opc[3] = {
1474 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1475 };
1476
1477 X86AddressMode AM;
1478 EmitFoldedLoad(N.getOperand(0), AM);
1479 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001480
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001481 return Result;
1482 }
1483
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001484 static const unsigned Opc[3] = {
1485 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1486 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001487 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001488 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1489 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001490 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001491 case ISD::SIGN_EXTEND: {
1492 int DestIs16 = N.getValueType() == MVT::i16;
1493 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1494
Chris Lattner590d8002005-01-09 18:52:44 +00001495 // FIXME: Legalize should promote bools to i8!
1496 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1497 "Sign extend from bool not implemented!");
1498
Chris Lattner4ff348b2005-01-17 06:26:58 +00001499 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001500 static const unsigned Opc[3] = {
1501 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1502 };
1503
1504 X86AddressMode AM;
1505 EmitFoldedLoad(N.getOperand(0), AM);
1506 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1507 return Result;
1508 }
1509
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001510 static const unsigned Opc[3] = {
1511 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1512 };
1513 Tmp1 = SelectExpr(N.getOperand(0));
1514 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1515 return Result;
1516 }
1517 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001518 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001519 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001520 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001521 switch (N.getValueType()) {
1522 default: assert(0 && "Unknown truncate!");
1523 case MVT::i1:
1524 case MVT::i8: Opc = X86::MOV8rm; break;
1525 case MVT::i16: Opc = X86::MOV16rm; break;
1526 }
1527 X86AddressMode AM;
1528 EmitFoldedLoad(N.getOperand(0), AM);
1529 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1530 return Result;
1531 }
1532
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001533 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1534 // a move out of AX or AL.
1535 switch (N.getOperand(0).getValueType()) {
1536 default: assert(0 && "Unknown truncate!");
1537 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1538 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1539 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1540 }
1541 Tmp1 = SelectExpr(N.getOperand(0));
1542 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1543
1544 switch (N.getValueType()) {
1545 default: assert(0 && "Unknown truncate!");
1546 case MVT::i1:
1547 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1548 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1549 }
1550 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1551 return Result;
1552
Chris Lattner590d8002005-01-09 18:52:44 +00001553 case ISD::SINT_TO_FP:
1554 case ISD::UINT_TO_FP: {
1555 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001556 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001557
1558 // Promote the integer to a type supported by FLD. We do this because there
1559 // are no unsigned FLD instructions, so we must promote an unsigned value to
1560 // a larger signed value, then use FLD on the larger value.
1561 //
1562 MVT::ValueType PromoteType = MVT::Other;
1563 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1564 unsigned PromoteOpcode = 0;
1565 unsigned RealDestReg = Result;
1566 switch (SrcTy) {
1567 case MVT::i1:
1568 case MVT::i8:
1569 // We don't have the facilities for directly loading byte sized data from
1570 // memory (even signed). Promote it to 16 bits.
1571 PromoteType = MVT::i16;
1572 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1573 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1574 break;
1575 case MVT::i16:
1576 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1577 PromoteType = MVT::i32;
1578 PromoteOpcode = X86::MOVZX32rr16;
1579 }
1580 break;
1581 default:
1582 // Don't fild into the real destination.
1583 if (Node->getOpcode() == ISD::UINT_TO_FP)
1584 Result = MakeReg(Node->getValueType(0));
1585 break;
1586 }
1587
1588 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001589
Chris Lattner590d8002005-01-09 18:52:44 +00001590 if (PromoteType != MVT::Other) {
1591 Tmp2 = MakeReg(PromoteType);
1592 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1593 SrcTy = PromoteType;
1594 Tmp1 = Tmp2;
1595 }
1596
1597 // Spill the integer to memory and reload it from there.
1598 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1599 MachineFunction *F = BB->getParent();
1600 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1601
1602 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00001603 case MVT::i32:
1604 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1605 FrameIdx).addReg(Tmp1);
1606 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1607 break;
1608 case MVT::i16:
1609 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1610 FrameIdx).addReg(Tmp1);
1611 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1612 break;
1613 default: break; // No promotion required.
1614 }
1615
Chris Lattner085c9952005-01-12 04:00:00 +00001616 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001617 // If this is a cast from uint -> double, we need to be careful when if
1618 // the "sign" bit is set. If so, we don't want to make a negative number,
1619 // we want to make a positive number. Emit code to add an offset if the
1620 // sign bit is set.
1621
1622 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1623 unsigned IsNeg = MakeReg(MVT::i32);
1624 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1625
1626 // Create a CP value that has the offset in one word and 0 in the other.
1627 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1628 0x4f80000000000000ULL);
1629 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1630 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1631 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00001632 }
1633 return RealDestReg;
1634 }
1635 case ISD::FP_TO_SINT:
1636 case ISD::FP_TO_UINT: {
1637 // FIXME: Most of this grunt work should be done by legalize!
1638 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1639
1640 // Change the floating point control register to use "round towards zero"
1641 // mode when truncating to an integer value.
1642 //
1643 MachineFunction *F = BB->getParent();
1644 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1645 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1646
1647 // Load the old value of the high byte of the control word...
1648 unsigned HighPartOfCW = MakeReg(MVT::i8);
1649 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1650 CWFrameIdx, 1);
1651
1652 // Set the high part to be round to zero...
1653 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1654 CWFrameIdx, 1).addImm(12);
1655
1656 // Reload the modified control word now...
1657 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001658
Chris Lattner590d8002005-01-09 18:52:44 +00001659 // Restore the memory image of control word to original value
1660 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1661 CWFrameIdx, 1).addReg(HighPartOfCW);
1662
1663 // We don't have the facilities for directly storing byte sized data to
1664 // memory. Promote it to 16 bits. We also must promote unsigned values to
1665 // larger classes because we only have signed FP stores.
1666 MVT::ValueType StoreClass = Node->getValueType(0);
1667 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1668 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00001669 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001670 case MVT::i8: StoreClass = MVT::i16; break;
1671 case MVT::i16: StoreClass = MVT::i32; break;
1672 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00001673 default: assert(0 && "Unknown store class!");
1674 }
1675
1676 // Spill the integer to memory and reload it from there.
1677 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1678 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1679
1680 switch (StoreClass) {
1681 default: assert(0 && "Unknown store class!");
1682 case MVT::i16:
1683 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1684 break;
1685 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001686 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001687 break;
Chris Lattner590d8002005-01-09 18:52:44 +00001688 }
1689
1690 switch (Node->getValueType(0)) {
1691 default:
1692 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00001693 case MVT::i32:
1694 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1695 break;
1696 case MVT::i16:
1697 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1698 break;
1699 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00001700 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001701 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1702 break;
1703 }
1704
1705 // Reload the original control word now.
1706 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1707 return Result;
1708 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001709 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001710 Op0 = N.getOperand(0);
1711 Op1 = N.getOperand(1);
1712
Chris Lattner44129b52005-01-25 20:03:11 +00001713 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001714 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001715 goto FoldAdd;
1716 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001717
Chris Lattner44129b52005-01-25 20:03:11 +00001718 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001719 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001720 switch (N.getValueType()) {
1721 default: assert(0 && "Cannot add this type!");
1722 case MVT::i1:
1723 case MVT::i8: Opc = X86::ADD8rm; break;
1724 case MVT::i16: Opc = X86::ADD16rm; break;
1725 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001726 case MVT::f64:
1727 // For F64, handle promoted load operations (from F32) as well!
1728 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1729 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001730 }
1731 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001732 EmitFoldedLoad(Op1, AM);
1733 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001734 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1735 return Result;
1736 }
1737
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001738 // See if we can codegen this as an LEA to fold operations together.
1739 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001740 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001741 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001742 MatchAddress(N, AM);
1743 ExprMap[N] = Result;
1744
1745 // If this is not just an add, emit the LEA. For a simple add (like
1746 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1747 // leave this as LEA, then peephole it to 'ADD' after two address elim
1748 // happens.
1749 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1750 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1751 X86AddressMode XAM = SelectAddrExprs(AM);
1752 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1753 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001754 }
1755 }
Chris Lattner11333092005-01-11 03:11:44 +00001756
Chris Lattnera5ade062005-01-11 21:19:59 +00001757 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001758 Opc = 0;
1759 if (CN->getValue() == 1) { // add X, 1 -> inc X
1760 switch (N.getValueType()) {
1761 default: assert(0 && "Cannot integer add this type!");
1762 case MVT::i8: Opc = X86::INC8r; break;
1763 case MVT::i16: Opc = X86::INC16r; break;
1764 case MVT::i32: Opc = X86::INC32r; break;
1765 }
1766 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1767 switch (N.getValueType()) {
1768 default: assert(0 && "Cannot integer add this type!");
1769 case MVT::i8: Opc = X86::DEC8r; break;
1770 case MVT::i16: Opc = X86::DEC16r; break;
1771 case MVT::i32: Opc = X86::DEC32r; break;
1772 }
1773 }
1774
1775 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001776 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001777 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1778 return Result;
1779 }
1780
1781 switch (N.getValueType()) {
1782 default: assert(0 && "Cannot add this type!");
1783 case MVT::i8: Opc = X86::ADD8ri; break;
1784 case MVT::i16: Opc = X86::ADD16ri; break;
1785 case MVT::i32: Opc = X86::ADD32ri; break;
1786 }
1787 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001788 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001789 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1790 return Result;
1791 }
1792 }
1793
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001794 switch (N.getValueType()) {
1795 default: assert(0 && "Cannot add this type!");
1796 case MVT::i8: Opc = X86::ADD8rr; break;
1797 case MVT::i16: Opc = X86::ADD16rr; break;
1798 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001799 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001800 }
Chris Lattner11333092005-01-11 03:11:44 +00001801
Chris Lattnera5ade062005-01-11 21:19:59 +00001802 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1803 Tmp1 = SelectExpr(Op0);
1804 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001805 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001806 Tmp2 = SelectExpr(Op1);
1807 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001808 }
1809
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001810 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1811 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001812
1813 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001814 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001815 case ISD::FSIN:
1816 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001817 case ISD::FSQRT:
1818 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001819 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001820 switch (N.getOpcode()) {
1821 default: assert(0 && "Unreachable!");
1822 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1823 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1824 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001825 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1826 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001827 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001828 return Result;
1829
Chris Lattner8db0af12005-04-06 04:21:07 +00001830 case ISD::MULHU:
1831 switch (N.getValueType()) {
1832 default: assert(0 && "Unsupported VT!");
1833 case MVT::i8: Tmp2 = X86::MUL8r; break;
1834 case MVT::i16: Tmp2 = X86::MUL16r; break;
1835 case MVT::i32: Tmp2 = X86::MUL32r; break;
1836 }
1837 // FALL THROUGH
1838 case ISD::MULHS: {
1839 unsigned MovOpc, LowReg, HiReg;
1840 switch (N.getValueType()) {
1841 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001842 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001843 MovOpc = X86::MOV8rr;
1844 LowReg = X86::AL;
1845 HiReg = X86::AH;
1846 Opc = X86::IMUL8r;
1847 break;
1848 case MVT::i16:
1849 MovOpc = X86::MOV16rr;
1850 LowReg = X86::AX;
1851 HiReg = X86::DX;
1852 Opc = X86::IMUL16r;
1853 break;
1854 case MVT::i32:
1855 MovOpc = X86::MOV32rr;
1856 LowReg = X86::EAX;
1857 HiReg = X86::EDX;
1858 Opc = X86::IMUL32r;
1859 break;
1860 }
1861 if (Node->getOpcode() != ISD::MULHS)
1862 Opc = Tmp2; // Get the MULHU opcode.
1863
1864 Op0 = Node->getOperand(0);
1865 Op1 = Node->getOperand(1);
1866 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1867 Tmp1 = SelectExpr(Op0);
1868 Tmp2 = SelectExpr(Op1);
1869 } else {
1870 Tmp2 = SelectExpr(Op1);
1871 Tmp1 = SelectExpr(Op0);
1872 }
1873
1874 // FIXME: Implement folding of loads into the memory operands here!
1875 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1876 BuildMI(BB, Opc, 1).addReg(Tmp2);
1877 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1878 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001879 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001880
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001881 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001882 case ISD::MUL:
1883 case ISD::AND:
1884 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001885 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001886 static const unsigned SUBTab[] = {
1887 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1888 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1889 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1890 };
1891 static const unsigned MULTab[] = {
1892 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1893 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1894 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1895 };
1896 static const unsigned ANDTab[] = {
1897 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1898 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001899 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001900 };
1901 static const unsigned ORTab[] = {
1902 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1903 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1904 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1905 };
1906 static const unsigned XORTab[] = {
1907 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1908 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1909 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1910 };
1911
1912 Op0 = Node->getOperand(0);
1913 Op1 = Node->getOperand(1);
1914
Chris Lattner30ea1e92005-01-19 07:37:26 +00001915 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1916 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001917 return Result;
1918
1919 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001920 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1921 if (CN->isNullValue()) { // 0 - N -> neg N
1922 switch (N.getValueType()) {
1923 default: assert(0 && "Cannot sub this type!");
1924 case MVT::i1:
1925 case MVT::i8: Opc = X86::NEG8r; break;
1926 case MVT::i16: Opc = X86::NEG16r; break;
1927 case MVT::i32: Opc = X86::NEG32r; break;
1928 }
1929 Tmp1 = SelectExpr(N.getOperand(1));
1930 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1931 return Result;
1932 }
1933
Chris Lattnera5ade062005-01-11 21:19:59 +00001934 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1935 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001936 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001937 switch (N.getValueType()) {
1938 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001939 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001940 case MVT::i8: Opc = X86::NOT8r; break;
1941 case MVT::i16: Opc = X86::NOT16r; break;
1942 case MVT::i32: Opc = X86::NOT32r; break;
1943 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001944 if (Opc) {
1945 Tmp1 = SelectExpr(Op0);
1946 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1947 return Result;
1948 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001949 }
1950
Chris Lattner2a4e5082005-01-17 06:48:02 +00001951 // Fold common multiplies into LEA instructions.
1952 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1953 switch ((int)CN->getValue()) {
1954 default: break;
1955 case 3:
1956 case 5:
1957 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001958 // Remove N from exprmap so SelectAddress doesn't get confused.
1959 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001960 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001961 SelectAddress(N, AM);
1962 // Restore it to the map.
1963 ExprMap[N] = Result;
1964 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1965 return Result;
1966 }
1967 }
1968
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001969 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001970 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001971 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001972 case MVT::i8: Opc = 0; break;
1973 case MVT::i16: Opc = 1; break;
1974 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001975 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001976 switch (Node->getOpcode()) {
1977 default: assert(0 && "Unreachable!");
1978 case ISD::SUB: Opc = SUBTab[Opc]; break;
1979 case ISD::MUL: Opc = MULTab[Opc]; break;
1980 case ISD::AND: Opc = ANDTab[Opc]; break;
1981 case ISD::OR: Opc = ORTab[Opc]; break;
1982 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001983 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001984 if (Opc) { // Can't fold MUL:i8 R, imm
1985 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001986 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1987 return Result;
1988 }
1989 }
Chris Lattner11333092005-01-11 03:11:44 +00001990
Chris Lattner44129b52005-01-25 20:03:11 +00001991 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001992 if (Node->getOpcode() != ISD::SUB) {
1993 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001994 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001995 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001996 // For FP, emit 'reverse' subract, with a memory operand.
1997 if (N.getValueType() == MVT::f64) {
1998 if (Op0.getOpcode() == ISD::EXTLOAD)
1999 Opc = X86::FSUBR32m;
2000 else
2001 Opc = X86::FSUBR64m;
2002
Chris Lattnera5ade062005-01-11 21:19:59 +00002003 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002004 EmitFoldedLoad(Op0, AM);
2005 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002006 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2007 return Result;
2008 }
2009 }
2010
Chris Lattner44129b52005-01-25 20:03:11 +00002011 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002012 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002013 switch (N.getValueType()) {
2014 default: assert(0 && "Cannot operate on this type!");
2015 case MVT::i1:
2016 case MVT::i8: Opc = 5; break;
2017 case MVT::i16: Opc = 6; break;
2018 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002019 // For F64, handle promoted load operations (from F32) as well!
2020 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002021 }
2022 switch (Node->getOpcode()) {
2023 default: assert(0 && "Unreachable!");
2024 case ISD::SUB: Opc = SUBTab[Opc]; break;
2025 case ISD::MUL: Opc = MULTab[Opc]; break;
2026 case ISD::AND: Opc = ANDTab[Opc]; break;
2027 case ISD::OR: Opc = ORTab[Opc]; break;
2028 case ISD::XOR: Opc = XORTab[Opc]; break;
2029 }
2030
2031 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002032 EmitFoldedLoad(Op1, AM);
2033 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002034 if (Opc) {
2035 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2036 } else {
2037 assert(Node->getOpcode() == ISD::MUL &&
2038 N.getValueType() == MVT::i8 && "Unexpected situation!");
2039 // Must use the MUL instruction, which forces use of AL.
2040 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2041 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2042 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2043 }
2044 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002045 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002046
2047 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2048 Tmp1 = SelectExpr(Op0);
2049 Tmp2 = SelectExpr(Op1);
2050 } else {
2051 Tmp2 = SelectExpr(Op1);
2052 Tmp1 = SelectExpr(Op0);
2053 }
2054
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002055 switch (N.getValueType()) {
2056 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002057 case MVT::i1:
2058 case MVT::i8: Opc = 10; break;
2059 case MVT::i16: Opc = 11; break;
2060 case MVT::i32: Opc = 12; break;
2061 case MVT::f32: Opc = 13; break;
2062 case MVT::f64: Opc = 14; break;
2063 }
2064 switch (Node->getOpcode()) {
2065 default: assert(0 && "Unreachable!");
2066 case ISD::SUB: Opc = SUBTab[Opc]; break;
2067 case ISD::MUL: Opc = MULTab[Opc]; break;
2068 case ISD::AND: Opc = ANDTab[Opc]; break;
2069 case ISD::OR: Opc = ORTab[Opc]; break;
2070 case ISD::XOR: Opc = XORTab[Opc]; break;
2071 }
2072 if (Opc) {
2073 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2074 } else {
2075 assert(Node->getOpcode() == ISD::MUL &&
2076 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002077 // Must use the MUL instruction, which forces use of AL.
2078 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2079 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2080 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002081 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002082 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002083 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002084 case ISD::ADD_PARTS:
2085 case ISD::SUB_PARTS: {
2086 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2087 "Not an i64 add/sub!");
2088 // Emit all of the operands.
2089 std::vector<unsigned> InVals;
2090 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2091 InVals.push_back(SelectExpr(N.getOperand(i)));
2092 if (N.getOpcode() == ISD::ADD_PARTS) {
2093 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2094 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2095 } else {
2096 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2097 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2098 }
2099 return Result+N.ResNo;
2100 }
2101
Chris Lattnerb38a7492005-04-02 04:01:14 +00002102 case ISD::SHL_PARTS:
2103 case ISD::SRA_PARTS:
2104 case ISD::SRL_PARTS: {
2105 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2106 "Not an i64 shift!");
2107 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2108 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2109 unsigned TmpReg = MakeReg(MVT::i32);
2110 if (N.getOpcode() == ISD::SRA_PARTS) {
2111 // If this is a SHR of a Long, then we need to do funny sign extension
2112 // stuff. TmpReg gets the value to use as the high-part if we are
2113 // shifting more than 32 bits.
2114 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2115 } else {
2116 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2117 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2118 }
2119
2120 // Initialize CL with the shift amount.
2121 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2122 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2123
2124 unsigned TmpReg2 = MakeReg(MVT::i32);
2125 unsigned TmpReg3 = MakeReg(MVT::i32);
2126 if (N.getOpcode() == ISD::SHL_PARTS) {
2127 // TmpReg2 = shld inHi, inLo
2128 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2129 .addReg(ShiftOpLo);
2130 // TmpReg3 = shl inLo, CL
2131 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002132
Chris Lattnerb38a7492005-04-02 04:01:14 +00002133 // Set the flags to indicate whether the shift was by more than 32 bits.
2134 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002135
Chris Lattnerb38a7492005-04-02 04:01:14 +00002136 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002137 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002138 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2139 // DestLo = (>32) ? TmpReg : TmpReg3;
2140 BuildMI(BB, X86::CMOVNE32rr, 2,
2141 Result).addReg(TmpReg3).addReg(TmpReg);
2142 } else {
2143 // TmpReg2 = shrd inLo, inHi
2144 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2145 .addReg(ShiftOpHi);
2146 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002147 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002148 : X86::SHR32rCL, 1, TmpReg3)
2149 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002150
Chris Lattnerb38a7492005-04-02 04:01:14 +00002151 // Set the flags to indicate whether the shift was by more than 32 bits.
2152 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002153
Chris Lattnerb38a7492005-04-02 04:01:14 +00002154 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002155 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002156 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002157
Chris Lattnerb38a7492005-04-02 04:01:14 +00002158 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002159 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002160 Result+1).addReg(TmpReg3).addReg(TmpReg);
2161 }
2162 return Result+N.ResNo;
2163 }
2164
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002165 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002166 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2167 Tmp2 = SelectExpr(N.getOperand(1));
2168 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002169 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002170 Tmp3 = SelectExpr(N.getOperand(2));
2171 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002172 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002173 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2174 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002175
2176 case ISD::SDIV:
2177 case ISD::UDIV:
2178 case ISD::SREM:
2179 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002180 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2181 "We don't support this operator!");
2182
Chris Lattner5bf26862005-04-13 03:29:53 +00002183 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002184 // We can fold loads into FpDIVs, but not really into any others.
2185 if (N.getValueType() == MVT::f64) {
2186 // Check for reversed and unreversed DIV.
2187 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2188 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2189 Opc = X86::FDIVR32m;
2190 else
2191 Opc = X86::FDIVR64m;
2192 X86AddressMode AM;
2193 EmitFoldedLoad(N.getOperand(0), AM);
2194 Tmp1 = SelectExpr(N.getOperand(1));
2195 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2196 return Result;
2197 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2198 N.getOperand(1).getOpcode() == ISD::LOAD) {
2199 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2200 Opc = X86::FDIV32m;
2201 else
2202 Opc = X86::FDIV64m;
2203 X86AddressMode AM;
2204 EmitFoldedLoad(N.getOperand(1), AM);
2205 Tmp1 = SelectExpr(N.getOperand(0));
2206 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2207 return Result;
2208 }
2209 }
2210
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2212 // FIXME: These special cases should be handled by the lowering impl!
2213 unsigned RHS = CN->getValue();
2214 bool isNeg = false;
2215 if ((int)RHS < 0) {
2216 isNeg = true;
2217 RHS = -RHS;
2218 }
2219 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2220 unsigned Log = log2(RHS);
2221 unsigned TmpReg = MakeReg(N.getValueType());
2222 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2223 switch (N.getValueType()) {
2224 default: assert("Unknown type to signed divide!");
2225 case MVT::i8:
2226 SAROpc = X86::SAR8ri;
2227 SHROpc = X86::SHR8ri;
2228 ADDOpc = X86::ADD8rr;
2229 NEGOpc = X86::NEG8r;
2230 break;
2231 case MVT::i16:
2232 SAROpc = X86::SAR16ri;
2233 SHROpc = X86::SHR16ri;
2234 ADDOpc = X86::ADD16rr;
2235 NEGOpc = X86::NEG16r;
2236 break;
2237 case MVT::i32:
2238 SAROpc = X86::SAR32ri;
2239 SHROpc = X86::SHR32ri;
2240 ADDOpc = X86::ADD32rr;
2241 NEGOpc = X86::NEG32r;
2242 break;
2243 }
Chris Lattner11333092005-01-11 03:11:44 +00002244 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002245 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2246 unsigned TmpReg2 = MakeReg(N.getValueType());
2247 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2248 unsigned TmpReg3 = MakeReg(N.getValueType());
2249 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002250
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002251 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2252 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2253 if (isNeg)
2254 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2255 return Result;
2256 }
2257 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002258 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002259
Chris Lattner11333092005-01-11 03:11:44 +00002260 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2261 Tmp1 = SelectExpr(N.getOperand(0));
2262 Tmp2 = SelectExpr(N.getOperand(1));
2263 } else {
2264 Tmp2 = SelectExpr(N.getOperand(1));
2265 Tmp1 = SelectExpr(N.getOperand(0));
2266 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002267
2268 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2269 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2270 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2271 switch (N.getValueType()) {
2272 default: assert(0 && "Cannot sdiv this type!");
2273 case MVT::i8:
2274 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2275 LoReg = X86::AL;
2276 HiReg = X86::AH;
2277 MovOpcode = X86::MOV8rr;
2278 ClrOpcode = X86::MOV8ri;
2279 SExtOpcode = X86::CBW;
2280 break;
2281 case MVT::i16:
2282 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2283 LoReg = X86::AX;
2284 HiReg = X86::DX;
2285 MovOpcode = X86::MOV16rr;
2286 ClrOpcode = X86::MOV16ri;
2287 SExtOpcode = X86::CWD;
2288 break;
2289 case MVT::i32:
2290 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002291 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 HiReg = X86::EDX;
2293 MovOpcode = X86::MOV32rr;
2294 ClrOpcode = X86::MOV32ri;
2295 SExtOpcode = X86::CDQ;
2296 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002297 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002298 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002299 return Result;
2300 }
2301
2302 // Set up the low part.
2303 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2304
2305 if (isSigned) {
2306 // Sign extend the low part into the high part.
2307 BuildMI(BB, SExtOpcode, 0);
2308 } else {
2309 // Zero out the high part, effectively zero extending the input.
2310 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2311 }
2312
2313 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002314 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002315
2316 // Get the result of the divide or rem.
2317 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2318 return Result;
2319 }
2320
2321 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002322 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002323 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2324 switch (N.getValueType()) {
2325 default: assert(0 && "Cannot shift this type!");
2326 case MVT::i8: Opc = X86::ADD8rr; break;
2327 case MVT::i16: Opc = X86::ADD16rr; break;
2328 case MVT::i32: Opc = X86::ADD32rr; break;
2329 }
2330 Tmp1 = SelectExpr(N.getOperand(0));
2331 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2332 return Result;
2333 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002334
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002335 switch (N.getValueType()) {
2336 default: assert(0 && "Cannot shift this type!");
2337 case MVT::i8: Opc = X86::SHL8ri; break;
2338 case MVT::i16: Opc = X86::SHL16ri; break;
2339 case MVT::i32: Opc = X86::SHL32ri; break;
2340 }
Chris Lattner11333092005-01-11 03:11:44 +00002341 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002342 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2343 return Result;
2344 }
Chris Lattner11333092005-01-11 03:11:44 +00002345
2346 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2347 Tmp1 = SelectExpr(N.getOperand(0));
2348 Tmp2 = SelectExpr(N.getOperand(1));
2349 } else {
2350 Tmp2 = SelectExpr(N.getOperand(1));
2351 Tmp1 = SelectExpr(N.getOperand(0));
2352 }
2353
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002354 switch (N.getValueType()) {
2355 default: assert(0 && "Cannot shift this type!");
2356 case MVT::i8 : Opc = X86::SHL8rCL; break;
2357 case MVT::i16: Opc = X86::SHL16rCL; break;
2358 case MVT::i32: Opc = X86::SHL32rCL; break;
2359 }
2360 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2361 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2362 return Result;
2363 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002364 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2365 switch (N.getValueType()) {
2366 default: assert(0 && "Cannot shift this type!");
2367 case MVT::i8: Opc = X86::SHR8ri; break;
2368 case MVT::i16: Opc = X86::SHR16ri; break;
2369 case MVT::i32: Opc = X86::SHR32ri; break;
2370 }
Chris Lattner11333092005-01-11 03:11:44 +00002371 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002372 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2373 return Result;
2374 }
Chris Lattner11333092005-01-11 03:11:44 +00002375
2376 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2377 Tmp1 = SelectExpr(N.getOperand(0));
2378 Tmp2 = SelectExpr(N.getOperand(1));
2379 } else {
2380 Tmp2 = SelectExpr(N.getOperand(1));
2381 Tmp1 = SelectExpr(N.getOperand(0));
2382 }
2383
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002384 switch (N.getValueType()) {
2385 default: assert(0 && "Cannot shift this type!");
2386 case MVT::i8 : Opc = X86::SHR8rCL; break;
2387 case MVT::i16: Opc = X86::SHR16rCL; break;
2388 case MVT::i32: Opc = X86::SHR32rCL; break;
2389 }
2390 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2391 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2392 return Result;
2393 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002394 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2395 switch (N.getValueType()) {
2396 default: assert(0 && "Cannot shift this type!");
2397 case MVT::i8: Opc = X86::SAR8ri; break;
2398 case MVT::i16: Opc = X86::SAR16ri; break;
2399 case MVT::i32: Opc = X86::SAR32ri; break;
2400 }
Chris Lattner11333092005-01-11 03:11:44 +00002401 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002402 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2403 return Result;
2404 }
Chris Lattner11333092005-01-11 03:11:44 +00002405
2406 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2407 Tmp1 = SelectExpr(N.getOperand(0));
2408 Tmp2 = SelectExpr(N.getOperand(1));
2409 } else {
2410 Tmp2 = SelectExpr(N.getOperand(1));
2411 Tmp1 = SelectExpr(N.getOperand(0));
2412 }
2413
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002414 switch (N.getValueType()) {
2415 default: assert(0 && "Cannot shift this type!");
2416 case MVT::i8 : Opc = X86::SAR8rCL; break;
2417 case MVT::i16: Opc = X86::SAR16rCL; break;
2418 case MVT::i32: Opc = X86::SAR32rCL; break;
2419 }
2420 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2421 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2422 return Result;
2423
2424 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002425 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002426 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2427 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2428 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002429 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002430 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002431 if (Result != 1) { // Generate the token
2432 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2433 assert(0 && "Load already emitted!?");
2434 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002435 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2436
Chris Lattner5188ad72005-01-08 19:28:19 +00002437 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002438 default: assert(0 && "Cannot load this type!");
2439 case MVT::i1:
2440 case MVT::i8: Opc = X86::MOV8rm; break;
2441 case MVT::i16: Opc = X86::MOV16rm; break;
2442 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002443 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2444 }
Chris Lattner11333092005-01-11 03:11:44 +00002445
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002446 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002447 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002448 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2449 } else {
2450 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002451
2452 SDOperand Chain = N.getOperand(0);
2453 SDOperand Address = N.getOperand(1);
2454 if (getRegPressure(Chain) > getRegPressure(Address)) {
2455 Select(Chain);
2456 SelectAddress(Address, AM);
2457 } else {
2458 SelectAddress(Address, AM);
2459 Select(Chain);
2460 }
2461
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002462 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2463 }
2464 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002465
2466 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2467 case ISD::ZEXTLOAD: {
2468 // Make sure we generate both values.
2469 if (Result != 1)
2470 ExprMap[N.getValue(1)] = 1; // Generate the token
2471 else
2472 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2473
Chris Lattnerda2ce112005-01-16 07:34:08 +00002474 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2475 if (Node->getValueType(0) == MVT::f64) {
2476 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2477 "Bad EXTLOAD!");
2478 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2479 CP->getIndex());
2480 return Result;
2481 }
2482
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002483 X86AddressMode AM;
2484 if (getRegPressure(Node->getOperand(0)) >
2485 getRegPressure(Node->getOperand(1))) {
2486 Select(Node->getOperand(0)); // chain
2487 SelectAddress(Node->getOperand(1), AM);
2488 } else {
2489 SelectAddress(Node->getOperand(1), AM);
2490 Select(Node->getOperand(0)); // chain
2491 }
2492
2493 switch (Node->getValueType(0)) {
2494 default: assert(0 && "Unknown type to sign extend to.");
2495 case MVT::f64:
2496 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2497 "Bad EXTLOAD!");
2498 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2499 break;
2500 case MVT::i32:
2501 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2502 default:
2503 assert(0 && "Bad zero extend!");
2504 case MVT::i1:
2505 case MVT::i8:
2506 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2507 break;
2508 case MVT::i16:
2509 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2510 break;
2511 }
2512 break;
2513 case MVT::i16:
2514 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2515 "Bad zero extend!");
2516 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2517 break;
2518 case MVT::i8:
2519 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2520 "Bad zero extend!");
2521 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2522 break;
2523 }
2524 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002525 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002526 case ISD::SEXTLOAD: {
2527 // Make sure we generate both values.
2528 if (Result != 1)
2529 ExprMap[N.getValue(1)] = 1; // Generate the token
2530 else
2531 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2532
2533 X86AddressMode AM;
2534 if (getRegPressure(Node->getOperand(0)) >
2535 getRegPressure(Node->getOperand(1))) {
2536 Select(Node->getOperand(0)); // chain
2537 SelectAddress(Node->getOperand(1), AM);
2538 } else {
2539 SelectAddress(Node->getOperand(1), AM);
2540 Select(Node->getOperand(0)); // chain
2541 }
2542
2543 switch (Node->getValueType(0)) {
2544 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2545 default: assert(0 && "Unknown type to sign extend to.");
2546 case MVT::i32:
2547 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2548 default:
2549 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2550 case MVT::i8:
2551 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2552 break;
2553 case MVT::i16:
2554 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2555 break;
2556 }
2557 break;
2558 case MVT::i16:
2559 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2560 "Cannot sign extend from bool!");
2561 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2562 break;
2563 }
2564 return Result;
2565 }
2566
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002567 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002568 // Generate both result values.
2569 if (Result != 1)
2570 ExprMap[N.getValue(1)] = 1; // Generate the token
2571 else
2572 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2573
2574 // FIXME: We are currently ignoring the requested alignment for handling
2575 // greater than the stack alignment. This will need to be revisited at some
2576 // point. Align = N.getOperand(2);
2577
2578 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2579 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2580 std::cerr << "Cannot allocate stack object with greater alignment than"
2581 << " the stack alignment yet!";
2582 abort();
2583 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002584
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002585 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002586 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002587 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2588 .addImm(CN->getValue());
2589 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002590 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2591 Select(N.getOperand(0));
2592 Tmp1 = SelectExpr(N.getOperand(1));
2593 } else {
2594 Tmp1 = SelectExpr(N.getOperand(1));
2595 Select(N.getOperand(0));
2596 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002597
2598 // Subtract size from stack pointer, thereby allocating some space.
2599 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2600 }
2601
2602 // Put a pointer to the space into the result register, by copying the stack
2603 // pointer.
2604 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2605 return Result;
2606
2607 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002608 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002609 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002610
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002611 if (GlobalAddressSDNode *GASD =
2612 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002613 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002614 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2615 } else if (ExternalSymbolSDNode *ESSDN =
2616 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002617 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002618 BuildMI(BB, X86::CALLpcrel32,
2619 1).addExternalSymbol(ESSDN->getSymbol(), true);
2620 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002621 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2622 Select(N.getOperand(0));
2623 Tmp1 = SelectExpr(N.getOperand(1));
2624 } else {
2625 Tmp1 = SelectExpr(N.getOperand(1));
2626 Select(N.getOperand(0));
2627 }
2628
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002629 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2630 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002631 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002632 default: assert(0 && "Unknown value type for call result!");
2633 case MVT::Other: return 1;
2634 case MVT::i1:
2635 case MVT::i8:
2636 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2637 break;
2638 case MVT::i16:
2639 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2640 break;
2641 case MVT::i32:
2642 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002643 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002644 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2645 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002646 case MVT::f64: // Floating-point return values live in %ST(0)
2647 ContainsFPCode = true;
2648 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2649 break;
2650 }
2651 return Result+N.ResNo;
2652 }
2653
2654 return 0;
2655}
2656
Chris Lattnere10269b2005-01-17 19:25:26 +00002657/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2658/// load/op/store instruction. If successful return true.
2659bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2660 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2661 SDOperand Chain = Node->getOperand(0);
2662 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002663 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002664
2665 // The chain has to be a load, the stored value must be an integer binary
2666 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002667 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002668 MVT::isFloatingPoint(StVal.getValueType()))
2669 return false;
2670
Chris Lattner5c659812005-01-17 22:10:42 +00002671 // Token chain must either be a factor node or the load to fold.
2672 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2673 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002674
Chris Lattner5c659812005-01-17 22:10:42 +00002675 SDOperand TheLoad;
2676
2677 // Check to see if there is a load from the same pointer that we're storing
2678 // to in either operand of the binop.
2679 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2680 StVal.getOperand(0).getOperand(1) == StPtr)
2681 TheLoad = StVal.getOperand(0);
2682 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2683 StVal.getOperand(1).getOperand(1) == StPtr)
2684 TheLoad = StVal.getOperand(1);
2685 else
2686 return false; // No matching load operand.
2687
2688 // We can only fold the load if there are no intervening side-effecting
2689 // operations. This means that the store uses the load as its token chain, or
2690 // there are only token factor nodes in between the store and load.
2691 if (Chain != TheLoad.getValue(1)) {
2692 // Okay, the other option is that we have a store referring to (possibly
2693 // nested) token factor nodes. For now, just try peeking through one level
2694 // of token factors to see if this is the case.
2695 bool ChainOk = false;
2696 if (Chain.getOpcode() == ISD::TokenFactor) {
2697 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2698 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2699 ChainOk = true;
2700 break;
2701 }
2702 }
2703
2704 if (!ChainOk) return false;
2705 }
2706
2707 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002708 return false;
2709
2710 // Make sure that one of the operands of the binop is the load, and that the
2711 // load folds into the binop.
2712 if (((StVal.getOperand(0) != TheLoad ||
2713 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2714 (StVal.getOperand(1) != TheLoad ||
2715 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2716 return false;
2717
2718 // Finally, check to see if this is one of the ops we can handle!
2719 static const unsigned ADDTAB[] = {
2720 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2721 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2722 };
2723 static const unsigned SUBTAB[] = {
2724 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2725 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2726 };
2727 static const unsigned ANDTAB[] = {
2728 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2729 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2730 };
2731 static const unsigned ORTAB[] = {
2732 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2733 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2734 };
2735 static const unsigned XORTAB[] = {
2736 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2737 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2738 };
2739 static const unsigned SHLTAB[] = {
2740 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2741 /*Have to put the reg in CL*/0, 0, 0,
2742 };
2743 static const unsigned SARTAB[] = {
2744 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2745 /*Have to put the reg in CL*/0, 0, 0,
2746 };
2747 static const unsigned SHRTAB[] = {
2748 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2749 /*Have to put the reg in CL*/0, 0, 0,
2750 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002751
Chris Lattnere10269b2005-01-17 19:25:26 +00002752 const unsigned *TabPtr = 0;
2753 switch (StVal.getOpcode()) {
2754 default:
2755 std::cerr << "CANNOT [mem] op= val: ";
2756 StVal.Val->dump(); std::cerr << "\n";
2757 case ISD::MUL:
2758 case ISD::SDIV:
2759 case ISD::UDIV:
2760 case ISD::SREM:
2761 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002762
Chris Lattnere10269b2005-01-17 19:25:26 +00002763 case ISD::ADD: TabPtr = ADDTAB; break;
2764 case ISD::SUB: TabPtr = SUBTAB; break;
2765 case ISD::AND: TabPtr = ANDTAB; break;
2766 case ISD:: OR: TabPtr = ORTAB; break;
2767 case ISD::XOR: TabPtr = XORTAB; break;
2768 case ISD::SHL: TabPtr = SHLTAB; break;
2769 case ISD::SRA: TabPtr = SARTAB; break;
2770 case ISD::SRL: TabPtr = SHRTAB; break;
2771 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002772
Chris Lattnere10269b2005-01-17 19:25:26 +00002773 // Handle: [mem] op= CST
2774 SDOperand Op0 = StVal.getOperand(0);
2775 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002776 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002777 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2778 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2779 default: break;
2780 case MVT::i1:
2781 case MVT::i8: Opc = TabPtr[0]; break;
2782 case MVT::i16: Opc = TabPtr[1]; break;
2783 case MVT::i32: Opc = TabPtr[2]; break;
2784 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002785
Chris Lattnere10269b2005-01-17 19:25:26 +00002786 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002787 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2788 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002789 Select(Chain);
2790
Chris Lattnere10269b2005-01-17 19:25:26 +00002791 X86AddressMode AM;
2792 if (getRegPressure(TheLoad.getOperand(0)) >
2793 getRegPressure(TheLoad.getOperand(1))) {
2794 Select(TheLoad.getOperand(0));
2795 SelectAddress(TheLoad.getOperand(1), AM);
2796 } else {
2797 SelectAddress(TheLoad.getOperand(1), AM);
2798 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002799 }
Chris Lattner5c659812005-01-17 22:10:42 +00002800
2801 if (StVal.getOpcode() == ISD::ADD) {
2802 if (CN->getValue() == 1) {
2803 switch (Op0.getValueType()) {
2804 default: break;
2805 case MVT::i8:
2806 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2807 return true;
2808 case MVT::i16: Opc = TabPtr[1];
2809 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2810 return true;
2811 case MVT::i32: Opc = TabPtr[2];
2812 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2813 return true;
2814 }
2815 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2816 switch (Op0.getValueType()) {
2817 default: break;
2818 case MVT::i8:
2819 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2820 return true;
2821 case MVT::i16: Opc = TabPtr[1];
2822 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2823 return true;
2824 case MVT::i32: Opc = TabPtr[2];
2825 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2826 return true;
2827 }
2828 }
2829 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002830
Chris Lattnere10269b2005-01-17 19:25:26 +00002831 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2832 return true;
2833 }
2834 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002835
Chris Lattnere10269b2005-01-17 19:25:26 +00002836 // If we have [mem] = V op [mem], try to turn it into:
2837 // [mem] = [mem] op V.
2838 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2839 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2840 StVal.getOpcode() != ISD::SRL)
2841 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002842
Chris Lattnere10269b2005-01-17 19:25:26 +00002843 if (Op0 != TheLoad) return false;
2844
2845 switch (Op0.getValueType()) {
2846 default: return false;
2847 case MVT::i1:
2848 case MVT::i8: Opc = TabPtr[3]; break;
2849 case MVT::i16: Opc = TabPtr[4]; break;
2850 case MVT::i32: Opc = TabPtr[5]; break;
2851 }
Chris Lattner5c659812005-01-17 22:10:42 +00002852
Chris Lattnerb422aea2005-01-18 17:35:28 +00002853 // Table entry doesn't exist?
2854 if (Opc == 0) return false;
2855
Chris Lattner4a108662005-01-18 03:51:59 +00002856 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2857 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002858 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002859 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002860
Chris Lattnere10269b2005-01-17 19:25:26 +00002861 X86AddressMode AM;
2862 SelectAddress(TheLoad.getOperand(1), AM);
2863 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002864 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002865 return true;
2866}
2867
2868
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002869void ISel::Select(SDOperand N) {
2870 unsigned Tmp1, Tmp2, Opc;
2871
Nate Begeman85fdeb22005-03-24 04:39:54 +00002872 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002873 return; // Already selected.
2874
Chris Lattner989de032005-01-11 06:14:36 +00002875 SDNode *Node = N.Val;
2876
2877 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002878 default:
Chris Lattner989de032005-01-11 06:14:36 +00002879 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002880 assert(0 && "Node not handled yet!");
2881 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002882 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002883 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002884 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002885 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2886 Select(Node->getOperand(OneFirst));
2887 Select(Node->getOperand(!OneFirst));
2888 } else {
2889 std::vector<std::pair<unsigned, unsigned> > OpsP;
2890 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2891 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2892 std::sort(OpsP.begin(), OpsP.end());
2893 std::reverse(OpsP.begin(), OpsP.end());
2894 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2895 Select(Node->getOperand(OpsP[i].second));
2896 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002897 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002898 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002899 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2900 Select(N.getOperand(0));
2901 Tmp1 = SelectExpr(N.getOperand(1));
2902 } else {
2903 Tmp1 = SelectExpr(N.getOperand(1));
2904 Select(N.getOperand(0));
2905 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002906 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002907
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002908 if (Tmp1 != Tmp2) {
2909 switch (N.getOperand(1).getValueType()) {
2910 default: assert(0 && "Invalid type for operation!");
2911 case MVT::i1:
2912 case MVT::i8: Opc = X86::MOV8rr; break;
2913 case MVT::i16: Opc = X86::MOV16rr; break;
2914 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002915 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002916 }
2917 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2918 }
2919 return;
2920 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002921 switch (N.getNumOperands()) {
2922 default:
2923 assert(0 && "Unknown return instruction!");
2924 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002925 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2926 N.getOperand(2).getValueType() == MVT::i32 &&
2927 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002928 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2929 Tmp1 = SelectExpr(N.getOperand(1));
2930 Tmp2 = SelectExpr(N.getOperand(2));
2931 } else {
2932 Tmp2 = SelectExpr(N.getOperand(2));
2933 Tmp1 = SelectExpr(N.getOperand(1));
2934 }
2935 Select(N.getOperand(0));
2936
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002937 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2938 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002939 break;
2940 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002941 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2942 Select(N.getOperand(0));
2943 Tmp1 = SelectExpr(N.getOperand(1));
2944 } else {
2945 Tmp1 = SelectExpr(N.getOperand(1));
2946 Select(N.getOperand(0));
2947 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002948 switch (N.getOperand(1).getValueType()) {
2949 default: assert(0 && "All other types should have been promoted!!");
2950 case MVT::f64:
2951 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002952 break;
2953 case MVT::i32:
2954 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002955 break;
2956 }
2957 break;
2958 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002959 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002960 break;
2961 }
2962 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2963 return;
2964 case ISD::BR: {
2965 Select(N.getOperand(0));
2966 MachineBasicBlock *Dest =
2967 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2968 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2969 return;
2970 }
2971
2972 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002973 MachineBasicBlock *Dest =
2974 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002975
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002976 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2977 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002978 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2979 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2980 Select(N.getOperand(0));
2981 Tmp1 = SelectExpr(N.getOperand(1));
2982 } else {
2983 Tmp1 = SelectExpr(N.getOperand(1));
2984 Select(N.getOperand(0));
2985 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002986 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2987 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2988 }
Chris Lattner11333092005-01-11 03:11:44 +00002989
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002990 return;
2991 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002992
Chris Lattner4df0de92005-01-17 00:00:33 +00002993 case ISD::LOAD:
2994 // If this load could be folded into the only using instruction, and if it
2995 // is safe to emit the instruction here, try to do so now.
2996 if (Node->hasNUsesOfValue(1, 0)) {
2997 SDOperand TheVal = N.getValue(0);
2998 SDNode *User = 0;
2999 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3000 assert(UI != Node->use_end() && "Didn't find use!");
3001 SDNode *UN = *UI;
3002 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3003 if (UN->getOperand(i) == TheVal) {
3004 User = UN;
3005 goto FoundIt;
3006 }
3007 }
3008 FoundIt:
3009 // Only handle unary operators right now.
3010 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003011 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003012 SelectExpr(SDOperand(User, 0));
3013 return;
3014 }
3015 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003016 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003017 SelectExpr(N);
3018 return;
3019
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003020 case ISD::EXTLOAD:
3021 case ISD::SEXTLOAD:
3022 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003023 case ISD::CALL:
3024 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003025 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003026 SelectExpr(N);
3027 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003028
3029 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3030 // On X86, we can represent all types except for Bool and Float natively.
3031 X86AddressMode AM;
3032 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003033 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3034 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3035 && "Unsupported TRUNCSTORE for this target!");
3036
3037 if (StoredTy == MVT::i16) {
3038 // FIXME: This is here just to allow testing. X86 doesn't really have a
3039 // TRUNCSTORE i16 operation, but this is required for targets that do not
3040 // have 16-bit integer registers. We occasionally disable 16-bit integer
3041 // registers to test the promotion code.
3042 Select(N.getOperand(0));
3043 Tmp1 = SelectExpr(N.getOperand(1));
3044 SelectAddress(N.getOperand(2), AM);
3045
3046 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3047 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3048 return;
3049 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003050
3051 // Store of constant bool?
3052 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3053 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3054 Select(N.getOperand(0));
3055 SelectAddress(N.getOperand(2), AM);
3056 } else {
3057 SelectAddress(N.getOperand(2), AM);
3058 Select(N.getOperand(0));
3059 }
3060 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3061 return;
3062 }
3063
3064 switch (StoredTy) {
3065 default: assert(0 && "Cannot truncstore this type!");
3066 case MVT::i1: Opc = X86::MOV8mr; break;
3067 case MVT::f32: Opc = X86::FST32m; break;
3068 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003069
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003070 std::vector<std::pair<unsigned, unsigned> > RP;
3071 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3072 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3073 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3074 std::sort(RP.begin(), RP.end());
3075
Chris Lattner572dd082005-02-23 05:57:21 +00003076 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003077 for (unsigned i = 0; i != 3; ++i)
3078 switch (RP[2-i].second) {
3079 default: assert(0 && "Unknown operand number!");
3080 case 0: Select(N.getOperand(0)); break;
3081 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3082 case 2: SelectAddress(N.getOperand(2), AM); break;
3083 }
3084
3085 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3086 return;
3087 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003088 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003089 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003090
3091 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3092 Opc = 0;
3093 switch (CN->getValueType(0)) {
3094 default: assert(0 && "Invalid type for operation!");
3095 case MVT::i1:
3096 case MVT::i8: Opc = X86::MOV8mi; break;
3097 case MVT::i16: Opc = X86::MOV16mi; break;
3098 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003099 case MVT::f64: break;
3100 }
3101 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003102 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3103 Select(N.getOperand(0));
3104 SelectAddress(N.getOperand(2), AM);
3105 } else {
3106 SelectAddress(N.getOperand(2), AM);
3107 Select(N.getOperand(0));
3108 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003109 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3110 return;
3111 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003112 } else if (GlobalAddressSDNode *GA =
3113 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3114 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3115
3116 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3117 Select(N.getOperand(0));
3118 SelectAddress(N.getOperand(2), AM);
3119 } else {
3120 SelectAddress(N.getOperand(2), AM);
3121 Select(N.getOperand(0));
3122 }
3123 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3124 AM).addGlobalAddress(GA->getGlobal());
3125 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003126 }
Chris Lattner837caa72005-01-11 23:21:30 +00003127
3128 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003129 if (TryToFoldLoadOpStore(Node))
3130 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003131
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003132 switch (N.getOperand(1).getValueType()) {
3133 default: assert(0 && "Cannot store this type!");
3134 case MVT::i1:
3135 case MVT::i8: Opc = X86::MOV8mr; break;
3136 case MVT::i16: Opc = X86::MOV16mr; break;
3137 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003138 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003139 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003140
Chris Lattner11333092005-01-11 03:11:44 +00003141 std::vector<std::pair<unsigned, unsigned> > RP;
3142 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3143 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3144 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3145 std::sort(RP.begin(), RP.end());
3146
Chris Lattner572dd082005-02-23 05:57:21 +00003147 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003148 for (unsigned i = 0; i != 3; ++i)
3149 switch (RP[2-i].second) {
3150 default: assert(0 && "Unknown operand number!");
3151 case 0: Select(N.getOperand(0)); break;
3152 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003153 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003154 }
3155
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003156 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3157 return;
3158 }
3159 case ISD::ADJCALLSTACKDOWN:
3160 case ISD::ADJCALLSTACKUP:
3161 Select(N.getOperand(0));
3162 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003163
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003164 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3165 X86::ADJCALLSTACKUP;
3166 BuildMI(BB, Opc, 1).addImm(Tmp1);
3167 return;
Chris Lattner989de032005-01-11 06:14:36 +00003168 case ISD::MEMSET: {
3169 Select(N.getOperand(0)); // Select the chain.
3170 unsigned Align =
3171 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3172 if (Align == 0) Align = 1;
3173
3174 // Turn the byte code into # iterations
3175 unsigned CountReg;
3176 unsigned Opcode;
3177 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3178 unsigned Val = ValC->getValue() & 255;
3179
3180 // If the value is a constant, then we can potentially use larger sets.
3181 switch (Align & 3) {
3182 case 2: // WORD aligned
3183 CountReg = MakeReg(MVT::i32);
3184 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3185 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3186 } else {
3187 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3188 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3189 }
3190 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3191 Opcode = X86::REP_STOSW;
3192 break;
3193 case 0: // DWORD aligned
3194 CountReg = MakeReg(MVT::i32);
3195 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3196 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3197 } else {
3198 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3199 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3200 }
3201 Val = (Val << 8) | Val;
3202 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3203 Opcode = X86::REP_STOSD;
3204 break;
3205 default: // BYTE aligned
3206 CountReg = SelectExpr(Node->getOperand(3));
3207 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3208 Opcode = X86::REP_STOSB;
3209 break;
3210 }
3211 } else {
3212 // If it's not a constant value we are storing, just fall back. We could
3213 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3214 unsigned ValReg = SelectExpr(Node->getOperand(2));
3215 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3216 CountReg = SelectExpr(Node->getOperand(3));
3217 Opcode = X86::REP_STOSB;
3218 }
3219
3220 // No matter what the alignment is, we put the source in ESI, the
3221 // destination in EDI, and the count in ECX.
3222 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3223 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3224 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3225 BuildMI(BB, Opcode, 0);
3226 return;
3227 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003228 case ISD::MEMCPY:
3229 Select(N.getOperand(0)); // Select the chain.
3230 unsigned Align =
3231 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3232 if (Align == 0) Align = 1;
3233
3234 // Turn the byte code into # iterations
3235 unsigned CountReg;
3236 unsigned Opcode;
3237 switch (Align & 3) {
3238 case 2: // WORD 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()/2);
3242 } else {
3243 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3244 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3245 }
3246 Opcode = X86::REP_MOVSW;
3247 break;
3248 case 0: // DWORD aligned
3249 CountReg = MakeReg(MVT::i32);
3250 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3251 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3252 } else {
3253 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3254 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3255 }
3256 Opcode = X86::REP_MOVSD;
3257 break;
3258 default: // BYTE aligned
3259 CountReg = SelectExpr(Node->getOperand(3));
3260 Opcode = X86::REP_MOVSB;
3261 break;
3262 }
3263
3264 // No matter what the alignment is, we put the source in ESI, the
3265 // destination in EDI, and the count in ECX.
3266 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3267 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3268 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3269 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3270 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3271 BuildMI(BB, Opcode, 0);
3272 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003273 }
3274 assert(0 && "Should not be reached!");
3275}
3276
3277
3278/// createX86PatternInstructionSelector - This pass converts an LLVM function
3279/// into a machine code representation using pattern matching and a machine
3280/// description file.
3281///
3282FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003283 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003284}