blob: 07378427497ee5533bdc1600c352e8db944ad2fa [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000028#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000031#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// X86TargetLowering - X86 Implementation of the TargetLowering interface
36namespace {
37 class X86TargetLowering : public TargetLowering {
38 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000039 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000040 public:
41 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
42 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000043
44 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000047 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000048 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000049
50 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000051 addRegisterClass(MVT::i8, X86::R8RegisterClass);
52 addRegisterClass(MVT::i16, X86::R16RegisterClass);
53 addRegisterClass(MVT::i32, X86::R32RegisterClass);
54 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000055
Chris Lattner8acb1ba2005-01-07 07:49:41 +000056 // FIXME: Eliminate these two classes when legalize can handle promotions
57 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000058/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000059
Chris Lattnerda4d4692005-04-09 03:22:37 +000060 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000061 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
62 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000064 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
65 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
66 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000067 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
68 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
69 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000070
Chris Lattnerc5dcb532005-04-30 04:25:35 +000071 if (!UnsafeFPMath) {
72 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
73 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
74 }
75
Chris Lattnerda2ce112005-01-16 07:34:08 +000076 // These should be promoted to a larger select which is supported.
77/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
78 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000079
Chris Lattner8acb1ba2005-01-07 07:49:41 +000080 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000081
Chris Lattner8acb1ba2005-01-07 07:49:41 +000082 addLegalFPImmediate(+0.0); // FLD0
83 addLegalFPImmediate(+1.0); // FLD1
84 addLegalFPImmediate(-0.0); // FLD0/FCHS
85 addLegalFPImmediate(-1.0); // FLD1/FCHS
86 }
87
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
92
93 /// LowerCallTo - This hook lowers an abstract call to a function into an
94 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000095 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000096 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
97 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000098
99 virtual std::pair<SDOperand, SDOperand>
100 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
101
102 virtual std::pair<SDOperand,SDOperand>
103 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
104 const Type *ArgTy, SelectionDAG &DAG);
105
106 virtual std::pair<SDOperand, SDOperand>
107 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
108 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000109 };
110}
111
112
113std::vector<SDOperand>
114X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
115 std::vector<SDOperand> ArgValues;
116
117 // Add DAG nodes to load the arguments... On entry to a function on the X86,
118 // the stack frame looks like this:
119 //
120 // [ESP] -- return address
121 // [ESP + 4] -- first argument (leftmost lexically)
122 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000123 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000124 //
125 MachineFunction &MF = DAG.getMachineFunction();
126 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000127
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000128 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000129 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000130 MVT::ValueType ObjectVT = getValueType(I->getType());
131 unsigned ArgIncrement = 4;
132 unsigned ObjSize;
133 switch (ObjectVT) {
134 default: assert(0 && "Unhandled argument type!");
135 case MVT::i1:
136 case MVT::i8: ObjSize = 1; break;
137 case MVT::i16: ObjSize = 2; break;
138 case MVT::i32: ObjSize = 4; break;
139 case MVT::i64: ObjSize = ArgIncrement = 8; break;
140 case MVT::f32: ObjSize = 4; break;
141 case MVT::f64: ObjSize = ArgIncrement = 8; break;
142 }
143 // Create the frame index object for this incoming parameter...
144 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000145
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000146 // Create the SelectionDAG nodes corresponding to a load from this parameter
147 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
148
149 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
150 // dead loads.
151 SDOperand ArgValue;
152 if (!I->use_empty())
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000153 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000154 else {
155 if (MVT::isInteger(ObjectVT))
156 ArgValue = DAG.getConstant(0, ObjectVT);
157 else
158 ArgValue = DAG.getConstantFP(0, ObjectVT);
159 }
160 ArgValues.push_back(ArgValue);
161
162 ArgOffset += ArgIncrement; // Move on to the next argument...
163 }
164
165 // If the function takes variable number of arguments, make a frame index for
166 // the start of the first vararg value... for expansion of llvm.va_start.
167 if (F.isVarArg())
168 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000169 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000170
171 // Finally, inform the code generator which regs we return values in.
172 switch (getValueType(F.getReturnType())) {
173 default: assert(0 && "Unknown type!");
174 case MVT::isVoid: break;
175 case MVT::i1:
176 case MVT::i8:
177 case MVT::i16:
178 case MVT::i32:
179 MF.addLiveOut(X86::EAX);
180 break;
181 case MVT::i64:
182 MF.addLiveOut(X86::EAX);
183 MF.addLiveOut(X86::EDX);
184 break;
185 case MVT::f32:
186 case MVT::f64:
187 MF.addLiveOut(X86::ST0);
188 break;
189 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000190 return ArgValues;
191}
192
Chris Lattner5188ad72005-01-08 19:28:19 +0000193std::pair<SDOperand, SDOperand>
194X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000195 const Type *RetTy, bool isVarArg,
196 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000197 // Count how many bytes are to be pushed on the stack.
198 unsigned NumBytes = 0;
199
200 if (Args.empty()) {
201 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000202 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
203 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000204 } else {
205 for (unsigned i = 0, e = Args.size(); i != e; ++i)
206 switch (getValueType(Args[i].second)) {
207 default: assert(0 && "Unknown value type!");
208 case MVT::i1:
209 case MVT::i8:
210 case MVT::i16:
211 case MVT::i32:
212 case MVT::f32:
213 NumBytes += 4;
214 break;
215 case MVT::i64:
216 case MVT::f64:
217 NumBytes += 8;
218 break;
219 }
220
Chris Lattner5188ad72005-01-08 19:28:19 +0000221 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
222 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000223
224 // Arguments go on the stack in reverse order, as specified by the ABI.
225 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000226 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
227 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000228 std::vector<SDOperand> Stores;
229
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000230 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
231 unsigned ArgReg;
232 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
233 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
234
235 switch (getValueType(Args[i].second)) {
236 default: assert(0 && "Unexpected ValueType for argument!");
237 case MVT::i1:
238 case MVT::i8:
239 case MVT::i16:
240 // Promote the integer to 32 bits. If the input type is signed use a
241 // sign extend, otherwise use a zero extend.
242 if (Args[i].second->isSigned())
243 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
244 else
245 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
246
247 // FALL THROUGH
248 case MVT::i32:
249 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000250 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000251 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000252 ArgOffset += 4;
253 break;
254 case MVT::i64:
255 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000256 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000257 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000258 ArgOffset += 8;
259 break;
260 }
261 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000262 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000263 }
264
265 std::vector<MVT::ValueType> RetVals;
266 MVT::ValueType RetTyVT = getValueType(RetTy);
267 if (RetTyVT != MVT::isVoid)
268 RetVals.push_back(RetTyVT);
269 RetVals.push_back(MVT::Other);
270
Chris Lattner5188ad72005-01-08 19:28:19 +0000271 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000272 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000273 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
274 DAG.getConstant(NumBytes, getPointerTy()));
275 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000276}
277
Chris Lattner14824582005-01-09 00:01:27 +0000278std::pair<SDOperand, SDOperand>
279X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
280 // vastart just returns the address of the VarArgsFrameIndex slot.
281 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
282}
283
284std::pair<SDOperand,SDOperand> X86TargetLowering::
285LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
286 const Type *ArgTy, SelectionDAG &DAG) {
287 MVT::ValueType ArgVT = getValueType(ArgTy);
288 SDOperand Result;
289 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000290 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000291 } else {
292 unsigned Amt;
293 if (ArgVT == MVT::i32)
294 Amt = 4;
295 else {
296 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
297 "Other types should have been promoted for varargs!");
298 Amt = 8;
299 }
300 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
301 DAG.getConstant(Amt, VAList.getValueType()));
302 }
303 return std::make_pair(Result, Chain);
304}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000305
Chris Lattner14824582005-01-09 00:01:27 +0000306
307std::pair<SDOperand, SDOperand> X86TargetLowering::
308LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
309 SelectionDAG &DAG) {
310 SDOperand Result;
311 if (Depth) // Depths > 0 not supported yet!
312 Result = DAG.getConstant(0, getPointerTy());
313 else {
314 if (ReturnAddrIndex == 0) {
315 // Set up a frame object for the return address.
316 MachineFunction &MF = DAG.getMachineFunction();
317 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
318 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000319
Chris Lattner14824582005-01-09 00:01:27 +0000320 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
321
322 if (!isFrameAddress)
323 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000324 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000325 else
326 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
327 DAG.getConstant(4, MVT::i32));
328 }
329 return std::make_pair(Result, Chain);
330}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000331
332
Chris Lattner98a8ba02005-01-18 01:06:26 +0000333namespace {
334 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
335 /// SDOperand's instead of register numbers for the leaves of the matched
336 /// tree.
337 struct X86ISelAddressMode {
338 enum {
339 RegBase,
340 FrameIndexBase,
341 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000342
Chris Lattner98a8ba02005-01-18 01:06:26 +0000343 struct { // This is really a union, discriminated by BaseType!
344 SDOperand Reg;
345 int FrameIndex;
346 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000347
Chris Lattner98a8ba02005-01-18 01:06:26 +0000348 unsigned Scale;
349 SDOperand IndexReg;
350 unsigned Disp;
351 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000352
Chris Lattner98a8ba02005-01-18 01:06:26 +0000353 X86ISelAddressMode()
354 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
355 }
356 };
357}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000358
359
360namespace {
361 Statistic<>
362 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
363
364 //===--------------------------------------------------------------------===//
365 /// ISel - X86 specific code to select X86 machine instructions for
366 /// SelectionDAG operations.
367 ///
368 class ISel : public SelectionDAGISel {
369 /// ContainsFPCode - Every instruction we select that uses or defines a FP
370 /// register should set this to true.
371 bool ContainsFPCode;
372
373 /// X86Lowering - This object fully describes how to lower LLVM code to an
374 /// X86-specific SelectionDAG.
375 X86TargetLowering X86Lowering;
376
Chris Lattner11333092005-01-11 03:11:44 +0000377 /// RegPressureMap - This keeps an approximate count of the number of
378 /// registers required to evaluate each node in the graph.
379 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000380
381 /// ExprMap - As shared expressions are codegen'd, we keep track of which
382 /// vreg the value is produced in, so we only emit one copy of each compiled
383 /// tree.
384 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000385
386 public:
387 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
388 }
389
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000390 virtual const char *getPassName() const {
391 return "X86 Pattern Instruction Selection";
392 }
393
Chris Lattner11333092005-01-11 03:11:44 +0000394 unsigned getRegPressure(SDOperand O) {
395 return RegPressureMap[O.Val];
396 }
397 unsigned ComputeRegPressure(SDOperand O);
398
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000399 /// InstructionSelectBasicBlock - This callback is invoked by
400 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000401 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000402
Chris Lattner44129b52005-01-25 20:03:11 +0000403 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
404 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000405 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000406 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000407
Chris Lattner30ea1e92005-01-19 07:37:26 +0000408 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000409 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000410 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000411 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
412 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000413 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000414
415 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
416 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
417 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000418 void Select(SDOperand N);
419 };
420}
421
Chris Lattner7dbcb752005-01-12 04:21:28 +0000422/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
423/// when it has created a SelectionDAG for us to codegen.
424void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
425 // While we're doing this, keep track of whether we see any FP code for
426 // FP_REG_KILL insertion.
427 ContainsFPCode = false;
428
429 // Scan the PHI nodes that already are inserted into this basic block. If any
430 // of them is a PHI of a floating point value, we need to insert an
431 // FP_REG_KILL.
432 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
433 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
434 I != E; ++I) {
435 assert(I->getOpcode() == X86::PHI &&
436 "Isn't just PHI nodes?");
437 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
438 X86::RFPRegisterClass) {
439 ContainsFPCode = true;
440 break;
441 }
442 }
443
444 // Compute the RegPressureMap, which is an approximation for the number of
445 // registers required to compute each node.
446 ComputeRegPressure(DAG.getRoot());
447
448 // Codegen the basic block.
449 Select(DAG.getRoot());
450
451 // Finally, look at all of the successors of this block. If any contain a PHI
452 // node of FP type, we need to insert an FP_REG_KILL in this block.
453 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
454 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
455 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
456 I != E && I->getOpcode() == X86::PHI; ++I) {
457 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
458 X86::RFPRegisterClass) {
459 ContainsFPCode = true;
460 break;
461 }
462 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000463
Chris Lattner7dbcb752005-01-12 04:21:28 +0000464 // Insert FP_REG_KILL instructions into basic blocks that need them. This
465 // only occurs due to the floating point stackifier not being aggressive
466 // enough to handle arbitrary global stackification.
467 //
468 // Currently we insert an FP_REG_KILL instruction into each block that uses or
469 // defines a floating point virtual register.
470 //
471 // When the global register allocators (like linear scan) finally update live
472 // variable analysis, we can keep floating point values in registers across
473 // basic blocks. This will be a huge win, but we are waiting on the global
474 // allocators before we can do this.
475 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000476 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000477 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
478 ++NumFPKill;
479 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000480
Chris Lattner7dbcb752005-01-12 04:21:28 +0000481 // Clear state used for selection.
482 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000483 RegPressureMap.clear();
484}
485
486
Chris Lattner11333092005-01-11 03:11:44 +0000487// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
488// for the number of registers required to compute each node. This is basically
489// computing a generalized form of the Sethi-Ullman number for each node.
490unsigned ISel::ComputeRegPressure(SDOperand O) {
491 SDNode *N = O.Val;
492 unsigned &Result = RegPressureMap[N];
493 if (Result) return Result;
494
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000495 // FIXME: Should operations like CALL (which clobber lots o regs) have a
496 // higher fixed cost??
497
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000498 if (N->getNumOperands() == 0) {
499 Result = 1;
500 } else {
501 unsigned MaxRegUse = 0;
502 unsigned NumExtraMaxRegUsers = 0;
503 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
504 unsigned Regs;
505 if (N->getOperand(i).getOpcode() == ISD::Constant)
506 Regs = 0;
507 else
508 Regs = ComputeRegPressure(N->getOperand(i));
509 if (Regs > MaxRegUse) {
510 MaxRegUse = Regs;
511 NumExtraMaxRegUsers = 0;
512 } else if (Regs == MaxRegUse &&
513 N->getOperand(i).getValueType() != MVT::Other) {
514 ++NumExtraMaxRegUsers;
515 }
Chris Lattner11333092005-01-11 03:11:44 +0000516 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000517
518 if (O.getOpcode() != ISD::TokenFactor)
519 Result = MaxRegUse+NumExtraMaxRegUsers;
520 else
Chris Lattner869e0432005-01-17 23:02:13 +0000521 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000522 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000523
Chris Lattner837caa72005-01-11 23:21:30 +0000524 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000525 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000526}
527
Chris Lattnerbf52d492005-01-20 16:50:16 +0000528/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
529/// The DAG cannot have cycles in it, by definition, so the visited set is not
530/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
531/// reuse, so it prevents exponential cases.
532///
533static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
534 std::set<SDNode*> &Visited) {
535 if (N == Op) return true; // Found it.
536 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000537 if (Node->getNumOperands() == 0 || // Leaf?
538 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000539 if (!Visited.insert(Node).second) return false; // Already visited?
540
541 // Recurse for the first N-1 operands.
542 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
543 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
544 return true;
545
546 // Tail recurse for the last operand.
547 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
548}
549
Chris Lattner98a8ba02005-01-18 01:06:26 +0000550X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
551 X86AddressMode Result;
552
553 // If we need to emit two register operands, emit the one with the highest
554 // register pressure first.
555 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
556 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000557 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000558 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000559 std::set<SDNode*> Visited;
560 EmitBaseThenIndex = true;
561 // If Base ends up pointing to Index, we must emit index first. This is
562 // because of the way we fold loads, we may end up doing bad things with
563 // the folded add.
564 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
565 EmitBaseThenIndex = false;
566 } else {
567 std::set<SDNode*> Visited;
568 EmitBaseThenIndex = false;
569 // If Base ends up pointing to Index, we must emit index first. This is
570 // because of the way we fold loads, we may end up doing bad things with
571 // the folded add.
572 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
573 EmitBaseThenIndex = true;
574 }
575
576 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000577 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
578 Result.IndexReg = SelectExpr(IAM.IndexReg);
579 } else {
580 Result.IndexReg = SelectExpr(IAM.IndexReg);
581 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
582 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000583
Chris Lattner98a8ba02005-01-18 01:06:26 +0000584 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
585 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
586 } else if (IAM.IndexReg.Val) {
587 Result.IndexReg = SelectExpr(IAM.IndexReg);
588 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000589
Chris Lattner98a8ba02005-01-18 01:06:26 +0000590 switch (IAM.BaseType) {
591 case X86ISelAddressMode::RegBase:
592 Result.BaseType = X86AddressMode::RegBase;
593 break;
594 case X86ISelAddressMode::FrameIndexBase:
595 Result.BaseType = X86AddressMode::FrameIndexBase;
596 Result.Base.FrameIndex = IAM.Base.FrameIndex;
597 break;
598 default:
599 assert(0 && "Unknown base type!");
600 break;
601 }
602 Result.Scale = IAM.Scale;
603 Result.Disp = IAM.Disp;
604 Result.GV = IAM.GV;
605 return Result;
606}
607
608/// SelectAddress - Pattern match the maximal addressing mode for this node and
609/// emit all of the leaf registers.
610void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
611 X86ISelAddressMode IAM;
612 MatchAddress(N, IAM);
613 AM = SelectAddrExprs(IAM);
614}
615
616/// MatchAddress - Add the specified node to the specified addressing mode,
617/// returning true if it cannot be done. This just pattern matches for the
618/// addressing mode, it does not cause any code to be emitted. For that, use
619/// SelectAddress.
620bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000621 switch (N.getOpcode()) {
622 default: break;
623 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000624 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
625 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000626 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
627 return false;
628 }
629 break;
630 case ISD::GlobalAddress:
631 if (AM.GV == 0) {
632 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
633 return false;
634 }
635 break;
636 case ISD::Constant:
637 AM.Disp += cast<ConstantSDNode>(N)->getValue();
638 return false;
639 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000640 // We might have folded the load into this shift, so don't regen the value
641 // if so.
642 if (ExprMap.count(N)) break;
643
Chris Lattner98a8ba02005-01-18 01:06:26 +0000644 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000645 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
646 unsigned Val = CN->getValue();
647 if (Val == 1 || Val == 2 || Val == 3) {
648 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000649 SDOperand ShVal = N.Val->getOperand(0);
650
651 // Okay, we know that we have a scale by now. However, if the scaled
652 // value is an add of something and a constant, we can fold the
653 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000654 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000655 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000656 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000657 ConstantSDNode *AddVal =
658 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
659 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000660 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000661 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000662 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000663 return false;
664 }
665 }
666 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000667 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000668 // We might have folded the load into this mul, so don't regen the value if
669 // so.
670 if (ExprMap.count(N)) break;
671
Chris Lattner947d5442005-01-11 19:37:02 +0000672 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000673 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
674 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000675 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
676 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
677 AM.Scale = unsigned(CN->getValue())-1;
678
679 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000680 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000681
682 // Okay, we know that we have a scale by now. However, if the scaled
683 // value is an add of something and a constant, we can fold the
684 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000685 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000686 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000687 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000688 ConstantSDNode *AddVal =
689 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
690 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000691 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000692 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000693 }
694
695 AM.IndexReg = AM.Base.Reg = Reg;
696 return false;
697 }
698 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000699
700 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000701 // We might have folded the load into this mul, so don't regen the value if
702 // so.
703 if (ExprMap.count(N)) break;
704
Chris Lattner98a8ba02005-01-18 01:06:26 +0000705 X86ISelAddressMode Backup = AM;
706 if (!MatchAddress(N.Val->getOperand(0), AM) &&
707 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000708 return false;
709 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000710 if (!MatchAddress(N.Val->getOperand(1), AM) &&
711 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000712 return false;
713 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000714 break;
715 }
716 }
717
Chris Lattnera95589b2005-01-11 04:40:19 +0000718 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000719 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000720 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000721 if (AM.IndexReg.Val == 0) {
722 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000723 AM.Scale = 1;
724 return false;
725 }
726
727 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000728 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000729 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000730
731 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000732 AM.BaseType = X86ISelAddressMode::RegBase;
733 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000734 return false;
735}
736
737/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
738/// assuming that the temporary registers are in the 8-bit register class.
739///
740/// Tmp1 = setcc1
741/// Tmp2 = setcc2
742/// DestReg = logicalop Tmp1, Tmp2
743///
744static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
745 unsigned SetCC2, unsigned LogicalOp,
746 unsigned DestReg) {
747 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
748 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
749 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
750 BuildMI(BB, SetCC1, 0, Tmp1);
751 BuildMI(BB, SetCC2, 0, Tmp2);
752 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
753}
754
755/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
756/// condition codes match the specified SetCCOpcode. Note that some conditions
757/// require multiple instructions to generate the correct value.
758static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
759 ISD::CondCode SetCCOpcode, bool isFP) {
760 unsigned Opc;
761 if (!isFP) {
762 switch (SetCCOpcode) {
763 default: assert(0 && "Illegal integer SetCC!");
764 case ISD::SETEQ: Opc = X86::SETEr; break;
765 case ISD::SETGT: Opc = X86::SETGr; break;
766 case ISD::SETGE: Opc = X86::SETGEr; break;
767 case ISD::SETLT: Opc = X86::SETLr; break;
768 case ISD::SETLE: Opc = X86::SETLEr; break;
769 case ISD::SETNE: Opc = X86::SETNEr; break;
770 case ISD::SETULT: Opc = X86::SETBr; break;
771 case ISD::SETUGT: Opc = X86::SETAr; break;
772 case ISD::SETULE: Opc = X86::SETBEr; break;
773 case ISD::SETUGE: Opc = X86::SETAEr; break;
774 }
775 } else {
776 // On a floating point condition, the flags are set as follows:
777 // ZF PF CF op
778 // 0 | 0 | 0 | X > Y
779 // 0 | 0 | 1 | X < Y
780 // 1 | 0 | 0 | X == Y
781 // 1 | 1 | 1 | unordered
782 //
783 switch (SetCCOpcode) {
784 default: assert(0 && "Invalid FP setcc!");
785 case ISD::SETUEQ:
786 case ISD::SETEQ:
787 Opc = X86::SETEr; // True if ZF = 1
788 break;
789 case ISD::SETOGT:
790 case ISD::SETGT:
791 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
792 break;
793 case ISD::SETOGE:
794 case ISD::SETGE:
795 Opc = X86::SETAEr; // True if CF = 0
796 break;
797 case ISD::SETULT:
798 case ISD::SETLT:
799 Opc = X86::SETBr; // True if CF = 1
800 break;
801 case ISD::SETULE:
802 case ISD::SETLE:
803 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
804 break;
805 case ISD::SETONE:
806 case ISD::SETNE:
807 Opc = X86::SETNEr; // True if ZF = 0
808 break;
809 case ISD::SETUO:
810 Opc = X86::SETPr; // True if PF = 1
811 break;
812 case ISD::SETO:
813 Opc = X86::SETNPr; // True if PF = 0
814 break;
815 case ISD::SETOEQ: // !PF & ZF
816 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
817 return;
818 case ISD::SETOLT: // !PF & CF
819 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
820 return;
821 case ISD::SETOLE: // !PF & (CF || ZF)
822 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
823 return;
824 case ISD::SETUGT: // PF | (!ZF & !CF)
825 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
826 return;
827 case ISD::SETUGE: // PF | !CF
828 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
829 return;
830 case ISD::SETUNE: // PF | !ZF
831 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
832 return;
833 }
834 }
835 BuildMI(BB, Opc, 0, DestReg);
836}
837
838
839/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
840/// the Dest block if the Cond condition is true. If we cannot fold this
841/// condition into the branch, return true.
842///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000843bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
844 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000845 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
846 // B) using two conditional branches instead of one condbr, two setcc's, and
847 // an or.
848 if ((Cond.getOpcode() == ISD::OR ||
849 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
850 // And and or set the flags for us, so there is no need to emit a TST of the
851 // result. It is only safe to do this if there is only a single use of the
852 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000853 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000854 SelectExpr(Cond);
855 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
856 return false;
857 }
858
859 // Codegen br not C -> JE.
860 if (Cond.getOpcode() == ISD::XOR)
861 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
862 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000863 unsigned CondR;
864 if (getRegPressure(Chain) > getRegPressure(Cond)) {
865 Select(Chain);
866 CondR = SelectExpr(Cond.Val->getOperand(0));
867 } else {
868 CondR = SelectExpr(Cond.Val->getOperand(0));
869 Select(Chain);
870 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000871 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
872 BuildMI(BB, X86::JE, 1).addMBB(Dest);
873 return false;
874 }
875
876 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
877 if (SetCC == 0)
878 return true; // Can only handle simple setcc's so far.
879
880 unsigned Opc;
881
882 // Handle integer conditions first.
883 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
884 switch (SetCC->getCondition()) {
885 default: assert(0 && "Illegal integer SetCC!");
886 case ISD::SETEQ: Opc = X86::JE; break;
887 case ISD::SETGT: Opc = X86::JG; break;
888 case ISD::SETGE: Opc = X86::JGE; break;
889 case ISD::SETLT: Opc = X86::JL; break;
890 case ISD::SETLE: Opc = X86::JLE; break;
891 case ISD::SETNE: Opc = X86::JNE; break;
892 case ISD::SETULT: Opc = X86::JB; break;
893 case ISD::SETUGT: Opc = X86::JA; break;
894 case ISD::SETULE: Opc = X86::JBE; break;
895 case ISD::SETUGE: Opc = X86::JAE; break;
896 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000897 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000898 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000899 BuildMI(BB, Opc, 1).addMBB(Dest);
900 return false;
901 }
902
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000903 unsigned Opc2 = 0; // Second branch if needed.
904
905 // On a floating point condition, the flags are set as follows:
906 // ZF PF CF op
907 // 0 | 0 | 0 | X > Y
908 // 0 | 0 | 1 | X < Y
909 // 1 | 0 | 0 | X == Y
910 // 1 | 1 | 1 | unordered
911 //
912 switch (SetCC->getCondition()) {
913 default: assert(0 && "Invalid FP setcc!");
914 case ISD::SETUEQ:
915 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
916 case ISD::SETOGT:
917 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
918 case ISD::SETOGE:
919 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
920 case ISD::SETULT:
921 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
922 case ISD::SETULE:
923 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
924 case ISD::SETONE:
925 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
926 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
927 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
928 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
929 Opc = X86::JA; // ZF = 0 & CF = 0
930 Opc2 = X86::JP; // PF = 1
931 break;
932 case ISD::SETUGE: // PF = 1 | CF = 0
933 Opc = X86::JAE; // CF = 0
934 Opc2 = X86::JP; // PF = 1
935 break;
936 case ISD::SETUNE: // PF = 1 | ZF = 0
937 Opc = X86::JNE; // ZF = 0
938 Opc2 = X86::JP; // PF = 1
939 break;
940 case ISD::SETOEQ: // PF = 0 & ZF = 1
941 //X86::JNP, X86::JE
942 //X86::AND8rr
943 return true; // FIXME: Emit more efficient code for this branch.
944 case ISD::SETOLT: // PF = 0 & CF = 1
945 //X86::JNP, X86::JB
946 //X86::AND8rr
947 return true; // FIXME: Emit more efficient code for this branch.
948 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
949 //X86::JNP, X86::JBE
950 //X86::AND8rr
951 return true; // FIXME: Emit more efficient code for this branch.
952 }
953
Chris Lattner6c07aee2005-01-11 04:06:27 +0000954 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000955 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000956 BuildMI(BB, Opc, 1).addMBB(Dest);
957 if (Opc2)
958 BuildMI(BB, Opc2, 1).addMBB(Dest);
959 return false;
960}
961
Chris Lattner24aad1b2005-01-10 22:10:13 +0000962/// EmitSelectCC - Emit code into BB that performs a select operation between
963/// the two registers RTrue and RFalse, generating a result into RDest. Return
964/// true if the fold cannot be performed.
965///
966void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
967 unsigned RTrue, unsigned RFalse, unsigned RDest) {
968 enum Condition {
969 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
970 NOT_SET
971 } CondCode = NOT_SET;
972
973 static const unsigned CMOVTAB16[] = {
974 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
975 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000976 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000977 };
978 static const unsigned CMOVTAB32[] = {
979 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
980 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000981 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000982 };
983 static const unsigned CMOVTABFP[] = {
984 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
985 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
986 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
987 };
988
989 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
990 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
991 switch (SetCC->getCondition()) {
992 default: assert(0 && "Unknown integer comparison!");
993 case ISD::SETEQ: CondCode = EQ; break;
994 case ISD::SETGT: CondCode = GT; break;
995 case ISD::SETGE: CondCode = GE; break;
996 case ISD::SETLT: CondCode = LT; break;
997 case ISD::SETLE: CondCode = LE; break;
998 case ISD::SETNE: CondCode = NE; break;
999 case ISD::SETULT: CondCode = B; break;
1000 case ISD::SETUGT: CondCode = A; break;
1001 case ISD::SETULE: CondCode = BE; break;
1002 case ISD::SETUGE: CondCode = AE; break;
1003 }
1004 } else {
1005 // On a floating point condition, the flags are set as follows:
1006 // ZF PF CF op
1007 // 0 | 0 | 0 | X > Y
1008 // 0 | 0 | 1 | X < Y
1009 // 1 | 0 | 0 | X == Y
1010 // 1 | 1 | 1 | unordered
1011 //
1012 switch (SetCC->getCondition()) {
1013 default: assert(0 && "Unknown FP comparison!");
1014 case ISD::SETUEQ:
1015 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1016 case ISD::SETOGT:
1017 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1018 case ISD::SETOGE:
1019 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1020 case ISD::SETULT:
1021 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1022 case ISD::SETULE:
1023 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1024 case ISD::SETONE:
1025 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1026 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1027 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1028 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1029 case ISD::SETUGE: // PF = 1 | CF = 0
1030 case ISD::SETUNE: // PF = 1 | ZF = 0
1031 case ISD::SETOEQ: // PF = 0 & ZF = 1
1032 case ISD::SETOLT: // PF = 0 & CF = 1
1033 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1034 // We cannot emit this comparison as a single cmov.
1035 break;
1036 }
1037 }
1038 }
1039
1040 unsigned Opc = 0;
1041 if (CondCode != NOT_SET) {
1042 switch (SVT) {
1043 default: assert(0 && "Cannot select this type!");
1044 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1045 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001046 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001047 }
1048 }
1049
1050 // Finally, if we weren't able to fold this, just emit the condition and test
1051 // it.
1052 if (CondCode == NOT_SET || Opc == 0) {
1053 // Get the condition into the zero flag.
1054 unsigned CondReg = SelectExpr(Cond);
1055 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1056
1057 switch (SVT) {
1058 default: assert(0 && "Cannot select this type!");
1059 case MVT::i16: Opc = X86::CMOVE16rr; break;
1060 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001061 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001062 }
1063 } else {
1064 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001065 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001066 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001067 }
1068 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1069}
1070
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001071void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001072 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001073 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1074 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001075 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001076 switch (RHS.getValueType()) {
1077 default: break;
1078 case MVT::i1:
1079 case MVT::i8: Opc = X86::CMP8mi; break;
1080 case MVT::i16: Opc = X86::CMP16mi; break;
1081 case MVT::i32: Opc = X86::CMP32mi; break;
1082 }
1083 if (Opc) {
1084 X86AddressMode AM;
1085 EmitFoldedLoad(LHS, AM);
1086 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1087 return;
1088 }
1089 }
1090
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001091 switch (RHS.getValueType()) {
1092 default: break;
1093 case MVT::i1:
1094 case MVT::i8: Opc = X86::CMP8ri; break;
1095 case MVT::i16: Opc = X86::CMP16ri; break;
1096 case MVT::i32: Opc = X86::CMP32ri; break;
1097 }
1098 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001099 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001100 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1101 return;
1102 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001103 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1104 if (CN->isExactlyValue(+0.0) ||
1105 CN->isExactlyValue(-0.0)) {
1106 unsigned Reg = SelectExpr(LHS);
1107 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1108 BuildMI(BB, X86::FNSTSW8r, 0);
1109 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001110 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001111 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001112 }
1113
Chris Lattneref6806c2005-01-12 02:02:48 +00001114 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001115 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001116 switch (RHS.getValueType()) {
1117 default: break;
1118 case MVT::i1:
1119 case MVT::i8: Opc = X86::CMP8mr; break;
1120 case MVT::i16: Opc = X86::CMP16mr; break;
1121 case MVT::i32: Opc = X86::CMP32mr; break;
1122 }
1123 if (Opc) {
1124 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001125 EmitFoldedLoad(LHS, AM);
1126 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001127 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1128 return;
1129 }
1130 }
1131
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001132 switch (LHS.getValueType()) {
1133 default: assert(0 && "Cannot compare this value!");
1134 case MVT::i1:
1135 case MVT::i8: Opc = X86::CMP8rr; break;
1136 case MVT::i16: Opc = X86::CMP16rr; break;
1137 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001138 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001139 }
Chris Lattner11333092005-01-11 03:11:44 +00001140 unsigned Tmp1, Tmp2;
1141 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1142 Tmp1 = SelectExpr(LHS);
1143 Tmp2 = SelectExpr(RHS);
1144 } else {
1145 Tmp2 = SelectExpr(RHS);
1146 Tmp1 = SelectExpr(LHS);
1147 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001148 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1149}
1150
Chris Lattnera5ade062005-01-11 21:19:59 +00001151/// isFoldableLoad - Return true if this is a load instruction that can safely
1152/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001153bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1154 if (Op.getOpcode() == ISD::LOAD) {
1155 // FIXME: currently can't fold constant pool indexes.
1156 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1157 return false;
1158 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1159 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1160 // FIXME: currently can't fold constant pool indexes.
1161 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1162 return false;
1163 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001164 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001165 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001166
1167 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001168 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1169 if (ExprMap.count(Op.getValue(1))) return false;
1170 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001171 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001172
Chris Lattner4ff348b2005-01-17 06:26:58 +00001173 // If there is not just one use of its value, we cannot fold.
1174 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1175
1176 // Finally, we cannot fold the load into the operation if this would induce a
1177 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1178 // operand of the operation we are folding the load into) can possible use the
1179 // chain node defined by the load.
1180 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1181 std::set<SDNode*> Visited;
1182 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1183 return false;
1184 }
1185 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001186}
1187
Chris Lattner4ff348b2005-01-17 06:26:58 +00001188
Chris Lattnera5ade062005-01-11 21:19:59 +00001189/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1190/// and compute the address being loaded into AM.
1191void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1192 SDOperand Chain = Op.getOperand(0);
1193 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001194
Chris Lattnera5ade062005-01-11 21:19:59 +00001195 if (getRegPressure(Chain) > getRegPressure(Address)) {
1196 Select(Chain);
1197 SelectAddress(Address, AM);
1198 } else {
1199 SelectAddress(Address, AM);
1200 Select(Chain);
1201 }
1202
1203 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001204 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1205 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001206 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001207 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001208}
1209
Chris Lattner30ea1e92005-01-19 07:37:26 +00001210// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1211// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1212// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1213// return true.
1214bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001215 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1216 // good!
1217 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1218 std::swap(Op1, Op2); // Op1 is the SHL now.
1219 } else {
1220 return false; // No match
1221 }
1222
1223 SDOperand ShlVal = Op1.getOperand(0);
1224 SDOperand ShlAmt = Op1.getOperand(1);
1225 SDOperand ShrVal = Op2.getOperand(0);
1226 SDOperand ShrAmt = Op2.getOperand(1);
1227
Chris Lattner30ea1e92005-01-19 07:37:26 +00001228 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1229
Chris Lattner85716372005-01-19 06:18:43 +00001230 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1231 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1232 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001233 if (SubCST->getValue() == RegSize) {
1234 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001235 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001236 if (ShrVal == ShlVal) {
1237 unsigned Reg, ShAmt;
1238 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1239 Reg = SelectExpr(ShrVal);
1240 ShAmt = SelectExpr(ShrAmt);
1241 } else {
1242 ShAmt = SelectExpr(ShrAmt);
1243 Reg = SelectExpr(ShrVal);
1244 }
1245 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1246 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1247 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1248 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1249 return true;
1250 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001251 unsigned AReg, BReg;
1252 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001253 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001254 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001255 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001256 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001257 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001258 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001259 unsigned ShAmt = SelectExpr(ShrAmt);
1260 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1261 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1262 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001263 return true;
1264 }
1265 }
1266
Chris Lattner4053b1e2005-01-19 08:07:05 +00001267 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1268 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1269 if (SubCST->getValue() == RegSize) {
1270 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1271 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1272 if (ShrVal == ShlVal) {
1273 unsigned Reg, ShAmt;
1274 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1275 Reg = SelectExpr(ShrVal);
1276 ShAmt = SelectExpr(ShlAmt);
1277 } else {
1278 ShAmt = SelectExpr(ShlAmt);
1279 Reg = SelectExpr(ShrVal);
1280 }
1281 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1282 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1283 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1284 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1285 return true;
1286 } else if (RegSize != 8) {
1287 unsigned AReg, BReg;
1288 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001289 AReg = SelectExpr(ShlVal);
1290 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001291 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001292 BReg = SelectExpr(ShrVal);
1293 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001294 }
1295 unsigned ShAmt = SelectExpr(ShlAmt);
1296 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1297 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1298 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1299 return true;
1300 }
1301 }
Chris Lattner85716372005-01-19 06:18:43 +00001302
Chris Lattner4053b1e2005-01-19 08:07:05 +00001303 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1304 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1305 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1306 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1307 // (A >> 5) | (A << 27) --> ROR A, 5
1308 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1309 if (ShrVal == ShlVal) {
1310 unsigned Reg = SelectExpr(ShrVal);
1311 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1312 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1313 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1314 return true;
1315 } else if (RegSize != 8) {
1316 unsigned AReg, BReg;
1317 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001318 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001319 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001320 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001321 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001322 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001323 }
1324 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1325 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1326 .addImm(ShrCst->getValue());
1327 return true;
1328 }
1329 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001330
Chris Lattner85716372005-01-19 06:18:43 +00001331 return false;
1332}
1333
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001334unsigned ISel::SelectExpr(SDOperand N) {
1335 unsigned Result;
1336 unsigned Tmp1, Tmp2, Tmp3;
1337 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001338 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001339 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001340
Chris Lattner7f2afac2005-01-14 22:37:41 +00001341 if (Node->getOpcode() == ISD::CopyFromReg) {
1342 // FIXME: Handle copy from physregs!
1343
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001344 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001345 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001346 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001347
Chris Lattnera5ade062005-01-11 21:19:59 +00001348 unsigned &Reg = ExprMap[N];
1349 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001350
Chris Lattnerb38a7492005-04-02 04:01:14 +00001351 switch (N.getOpcode()) {
1352 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001353 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001354 MakeReg(N.getValueType()) : 1;
1355 break;
1356 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001357 // If this is a call instruction, make sure to prepare ALL of the result
1358 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001359 if (Node->getNumValues() == 1)
1360 Reg = Result = 1; // Void call, just a chain.
1361 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001362 Result = MakeReg(Node->getValueType(0));
1363 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001364 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001365 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001366 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001367 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001368 break;
1369 case ISD::ADD_PARTS:
1370 case ISD::SUB_PARTS:
1371 case ISD::SHL_PARTS:
1372 case ISD::SRL_PARTS:
1373 case ISD::SRA_PARTS:
1374 Result = MakeReg(Node->getValueType(0));
1375 ExprMap[N.getValue(0)] = Result;
1376 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1377 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1378 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001379 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001380
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001381 switch (N.getOpcode()) {
1382 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001383 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001384 assert(0 && "Node not handled!\n");
1385 case ISD::FrameIndex:
1386 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1387 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1388 return Result;
1389 case ISD::ConstantPool:
1390 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1391 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1392 return Result;
1393 case ISD::ConstantFP:
1394 ContainsFPCode = true;
1395 Tmp1 = Result; // Intermediate Register
1396 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1397 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1398 Tmp1 = MakeReg(MVT::f64);
1399
1400 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1401 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1402 BuildMI(BB, X86::FLD0, 0, Tmp1);
1403 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1404 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1405 BuildMI(BB, X86::FLD1, 0, Tmp1);
1406 else
1407 assert(0 && "Unexpected constant!");
1408 if (Tmp1 != Result)
1409 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1410 return Result;
1411 case ISD::Constant:
1412 switch (N.getValueType()) {
1413 default: assert(0 && "Cannot use constants of this type!");
1414 case MVT::i1:
1415 case MVT::i8: Opc = X86::MOV8ri; break;
1416 case MVT::i16: Opc = X86::MOV16ri; break;
1417 case MVT::i32: Opc = X86::MOV32ri; break;
1418 }
1419 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1420 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001421 case ISD::UNDEF:
1422 if (Node->getValueType(0) == MVT::f64) {
1423 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1424 BuildMI(BB, X86::FLD0, 0, Result);
1425 } else {
1426 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1427 }
1428 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001429 case ISD::GlobalAddress: {
1430 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1431 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1432 return Result;
1433 }
1434 case ISD::ExternalSymbol: {
1435 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1436 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1437 return Result;
1438 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001439 case ISD::ZERO_EXTEND: {
1440 int DestIs16 = N.getValueType() == MVT::i16;
1441 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001442
1443 // FIXME: This hack is here for zero extension casts from bool to i8. This
1444 // would not be needed if bools were promoted by Legalize.
1445 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001446 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001447 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1448 return Result;
1449 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001450
Chris Lattner4ff348b2005-01-17 06:26:58 +00001451 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001452 static const unsigned Opc[3] = {
1453 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1454 };
1455
1456 X86AddressMode AM;
1457 EmitFoldedLoad(N.getOperand(0), AM);
1458 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001459
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001460 return Result;
1461 }
1462
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001463 static const unsigned Opc[3] = {
1464 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1465 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001466 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001467 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1468 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001469 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001470 case ISD::SIGN_EXTEND: {
1471 int DestIs16 = N.getValueType() == MVT::i16;
1472 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1473
Chris Lattner590d8002005-01-09 18:52:44 +00001474 // FIXME: Legalize should promote bools to i8!
1475 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1476 "Sign extend from bool not implemented!");
1477
Chris Lattner4ff348b2005-01-17 06:26:58 +00001478 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001479 static const unsigned Opc[3] = {
1480 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1481 };
1482
1483 X86AddressMode AM;
1484 EmitFoldedLoad(N.getOperand(0), AM);
1485 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1486 return Result;
1487 }
1488
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001489 static const unsigned Opc[3] = {
1490 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1491 };
1492 Tmp1 = SelectExpr(N.getOperand(0));
1493 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1494 return Result;
1495 }
1496 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001497 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001498 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001499 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001500 switch (N.getValueType()) {
1501 default: assert(0 && "Unknown truncate!");
1502 case MVT::i1:
1503 case MVT::i8: Opc = X86::MOV8rm; break;
1504 case MVT::i16: Opc = X86::MOV16rm; break;
1505 }
1506 X86AddressMode AM;
1507 EmitFoldedLoad(N.getOperand(0), AM);
1508 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1509 return Result;
1510 }
1511
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001512 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1513 // a move out of AX or AL.
1514 switch (N.getOperand(0).getValueType()) {
1515 default: assert(0 && "Unknown truncate!");
1516 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1517 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1518 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1519 }
1520 Tmp1 = SelectExpr(N.getOperand(0));
1521 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1522
1523 switch (N.getValueType()) {
1524 default: assert(0 && "Unknown truncate!");
1525 case MVT::i1:
1526 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1527 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1528 }
1529 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1530 return Result;
1531
Chris Lattner590d8002005-01-09 18:52:44 +00001532 case ISD::SINT_TO_FP:
1533 case ISD::UINT_TO_FP: {
1534 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001535 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001536
1537 // Promote the integer to a type supported by FLD. We do this because there
1538 // are no unsigned FLD instructions, so we must promote an unsigned value to
1539 // a larger signed value, then use FLD on the larger value.
1540 //
1541 MVT::ValueType PromoteType = MVT::Other;
1542 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1543 unsigned PromoteOpcode = 0;
1544 unsigned RealDestReg = Result;
1545 switch (SrcTy) {
1546 case MVT::i1:
1547 case MVT::i8:
1548 // We don't have the facilities for directly loading byte sized data from
1549 // memory (even signed). Promote it to 16 bits.
1550 PromoteType = MVT::i16;
1551 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1552 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1553 break;
1554 case MVT::i16:
1555 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1556 PromoteType = MVT::i32;
1557 PromoteOpcode = X86::MOVZX32rr16;
1558 }
1559 break;
1560 default:
1561 // Don't fild into the real destination.
1562 if (Node->getOpcode() == ISD::UINT_TO_FP)
1563 Result = MakeReg(Node->getValueType(0));
1564 break;
1565 }
1566
1567 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001568
Chris Lattner590d8002005-01-09 18:52:44 +00001569 if (PromoteType != MVT::Other) {
1570 Tmp2 = MakeReg(PromoteType);
1571 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1572 SrcTy = PromoteType;
1573 Tmp1 = Tmp2;
1574 }
1575
1576 // Spill the integer to memory and reload it from there.
1577 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1578 MachineFunction *F = BB->getParent();
1579 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1580
1581 switch (SrcTy) {
1582 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001583 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001584 // FIXME: this won't work for cast [u]long to FP
1585 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1586 FrameIdx).addReg(Tmp1);
1587 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1588 FrameIdx, 4).addReg(Tmp1+1);
1589 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1590 break;
1591 case MVT::i32:
1592 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1593 FrameIdx).addReg(Tmp1);
1594 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1595 break;
1596 case MVT::i16:
1597 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1598 FrameIdx).addReg(Tmp1);
1599 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1600 break;
1601 default: break; // No promotion required.
1602 }
1603
Chris Lattner085c9952005-01-12 04:00:00 +00001604 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001605 // If this is a cast from uint -> double, we need to be careful when if
1606 // the "sign" bit is set. If so, we don't want to make a negative number,
1607 // we want to make a positive number. Emit code to add an offset if the
1608 // sign bit is set.
1609
1610 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1611 unsigned IsNeg = MakeReg(MVT::i32);
1612 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1613
1614 // Create a CP value that has the offset in one word and 0 in the other.
1615 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1616 0x4f80000000000000ULL);
1617 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1618 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1619 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1620
1621 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1622 // We need special handling for unsigned 64-bit integer sources. If the
1623 // input number has the "sign bit" set, then we loaded it incorrectly as a
1624 // negative 64-bit number. In this case, add an offset value.
1625
1626 // Emit a test instruction to see if the dynamic input value was signed.
1627 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1628
1629 // If the sign bit is set, get a pointer to an offset, otherwise get a
1630 // pointer to a zero.
1631 MachineConstantPool *CP = F->getConstantPool();
1632 unsigned Zero = MakeReg(MVT::i32);
1633 Constant *Null = Constant::getNullValue(Type::UIntTy);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001634 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
Chris Lattner590d8002005-01-09 18:52:44 +00001635 CP->getConstantPoolIndex(Null));
1636 unsigned Offset = MakeReg(MVT::i32);
1637 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001638
Chris Lattner590d8002005-01-09 18:52:44 +00001639 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1640 CP->getConstantPoolIndex(OffsetCst));
1641 unsigned Addr = MakeReg(MVT::i32);
1642 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1643
1644 // Load the constant for an add. FIXME: this could make an 'fadd' that
1645 // reads directly from memory, but we don't support these yet.
1646 unsigned ConstReg = MakeReg(MVT::f64);
1647 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1648
1649 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1650 }
1651 return RealDestReg;
1652 }
1653 case ISD::FP_TO_SINT:
1654 case ISD::FP_TO_UINT: {
1655 // FIXME: Most of this grunt work should be done by legalize!
1656 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1657
1658 // Change the floating point control register to use "round towards zero"
1659 // mode when truncating to an integer value.
1660 //
1661 MachineFunction *F = BB->getParent();
1662 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1663 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1664
1665 // Load the old value of the high byte of the control word...
1666 unsigned HighPartOfCW = MakeReg(MVT::i8);
1667 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1668 CWFrameIdx, 1);
1669
1670 // Set the high part to be round to zero...
1671 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1672 CWFrameIdx, 1).addImm(12);
1673
1674 // Reload the modified control word now...
1675 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001676
Chris Lattner590d8002005-01-09 18:52:44 +00001677 // Restore the memory image of control word to original value
1678 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1679 CWFrameIdx, 1).addReg(HighPartOfCW);
1680
1681 // We don't have the facilities for directly storing byte sized data to
1682 // memory. Promote it to 16 bits. We also must promote unsigned values to
1683 // larger classes because we only have signed FP stores.
1684 MVT::ValueType StoreClass = Node->getValueType(0);
1685 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1686 switch (StoreClass) {
1687 case MVT::i8: StoreClass = MVT::i16; break;
1688 case MVT::i16: StoreClass = MVT::i32; break;
1689 case MVT::i32: StoreClass = MVT::i64; break;
1690 // The following treatment of cLong may not be perfectly right,
1691 // but it survives chains of casts of the form
1692 // double->ulong->double.
1693 case MVT::i64: StoreClass = MVT::i64; break;
1694 default: assert(0 && "Unknown store class!");
1695 }
1696
1697 // Spill the integer to memory and reload it from there.
1698 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1699 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1700
1701 switch (StoreClass) {
1702 default: assert(0 && "Unknown store class!");
1703 case MVT::i16:
1704 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1705 break;
1706 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001707 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001708 break;
1709 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001710 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001711 break;
1712 }
1713
1714 switch (Node->getValueType(0)) {
1715 default:
1716 assert(0 && "Unknown integer type!");
1717 case MVT::i64:
1718 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001719 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001720 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1721 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1722 case MVT::i32:
1723 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1724 break;
1725 case MVT::i16:
1726 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1727 break;
1728 case MVT::i8:
1729 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1730 break;
1731 }
1732
1733 // Reload the original control word now.
1734 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1735 return Result;
1736 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001737 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001738 Op0 = N.getOperand(0);
1739 Op1 = N.getOperand(1);
1740
Chris Lattner44129b52005-01-25 20:03:11 +00001741 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001742 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001743 goto FoldAdd;
1744 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001745
Chris Lattner44129b52005-01-25 20:03:11 +00001746 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001747 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001748 switch (N.getValueType()) {
1749 default: assert(0 && "Cannot add this type!");
1750 case MVT::i1:
1751 case MVT::i8: Opc = X86::ADD8rm; break;
1752 case MVT::i16: Opc = X86::ADD16rm; break;
1753 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001754 case MVT::f64:
1755 // For F64, handle promoted load operations (from F32) as well!
1756 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1757 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001758 }
1759 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001760 EmitFoldedLoad(Op1, AM);
1761 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001762 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1763 return Result;
1764 }
1765
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001766 // See if we can codegen this as an LEA to fold operations together.
1767 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001768 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001769 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001770 MatchAddress(N, AM);
1771 ExprMap[N] = Result;
1772
1773 // If this is not just an add, emit the LEA. For a simple add (like
1774 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1775 // leave this as LEA, then peephole it to 'ADD' after two address elim
1776 // happens.
1777 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1778 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1779 X86AddressMode XAM = SelectAddrExprs(AM);
1780 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1781 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001782 }
1783 }
Chris Lattner11333092005-01-11 03:11:44 +00001784
Chris Lattnera5ade062005-01-11 21:19:59 +00001785 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001786 Opc = 0;
1787 if (CN->getValue() == 1) { // add X, 1 -> inc X
1788 switch (N.getValueType()) {
1789 default: assert(0 && "Cannot integer add this type!");
1790 case MVT::i8: Opc = X86::INC8r; break;
1791 case MVT::i16: Opc = X86::INC16r; break;
1792 case MVT::i32: Opc = X86::INC32r; break;
1793 }
1794 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1795 switch (N.getValueType()) {
1796 default: assert(0 && "Cannot integer add this type!");
1797 case MVT::i8: Opc = X86::DEC8r; break;
1798 case MVT::i16: Opc = X86::DEC16r; break;
1799 case MVT::i32: Opc = X86::DEC32r; break;
1800 }
1801 }
1802
1803 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001804 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001805 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1806 return Result;
1807 }
1808
1809 switch (N.getValueType()) {
1810 default: assert(0 && "Cannot add this type!");
1811 case MVT::i8: Opc = X86::ADD8ri; break;
1812 case MVT::i16: Opc = X86::ADD16ri; break;
1813 case MVT::i32: Opc = X86::ADD32ri; break;
1814 }
1815 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001816 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001817 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1818 return Result;
1819 }
1820 }
1821
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001822 switch (N.getValueType()) {
1823 default: assert(0 && "Cannot add this type!");
1824 case MVT::i8: Opc = X86::ADD8rr; break;
1825 case MVT::i16: Opc = X86::ADD16rr; break;
1826 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001827 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001828 }
Chris Lattner11333092005-01-11 03:11:44 +00001829
Chris Lattnera5ade062005-01-11 21:19:59 +00001830 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1831 Tmp1 = SelectExpr(Op0);
1832 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001833 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001834 Tmp2 = SelectExpr(Op1);
1835 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001836 }
1837
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001838 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1839 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001840
1841 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001842 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001843 case ISD::FSIN:
1844 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001845 case ISD::FSQRT:
1846 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001847 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001848 switch (N.getOpcode()) {
1849 default: assert(0 && "Unreachable!");
1850 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1851 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1852 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001853 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1854 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001855 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001856 return Result;
1857
Chris Lattner8db0af12005-04-06 04:21:07 +00001858 case ISD::MULHU:
1859 switch (N.getValueType()) {
1860 default: assert(0 && "Unsupported VT!");
1861 case MVT::i8: Tmp2 = X86::MUL8r; break;
1862 case MVT::i16: Tmp2 = X86::MUL16r; break;
1863 case MVT::i32: Tmp2 = X86::MUL32r; break;
1864 }
1865 // FALL THROUGH
1866 case ISD::MULHS: {
1867 unsigned MovOpc, LowReg, HiReg;
1868 switch (N.getValueType()) {
1869 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001870 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001871 MovOpc = X86::MOV8rr;
1872 LowReg = X86::AL;
1873 HiReg = X86::AH;
1874 Opc = X86::IMUL8r;
1875 break;
1876 case MVT::i16:
1877 MovOpc = X86::MOV16rr;
1878 LowReg = X86::AX;
1879 HiReg = X86::DX;
1880 Opc = X86::IMUL16r;
1881 break;
1882 case MVT::i32:
1883 MovOpc = X86::MOV32rr;
1884 LowReg = X86::EAX;
1885 HiReg = X86::EDX;
1886 Opc = X86::IMUL32r;
1887 break;
1888 }
1889 if (Node->getOpcode() != ISD::MULHS)
1890 Opc = Tmp2; // Get the MULHU opcode.
1891
1892 Op0 = Node->getOperand(0);
1893 Op1 = Node->getOperand(1);
1894 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1895 Tmp1 = SelectExpr(Op0);
1896 Tmp2 = SelectExpr(Op1);
1897 } else {
1898 Tmp2 = SelectExpr(Op1);
1899 Tmp1 = SelectExpr(Op0);
1900 }
1901
1902 // FIXME: Implement folding of loads into the memory operands here!
1903 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1904 BuildMI(BB, Opc, 1).addReg(Tmp2);
1905 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1906 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001907 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001908
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001909 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001910 case ISD::MUL:
1911 case ISD::AND:
1912 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001913 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001914 static const unsigned SUBTab[] = {
1915 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1916 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1917 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1918 };
1919 static const unsigned MULTab[] = {
1920 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1921 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1922 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1923 };
1924 static const unsigned ANDTab[] = {
1925 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1926 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001927 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001928 };
1929 static const unsigned ORTab[] = {
1930 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1931 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1932 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1933 };
1934 static const unsigned XORTab[] = {
1935 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1936 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1937 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1938 };
1939
1940 Op0 = Node->getOperand(0);
1941 Op1 = Node->getOperand(1);
1942
Chris Lattner30ea1e92005-01-19 07:37:26 +00001943 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1944 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001945 return Result;
1946
1947 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001948 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1949 if (CN->isNullValue()) { // 0 - N -> neg N
1950 switch (N.getValueType()) {
1951 default: assert(0 && "Cannot sub this type!");
1952 case MVT::i1:
1953 case MVT::i8: Opc = X86::NEG8r; break;
1954 case MVT::i16: Opc = X86::NEG16r; break;
1955 case MVT::i32: Opc = X86::NEG32r; break;
1956 }
1957 Tmp1 = SelectExpr(N.getOperand(1));
1958 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1959 return Result;
1960 }
1961
Chris Lattnera5ade062005-01-11 21:19:59 +00001962 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1963 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001964 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001965 switch (N.getValueType()) {
1966 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001967 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001968 case MVT::i8: Opc = X86::NOT8r; break;
1969 case MVT::i16: Opc = X86::NOT16r; break;
1970 case MVT::i32: Opc = X86::NOT32r; break;
1971 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001972 if (Opc) {
1973 Tmp1 = SelectExpr(Op0);
1974 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1975 return Result;
1976 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001977 }
1978
Chris Lattner2a4e5082005-01-17 06:48:02 +00001979 // Fold common multiplies into LEA instructions.
1980 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1981 switch ((int)CN->getValue()) {
1982 default: break;
1983 case 3:
1984 case 5:
1985 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001986 // Remove N from exprmap so SelectAddress doesn't get confused.
1987 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001988 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001989 SelectAddress(N, AM);
1990 // Restore it to the map.
1991 ExprMap[N] = Result;
1992 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1993 return Result;
1994 }
1995 }
1996
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001997 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001998 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001999 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002000 case MVT::i8: Opc = 0; break;
2001 case MVT::i16: Opc = 1; break;
2002 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002003 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002004 switch (Node->getOpcode()) {
2005 default: assert(0 && "Unreachable!");
2006 case ISD::SUB: Opc = SUBTab[Opc]; break;
2007 case ISD::MUL: Opc = MULTab[Opc]; break;
2008 case ISD::AND: Opc = ANDTab[Opc]; break;
2009 case ISD::OR: Opc = ORTab[Opc]; break;
2010 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002011 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002012 if (Opc) { // Can't fold MUL:i8 R, imm
2013 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002014 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2015 return Result;
2016 }
2017 }
Chris Lattner11333092005-01-11 03:11:44 +00002018
Chris Lattner44129b52005-01-25 20:03:11 +00002019 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002020 if (Node->getOpcode() != ISD::SUB) {
2021 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002022 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002023 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002024 // For FP, emit 'reverse' subract, with a memory operand.
2025 if (N.getValueType() == MVT::f64) {
2026 if (Op0.getOpcode() == ISD::EXTLOAD)
2027 Opc = X86::FSUBR32m;
2028 else
2029 Opc = X86::FSUBR64m;
2030
Chris Lattnera5ade062005-01-11 21:19:59 +00002031 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002032 EmitFoldedLoad(Op0, AM);
2033 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002034 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2035 return Result;
2036 }
2037 }
2038
Chris Lattner44129b52005-01-25 20:03:11 +00002039 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002040 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002041 switch (N.getValueType()) {
2042 default: assert(0 && "Cannot operate on this type!");
2043 case MVT::i1:
2044 case MVT::i8: Opc = 5; break;
2045 case MVT::i16: Opc = 6; break;
2046 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002047 // For F64, handle promoted load operations (from F32) as well!
2048 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002049 }
2050 switch (Node->getOpcode()) {
2051 default: assert(0 && "Unreachable!");
2052 case ISD::SUB: Opc = SUBTab[Opc]; break;
2053 case ISD::MUL: Opc = MULTab[Opc]; break;
2054 case ISD::AND: Opc = ANDTab[Opc]; break;
2055 case ISD::OR: Opc = ORTab[Opc]; break;
2056 case ISD::XOR: Opc = XORTab[Opc]; break;
2057 }
2058
2059 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002060 EmitFoldedLoad(Op1, AM);
2061 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002062 if (Opc) {
2063 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2064 } else {
2065 assert(Node->getOpcode() == ISD::MUL &&
2066 N.getValueType() == MVT::i8 && "Unexpected situation!");
2067 // Must use the MUL instruction, which forces use of AL.
2068 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2069 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2070 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2071 }
2072 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002073 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002074
2075 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2076 Tmp1 = SelectExpr(Op0);
2077 Tmp2 = SelectExpr(Op1);
2078 } else {
2079 Tmp2 = SelectExpr(Op1);
2080 Tmp1 = SelectExpr(Op0);
2081 }
2082
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002083 switch (N.getValueType()) {
2084 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002085 case MVT::i1:
2086 case MVT::i8: Opc = 10; break;
2087 case MVT::i16: Opc = 11; break;
2088 case MVT::i32: Opc = 12; break;
2089 case MVT::f32: Opc = 13; break;
2090 case MVT::f64: Opc = 14; break;
2091 }
2092 switch (Node->getOpcode()) {
2093 default: assert(0 && "Unreachable!");
2094 case ISD::SUB: Opc = SUBTab[Opc]; break;
2095 case ISD::MUL: Opc = MULTab[Opc]; break;
2096 case ISD::AND: Opc = ANDTab[Opc]; break;
2097 case ISD::OR: Opc = ORTab[Opc]; break;
2098 case ISD::XOR: Opc = XORTab[Opc]; break;
2099 }
2100 if (Opc) {
2101 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2102 } else {
2103 assert(Node->getOpcode() == ISD::MUL &&
2104 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002105 // Must use the MUL instruction, which forces use of AL.
2106 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2107 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2108 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002109 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002110 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002111 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002112 case ISD::ADD_PARTS:
2113 case ISD::SUB_PARTS: {
2114 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2115 "Not an i64 add/sub!");
2116 // Emit all of the operands.
2117 std::vector<unsigned> InVals;
2118 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2119 InVals.push_back(SelectExpr(N.getOperand(i)));
2120 if (N.getOpcode() == ISD::ADD_PARTS) {
2121 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2122 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2123 } else {
2124 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2125 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2126 }
2127 return Result+N.ResNo;
2128 }
2129
Chris Lattnerb38a7492005-04-02 04:01:14 +00002130 case ISD::SHL_PARTS:
2131 case ISD::SRA_PARTS:
2132 case ISD::SRL_PARTS: {
2133 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2134 "Not an i64 shift!");
2135 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2136 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2137 unsigned TmpReg = MakeReg(MVT::i32);
2138 if (N.getOpcode() == ISD::SRA_PARTS) {
2139 // If this is a SHR of a Long, then we need to do funny sign extension
2140 // stuff. TmpReg gets the value to use as the high-part if we are
2141 // shifting more than 32 bits.
2142 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2143 } else {
2144 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2145 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2146 }
2147
2148 // Initialize CL with the shift amount.
2149 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2150 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2151
2152 unsigned TmpReg2 = MakeReg(MVT::i32);
2153 unsigned TmpReg3 = MakeReg(MVT::i32);
2154 if (N.getOpcode() == ISD::SHL_PARTS) {
2155 // TmpReg2 = shld inHi, inLo
2156 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2157 .addReg(ShiftOpLo);
2158 // TmpReg3 = shl inLo, CL
2159 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002160
Chris Lattnerb38a7492005-04-02 04:01:14 +00002161 // Set the flags to indicate whether the shift was by more than 32 bits.
2162 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002163
Chris Lattnerb38a7492005-04-02 04:01:14 +00002164 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002165 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002166 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2167 // DestLo = (>32) ? TmpReg : TmpReg3;
2168 BuildMI(BB, X86::CMOVNE32rr, 2,
2169 Result).addReg(TmpReg3).addReg(TmpReg);
2170 } else {
2171 // TmpReg2 = shrd inLo, inHi
2172 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2173 .addReg(ShiftOpHi);
2174 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002175 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002176 : X86::SHR32rCL, 1, TmpReg3)
2177 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002178
Chris Lattnerb38a7492005-04-02 04:01:14 +00002179 // Set the flags to indicate whether the shift was by more than 32 bits.
2180 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002181
Chris Lattnerb38a7492005-04-02 04:01:14 +00002182 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002183 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002184 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002185
Chris Lattnerb38a7492005-04-02 04:01:14 +00002186 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002187 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002188 Result+1).addReg(TmpReg3).addReg(TmpReg);
2189 }
2190 return Result+N.ResNo;
2191 }
2192
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002193 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002194 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2195 Tmp2 = SelectExpr(N.getOperand(1));
2196 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002197 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002198 Tmp3 = SelectExpr(N.getOperand(2));
2199 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002200 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002201 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2202 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002203
2204 case ISD::SDIV:
2205 case ISD::UDIV:
2206 case ISD::SREM:
2207 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002208 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2209 "We don't support this operator!");
2210
Chris Lattner5bf26862005-04-13 03:29:53 +00002211 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002212 // We can fold loads into FpDIVs, but not really into any others.
2213 if (N.getValueType() == MVT::f64) {
2214 // Check for reversed and unreversed DIV.
2215 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2216 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2217 Opc = X86::FDIVR32m;
2218 else
2219 Opc = X86::FDIVR64m;
2220 X86AddressMode AM;
2221 EmitFoldedLoad(N.getOperand(0), AM);
2222 Tmp1 = SelectExpr(N.getOperand(1));
2223 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2224 return Result;
2225 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2226 N.getOperand(1).getOpcode() == ISD::LOAD) {
2227 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2228 Opc = X86::FDIV32m;
2229 else
2230 Opc = X86::FDIV64m;
2231 X86AddressMode AM;
2232 EmitFoldedLoad(N.getOperand(1), AM);
2233 Tmp1 = SelectExpr(N.getOperand(0));
2234 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2235 return Result;
2236 }
2237 }
2238
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002239 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2240 // FIXME: These special cases should be handled by the lowering impl!
2241 unsigned RHS = CN->getValue();
2242 bool isNeg = false;
2243 if ((int)RHS < 0) {
2244 isNeg = true;
2245 RHS = -RHS;
2246 }
2247 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2248 unsigned Log = log2(RHS);
2249 unsigned TmpReg = MakeReg(N.getValueType());
2250 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2251 switch (N.getValueType()) {
2252 default: assert("Unknown type to signed divide!");
2253 case MVT::i8:
2254 SAROpc = X86::SAR8ri;
2255 SHROpc = X86::SHR8ri;
2256 ADDOpc = X86::ADD8rr;
2257 NEGOpc = X86::NEG8r;
2258 break;
2259 case MVT::i16:
2260 SAROpc = X86::SAR16ri;
2261 SHROpc = X86::SHR16ri;
2262 ADDOpc = X86::ADD16rr;
2263 NEGOpc = X86::NEG16r;
2264 break;
2265 case MVT::i32:
2266 SAROpc = X86::SAR32ri;
2267 SHROpc = X86::SHR32ri;
2268 ADDOpc = X86::ADD32rr;
2269 NEGOpc = X86::NEG32r;
2270 break;
2271 }
Chris Lattner11333092005-01-11 03:11:44 +00002272 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002273 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2274 unsigned TmpReg2 = MakeReg(N.getValueType());
2275 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2276 unsigned TmpReg3 = MakeReg(N.getValueType());
2277 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002278
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2280 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2281 if (isNeg)
2282 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2283 return Result;
2284 }
2285 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002286 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002287
Chris Lattner11333092005-01-11 03:11:44 +00002288 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2289 Tmp1 = SelectExpr(N.getOperand(0));
2290 Tmp2 = SelectExpr(N.getOperand(1));
2291 } else {
2292 Tmp2 = SelectExpr(N.getOperand(1));
2293 Tmp1 = SelectExpr(N.getOperand(0));
2294 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002295
2296 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2297 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2298 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2299 switch (N.getValueType()) {
2300 default: assert(0 && "Cannot sdiv this type!");
2301 case MVT::i8:
2302 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2303 LoReg = X86::AL;
2304 HiReg = X86::AH;
2305 MovOpcode = X86::MOV8rr;
2306 ClrOpcode = X86::MOV8ri;
2307 SExtOpcode = X86::CBW;
2308 break;
2309 case MVT::i16:
2310 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2311 LoReg = X86::AX;
2312 HiReg = X86::DX;
2313 MovOpcode = X86::MOV16rr;
2314 ClrOpcode = X86::MOV16ri;
2315 SExtOpcode = X86::CWD;
2316 break;
2317 case MVT::i32:
2318 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002319 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002320 HiReg = X86::EDX;
2321 MovOpcode = X86::MOV32rr;
2322 ClrOpcode = X86::MOV32ri;
2323 SExtOpcode = X86::CDQ;
2324 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002325 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002326 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002327 return Result;
2328 }
2329
2330 // Set up the low part.
2331 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2332
2333 if (isSigned) {
2334 // Sign extend the low part into the high part.
2335 BuildMI(BB, SExtOpcode, 0);
2336 } else {
2337 // Zero out the high part, effectively zero extending the input.
2338 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2339 }
2340
2341 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002342 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002343
2344 // Get the result of the divide or rem.
2345 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2346 return Result;
2347 }
2348
2349 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002350 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002351 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2352 switch (N.getValueType()) {
2353 default: assert(0 && "Cannot shift this type!");
2354 case MVT::i8: Opc = X86::ADD8rr; break;
2355 case MVT::i16: Opc = X86::ADD16rr; break;
2356 case MVT::i32: Opc = X86::ADD32rr; break;
2357 }
2358 Tmp1 = SelectExpr(N.getOperand(0));
2359 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2360 return Result;
2361 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002362
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002363 switch (N.getValueType()) {
2364 default: assert(0 && "Cannot shift this type!");
2365 case MVT::i8: Opc = X86::SHL8ri; break;
2366 case MVT::i16: Opc = X86::SHL16ri; break;
2367 case MVT::i32: Opc = X86::SHL32ri; break;
2368 }
Chris Lattner11333092005-01-11 03:11:44 +00002369 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002370 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2371 return Result;
2372 }
Chris Lattner11333092005-01-11 03:11:44 +00002373
2374 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2375 Tmp1 = SelectExpr(N.getOperand(0));
2376 Tmp2 = SelectExpr(N.getOperand(1));
2377 } else {
2378 Tmp2 = SelectExpr(N.getOperand(1));
2379 Tmp1 = SelectExpr(N.getOperand(0));
2380 }
2381
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002382 switch (N.getValueType()) {
2383 default: assert(0 && "Cannot shift this type!");
2384 case MVT::i8 : Opc = X86::SHL8rCL; break;
2385 case MVT::i16: Opc = X86::SHL16rCL; break;
2386 case MVT::i32: Opc = X86::SHL32rCL; break;
2387 }
2388 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2389 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2390 return Result;
2391 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002392 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2393 switch (N.getValueType()) {
2394 default: assert(0 && "Cannot shift this type!");
2395 case MVT::i8: Opc = X86::SHR8ri; break;
2396 case MVT::i16: Opc = X86::SHR16ri; break;
2397 case MVT::i32: Opc = X86::SHR32ri; break;
2398 }
Chris Lattner11333092005-01-11 03:11:44 +00002399 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002400 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2401 return Result;
2402 }
Chris Lattner11333092005-01-11 03:11:44 +00002403
2404 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2405 Tmp1 = SelectExpr(N.getOperand(0));
2406 Tmp2 = SelectExpr(N.getOperand(1));
2407 } else {
2408 Tmp2 = SelectExpr(N.getOperand(1));
2409 Tmp1 = SelectExpr(N.getOperand(0));
2410 }
2411
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002412 switch (N.getValueType()) {
2413 default: assert(0 && "Cannot shift this type!");
2414 case MVT::i8 : Opc = X86::SHR8rCL; break;
2415 case MVT::i16: Opc = X86::SHR16rCL; break;
2416 case MVT::i32: Opc = X86::SHR32rCL; break;
2417 }
2418 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2419 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2420 return Result;
2421 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002422 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2423 switch (N.getValueType()) {
2424 default: assert(0 && "Cannot shift this type!");
2425 case MVT::i8: Opc = X86::SAR8ri; break;
2426 case MVT::i16: Opc = X86::SAR16ri; break;
2427 case MVT::i32: Opc = X86::SAR32ri; break;
2428 }
Chris Lattner11333092005-01-11 03:11:44 +00002429 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002430 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2431 return Result;
2432 }
Chris Lattner11333092005-01-11 03:11:44 +00002433
2434 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2435 Tmp1 = SelectExpr(N.getOperand(0));
2436 Tmp2 = SelectExpr(N.getOperand(1));
2437 } else {
2438 Tmp2 = SelectExpr(N.getOperand(1));
2439 Tmp1 = SelectExpr(N.getOperand(0));
2440 }
2441
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002442 switch (N.getValueType()) {
2443 default: assert(0 && "Cannot shift this type!");
2444 case MVT::i8 : Opc = X86::SAR8rCL; break;
2445 case MVT::i16: Opc = X86::SAR16rCL; break;
2446 case MVT::i32: Opc = X86::SAR32rCL; break;
2447 }
2448 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2449 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2450 return Result;
2451
2452 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002453 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002454 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2455 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2456 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002457 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002458 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002459 if (Result != 1) { // Generate the token
2460 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2461 assert(0 && "Load already emitted!?");
2462 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002463 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2464
Chris Lattner5188ad72005-01-08 19:28:19 +00002465 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002466 default: assert(0 && "Cannot load this type!");
2467 case MVT::i1:
2468 case MVT::i8: Opc = X86::MOV8rm; break;
2469 case MVT::i16: Opc = X86::MOV16rm; break;
2470 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002471 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2472 }
Chris Lattner11333092005-01-11 03:11:44 +00002473
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002474 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002475 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002476 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2477 } else {
2478 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002479
2480 SDOperand Chain = N.getOperand(0);
2481 SDOperand Address = N.getOperand(1);
2482 if (getRegPressure(Chain) > getRegPressure(Address)) {
2483 Select(Chain);
2484 SelectAddress(Address, AM);
2485 } else {
2486 SelectAddress(Address, AM);
2487 Select(Chain);
2488 }
2489
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002490 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2491 }
2492 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002493
2494 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2495 case ISD::ZEXTLOAD: {
2496 // Make sure we generate both values.
2497 if (Result != 1)
2498 ExprMap[N.getValue(1)] = 1; // Generate the token
2499 else
2500 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2501
Chris Lattnerda2ce112005-01-16 07:34:08 +00002502 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2503 if (Node->getValueType(0) == MVT::f64) {
2504 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2505 "Bad EXTLOAD!");
2506 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2507 CP->getIndex());
2508 return Result;
2509 }
2510
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002511 X86AddressMode AM;
2512 if (getRegPressure(Node->getOperand(0)) >
2513 getRegPressure(Node->getOperand(1))) {
2514 Select(Node->getOperand(0)); // chain
2515 SelectAddress(Node->getOperand(1), AM);
2516 } else {
2517 SelectAddress(Node->getOperand(1), AM);
2518 Select(Node->getOperand(0)); // chain
2519 }
2520
2521 switch (Node->getValueType(0)) {
2522 default: assert(0 && "Unknown type to sign extend to.");
2523 case MVT::f64:
2524 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2525 "Bad EXTLOAD!");
2526 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2527 break;
2528 case MVT::i32:
2529 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2530 default:
2531 assert(0 && "Bad zero extend!");
2532 case MVT::i1:
2533 case MVT::i8:
2534 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2535 break;
2536 case MVT::i16:
2537 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2538 break;
2539 }
2540 break;
2541 case MVT::i16:
2542 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2543 "Bad zero extend!");
2544 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2545 break;
2546 case MVT::i8:
2547 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2548 "Bad zero extend!");
2549 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2550 break;
2551 }
2552 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002553 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002554 case ISD::SEXTLOAD: {
2555 // Make sure we generate both values.
2556 if (Result != 1)
2557 ExprMap[N.getValue(1)] = 1; // Generate the token
2558 else
2559 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2560
2561 X86AddressMode AM;
2562 if (getRegPressure(Node->getOperand(0)) >
2563 getRegPressure(Node->getOperand(1))) {
2564 Select(Node->getOperand(0)); // chain
2565 SelectAddress(Node->getOperand(1), AM);
2566 } else {
2567 SelectAddress(Node->getOperand(1), AM);
2568 Select(Node->getOperand(0)); // chain
2569 }
2570
2571 switch (Node->getValueType(0)) {
2572 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2573 default: assert(0 && "Unknown type to sign extend to.");
2574 case MVT::i32:
2575 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2576 default:
2577 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2578 case MVT::i8:
2579 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2580 break;
2581 case MVT::i16:
2582 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2583 break;
2584 }
2585 break;
2586 case MVT::i16:
2587 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2588 "Cannot sign extend from bool!");
2589 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2590 break;
2591 }
2592 return Result;
2593 }
2594
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002595 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002596 // Generate both result values.
2597 if (Result != 1)
2598 ExprMap[N.getValue(1)] = 1; // Generate the token
2599 else
2600 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2601
2602 // FIXME: We are currently ignoring the requested alignment for handling
2603 // greater than the stack alignment. This will need to be revisited at some
2604 // point. Align = N.getOperand(2);
2605
2606 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2607 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2608 std::cerr << "Cannot allocate stack object with greater alignment than"
2609 << " the stack alignment yet!";
2610 abort();
2611 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002612
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002613 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002614 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002615 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2616 .addImm(CN->getValue());
2617 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002618 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2619 Select(N.getOperand(0));
2620 Tmp1 = SelectExpr(N.getOperand(1));
2621 } else {
2622 Tmp1 = SelectExpr(N.getOperand(1));
2623 Select(N.getOperand(0));
2624 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002625
2626 // Subtract size from stack pointer, thereby allocating some space.
2627 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2628 }
2629
2630 // Put a pointer to the space into the result register, by copying the stack
2631 // pointer.
2632 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2633 return Result;
2634
2635 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002636 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002637 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002638
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002639 if (GlobalAddressSDNode *GASD =
2640 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002641 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002642 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2643 } else if (ExternalSymbolSDNode *ESSDN =
2644 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002645 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002646 BuildMI(BB, X86::CALLpcrel32,
2647 1).addExternalSymbol(ESSDN->getSymbol(), true);
2648 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002649 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2650 Select(N.getOperand(0));
2651 Tmp1 = SelectExpr(N.getOperand(1));
2652 } else {
2653 Tmp1 = SelectExpr(N.getOperand(1));
2654 Select(N.getOperand(0));
2655 }
2656
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002657 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2658 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002659 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002660 default: assert(0 && "Unknown value type for call result!");
2661 case MVT::Other: return 1;
2662 case MVT::i1:
2663 case MVT::i8:
2664 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2665 break;
2666 case MVT::i16:
2667 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2668 break;
2669 case MVT::i32:
2670 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002671 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002672 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2673 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002674 case MVT::f64: // Floating-point return values live in %ST(0)
2675 ContainsFPCode = true;
2676 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2677 break;
2678 }
2679 return Result+N.ResNo;
2680 }
2681
2682 return 0;
2683}
2684
Chris Lattnere10269b2005-01-17 19:25:26 +00002685/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2686/// load/op/store instruction. If successful return true.
2687bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2688 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2689 SDOperand Chain = Node->getOperand(0);
2690 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002691 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002692
2693 // The chain has to be a load, the stored value must be an integer binary
2694 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002695 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002696 MVT::isFloatingPoint(StVal.getValueType()))
2697 return false;
2698
Chris Lattner5c659812005-01-17 22:10:42 +00002699 // Token chain must either be a factor node or the load to fold.
2700 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2701 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002702
Chris Lattner5c659812005-01-17 22:10:42 +00002703 SDOperand TheLoad;
2704
2705 // Check to see if there is a load from the same pointer that we're storing
2706 // to in either operand of the binop.
2707 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2708 StVal.getOperand(0).getOperand(1) == StPtr)
2709 TheLoad = StVal.getOperand(0);
2710 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2711 StVal.getOperand(1).getOperand(1) == StPtr)
2712 TheLoad = StVal.getOperand(1);
2713 else
2714 return false; // No matching load operand.
2715
2716 // We can only fold the load if there are no intervening side-effecting
2717 // operations. This means that the store uses the load as its token chain, or
2718 // there are only token factor nodes in between the store and load.
2719 if (Chain != TheLoad.getValue(1)) {
2720 // Okay, the other option is that we have a store referring to (possibly
2721 // nested) token factor nodes. For now, just try peeking through one level
2722 // of token factors to see if this is the case.
2723 bool ChainOk = false;
2724 if (Chain.getOpcode() == ISD::TokenFactor) {
2725 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2726 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2727 ChainOk = true;
2728 break;
2729 }
2730 }
2731
2732 if (!ChainOk) return false;
2733 }
2734
2735 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002736 return false;
2737
2738 // Make sure that one of the operands of the binop is the load, and that the
2739 // load folds into the binop.
2740 if (((StVal.getOperand(0) != TheLoad ||
2741 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2742 (StVal.getOperand(1) != TheLoad ||
2743 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2744 return false;
2745
2746 // Finally, check to see if this is one of the ops we can handle!
2747 static const unsigned ADDTAB[] = {
2748 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2749 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2750 };
2751 static const unsigned SUBTAB[] = {
2752 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2753 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2754 };
2755 static const unsigned ANDTAB[] = {
2756 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2757 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2758 };
2759 static const unsigned ORTAB[] = {
2760 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2761 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2762 };
2763 static const unsigned XORTAB[] = {
2764 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2765 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2766 };
2767 static const unsigned SHLTAB[] = {
2768 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2769 /*Have to put the reg in CL*/0, 0, 0,
2770 };
2771 static const unsigned SARTAB[] = {
2772 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2773 /*Have to put the reg in CL*/0, 0, 0,
2774 };
2775 static const unsigned SHRTAB[] = {
2776 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2777 /*Have to put the reg in CL*/0, 0, 0,
2778 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002779
Chris Lattnere10269b2005-01-17 19:25:26 +00002780 const unsigned *TabPtr = 0;
2781 switch (StVal.getOpcode()) {
2782 default:
2783 std::cerr << "CANNOT [mem] op= val: ";
2784 StVal.Val->dump(); std::cerr << "\n";
2785 case ISD::MUL:
2786 case ISD::SDIV:
2787 case ISD::UDIV:
2788 case ISD::SREM:
2789 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002790
Chris Lattnere10269b2005-01-17 19:25:26 +00002791 case ISD::ADD: TabPtr = ADDTAB; break;
2792 case ISD::SUB: TabPtr = SUBTAB; break;
2793 case ISD::AND: TabPtr = ANDTAB; break;
2794 case ISD:: OR: TabPtr = ORTAB; break;
2795 case ISD::XOR: TabPtr = XORTAB; break;
2796 case ISD::SHL: TabPtr = SHLTAB; break;
2797 case ISD::SRA: TabPtr = SARTAB; break;
2798 case ISD::SRL: TabPtr = SHRTAB; break;
2799 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002800
Chris Lattnere10269b2005-01-17 19:25:26 +00002801 // Handle: [mem] op= CST
2802 SDOperand Op0 = StVal.getOperand(0);
2803 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002804 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002805 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2806 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2807 default: break;
2808 case MVT::i1:
2809 case MVT::i8: Opc = TabPtr[0]; break;
2810 case MVT::i16: Opc = TabPtr[1]; break;
2811 case MVT::i32: Opc = TabPtr[2]; break;
2812 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002813
Chris Lattnere10269b2005-01-17 19:25:26 +00002814 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002815 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2816 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002817 Select(Chain);
2818
Chris Lattnere10269b2005-01-17 19:25:26 +00002819 X86AddressMode AM;
2820 if (getRegPressure(TheLoad.getOperand(0)) >
2821 getRegPressure(TheLoad.getOperand(1))) {
2822 Select(TheLoad.getOperand(0));
2823 SelectAddress(TheLoad.getOperand(1), AM);
2824 } else {
2825 SelectAddress(TheLoad.getOperand(1), AM);
2826 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002827 }
Chris Lattner5c659812005-01-17 22:10:42 +00002828
2829 if (StVal.getOpcode() == ISD::ADD) {
2830 if (CN->getValue() == 1) {
2831 switch (Op0.getValueType()) {
2832 default: break;
2833 case MVT::i8:
2834 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2835 return true;
2836 case MVT::i16: Opc = TabPtr[1];
2837 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2838 return true;
2839 case MVT::i32: Opc = TabPtr[2];
2840 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2841 return true;
2842 }
2843 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2844 switch (Op0.getValueType()) {
2845 default: break;
2846 case MVT::i8:
2847 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2848 return true;
2849 case MVT::i16: Opc = TabPtr[1];
2850 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2851 return true;
2852 case MVT::i32: Opc = TabPtr[2];
2853 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2854 return true;
2855 }
2856 }
2857 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002858
Chris Lattnere10269b2005-01-17 19:25:26 +00002859 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2860 return true;
2861 }
2862 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002863
Chris Lattnere10269b2005-01-17 19:25:26 +00002864 // If we have [mem] = V op [mem], try to turn it into:
2865 // [mem] = [mem] op V.
2866 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2867 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2868 StVal.getOpcode() != ISD::SRL)
2869 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002870
Chris Lattnere10269b2005-01-17 19:25:26 +00002871 if (Op0 != TheLoad) return false;
2872
2873 switch (Op0.getValueType()) {
2874 default: return false;
2875 case MVT::i1:
2876 case MVT::i8: Opc = TabPtr[3]; break;
2877 case MVT::i16: Opc = TabPtr[4]; break;
2878 case MVT::i32: Opc = TabPtr[5]; break;
2879 }
Chris Lattner5c659812005-01-17 22:10:42 +00002880
Chris Lattnerb422aea2005-01-18 17:35:28 +00002881 // Table entry doesn't exist?
2882 if (Opc == 0) return false;
2883
Chris Lattner4a108662005-01-18 03:51:59 +00002884 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2885 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002886 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002887 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002888
Chris Lattnere10269b2005-01-17 19:25:26 +00002889 X86AddressMode AM;
2890 SelectAddress(TheLoad.getOperand(1), AM);
2891 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002892 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002893 return true;
2894}
2895
2896
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002897void ISel::Select(SDOperand N) {
2898 unsigned Tmp1, Tmp2, Opc;
2899
Nate Begeman85fdeb22005-03-24 04:39:54 +00002900 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002901 return; // Already selected.
2902
Chris Lattner989de032005-01-11 06:14:36 +00002903 SDNode *Node = N.Val;
2904
2905 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002906 default:
Chris Lattner989de032005-01-11 06:14:36 +00002907 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002908 assert(0 && "Node not handled yet!");
2909 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002910 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002911 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002912 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002913 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2914 Select(Node->getOperand(OneFirst));
2915 Select(Node->getOperand(!OneFirst));
2916 } else {
2917 std::vector<std::pair<unsigned, unsigned> > OpsP;
2918 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2919 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2920 std::sort(OpsP.begin(), OpsP.end());
2921 std::reverse(OpsP.begin(), OpsP.end());
2922 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2923 Select(Node->getOperand(OpsP[i].second));
2924 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002925 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002926 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002927 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2928 Select(N.getOperand(0));
2929 Tmp1 = SelectExpr(N.getOperand(1));
2930 } else {
2931 Tmp1 = SelectExpr(N.getOperand(1));
2932 Select(N.getOperand(0));
2933 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002934 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002935
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002936 if (Tmp1 != Tmp2) {
2937 switch (N.getOperand(1).getValueType()) {
2938 default: assert(0 && "Invalid type for operation!");
2939 case MVT::i1:
2940 case MVT::i8: Opc = X86::MOV8rr; break;
2941 case MVT::i16: Opc = X86::MOV16rr; break;
2942 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002943 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002944 }
2945 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2946 }
2947 return;
2948 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002949 switch (N.getNumOperands()) {
2950 default:
2951 assert(0 && "Unknown return instruction!");
2952 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002953 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2954 N.getOperand(2).getValueType() == MVT::i32 &&
2955 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002956 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2957 Tmp1 = SelectExpr(N.getOperand(1));
2958 Tmp2 = SelectExpr(N.getOperand(2));
2959 } else {
2960 Tmp2 = SelectExpr(N.getOperand(2));
2961 Tmp1 = SelectExpr(N.getOperand(1));
2962 }
2963 Select(N.getOperand(0));
2964
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002965 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2966 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002967 break;
2968 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002969 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2970 Select(N.getOperand(0));
2971 Tmp1 = SelectExpr(N.getOperand(1));
2972 } else {
2973 Tmp1 = SelectExpr(N.getOperand(1));
2974 Select(N.getOperand(0));
2975 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002976 switch (N.getOperand(1).getValueType()) {
2977 default: assert(0 && "All other types should have been promoted!!");
2978 case MVT::f64:
2979 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002980 break;
2981 case MVT::i32:
2982 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002983 break;
2984 }
2985 break;
2986 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002987 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002988 break;
2989 }
2990 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2991 return;
2992 case ISD::BR: {
2993 Select(N.getOperand(0));
2994 MachineBasicBlock *Dest =
2995 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2996 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2997 return;
2998 }
2999
3000 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003001 MachineBasicBlock *Dest =
3002 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003003
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003004 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3005 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003006 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3007 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3008 Select(N.getOperand(0));
3009 Tmp1 = SelectExpr(N.getOperand(1));
3010 } else {
3011 Tmp1 = SelectExpr(N.getOperand(1));
3012 Select(N.getOperand(0));
3013 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003014 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3015 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3016 }
Chris Lattner11333092005-01-11 03:11:44 +00003017
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003018 return;
3019 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003020
Chris Lattner4df0de92005-01-17 00:00:33 +00003021 case ISD::LOAD:
3022 // If this load could be folded into the only using instruction, and if it
3023 // is safe to emit the instruction here, try to do so now.
3024 if (Node->hasNUsesOfValue(1, 0)) {
3025 SDOperand TheVal = N.getValue(0);
3026 SDNode *User = 0;
3027 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3028 assert(UI != Node->use_end() && "Didn't find use!");
3029 SDNode *UN = *UI;
3030 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3031 if (UN->getOperand(i) == TheVal) {
3032 User = UN;
3033 goto FoundIt;
3034 }
3035 }
3036 FoundIt:
3037 // Only handle unary operators right now.
3038 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003039 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003040 SelectExpr(SDOperand(User, 0));
3041 return;
3042 }
3043 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003044 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003045 SelectExpr(N);
3046 return;
3047
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003048 case ISD::EXTLOAD:
3049 case ISD::SEXTLOAD:
3050 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003051 case ISD::CALL:
3052 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003053 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003054 SelectExpr(N);
3055 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003056
3057 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3058 // On X86, we can represent all types except for Bool and Float natively.
3059 X86AddressMode AM;
3060 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003061 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3062 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3063 && "Unsupported TRUNCSTORE for this target!");
3064
3065 if (StoredTy == MVT::i16) {
3066 // FIXME: This is here just to allow testing. X86 doesn't really have a
3067 // TRUNCSTORE i16 operation, but this is required for targets that do not
3068 // have 16-bit integer registers. We occasionally disable 16-bit integer
3069 // registers to test the promotion code.
3070 Select(N.getOperand(0));
3071 Tmp1 = SelectExpr(N.getOperand(1));
3072 SelectAddress(N.getOperand(2), AM);
3073
3074 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3075 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3076 return;
3077 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003078
3079 // Store of constant bool?
3080 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3081 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3082 Select(N.getOperand(0));
3083 SelectAddress(N.getOperand(2), AM);
3084 } else {
3085 SelectAddress(N.getOperand(2), AM);
3086 Select(N.getOperand(0));
3087 }
3088 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3089 return;
3090 }
3091
3092 switch (StoredTy) {
3093 default: assert(0 && "Cannot truncstore this type!");
3094 case MVT::i1: Opc = X86::MOV8mr; break;
3095 case MVT::f32: Opc = X86::FST32m; break;
3096 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003097
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003098 std::vector<std::pair<unsigned, unsigned> > RP;
3099 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3100 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3101 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3102 std::sort(RP.begin(), RP.end());
3103
Chris Lattner572dd082005-02-23 05:57:21 +00003104 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003105 for (unsigned i = 0; i != 3; ++i)
3106 switch (RP[2-i].second) {
3107 default: assert(0 && "Unknown operand number!");
3108 case 0: Select(N.getOperand(0)); break;
3109 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3110 case 2: SelectAddress(N.getOperand(2), AM); break;
3111 }
3112
3113 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3114 return;
3115 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003116 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003117 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003118
3119 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3120 Opc = 0;
3121 switch (CN->getValueType(0)) {
3122 default: assert(0 && "Invalid type for operation!");
3123 case MVT::i1:
3124 case MVT::i8: Opc = X86::MOV8mi; break;
3125 case MVT::i16: Opc = X86::MOV16mi; break;
3126 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003127 case MVT::f64: break;
3128 }
3129 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003130 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3131 Select(N.getOperand(0));
3132 SelectAddress(N.getOperand(2), AM);
3133 } else {
3134 SelectAddress(N.getOperand(2), AM);
3135 Select(N.getOperand(0));
3136 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003137 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3138 return;
3139 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003140 } else if (GlobalAddressSDNode *GA =
3141 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3142 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3143
3144 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3145 Select(N.getOperand(0));
3146 SelectAddress(N.getOperand(2), AM);
3147 } else {
3148 SelectAddress(N.getOperand(2), AM);
3149 Select(N.getOperand(0));
3150 }
3151 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3152 AM).addGlobalAddress(GA->getGlobal());
3153 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003154 }
Chris Lattner837caa72005-01-11 23:21:30 +00003155
3156 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003157 if (TryToFoldLoadOpStore(Node))
3158 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003159
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003160 switch (N.getOperand(1).getValueType()) {
3161 default: assert(0 && "Cannot store this type!");
3162 case MVT::i1:
3163 case MVT::i8: Opc = X86::MOV8mr; break;
3164 case MVT::i16: Opc = X86::MOV16mr; break;
3165 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003166 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003167 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003168
Chris Lattner11333092005-01-11 03:11:44 +00003169 std::vector<std::pair<unsigned, unsigned> > RP;
3170 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3171 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3172 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3173 std::sort(RP.begin(), RP.end());
3174
Chris Lattner572dd082005-02-23 05:57:21 +00003175 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003176 for (unsigned i = 0; i != 3; ++i)
3177 switch (RP[2-i].second) {
3178 default: assert(0 && "Unknown operand number!");
3179 case 0: Select(N.getOperand(0)); break;
3180 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003181 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003182 }
3183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003184 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3185 return;
3186 }
3187 case ISD::ADJCALLSTACKDOWN:
3188 case ISD::ADJCALLSTACKUP:
3189 Select(N.getOperand(0));
3190 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003191
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003192 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3193 X86::ADJCALLSTACKUP;
3194 BuildMI(BB, Opc, 1).addImm(Tmp1);
3195 return;
Chris Lattner989de032005-01-11 06:14:36 +00003196 case ISD::MEMSET: {
3197 Select(N.getOperand(0)); // Select the chain.
3198 unsigned Align =
3199 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3200 if (Align == 0) Align = 1;
3201
3202 // Turn the byte code into # iterations
3203 unsigned CountReg;
3204 unsigned Opcode;
3205 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3206 unsigned Val = ValC->getValue() & 255;
3207
3208 // If the value is a constant, then we can potentially use larger sets.
3209 switch (Align & 3) {
3210 case 2: // WORD aligned
3211 CountReg = MakeReg(MVT::i32);
3212 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3213 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3214 } else {
3215 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3216 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3217 }
3218 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3219 Opcode = X86::REP_STOSW;
3220 break;
3221 case 0: // DWORD aligned
3222 CountReg = MakeReg(MVT::i32);
3223 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3224 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3225 } else {
3226 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3227 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3228 }
3229 Val = (Val << 8) | Val;
3230 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3231 Opcode = X86::REP_STOSD;
3232 break;
3233 default: // BYTE aligned
3234 CountReg = SelectExpr(Node->getOperand(3));
3235 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3236 Opcode = X86::REP_STOSB;
3237 break;
3238 }
3239 } else {
3240 // If it's not a constant value we are storing, just fall back. We could
3241 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3242 unsigned ValReg = SelectExpr(Node->getOperand(2));
3243 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3244 CountReg = SelectExpr(Node->getOperand(3));
3245 Opcode = X86::REP_STOSB;
3246 }
3247
3248 // No matter what the alignment is, we put the source in ESI, the
3249 // destination in EDI, and the count in ECX.
3250 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3251 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3252 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3253 BuildMI(BB, Opcode, 0);
3254 return;
3255 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003256 case ISD::MEMCPY:
3257 Select(N.getOperand(0)); // Select the chain.
3258 unsigned Align =
3259 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3260 if (Align == 0) Align = 1;
3261
3262 // Turn the byte code into # iterations
3263 unsigned CountReg;
3264 unsigned Opcode;
3265 switch (Align & 3) {
3266 case 2: // WORD aligned
3267 CountReg = MakeReg(MVT::i32);
3268 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3269 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3270 } else {
3271 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3272 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3273 }
3274 Opcode = X86::REP_MOVSW;
3275 break;
3276 case 0: // DWORD aligned
3277 CountReg = MakeReg(MVT::i32);
3278 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3279 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3280 } else {
3281 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3282 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3283 }
3284 Opcode = X86::REP_MOVSD;
3285 break;
3286 default: // BYTE aligned
3287 CountReg = SelectExpr(Node->getOperand(3));
3288 Opcode = X86::REP_MOVSB;
3289 break;
3290 }
3291
3292 // No matter what the alignment is, we put the source in ESI, the
3293 // destination in EDI, and the count in ECX.
3294 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3295 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3296 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3297 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3298 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3299 BuildMI(BB, Opcode, 0);
3300 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003301 }
3302 assert(0 && "Should not be reached!");
3303}
3304
3305
3306/// createX86PatternInstructionSelector - This pass converts an LLVM function
3307/// into a machine code representation using pattern matching and a machine
3308/// description file.
3309///
3310FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003311 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003312}