blob: 0de651c6266beea727bb3f1efdfa0dfc762b7f7a [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000017#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000019#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000029#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000030#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000033#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// X86TargetLowering - X86 Implementation of the TargetLowering interface
38namespace {
39 class X86TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000041 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000042 public:
43 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000045
46 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000049 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000051
52 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000057
Chris Lattner8acb1ba2005-01-07 07:49:41 +000058 // FIXME: Eliminate these two classes when legalize can handle promotions
59 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000060/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000061
Chris Lattnerda4d4692005-04-09 03:22:37 +000062 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000065 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000066 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
67 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
68 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000069 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000071 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000072
Chris Lattner4e6ce5f2005-05-09 20:37:29 +000073 setOperationAction(ISD::READIO , MVT::i1 , Expand);
74 setOperationAction(ISD::READIO , MVT::i8 , Expand);
75 setOperationAction(ISD::READIO , MVT::i16 , Expand);
76 setOperationAction(ISD::READIO , MVT::i32 , Expand);
77 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
78 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
79 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
80 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
81
Chris Lattnerc5dcb532005-04-30 04:25:35 +000082 if (!UnsafeFPMath) {
83 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
84 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
85 }
86
Chris Lattnerda2ce112005-01-16 07:34:08 +000087 // These should be promoted to a larger select which is supported.
88/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
89 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000090
Chris Lattner8acb1ba2005-01-07 07:49:41 +000091 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000092
Chris Lattner8acb1ba2005-01-07 07:49:41 +000093 addLegalFPImmediate(+0.0); // FLD0
94 addLegalFPImmediate(+1.0); // FLD1
95 addLegalFPImmediate(-0.0); // FLD0/FCHS
96 addLegalFPImmediate(-1.0); // FLD1/FCHS
97 }
98
99 /// LowerArguments - This hook must be implemented to indicate how we should
100 /// lower the arguments for the specified function, into the specified DAG.
101 virtual std::vector<SDOperand>
102 LowerArguments(Function &F, SelectionDAG &DAG);
103
104 /// LowerCallTo - This hook lowers an abstract call to a function into an
105 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000106 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000107 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
108 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
112
113 virtual std::pair<SDOperand,SDOperand>
114 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
115 const Type *ArgTy, SelectionDAG &DAG);
116
117 virtual std::pair<SDOperand, SDOperand>
118 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
119 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000120 };
121}
122
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000123std::vector<SDOperand>
124X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
125 std::vector<SDOperand> ArgValues;
126
Chris Lattner6415bb42005-05-10 03:53:18 +0000127 MachineFunction &MF = DAG.getMachineFunction();
128 MachineFrameInfo *MFI = MF.getFrameInfo();
129
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000130 // Add DAG nodes to load the arguments... On entry to a function on the X86,
131 // the stack frame looks like this:
132 //
133 // [ESP] -- return address
134 // [ESP + 4] -- first argument (leftmost lexically)
135 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000136 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000137 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000138 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000139 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000140 MVT::ValueType ObjectVT = getValueType(I->getType());
141 unsigned ArgIncrement = 4;
142 unsigned ObjSize;
143 switch (ObjectVT) {
144 default: assert(0 && "Unhandled argument type!");
145 case MVT::i1:
146 case MVT::i8: ObjSize = 1; break;
147 case MVT::i16: ObjSize = 2; break;
148 case MVT::i32: ObjSize = 4; break;
149 case MVT::i64: ObjSize = ArgIncrement = 8; break;
150 case MVT::f32: ObjSize = 4; break;
151 case MVT::f64: ObjSize = ArgIncrement = 8; break;
152 }
153 // Create the frame index object for this incoming parameter...
154 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000155
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000156 // Create the SelectionDAG nodes corresponding to a load from this parameter
157 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
158
159 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
160 // dead loads.
161 SDOperand ArgValue;
162 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000163 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
164 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000165 else {
166 if (MVT::isInteger(ObjectVT))
167 ArgValue = DAG.getConstant(0, ObjectVT);
168 else
169 ArgValue = DAG.getConstantFP(0, ObjectVT);
170 }
171 ArgValues.push_back(ArgValue);
172
173 ArgOffset += ArgIncrement; // Move on to the next argument...
174 }
175
176 // If the function takes variable number of arguments, make a frame index for
177 // the start of the first vararg value... for expansion of llvm.va_start.
178 if (F.isVarArg())
179 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000180 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000181
182 // Finally, inform the code generator which regs we return values in.
183 switch (getValueType(F.getReturnType())) {
184 default: assert(0 && "Unknown type!");
185 case MVT::isVoid: break;
186 case MVT::i1:
187 case MVT::i8:
188 case MVT::i16:
189 case MVT::i32:
190 MF.addLiveOut(X86::EAX);
191 break;
192 case MVT::i64:
193 MF.addLiveOut(X86::EAX);
194 MF.addLiveOut(X86::EDX);
195 break;
196 case MVT::f32:
197 case MVT::f64:
198 MF.addLiveOut(X86::ST0);
199 break;
200 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000201 return ArgValues;
202}
203
Chris Lattner5188ad72005-01-08 19:28:19 +0000204std::pair<SDOperand, SDOperand>
205X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000206 const Type *RetTy, bool isVarArg,
207 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000208 // Count how many bytes are to be pushed on the stack.
209 unsigned NumBytes = 0;
210
211 if (Args.empty()) {
212 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000213 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
214 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000215 } else {
216 for (unsigned i = 0, e = Args.size(); i != e; ++i)
217 switch (getValueType(Args[i].second)) {
218 default: assert(0 && "Unknown value type!");
219 case MVT::i1:
220 case MVT::i8:
221 case MVT::i16:
222 case MVT::i32:
223 case MVT::f32:
224 NumBytes += 4;
225 break;
226 case MVT::i64:
227 case MVT::f64:
228 NumBytes += 8;
229 break;
230 }
231
Chris Lattner5188ad72005-01-08 19:28:19 +0000232 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
233 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000234
235 // Arguments go on the stack in reverse order, as specified by the ABI.
236 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000237 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
238 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000239 std::vector<SDOperand> Stores;
240
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000241 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000242 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
243 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
244
245 switch (getValueType(Args[i].second)) {
246 default: assert(0 && "Unexpected ValueType for argument!");
247 case MVT::i1:
248 case MVT::i8:
249 case MVT::i16:
250 // Promote the integer to 32 bits. If the input type is signed use a
251 // sign extend, otherwise use a zero extend.
252 if (Args[i].second->isSigned())
253 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
254 else
255 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
256
257 // FALL THROUGH
258 case MVT::i32:
259 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000260 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000261 Args[i].first, PtrOff,
262 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000263 ArgOffset += 4;
264 break;
265 case MVT::i64:
266 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000267 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000268 Args[i].first, PtrOff,
269 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000270 ArgOffset += 8;
271 break;
272 }
273 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000274 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000275 }
276
277 std::vector<MVT::ValueType> RetVals;
278 MVT::ValueType RetTyVT = getValueType(RetTy);
279 if (RetTyVT != MVT::isVoid)
280 RetVals.push_back(RetTyVT);
281 RetVals.push_back(MVT::Other);
282
Chris Lattner5188ad72005-01-08 19:28:19 +0000283 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000284 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000285 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
286 DAG.getConstant(NumBytes, getPointerTy()));
287 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000288}
289
Chris Lattner14824582005-01-09 00:01:27 +0000290std::pair<SDOperand, SDOperand>
291X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
292 // vastart just returns the address of the VarArgsFrameIndex slot.
293 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
294}
295
296std::pair<SDOperand,SDOperand> X86TargetLowering::
297LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
298 const Type *ArgTy, SelectionDAG &DAG) {
299 MVT::ValueType ArgVT = getValueType(ArgTy);
300 SDOperand Result;
301 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000302 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
303 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000304 } else {
305 unsigned Amt;
306 if (ArgVT == MVT::i32)
307 Amt = 4;
308 else {
309 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
310 "Other types should have been promoted for varargs!");
311 Amt = 8;
312 }
313 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
314 DAG.getConstant(Amt, VAList.getValueType()));
315 }
316 return std::make_pair(Result, Chain);
317}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000318
Chris Lattner14824582005-01-09 00:01:27 +0000319
320std::pair<SDOperand, SDOperand> X86TargetLowering::
321LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
322 SelectionDAG &DAG) {
323 SDOperand Result;
324 if (Depth) // Depths > 0 not supported yet!
325 Result = DAG.getConstant(0, getPointerTy());
326 else {
327 if (ReturnAddrIndex == 0) {
328 // Set up a frame object for the return address.
329 MachineFunction &MF = DAG.getMachineFunction();
330 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
331 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000332
Chris Lattner14824582005-01-09 00:01:27 +0000333 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
334
335 if (!isFrameAddress)
336 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000337 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000338 else
339 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
340 DAG.getConstant(4, MVT::i32));
341 }
342 return std::make_pair(Result, Chain);
343}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000344
345
Chris Lattner98a8ba02005-01-18 01:06:26 +0000346namespace {
347 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
348 /// SDOperand's instead of register numbers for the leaves of the matched
349 /// tree.
350 struct X86ISelAddressMode {
351 enum {
352 RegBase,
353 FrameIndexBase,
354 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000355
Chris Lattner98a8ba02005-01-18 01:06:26 +0000356 struct { // This is really a union, discriminated by BaseType!
357 SDOperand Reg;
358 int FrameIndex;
359 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000360
Chris Lattner98a8ba02005-01-18 01:06:26 +0000361 unsigned Scale;
362 SDOperand IndexReg;
363 unsigned Disp;
364 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000365
Chris Lattner98a8ba02005-01-18 01:06:26 +0000366 X86ISelAddressMode()
367 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
368 }
369 };
370}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000371
372
373namespace {
374 Statistic<>
375 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
376
377 //===--------------------------------------------------------------------===//
378 /// ISel - X86 specific code to select X86 machine instructions for
379 /// SelectionDAG operations.
380 ///
381 class ISel : public SelectionDAGISel {
382 /// ContainsFPCode - Every instruction we select that uses or defines a FP
383 /// register should set this to true.
384 bool ContainsFPCode;
385
386 /// X86Lowering - This object fully describes how to lower LLVM code to an
387 /// X86-specific SelectionDAG.
388 X86TargetLowering X86Lowering;
389
Chris Lattner11333092005-01-11 03:11:44 +0000390 /// RegPressureMap - This keeps an approximate count of the number of
391 /// registers required to evaluate each node in the graph.
392 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000393
394 /// ExprMap - As shared expressions are codegen'd, we keep track of which
395 /// vreg the value is produced in, so we only emit one copy of each compiled
396 /// tree.
397 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000398
399 public:
400 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
401 }
402
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000403 virtual const char *getPassName() const {
404 return "X86 Pattern Instruction Selection";
405 }
406
Chris Lattner11333092005-01-11 03:11:44 +0000407 unsigned getRegPressure(SDOperand O) {
408 return RegPressureMap[O.Val];
409 }
410 unsigned ComputeRegPressure(SDOperand O);
411
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000412 /// InstructionSelectBasicBlock - This callback is invoked by
413 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000414 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000415
Chris Lattner44129b52005-01-25 20:03:11 +0000416 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
417 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000418 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000419 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000420
Chris Lattner30ea1e92005-01-19 07:37:26 +0000421 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000422 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000423 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000424 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
425 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000426 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000427
428 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
429 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
430 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000431 void Select(SDOperand N);
432 };
433}
434
Chris Lattner6415bb42005-05-10 03:53:18 +0000435/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
436/// the main function.
437static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
438 MachineFrameInfo *MFI) {
439 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
440 int CWFrameIdx = MFI->CreateStackObject(2, 2);
441 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
442
443 // Set the high part to be 64-bit precision.
444 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
445 CWFrameIdx, 1).addImm(2);
446
447 // Reload the modified control word now.
448 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
449}
450
Chris Lattner7dbcb752005-01-12 04:21:28 +0000451/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
452/// when it has created a SelectionDAG for us to codegen.
453void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
454 // While we're doing this, keep track of whether we see any FP code for
455 // FP_REG_KILL insertion.
456 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +0000457 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000458
459 // Scan the PHI nodes that already are inserted into this basic block. If any
460 // of them is a PHI of a floating point value, we need to insert an
461 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +0000462 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000463 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
464 I != E; ++I) {
465 assert(I->getOpcode() == X86::PHI &&
466 "Isn't just PHI nodes?");
467 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
468 X86::RFPRegisterClass) {
469 ContainsFPCode = true;
470 break;
471 }
472 }
473
Chris Lattner6415bb42005-05-10 03:53:18 +0000474 // If this is the entry block of main, emit special code for main.
475 if (BB == MF->begin()) {
476 const Function *F = MF->getFunction();
477 if (F->hasExternalLinkage() && F->getName() == "main")
478 EmitSpecialCodeForMain(BB, MF->getFrameInfo());
479 }
480
Chris Lattner7dbcb752005-01-12 04:21:28 +0000481 // Compute the RegPressureMap, which is an approximation for the number of
482 // registers required to compute each node.
483 ComputeRegPressure(DAG.getRoot());
484
485 // Codegen the basic block.
486 Select(DAG.getRoot());
487
488 // Finally, look at all of the successors of this block. If any contain a PHI
489 // node of FP type, we need to insert an FP_REG_KILL in this block.
490 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
491 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
492 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
493 I != E && I->getOpcode() == X86::PHI; ++I) {
494 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
495 X86::RFPRegisterClass) {
496 ContainsFPCode = true;
497 break;
498 }
499 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000500
Chris Lattnere3e0f272005-05-09 03:36:39 +0000501 // Final check, check LLVM BB's that are successors to the LLVM BB
502 // corresponding to BB for FP PHI nodes.
503 const BasicBlock *LLVMBB = BB->getBasicBlock();
504 const PHINode *PN;
505 if (!ContainsFPCode)
506 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
507 SI != E && !ContainsFPCode; ++SI)
508 for (BasicBlock::const_iterator II = SI->begin();
509 (PN = dyn_cast<PHINode>(II)); ++II)
510 if (PN->getType()->isFloatingPoint()) {
511 ContainsFPCode = true;
512 break;
513 }
514
515
Chris Lattner7dbcb752005-01-12 04:21:28 +0000516 // Insert FP_REG_KILL instructions into basic blocks that need them. This
517 // only occurs due to the floating point stackifier not being aggressive
518 // enough to handle arbitrary global stackification.
519 //
520 // Currently we insert an FP_REG_KILL instruction into each block that uses or
521 // defines a floating point virtual register.
522 //
523 // When the global register allocators (like linear scan) finally update live
524 // variable analysis, we can keep floating point values in registers across
525 // basic blocks. This will be a huge win, but we are waiting on the global
526 // allocators before we can do this.
527 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000528 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000529 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
530 ++NumFPKill;
531 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000532
Chris Lattner7dbcb752005-01-12 04:21:28 +0000533 // Clear state used for selection.
534 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000535 RegPressureMap.clear();
536}
537
538
Chris Lattner11333092005-01-11 03:11:44 +0000539// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
540// for the number of registers required to compute each node. This is basically
541// computing a generalized form of the Sethi-Ullman number for each node.
542unsigned ISel::ComputeRegPressure(SDOperand O) {
543 SDNode *N = O.Val;
544 unsigned &Result = RegPressureMap[N];
545 if (Result) return Result;
546
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000547 // FIXME: Should operations like CALL (which clobber lots o regs) have a
548 // higher fixed cost??
549
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000550 if (N->getNumOperands() == 0) {
551 Result = 1;
552 } else {
553 unsigned MaxRegUse = 0;
554 unsigned NumExtraMaxRegUsers = 0;
555 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
556 unsigned Regs;
557 if (N->getOperand(i).getOpcode() == ISD::Constant)
558 Regs = 0;
559 else
560 Regs = ComputeRegPressure(N->getOperand(i));
561 if (Regs > MaxRegUse) {
562 MaxRegUse = Regs;
563 NumExtraMaxRegUsers = 0;
564 } else if (Regs == MaxRegUse &&
565 N->getOperand(i).getValueType() != MVT::Other) {
566 ++NumExtraMaxRegUsers;
567 }
Chris Lattner11333092005-01-11 03:11:44 +0000568 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000569
570 if (O.getOpcode() != ISD::TokenFactor)
571 Result = MaxRegUse+NumExtraMaxRegUsers;
572 else
Chris Lattner869e0432005-01-17 23:02:13 +0000573 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000574 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000575
Chris Lattner837caa72005-01-11 23:21:30 +0000576 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000577 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000578}
579
Chris Lattnerbf52d492005-01-20 16:50:16 +0000580/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
581/// The DAG cannot have cycles in it, by definition, so the visited set is not
582/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
583/// reuse, so it prevents exponential cases.
584///
585static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
586 std::set<SDNode*> &Visited) {
587 if (N == Op) return true; // Found it.
588 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000589 if (Node->getNumOperands() == 0 || // Leaf?
590 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000591 if (!Visited.insert(Node).second) return false; // Already visited?
592
593 // Recurse for the first N-1 operands.
594 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
595 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
596 return true;
597
598 // Tail recurse for the last operand.
599 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
600}
601
Chris Lattner98a8ba02005-01-18 01:06:26 +0000602X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
603 X86AddressMode Result;
604
605 // If we need to emit two register operands, emit the one with the highest
606 // register pressure first.
607 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
608 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000609 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000610 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000611 std::set<SDNode*> Visited;
612 EmitBaseThenIndex = true;
613 // If Base ends up pointing to Index, we must emit index first. This is
614 // because of the way we fold loads, we may end up doing bad things with
615 // the folded add.
616 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
617 EmitBaseThenIndex = false;
618 } else {
619 std::set<SDNode*> Visited;
620 EmitBaseThenIndex = false;
621 // If Base ends up pointing to Index, we must emit index first. This is
622 // because of the way we fold loads, we may end up doing bad things with
623 // the folded add.
624 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
625 EmitBaseThenIndex = true;
626 }
627
628 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000629 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
630 Result.IndexReg = SelectExpr(IAM.IndexReg);
631 } else {
632 Result.IndexReg = SelectExpr(IAM.IndexReg);
633 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
634 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000635
Chris Lattner98a8ba02005-01-18 01:06:26 +0000636 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
637 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
638 } else if (IAM.IndexReg.Val) {
639 Result.IndexReg = SelectExpr(IAM.IndexReg);
640 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000641
Chris Lattner98a8ba02005-01-18 01:06:26 +0000642 switch (IAM.BaseType) {
643 case X86ISelAddressMode::RegBase:
644 Result.BaseType = X86AddressMode::RegBase;
645 break;
646 case X86ISelAddressMode::FrameIndexBase:
647 Result.BaseType = X86AddressMode::FrameIndexBase;
648 Result.Base.FrameIndex = IAM.Base.FrameIndex;
649 break;
650 default:
651 assert(0 && "Unknown base type!");
652 break;
653 }
654 Result.Scale = IAM.Scale;
655 Result.Disp = IAM.Disp;
656 Result.GV = IAM.GV;
657 return Result;
658}
659
660/// SelectAddress - Pattern match the maximal addressing mode for this node and
661/// emit all of the leaf registers.
662void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
663 X86ISelAddressMode IAM;
664 MatchAddress(N, IAM);
665 AM = SelectAddrExprs(IAM);
666}
667
668/// MatchAddress - Add the specified node to the specified addressing mode,
669/// returning true if it cannot be done. This just pattern matches for the
670/// addressing mode, it does not cause any code to be emitted. For that, use
671/// SelectAddress.
672bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000673 switch (N.getOpcode()) {
674 default: break;
675 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000676 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
677 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000678 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
679 return false;
680 }
681 break;
682 case ISD::GlobalAddress:
683 if (AM.GV == 0) {
684 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
685 return false;
686 }
687 break;
688 case ISD::Constant:
689 AM.Disp += cast<ConstantSDNode>(N)->getValue();
690 return false;
691 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000692 // We might have folded the load into this shift, so don't regen the value
693 // if so.
694 if (ExprMap.count(N)) break;
695
Chris Lattner98a8ba02005-01-18 01:06:26 +0000696 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000697 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
698 unsigned Val = CN->getValue();
699 if (Val == 1 || Val == 2 || Val == 3) {
700 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000701 SDOperand ShVal = N.Val->getOperand(0);
702
703 // Okay, we know that we have a scale by now. However, if the scaled
704 // value is an add of something and a constant, we can fold the
705 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000706 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000707 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000708 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000709 ConstantSDNode *AddVal =
710 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
711 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000712 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000713 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000714 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000715 return false;
716 }
717 }
718 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000719 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000720 // We might have folded the load into this mul, so don't regen the value if
721 // so.
722 if (ExprMap.count(N)) break;
723
Chris Lattner947d5442005-01-11 19:37:02 +0000724 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000725 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
726 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000727 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
728 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
729 AM.Scale = unsigned(CN->getValue())-1;
730
731 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000732 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000733
734 // Okay, we know that we have a scale by now. However, if the scaled
735 // value is an add of something and a constant, we can fold the
736 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000737 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000738 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000739 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000740 ConstantSDNode *AddVal =
741 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
742 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000743 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000744 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000745 }
746
747 AM.IndexReg = AM.Base.Reg = Reg;
748 return false;
749 }
750 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000751
752 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000753 // We might have folded the load into this mul, so don't regen the value if
754 // so.
755 if (ExprMap.count(N)) break;
756
Chris Lattner98a8ba02005-01-18 01:06:26 +0000757 X86ISelAddressMode Backup = AM;
758 if (!MatchAddress(N.Val->getOperand(0), AM) &&
759 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000760 return false;
761 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000762 if (!MatchAddress(N.Val->getOperand(1), AM) &&
763 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000764 return false;
765 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000766 break;
767 }
768 }
769
Chris Lattnera95589b2005-01-11 04:40:19 +0000770 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000771 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000772 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000773 if (AM.IndexReg.Val == 0) {
774 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000775 AM.Scale = 1;
776 return false;
777 }
778
779 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000780 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000781 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000782
783 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000784 AM.BaseType = X86ISelAddressMode::RegBase;
785 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000786 return false;
787}
788
789/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
790/// assuming that the temporary registers are in the 8-bit register class.
791///
792/// Tmp1 = setcc1
793/// Tmp2 = setcc2
794/// DestReg = logicalop Tmp1, Tmp2
795///
796static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
797 unsigned SetCC2, unsigned LogicalOp,
798 unsigned DestReg) {
799 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
800 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
801 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
802 BuildMI(BB, SetCC1, 0, Tmp1);
803 BuildMI(BB, SetCC2, 0, Tmp2);
804 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
805}
806
807/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
808/// condition codes match the specified SetCCOpcode. Note that some conditions
809/// require multiple instructions to generate the correct value.
810static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
811 ISD::CondCode SetCCOpcode, bool isFP) {
812 unsigned Opc;
813 if (!isFP) {
814 switch (SetCCOpcode) {
815 default: assert(0 && "Illegal integer SetCC!");
816 case ISD::SETEQ: Opc = X86::SETEr; break;
817 case ISD::SETGT: Opc = X86::SETGr; break;
818 case ISD::SETGE: Opc = X86::SETGEr; break;
819 case ISD::SETLT: Opc = X86::SETLr; break;
820 case ISD::SETLE: Opc = X86::SETLEr; break;
821 case ISD::SETNE: Opc = X86::SETNEr; break;
822 case ISD::SETULT: Opc = X86::SETBr; break;
823 case ISD::SETUGT: Opc = X86::SETAr; break;
824 case ISD::SETULE: Opc = X86::SETBEr; break;
825 case ISD::SETUGE: Opc = X86::SETAEr; break;
826 }
827 } else {
828 // On a floating point condition, the flags are set as follows:
829 // ZF PF CF op
830 // 0 | 0 | 0 | X > Y
831 // 0 | 0 | 1 | X < Y
832 // 1 | 0 | 0 | X == Y
833 // 1 | 1 | 1 | unordered
834 //
835 switch (SetCCOpcode) {
836 default: assert(0 && "Invalid FP setcc!");
837 case ISD::SETUEQ:
838 case ISD::SETEQ:
839 Opc = X86::SETEr; // True if ZF = 1
840 break;
841 case ISD::SETOGT:
842 case ISD::SETGT:
843 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
844 break;
845 case ISD::SETOGE:
846 case ISD::SETGE:
847 Opc = X86::SETAEr; // True if CF = 0
848 break;
849 case ISD::SETULT:
850 case ISD::SETLT:
851 Opc = X86::SETBr; // True if CF = 1
852 break;
853 case ISD::SETULE:
854 case ISD::SETLE:
855 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
856 break;
857 case ISD::SETONE:
858 case ISD::SETNE:
859 Opc = X86::SETNEr; // True if ZF = 0
860 break;
861 case ISD::SETUO:
862 Opc = X86::SETPr; // True if PF = 1
863 break;
864 case ISD::SETO:
865 Opc = X86::SETNPr; // True if PF = 0
866 break;
867 case ISD::SETOEQ: // !PF & ZF
868 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
869 return;
870 case ISD::SETOLT: // !PF & CF
871 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
872 return;
873 case ISD::SETOLE: // !PF & (CF || ZF)
874 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
875 return;
876 case ISD::SETUGT: // PF | (!ZF & !CF)
877 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
878 return;
879 case ISD::SETUGE: // PF | !CF
880 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
881 return;
882 case ISD::SETUNE: // PF | !ZF
883 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
884 return;
885 }
886 }
887 BuildMI(BB, Opc, 0, DestReg);
888}
889
890
891/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
892/// the Dest block if the Cond condition is true. If we cannot fold this
893/// condition into the branch, return true.
894///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000895bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
896 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000897 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
898 // B) using two conditional branches instead of one condbr, two setcc's, and
899 // an or.
900 if ((Cond.getOpcode() == ISD::OR ||
901 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
902 // And and or set the flags for us, so there is no need to emit a TST of the
903 // result. It is only safe to do this if there is only a single use of the
904 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000905 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000906 SelectExpr(Cond);
907 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
908 return false;
909 }
910
911 // Codegen br not C -> JE.
912 if (Cond.getOpcode() == ISD::XOR)
913 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
914 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000915 unsigned CondR;
916 if (getRegPressure(Chain) > getRegPressure(Cond)) {
917 Select(Chain);
918 CondR = SelectExpr(Cond.Val->getOperand(0));
919 } else {
920 CondR = SelectExpr(Cond.Val->getOperand(0));
921 Select(Chain);
922 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000923 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
924 BuildMI(BB, X86::JE, 1).addMBB(Dest);
925 return false;
926 }
927
928 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
929 if (SetCC == 0)
930 return true; // Can only handle simple setcc's so far.
931
932 unsigned Opc;
933
934 // Handle integer conditions first.
935 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
936 switch (SetCC->getCondition()) {
937 default: assert(0 && "Illegal integer SetCC!");
938 case ISD::SETEQ: Opc = X86::JE; break;
939 case ISD::SETGT: Opc = X86::JG; break;
940 case ISD::SETGE: Opc = X86::JGE; break;
941 case ISD::SETLT: Opc = X86::JL; break;
942 case ISD::SETLE: Opc = X86::JLE; break;
943 case ISD::SETNE: Opc = X86::JNE; break;
944 case ISD::SETULT: Opc = X86::JB; break;
945 case ISD::SETUGT: Opc = X86::JA; break;
946 case ISD::SETULE: Opc = X86::JBE; break;
947 case ISD::SETUGE: Opc = X86::JAE; break;
948 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000949 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000950 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000951 BuildMI(BB, Opc, 1).addMBB(Dest);
952 return false;
953 }
954
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000955 unsigned Opc2 = 0; // Second branch if needed.
956
957 // On a floating point condition, the flags are set as follows:
958 // ZF PF CF op
959 // 0 | 0 | 0 | X > Y
960 // 0 | 0 | 1 | X < Y
961 // 1 | 0 | 0 | X == Y
962 // 1 | 1 | 1 | unordered
963 //
964 switch (SetCC->getCondition()) {
965 default: assert(0 && "Invalid FP setcc!");
966 case ISD::SETUEQ:
967 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
968 case ISD::SETOGT:
969 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
970 case ISD::SETOGE:
971 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
972 case ISD::SETULT:
973 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
974 case ISD::SETULE:
975 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
976 case ISD::SETONE:
977 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
978 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
979 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
980 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
981 Opc = X86::JA; // ZF = 0 & CF = 0
982 Opc2 = X86::JP; // PF = 1
983 break;
984 case ISD::SETUGE: // PF = 1 | CF = 0
985 Opc = X86::JAE; // CF = 0
986 Opc2 = X86::JP; // PF = 1
987 break;
988 case ISD::SETUNE: // PF = 1 | ZF = 0
989 Opc = X86::JNE; // ZF = 0
990 Opc2 = X86::JP; // PF = 1
991 break;
992 case ISD::SETOEQ: // PF = 0 & ZF = 1
993 //X86::JNP, X86::JE
994 //X86::AND8rr
995 return true; // FIXME: Emit more efficient code for this branch.
996 case ISD::SETOLT: // PF = 0 & CF = 1
997 //X86::JNP, X86::JB
998 //X86::AND8rr
999 return true; // FIXME: Emit more efficient code for this branch.
1000 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1001 //X86::JNP, X86::JBE
1002 //X86::AND8rr
1003 return true; // FIXME: Emit more efficient code for this branch.
1004 }
1005
Chris Lattner6c07aee2005-01-11 04:06:27 +00001006 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001007 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001008 BuildMI(BB, Opc, 1).addMBB(Dest);
1009 if (Opc2)
1010 BuildMI(BB, Opc2, 1).addMBB(Dest);
1011 return false;
1012}
1013
Chris Lattner24aad1b2005-01-10 22:10:13 +00001014/// EmitSelectCC - Emit code into BB that performs a select operation between
1015/// the two registers RTrue and RFalse, generating a result into RDest. Return
1016/// true if the fold cannot be performed.
1017///
1018void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1019 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1020 enum Condition {
1021 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1022 NOT_SET
1023 } CondCode = NOT_SET;
1024
1025 static const unsigned CMOVTAB16[] = {
1026 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1027 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001028 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001029 };
1030 static const unsigned CMOVTAB32[] = {
1031 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1032 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001033 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001034 };
1035 static const unsigned CMOVTABFP[] = {
1036 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1037 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1038 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1039 };
1040
1041 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1042 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1043 switch (SetCC->getCondition()) {
1044 default: assert(0 && "Unknown integer comparison!");
1045 case ISD::SETEQ: CondCode = EQ; break;
1046 case ISD::SETGT: CondCode = GT; break;
1047 case ISD::SETGE: CondCode = GE; break;
1048 case ISD::SETLT: CondCode = LT; break;
1049 case ISD::SETLE: CondCode = LE; break;
1050 case ISD::SETNE: CondCode = NE; break;
1051 case ISD::SETULT: CondCode = B; break;
1052 case ISD::SETUGT: CondCode = A; break;
1053 case ISD::SETULE: CondCode = BE; break;
1054 case ISD::SETUGE: CondCode = AE; break;
1055 }
1056 } else {
1057 // On a floating point condition, the flags are set as follows:
1058 // ZF PF CF op
1059 // 0 | 0 | 0 | X > Y
1060 // 0 | 0 | 1 | X < Y
1061 // 1 | 0 | 0 | X == Y
1062 // 1 | 1 | 1 | unordered
1063 //
1064 switch (SetCC->getCondition()) {
1065 default: assert(0 && "Unknown FP comparison!");
1066 case ISD::SETUEQ:
1067 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1068 case ISD::SETOGT:
1069 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1070 case ISD::SETOGE:
1071 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1072 case ISD::SETULT:
1073 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1074 case ISD::SETULE:
1075 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1076 case ISD::SETONE:
1077 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1078 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1079 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1080 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1081 case ISD::SETUGE: // PF = 1 | CF = 0
1082 case ISD::SETUNE: // PF = 1 | ZF = 0
1083 case ISD::SETOEQ: // PF = 0 & ZF = 1
1084 case ISD::SETOLT: // PF = 0 & CF = 1
1085 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1086 // We cannot emit this comparison as a single cmov.
1087 break;
1088 }
1089 }
1090 }
1091
1092 unsigned Opc = 0;
1093 if (CondCode != NOT_SET) {
1094 switch (SVT) {
1095 default: assert(0 && "Cannot select this type!");
1096 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1097 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001098 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001099 }
1100 }
1101
1102 // Finally, if we weren't able to fold this, just emit the condition and test
1103 // it.
1104 if (CondCode == NOT_SET || Opc == 0) {
1105 // Get the condition into the zero flag.
1106 unsigned CondReg = SelectExpr(Cond);
1107 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1108
1109 switch (SVT) {
1110 default: assert(0 && "Cannot select this type!");
1111 case MVT::i16: Opc = X86::CMOVE16rr; break;
1112 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001113 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001114 }
1115 } else {
1116 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001117 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001118 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001119 }
1120 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1121}
1122
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001123void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001124 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001125 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1126 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001127 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001128 switch (RHS.getValueType()) {
1129 default: break;
1130 case MVT::i1:
1131 case MVT::i8: Opc = X86::CMP8mi; break;
1132 case MVT::i16: Opc = X86::CMP16mi; break;
1133 case MVT::i32: Opc = X86::CMP32mi; break;
1134 }
1135 if (Opc) {
1136 X86AddressMode AM;
1137 EmitFoldedLoad(LHS, AM);
1138 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1139 return;
1140 }
1141 }
1142
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001143 switch (RHS.getValueType()) {
1144 default: break;
1145 case MVT::i1:
1146 case MVT::i8: Opc = X86::CMP8ri; break;
1147 case MVT::i16: Opc = X86::CMP16ri; break;
1148 case MVT::i32: Opc = X86::CMP32ri; break;
1149 }
1150 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001151 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001152 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1153 return;
1154 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001155 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1156 if (CN->isExactlyValue(+0.0) ||
1157 CN->isExactlyValue(-0.0)) {
1158 unsigned Reg = SelectExpr(LHS);
1159 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1160 BuildMI(BB, X86::FNSTSW8r, 0);
1161 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001162 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001163 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001164 }
1165
Chris Lattneref6806c2005-01-12 02:02:48 +00001166 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001167 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001168 switch (RHS.getValueType()) {
1169 default: break;
1170 case MVT::i1:
1171 case MVT::i8: Opc = X86::CMP8mr; break;
1172 case MVT::i16: Opc = X86::CMP16mr; break;
1173 case MVT::i32: Opc = X86::CMP32mr; break;
1174 }
1175 if (Opc) {
1176 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001177 EmitFoldedLoad(LHS, AM);
1178 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001179 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1180 return;
1181 }
1182 }
1183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001184 switch (LHS.getValueType()) {
1185 default: assert(0 && "Cannot compare this value!");
1186 case MVT::i1:
1187 case MVT::i8: Opc = X86::CMP8rr; break;
1188 case MVT::i16: Opc = X86::CMP16rr; break;
1189 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001190 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001191 }
Chris Lattner11333092005-01-11 03:11:44 +00001192 unsigned Tmp1, Tmp2;
1193 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1194 Tmp1 = SelectExpr(LHS);
1195 Tmp2 = SelectExpr(RHS);
1196 } else {
1197 Tmp2 = SelectExpr(RHS);
1198 Tmp1 = SelectExpr(LHS);
1199 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001200 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1201}
1202
Chris Lattnera5ade062005-01-11 21:19:59 +00001203/// isFoldableLoad - Return true if this is a load instruction that can safely
1204/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001205bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1206 if (Op.getOpcode() == ISD::LOAD) {
1207 // FIXME: currently can't fold constant pool indexes.
1208 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1209 return false;
1210 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1211 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1212 // FIXME: currently can't fold constant pool indexes.
1213 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1214 return false;
1215 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001216 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001217 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001218
1219 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001220 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1221 if (ExprMap.count(Op.getValue(1))) return false;
1222 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001223 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001224
Chris Lattner4ff348b2005-01-17 06:26:58 +00001225 // If there is not just one use of its value, we cannot fold.
1226 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1227
1228 // Finally, we cannot fold the load into the operation if this would induce a
1229 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1230 // operand of the operation we are folding the load into) can possible use the
1231 // chain node defined by the load.
1232 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1233 std::set<SDNode*> Visited;
1234 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1235 return false;
1236 }
1237 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001238}
1239
Chris Lattner4ff348b2005-01-17 06:26:58 +00001240
Chris Lattnera5ade062005-01-11 21:19:59 +00001241/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1242/// and compute the address being loaded into AM.
1243void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1244 SDOperand Chain = Op.getOperand(0);
1245 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001246
Chris Lattnera5ade062005-01-11 21:19:59 +00001247 if (getRegPressure(Chain) > getRegPressure(Address)) {
1248 Select(Chain);
1249 SelectAddress(Address, AM);
1250 } else {
1251 SelectAddress(Address, AM);
1252 Select(Chain);
1253 }
1254
1255 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001256 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1257 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001258 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001259 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001260}
1261
Chris Lattner30ea1e92005-01-19 07:37:26 +00001262// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1263// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1264// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1265// return true.
1266bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001267 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1268 // good!
1269 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1270 std::swap(Op1, Op2); // Op1 is the SHL now.
1271 } else {
1272 return false; // No match
1273 }
1274
1275 SDOperand ShlVal = Op1.getOperand(0);
1276 SDOperand ShlAmt = Op1.getOperand(1);
1277 SDOperand ShrVal = Op2.getOperand(0);
1278 SDOperand ShrAmt = Op2.getOperand(1);
1279
Chris Lattner30ea1e92005-01-19 07:37:26 +00001280 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1281
Chris Lattner85716372005-01-19 06:18:43 +00001282 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1283 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1284 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001285 if (SubCST->getValue() == RegSize) {
1286 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001287 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001288 if (ShrVal == ShlVal) {
1289 unsigned Reg, ShAmt;
1290 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1291 Reg = SelectExpr(ShrVal);
1292 ShAmt = SelectExpr(ShrAmt);
1293 } else {
1294 ShAmt = SelectExpr(ShrAmt);
1295 Reg = SelectExpr(ShrVal);
1296 }
1297 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1298 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1299 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1300 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1301 return true;
1302 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001303 unsigned AReg, BReg;
1304 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001305 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001306 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001307 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001308 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001309 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001310 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001311 unsigned ShAmt = SelectExpr(ShrAmt);
1312 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1313 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1314 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001315 return true;
1316 }
1317 }
1318
Chris Lattner4053b1e2005-01-19 08:07:05 +00001319 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1320 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1321 if (SubCST->getValue() == RegSize) {
1322 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1323 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1324 if (ShrVal == ShlVal) {
1325 unsigned Reg, ShAmt;
1326 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1327 Reg = SelectExpr(ShrVal);
1328 ShAmt = SelectExpr(ShlAmt);
1329 } else {
1330 ShAmt = SelectExpr(ShlAmt);
1331 Reg = SelectExpr(ShrVal);
1332 }
1333 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1334 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1335 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1336 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1337 return true;
1338 } else if (RegSize != 8) {
1339 unsigned AReg, BReg;
1340 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001341 AReg = SelectExpr(ShlVal);
1342 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001343 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001344 BReg = SelectExpr(ShrVal);
1345 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001346 }
1347 unsigned ShAmt = SelectExpr(ShlAmt);
1348 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1349 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1350 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1351 return true;
1352 }
1353 }
Chris Lattner85716372005-01-19 06:18:43 +00001354
Chris Lattner4053b1e2005-01-19 08:07:05 +00001355 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1356 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1357 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1358 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1359 // (A >> 5) | (A << 27) --> ROR A, 5
1360 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1361 if (ShrVal == ShlVal) {
1362 unsigned Reg = SelectExpr(ShrVal);
1363 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1364 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1365 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1366 return true;
1367 } else if (RegSize != 8) {
1368 unsigned AReg, BReg;
1369 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001370 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001371 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001372 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001373 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001374 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001375 }
1376 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1377 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1378 .addImm(ShrCst->getValue());
1379 return true;
1380 }
1381 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001382
Chris Lattner85716372005-01-19 06:18:43 +00001383 return false;
1384}
1385
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001386unsigned ISel::SelectExpr(SDOperand N) {
1387 unsigned Result;
1388 unsigned Tmp1, Tmp2, Tmp3;
1389 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001390 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001391 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001392
Chris Lattner7f2afac2005-01-14 22:37:41 +00001393 if (Node->getOpcode() == ISD::CopyFromReg) {
1394 // FIXME: Handle copy from physregs!
1395
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001396 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001397 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001398 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001399
Chris Lattnera5ade062005-01-11 21:19:59 +00001400 unsigned &Reg = ExprMap[N];
1401 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001402
Chris Lattnerb38a7492005-04-02 04:01:14 +00001403 switch (N.getOpcode()) {
1404 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001405 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001406 MakeReg(N.getValueType()) : 1;
1407 break;
1408 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001409 // If this is a call instruction, make sure to prepare ALL of the result
1410 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001411 if (Node->getNumValues() == 1)
1412 Reg = Result = 1; // Void call, just a chain.
1413 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001414 Result = MakeReg(Node->getValueType(0));
1415 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001416 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001417 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001418 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001419 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001420 break;
1421 case ISD::ADD_PARTS:
1422 case ISD::SUB_PARTS:
1423 case ISD::SHL_PARTS:
1424 case ISD::SRL_PARTS:
1425 case ISD::SRA_PARTS:
1426 Result = MakeReg(Node->getValueType(0));
1427 ExprMap[N.getValue(0)] = Result;
1428 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1429 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1430 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001431 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001432
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001433 switch (N.getOpcode()) {
1434 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001435 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001436 assert(0 && "Node not handled!\n");
1437 case ISD::FrameIndex:
1438 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1439 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1440 return Result;
1441 case ISD::ConstantPool:
1442 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1443 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1444 return Result;
1445 case ISD::ConstantFP:
1446 ContainsFPCode = true;
1447 Tmp1 = Result; // Intermediate Register
1448 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1449 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1450 Tmp1 = MakeReg(MVT::f64);
1451
1452 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1453 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1454 BuildMI(BB, X86::FLD0, 0, Tmp1);
1455 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1456 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1457 BuildMI(BB, X86::FLD1, 0, Tmp1);
1458 else
1459 assert(0 && "Unexpected constant!");
1460 if (Tmp1 != Result)
1461 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1462 return Result;
1463 case ISD::Constant:
1464 switch (N.getValueType()) {
1465 default: assert(0 && "Cannot use constants of this type!");
1466 case MVT::i1:
1467 case MVT::i8: Opc = X86::MOV8ri; break;
1468 case MVT::i16: Opc = X86::MOV16ri; break;
1469 case MVT::i32: Opc = X86::MOV32ri; break;
1470 }
1471 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1472 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001473 case ISD::UNDEF:
1474 if (Node->getValueType(0) == MVT::f64) {
1475 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1476 BuildMI(BB, X86::FLD0, 0, Result);
1477 } else {
1478 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1479 }
1480 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001481 case ISD::GlobalAddress: {
1482 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1483 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1484 return Result;
1485 }
1486 case ISD::ExternalSymbol: {
1487 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1488 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1489 return Result;
1490 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001491 case ISD::ZERO_EXTEND: {
1492 int DestIs16 = N.getValueType() == MVT::i16;
1493 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001494
1495 // FIXME: This hack is here for zero extension casts from bool to i8. This
1496 // would not be needed if bools were promoted by Legalize.
1497 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001498 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001499 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1500 return Result;
1501 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001502
Chris Lattner4ff348b2005-01-17 06:26:58 +00001503 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001504 static const unsigned Opc[3] = {
1505 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1506 };
1507
1508 X86AddressMode AM;
1509 EmitFoldedLoad(N.getOperand(0), AM);
1510 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001511
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001512 return Result;
1513 }
1514
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001515 static const unsigned Opc[3] = {
1516 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1517 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001518 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001519 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1520 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001521 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001522 case ISD::SIGN_EXTEND: {
1523 int DestIs16 = N.getValueType() == MVT::i16;
1524 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1525
Chris Lattner590d8002005-01-09 18:52:44 +00001526 // FIXME: Legalize should promote bools to i8!
1527 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1528 "Sign extend from bool not implemented!");
1529
Chris Lattner4ff348b2005-01-17 06:26:58 +00001530 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001531 static const unsigned Opc[3] = {
1532 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1533 };
1534
1535 X86AddressMode AM;
1536 EmitFoldedLoad(N.getOperand(0), AM);
1537 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1538 return Result;
1539 }
1540
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001541 static const unsigned Opc[3] = {
1542 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1543 };
1544 Tmp1 = SelectExpr(N.getOperand(0));
1545 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1546 return Result;
1547 }
1548 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001549 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001550 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001551 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001552 switch (N.getValueType()) {
1553 default: assert(0 && "Unknown truncate!");
1554 case MVT::i1:
1555 case MVT::i8: Opc = X86::MOV8rm; break;
1556 case MVT::i16: Opc = X86::MOV16rm; break;
1557 }
1558 X86AddressMode AM;
1559 EmitFoldedLoad(N.getOperand(0), AM);
1560 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1561 return Result;
1562 }
1563
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001564 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1565 // a move out of AX or AL.
1566 switch (N.getOperand(0).getValueType()) {
1567 default: assert(0 && "Unknown truncate!");
1568 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1569 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1570 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1571 }
1572 Tmp1 = SelectExpr(N.getOperand(0));
1573 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1574
1575 switch (N.getValueType()) {
1576 default: assert(0 && "Unknown truncate!");
1577 case MVT::i1:
1578 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1579 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1580 }
1581 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1582 return Result;
1583
Chris Lattner590d8002005-01-09 18:52:44 +00001584 case ISD::SINT_TO_FP:
1585 case ISD::UINT_TO_FP: {
1586 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001587 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001588
1589 // Promote the integer to a type supported by FLD. We do this because there
1590 // are no unsigned FLD instructions, so we must promote an unsigned value to
1591 // a larger signed value, then use FLD on the larger value.
1592 //
1593 MVT::ValueType PromoteType = MVT::Other;
1594 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1595 unsigned PromoteOpcode = 0;
1596 unsigned RealDestReg = Result;
1597 switch (SrcTy) {
1598 case MVT::i1:
1599 case MVT::i8:
1600 // We don't have the facilities for directly loading byte sized data from
1601 // memory (even signed). Promote it to 16 bits.
1602 PromoteType = MVT::i16;
1603 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1604 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1605 break;
1606 case MVT::i16:
1607 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1608 PromoteType = MVT::i32;
1609 PromoteOpcode = X86::MOVZX32rr16;
1610 }
1611 break;
1612 default:
1613 // Don't fild into the real destination.
1614 if (Node->getOpcode() == ISD::UINT_TO_FP)
1615 Result = MakeReg(Node->getValueType(0));
1616 break;
1617 }
1618
1619 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001620
Chris Lattner590d8002005-01-09 18:52:44 +00001621 if (PromoteType != MVT::Other) {
1622 Tmp2 = MakeReg(PromoteType);
1623 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1624 SrcTy = PromoteType;
1625 Tmp1 = Tmp2;
1626 }
1627
1628 // Spill the integer to memory and reload it from there.
1629 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1630 MachineFunction *F = BB->getParent();
1631 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1632
1633 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00001634 case MVT::i32:
1635 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1636 FrameIdx).addReg(Tmp1);
1637 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1638 break;
1639 case MVT::i16:
1640 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1641 FrameIdx).addReg(Tmp1);
1642 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1643 break;
1644 default: break; // No promotion required.
1645 }
1646
Chris Lattner085c9952005-01-12 04:00:00 +00001647 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001648 // If this is a cast from uint -> double, we need to be careful when if
1649 // the "sign" bit is set. If so, we don't want to make a negative number,
1650 // we want to make a positive number. Emit code to add an offset if the
1651 // sign bit is set.
1652
1653 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1654 unsigned IsNeg = MakeReg(MVT::i32);
1655 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1656
1657 // Create a CP value that has the offset in one word and 0 in the other.
1658 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1659 0x4f80000000000000ULL);
1660 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1661 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1662 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00001663 }
1664 return RealDestReg;
1665 }
1666 case ISD::FP_TO_SINT:
1667 case ISD::FP_TO_UINT: {
1668 // FIXME: Most of this grunt work should be done by legalize!
1669 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1670
1671 // Change the floating point control register to use "round towards zero"
1672 // mode when truncating to an integer value.
1673 //
1674 MachineFunction *F = BB->getParent();
1675 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1676 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1677
1678 // Load the old value of the high byte of the control word...
1679 unsigned HighPartOfCW = MakeReg(MVT::i8);
1680 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1681 CWFrameIdx, 1);
1682
1683 // Set the high part to be round to zero...
1684 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1685 CWFrameIdx, 1).addImm(12);
1686
1687 // Reload the modified control word now...
1688 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001689
Chris Lattner590d8002005-01-09 18:52:44 +00001690 // Restore the memory image of control word to original value
1691 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1692 CWFrameIdx, 1).addReg(HighPartOfCW);
1693
1694 // We don't have the facilities for directly storing byte sized data to
1695 // memory. Promote it to 16 bits. We also must promote unsigned values to
1696 // larger classes because we only have signed FP stores.
1697 MVT::ValueType StoreClass = Node->getValueType(0);
1698 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1699 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00001700 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001701 case MVT::i8: StoreClass = MVT::i16; break;
1702 case MVT::i16: StoreClass = MVT::i32; break;
1703 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00001704 default: assert(0 && "Unknown store class!");
1705 }
1706
1707 // Spill the integer to memory and reload it from there.
1708 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1709 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1710
1711 switch (StoreClass) {
1712 default: assert(0 && "Unknown store class!");
1713 case MVT::i16:
1714 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1715 break;
1716 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001717 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001718 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00001719 case MVT::i64:
1720 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
1721 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00001722
1723 switch (Node->getValueType(0)) {
1724 default:
1725 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00001726 case MVT::i32:
1727 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1728 break;
1729 case MVT::i16:
1730 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1731 break;
1732 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00001733 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001734 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1735 break;
1736 }
1737
1738 // Reload the original control word now.
1739 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1740 return Result;
1741 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001742 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001743 Op0 = N.getOperand(0);
1744 Op1 = N.getOperand(1);
1745
Chris Lattner44129b52005-01-25 20:03:11 +00001746 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001747 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001748 goto FoldAdd;
1749 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001750
Chris Lattner44129b52005-01-25 20:03:11 +00001751 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001752 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001753 switch (N.getValueType()) {
1754 default: assert(0 && "Cannot add this type!");
1755 case MVT::i1:
1756 case MVT::i8: Opc = X86::ADD8rm; break;
1757 case MVT::i16: Opc = X86::ADD16rm; break;
1758 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001759 case MVT::f64:
1760 // For F64, handle promoted load operations (from F32) as well!
1761 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1762 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001763 }
1764 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001765 EmitFoldedLoad(Op1, AM);
1766 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001767 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1768 return Result;
1769 }
1770
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001771 // See if we can codegen this as an LEA to fold operations together.
1772 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001773 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001774 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001775 MatchAddress(N, AM);
1776 ExprMap[N] = Result;
1777
1778 // If this is not just an add, emit the LEA. For a simple add (like
1779 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1780 // leave this as LEA, then peephole it to 'ADD' after two address elim
1781 // happens.
1782 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1783 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1784 X86AddressMode XAM = SelectAddrExprs(AM);
1785 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1786 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001787 }
1788 }
Chris Lattner11333092005-01-11 03:11:44 +00001789
Chris Lattnera5ade062005-01-11 21:19:59 +00001790 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001791 Opc = 0;
1792 if (CN->getValue() == 1) { // add X, 1 -> inc X
1793 switch (N.getValueType()) {
1794 default: assert(0 && "Cannot integer add this type!");
1795 case MVT::i8: Opc = X86::INC8r; break;
1796 case MVT::i16: Opc = X86::INC16r; break;
1797 case MVT::i32: Opc = X86::INC32r; break;
1798 }
1799 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1800 switch (N.getValueType()) {
1801 default: assert(0 && "Cannot integer add this type!");
1802 case MVT::i8: Opc = X86::DEC8r; break;
1803 case MVT::i16: Opc = X86::DEC16r; break;
1804 case MVT::i32: Opc = X86::DEC32r; break;
1805 }
1806 }
1807
1808 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001809 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001810 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1811 return Result;
1812 }
1813
1814 switch (N.getValueType()) {
1815 default: assert(0 && "Cannot add this type!");
1816 case MVT::i8: Opc = X86::ADD8ri; break;
1817 case MVT::i16: Opc = X86::ADD16ri; break;
1818 case MVT::i32: Opc = X86::ADD32ri; break;
1819 }
1820 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001821 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1823 return Result;
1824 }
1825 }
1826
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001827 switch (N.getValueType()) {
1828 default: assert(0 && "Cannot add this type!");
1829 case MVT::i8: Opc = X86::ADD8rr; break;
1830 case MVT::i16: Opc = X86::ADD16rr; break;
1831 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001832 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001833 }
Chris Lattner11333092005-01-11 03:11:44 +00001834
Chris Lattnera5ade062005-01-11 21:19:59 +00001835 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1836 Tmp1 = SelectExpr(Op0);
1837 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001838 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001839 Tmp2 = SelectExpr(Op1);
1840 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001841 }
1842
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001843 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001845
1846 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001847 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001848 case ISD::FSIN:
1849 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001850 case ISD::FSQRT:
1851 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001852 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001853 switch (N.getOpcode()) {
1854 default: assert(0 && "Unreachable!");
1855 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1856 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1857 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001858 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1859 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001860 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001861 return Result;
1862
Chris Lattner8db0af12005-04-06 04:21:07 +00001863 case ISD::MULHU:
1864 switch (N.getValueType()) {
1865 default: assert(0 && "Unsupported VT!");
1866 case MVT::i8: Tmp2 = X86::MUL8r; break;
1867 case MVT::i16: Tmp2 = X86::MUL16r; break;
1868 case MVT::i32: Tmp2 = X86::MUL32r; break;
1869 }
1870 // FALL THROUGH
1871 case ISD::MULHS: {
1872 unsigned MovOpc, LowReg, HiReg;
1873 switch (N.getValueType()) {
1874 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001875 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001876 MovOpc = X86::MOV8rr;
1877 LowReg = X86::AL;
1878 HiReg = X86::AH;
1879 Opc = X86::IMUL8r;
1880 break;
1881 case MVT::i16:
1882 MovOpc = X86::MOV16rr;
1883 LowReg = X86::AX;
1884 HiReg = X86::DX;
1885 Opc = X86::IMUL16r;
1886 break;
1887 case MVT::i32:
1888 MovOpc = X86::MOV32rr;
1889 LowReg = X86::EAX;
1890 HiReg = X86::EDX;
1891 Opc = X86::IMUL32r;
1892 break;
1893 }
1894 if (Node->getOpcode() != ISD::MULHS)
1895 Opc = Tmp2; // Get the MULHU opcode.
1896
1897 Op0 = Node->getOperand(0);
1898 Op1 = Node->getOperand(1);
1899 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1900 Tmp1 = SelectExpr(Op0);
1901 Tmp2 = SelectExpr(Op1);
1902 } else {
1903 Tmp2 = SelectExpr(Op1);
1904 Tmp1 = SelectExpr(Op0);
1905 }
1906
1907 // FIXME: Implement folding of loads into the memory operands here!
1908 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1909 BuildMI(BB, Opc, 1).addReg(Tmp2);
1910 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1911 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001912 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001913
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001914 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001915 case ISD::MUL:
1916 case ISD::AND:
1917 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001918 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001919 static const unsigned SUBTab[] = {
1920 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1921 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1922 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1923 };
1924 static const unsigned MULTab[] = {
1925 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1926 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1927 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1928 };
1929 static const unsigned ANDTab[] = {
1930 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1931 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001932 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001933 };
1934 static const unsigned ORTab[] = {
1935 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1936 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1937 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1938 };
1939 static const unsigned XORTab[] = {
1940 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1941 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1942 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1943 };
1944
1945 Op0 = Node->getOperand(0);
1946 Op1 = Node->getOperand(1);
1947
Chris Lattner30ea1e92005-01-19 07:37:26 +00001948 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1949 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001950 return Result;
1951
1952 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001953 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1954 if (CN->isNullValue()) { // 0 - N -> neg N
1955 switch (N.getValueType()) {
1956 default: assert(0 && "Cannot sub this type!");
1957 case MVT::i1:
1958 case MVT::i8: Opc = X86::NEG8r; break;
1959 case MVT::i16: Opc = X86::NEG16r; break;
1960 case MVT::i32: Opc = X86::NEG32r; break;
1961 }
1962 Tmp1 = SelectExpr(N.getOperand(1));
1963 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1964 return Result;
1965 }
1966
Chris Lattnera5ade062005-01-11 21:19:59 +00001967 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1968 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001969 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001970 switch (N.getValueType()) {
1971 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001972 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001973 case MVT::i8: Opc = X86::NOT8r; break;
1974 case MVT::i16: Opc = X86::NOT16r; break;
1975 case MVT::i32: Opc = X86::NOT32r; break;
1976 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001977 if (Opc) {
1978 Tmp1 = SelectExpr(Op0);
1979 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1980 return Result;
1981 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001982 }
1983
Chris Lattner2a4e5082005-01-17 06:48:02 +00001984 // Fold common multiplies into LEA instructions.
1985 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1986 switch ((int)CN->getValue()) {
1987 default: break;
1988 case 3:
1989 case 5:
1990 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001991 // Remove N from exprmap so SelectAddress doesn't get confused.
1992 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001993 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001994 SelectAddress(N, AM);
1995 // Restore it to the map.
1996 ExprMap[N] = Result;
1997 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1998 return Result;
1999 }
2000 }
2001
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002003 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002004 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002005 case MVT::i8: Opc = 0; break;
2006 case MVT::i16: Opc = 1; break;
2007 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002008 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002009 switch (Node->getOpcode()) {
2010 default: assert(0 && "Unreachable!");
2011 case ISD::SUB: Opc = SUBTab[Opc]; break;
2012 case ISD::MUL: Opc = MULTab[Opc]; break;
2013 case ISD::AND: Opc = ANDTab[Opc]; break;
2014 case ISD::OR: Opc = ORTab[Opc]; break;
2015 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002017 if (Opc) { // Can't fold MUL:i8 R, imm
2018 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002019 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2020 return Result;
2021 }
2022 }
Chris Lattner11333092005-01-11 03:11:44 +00002023
Chris Lattner44129b52005-01-25 20:03:11 +00002024 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002025 if (Node->getOpcode() != ISD::SUB) {
2026 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002027 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002028 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002029 // For FP, emit 'reverse' subract, with a memory operand.
2030 if (N.getValueType() == MVT::f64) {
2031 if (Op0.getOpcode() == ISD::EXTLOAD)
2032 Opc = X86::FSUBR32m;
2033 else
2034 Opc = X86::FSUBR64m;
2035
Chris Lattnera5ade062005-01-11 21:19:59 +00002036 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002037 EmitFoldedLoad(Op0, AM);
2038 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002039 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2040 return Result;
2041 }
2042 }
2043
Chris Lattner44129b52005-01-25 20:03:11 +00002044 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002045 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002046 switch (N.getValueType()) {
2047 default: assert(0 && "Cannot operate on this type!");
2048 case MVT::i1:
2049 case MVT::i8: Opc = 5; break;
2050 case MVT::i16: Opc = 6; break;
2051 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002052 // For F64, handle promoted load operations (from F32) as well!
2053 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002054 }
2055 switch (Node->getOpcode()) {
2056 default: assert(0 && "Unreachable!");
2057 case ISD::SUB: Opc = SUBTab[Opc]; break;
2058 case ISD::MUL: Opc = MULTab[Opc]; break;
2059 case ISD::AND: Opc = ANDTab[Opc]; break;
2060 case ISD::OR: Opc = ORTab[Opc]; break;
2061 case ISD::XOR: Opc = XORTab[Opc]; break;
2062 }
2063
2064 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002065 EmitFoldedLoad(Op1, AM);
2066 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002067 if (Opc) {
2068 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2069 } else {
2070 assert(Node->getOpcode() == ISD::MUL &&
2071 N.getValueType() == MVT::i8 && "Unexpected situation!");
2072 // Must use the MUL instruction, which forces use of AL.
2073 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2074 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2075 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2076 }
2077 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002078 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002079
2080 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2081 Tmp1 = SelectExpr(Op0);
2082 Tmp2 = SelectExpr(Op1);
2083 } else {
2084 Tmp2 = SelectExpr(Op1);
2085 Tmp1 = SelectExpr(Op0);
2086 }
2087
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002088 switch (N.getValueType()) {
2089 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002090 case MVT::i1:
2091 case MVT::i8: Opc = 10; break;
2092 case MVT::i16: Opc = 11; break;
2093 case MVT::i32: Opc = 12; break;
2094 case MVT::f32: Opc = 13; break;
2095 case MVT::f64: Opc = 14; break;
2096 }
2097 switch (Node->getOpcode()) {
2098 default: assert(0 && "Unreachable!");
2099 case ISD::SUB: Opc = SUBTab[Opc]; break;
2100 case ISD::MUL: Opc = MULTab[Opc]; break;
2101 case ISD::AND: Opc = ANDTab[Opc]; break;
2102 case ISD::OR: Opc = ORTab[Opc]; break;
2103 case ISD::XOR: Opc = XORTab[Opc]; break;
2104 }
2105 if (Opc) {
2106 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2107 } else {
2108 assert(Node->getOpcode() == ISD::MUL &&
2109 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002110 // Must use the MUL instruction, which forces use of AL.
2111 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2112 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2113 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002114 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002115 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002116 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002117 case ISD::ADD_PARTS:
2118 case ISD::SUB_PARTS: {
2119 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2120 "Not an i64 add/sub!");
2121 // Emit all of the operands.
2122 std::vector<unsigned> InVals;
2123 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2124 InVals.push_back(SelectExpr(N.getOperand(i)));
2125 if (N.getOpcode() == ISD::ADD_PARTS) {
2126 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2127 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2128 } else {
2129 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2130 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2131 }
2132 return Result+N.ResNo;
2133 }
2134
Chris Lattnerb38a7492005-04-02 04:01:14 +00002135 case ISD::SHL_PARTS:
2136 case ISD::SRA_PARTS:
2137 case ISD::SRL_PARTS: {
2138 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2139 "Not an i64 shift!");
2140 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2141 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2142 unsigned TmpReg = MakeReg(MVT::i32);
2143 if (N.getOpcode() == ISD::SRA_PARTS) {
2144 // If this is a SHR of a Long, then we need to do funny sign extension
2145 // stuff. TmpReg gets the value to use as the high-part if we are
2146 // shifting more than 32 bits.
2147 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2148 } else {
2149 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2150 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2151 }
2152
2153 // Initialize CL with the shift amount.
2154 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2155 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2156
2157 unsigned TmpReg2 = MakeReg(MVT::i32);
2158 unsigned TmpReg3 = MakeReg(MVT::i32);
2159 if (N.getOpcode() == ISD::SHL_PARTS) {
2160 // TmpReg2 = shld inHi, inLo
2161 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2162 .addReg(ShiftOpLo);
2163 // TmpReg3 = shl inLo, CL
2164 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002165
Chris Lattnerb38a7492005-04-02 04:01:14 +00002166 // Set the flags to indicate whether the shift was by more than 32 bits.
2167 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002168
Chris Lattnerb38a7492005-04-02 04:01:14 +00002169 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002170 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002171 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2172 // DestLo = (>32) ? TmpReg : TmpReg3;
2173 BuildMI(BB, X86::CMOVNE32rr, 2,
2174 Result).addReg(TmpReg3).addReg(TmpReg);
2175 } else {
2176 // TmpReg2 = shrd inLo, inHi
2177 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2178 .addReg(ShiftOpHi);
2179 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002180 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002181 : X86::SHR32rCL, 1, TmpReg3)
2182 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002183
Chris Lattnerb38a7492005-04-02 04:01:14 +00002184 // Set the flags to indicate whether the shift was by more than 32 bits.
2185 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002186
Chris Lattnerb38a7492005-04-02 04:01:14 +00002187 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002188 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002189 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002190
Chris Lattnerb38a7492005-04-02 04:01:14 +00002191 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002192 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002193 Result+1).addReg(TmpReg3).addReg(TmpReg);
2194 }
2195 return Result+N.ResNo;
2196 }
2197
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002198 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002199 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2200 Tmp2 = SelectExpr(N.getOperand(1));
2201 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002202 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002203 Tmp3 = SelectExpr(N.getOperand(2));
2204 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002205 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002206 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2207 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002208
2209 case ISD::SDIV:
2210 case ISD::UDIV:
2211 case ISD::SREM:
2212 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002213 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2214 "We don't support this operator!");
2215
Chris Lattner5bf26862005-04-13 03:29:53 +00002216 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002217 // We can fold loads into FpDIVs, but not really into any others.
2218 if (N.getValueType() == MVT::f64) {
2219 // Check for reversed and unreversed DIV.
2220 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2221 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2222 Opc = X86::FDIVR32m;
2223 else
2224 Opc = X86::FDIVR64m;
2225 X86AddressMode AM;
2226 EmitFoldedLoad(N.getOperand(0), AM);
2227 Tmp1 = SelectExpr(N.getOperand(1));
2228 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2229 return Result;
2230 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2231 N.getOperand(1).getOpcode() == ISD::LOAD) {
2232 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2233 Opc = X86::FDIV32m;
2234 else
2235 Opc = X86::FDIV64m;
2236 X86AddressMode AM;
2237 EmitFoldedLoad(N.getOperand(1), AM);
2238 Tmp1 = SelectExpr(N.getOperand(0));
2239 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2240 return Result;
2241 }
2242 }
2243
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002244 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2245 // FIXME: These special cases should be handled by the lowering impl!
2246 unsigned RHS = CN->getValue();
2247 bool isNeg = false;
2248 if ((int)RHS < 0) {
2249 isNeg = true;
2250 RHS = -RHS;
2251 }
2252 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2253 unsigned Log = log2(RHS);
2254 unsigned TmpReg = MakeReg(N.getValueType());
2255 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2256 switch (N.getValueType()) {
2257 default: assert("Unknown type to signed divide!");
2258 case MVT::i8:
2259 SAROpc = X86::SAR8ri;
2260 SHROpc = X86::SHR8ri;
2261 ADDOpc = X86::ADD8rr;
2262 NEGOpc = X86::NEG8r;
2263 break;
2264 case MVT::i16:
2265 SAROpc = X86::SAR16ri;
2266 SHROpc = X86::SHR16ri;
2267 ADDOpc = X86::ADD16rr;
2268 NEGOpc = X86::NEG16r;
2269 break;
2270 case MVT::i32:
2271 SAROpc = X86::SAR32ri;
2272 SHROpc = X86::SHR32ri;
2273 ADDOpc = X86::ADD32rr;
2274 NEGOpc = X86::NEG32r;
2275 break;
2276 }
Chris Lattner11333092005-01-11 03:11:44 +00002277 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002278 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2279 unsigned TmpReg2 = MakeReg(N.getValueType());
2280 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2281 unsigned TmpReg3 = MakeReg(N.getValueType());
2282 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002283
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002284 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2285 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2286 if (isNeg)
2287 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2288 return Result;
2289 }
2290 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002291 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292
Chris Lattner11333092005-01-11 03:11:44 +00002293 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2294 Tmp1 = SelectExpr(N.getOperand(0));
2295 Tmp2 = SelectExpr(N.getOperand(1));
2296 } else {
2297 Tmp2 = SelectExpr(N.getOperand(1));
2298 Tmp1 = SelectExpr(N.getOperand(0));
2299 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002300
2301 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2302 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2303 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2304 switch (N.getValueType()) {
2305 default: assert(0 && "Cannot sdiv this type!");
2306 case MVT::i8:
2307 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2308 LoReg = X86::AL;
2309 HiReg = X86::AH;
2310 MovOpcode = X86::MOV8rr;
2311 ClrOpcode = X86::MOV8ri;
2312 SExtOpcode = X86::CBW;
2313 break;
2314 case MVT::i16:
2315 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2316 LoReg = X86::AX;
2317 HiReg = X86::DX;
2318 MovOpcode = X86::MOV16rr;
2319 ClrOpcode = X86::MOV16ri;
2320 SExtOpcode = X86::CWD;
2321 break;
2322 case MVT::i32:
2323 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002324 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002325 HiReg = X86::EDX;
2326 MovOpcode = X86::MOV32rr;
2327 ClrOpcode = X86::MOV32ri;
2328 SExtOpcode = X86::CDQ;
2329 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002330 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002331 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002332 return Result;
2333 }
2334
2335 // Set up the low part.
2336 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2337
2338 if (isSigned) {
2339 // Sign extend the low part into the high part.
2340 BuildMI(BB, SExtOpcode, 0);
2341 } else {
2342 // Zero out the high part, effectively zero extending the input.
2343 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2344 }
2345
2346 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002347 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002348
2349 // Get the result of the divide or rem.
2350 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2351 return Result;
2352 }
2353
2354 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002355 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002356 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2357 switch (N.getValueType()) {
2358 default: assert(0 && "Cannot shift this type!");
2359 case MVT::i8: Opc = X86::ADD8rr; break;
2360 case MVT::i16: Opc = X86::ADD16rr; break;
2361 case MVT::i32: Opc = X86::ADD32rr; break;
2362 }
2363 Tmp1 = SelectExpr(N.getOperand(0));
2364 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2365 return Result;
2366 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002367
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002368 switch (N.getValueType()) {
2369 default: assert(0 && "Cannot shift this type!");
2370 case MVT::i8: Opc = X86::SHL8ri; break;
2371 case MVT::i16: Opc = X86::SHL16ri; break;
2372 case MVT::i32: Opc = X86::SHL32ri; break;
2373 }
Chris Lattner11333092005-01-11 03:11:44 +00002374 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002375 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2376 return Result;
2377 }
Chris Lattner11333092005-01-11 03:11:44 +00002378
2379 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2380 Tmp1 = SelectExpr(N.getOperand(0));
2381 Tmp2 = SelectExpr(N.getOperand(1));
2382 } else {
2383 Tmp2 = SelectExpr(N.getOperand(1));
2384 Tmp1 = SelectExpr(N.getOperand(0));
2385 }
2386
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002387 switch (N.getValueType()) {
2388 default: assert(0 && "Cannot shift this type!");
2389 case MVT::i8 : Opc = X86::SHL8rCL; break;
2390 case MVT::i16: Opc = X86::SHL16rCL; break;
2391 case MVT::i32: Opc = X86::SHL32rCL; break;
2392 }
2393 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2394 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2395 return Result;
2396 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002397 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2398 switch (N.getValueType()) {
2399 default: assert(0 && "Cannot shift this type!");
2400 case MVT::i8: Opc = X86::SHR8ri; break;
2401 case MVT::i16: Opc = X86::SHR16ri; break;
2402 case MVT::i32: Opc = X86::SHR32ri; break;
2403 }
Chris Lattner11333092005-01-11 03:11:44 +00002404 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002405 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2406 return Result;
2407 }
Chris Lattner11333092005-01-11 03:11:44 +00002408
2409 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2410 Tmp1 = SelectExpr(N.getOperand(0));
2411 Tmp2 = SelectExpr(N.getOperand(1));
2412 } else {
2413 Tmp2 = SelectExpr(N.getOperand(1));
2414 Tmp1 = SelectExpr(N.getOperand(0));
2415 }
2416
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002417 switch (N.getValueType()) {
2418 default: assert(0 && "Cannot shift this type!");
2419 case MVT::i8 : Opc = X86::SHR8rCL; break;
2420 case MVT::i16: Opc = X86::SHR16rCL; break;
2421 case MVT::i32: Opc = X86::SHR32rCL; break;
2422 }
2423 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2424 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2425 return Result;
2426 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002427 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2428 switch (N.getValueType()) {
2429 default: assert(0 && "Cannot shift this type!");
2430 case MVT::i8: Opc = X86::SAR8ri; break;
2431 case MVT::i16: Opc = X86::SAR16ri; break;
2432 case MVT::i32: Opc = X86::SAR32ri; break;
2433 }
Chris Lattner11333092005-01-11 03:11:44 +00002434 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002435 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2436 return Result;
2437 }
Chris Lattner11333092005-01-11 03:11:44 +00002438
2439 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2440 Tmp1 = SelectExpr(N.getOperand(0));
2441 Tmp2 = SelectExpr(N.getOperand(1));
2442 } else {
2443 Tmp2 = SelectExpr(N.getOperand(1));
2444 Tmp1 = SelectExpr(N.getOperand(0));
2445 }
2446
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002447 switch (N.getValueType()) {
2448 default: assert(0 && "Cannot shift this type!");
2449 case MVT::i8 : Opc = X86::SAR8rCL; break;
2450 case MVT::i16: Opc = X86::SAR16rCL; break;
2451 case MVT::i32: Opc = X86::SAR32rCL; break;
2452 }
2453 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2454 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2455 return Result;
2456
2457 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002458 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2460 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2461 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002462 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002463 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002464 if (Result != 1) { // Generate the token
2465 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2466 assert(0 && "Load already emitted!?");
2467 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002468 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2469
Chris Lattner5188ad72005-01-08 19:28:19 +00002470 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002471 default: assert(0 && "Cannot load this type!");
2472 case MVT::i1:
2473 case MVT::i8: Opc = X86::MOV8rm; break;
2474 case MVT::i16: Opc = X86::MOV16rm; break;
2475 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002476 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2477 }
Chris Lattner11333092005-01-11 03:11:44 +00002478
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002479 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002480 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002481 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2482 } else {
2483 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002484
2485 SDOperand Chain = N.getOperand(0);
2486 SDOperand Address = N.getOperand(1);
2487 if (getRegPressure(Chain) > getRegPressure(Address)) {
2488 Select(Chain);
2489 SelectAddress(Address, AM);
2490 } else {
2491 SelectAddress(Address, AM);
2492 Select(Chain);
2493 }
2494
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002495 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2496 }
2497 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002498
2499 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2500 case ISD::ZEXTLOAD: {
2501 // Make sure we generate both values.
2502 if (Result != 1)
2503 ExprMap[N.getValue(1)] = 1; // Generate the token
2504 else
2505 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2506
Chris Lattnerda2ce112005-01-16 07:34:08 +00002507 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2508 if (Node->getValueType(0) == MVT::f64) {
2509 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2510 "Bad EXTLOAD!");
2511 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2512 CP->getIndex());
2513 return Result;
2514 }
2515
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002516 X86AddressMode AM;
2517 if (getRegPressure(Node->getOperand(0)) >
2518 getRegPressure(Node->getOperand(1))) {
2519 Select(Node->getOperand(0)); // chain
2520 SelectAddress(Node->getOperand(1), AM);
2521 } else {
2522 SelectAddress(Node->getOperand(1), AM);
2523 Select(Node->getOperand(0)); // chain
2524 }
2525
2526 switch (Node->getValueType(0)) {
2527 default: assert(0 && "Unknown type to sign extend to.");
2528 case MVT::f64:
2529 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2530 "Bad EXTLOAD!");
2531 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2532 break;
2533 case MVT::i32:
2534 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2535 default:
2536 assert(0 && "Bad zero extend!");
2537 case MVT::i1:
2538 case MVT::i8:
2539 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2540 break;
2541 case MVT::i16:
2542 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2543 break;
2544 }
2545 break;
2546 case MVT::i16:
2547 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2548 "Bad zero extend!");
2549 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2550 break;
2551 case MVT::i8:
2552 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2553 "Bad zero extend!");
2554 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2555 break;
2556 }
2557 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002558 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002559 case ISD::SEXTLOAD: {
2560 // Make sure we generate both values.
2561 if (Result != 1)
2562 ExprMap[N.getValue(1)] = 1; // Generate the token
2563 else
2564 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2565
2566 X86AddressMode AM;
2567 if (getRegPressure(Node->getOperand(0)) >
2568 getRegPressure(Node->getOperand(1))) {
2569 Select(Node->getOperand(0)); // chain
2570 SelectAddress(Node->getOperand(1), AM);
2571 } else {
2572 SelectAddress(Node->getOperand(1), AM);
2573 Select(Node->getOperand(0)); // chain
2574 }
2575
2576 switch (Node->getValueType(0)) {
2577 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2578 default: assert(0 && "Unknown type to sign extend to.");
2579 case MVT::i32:
2580 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2581 default:
2582 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2583 case MVT::i8:
2584 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2585 break;
2586 case MVT::i16:
2587 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2588 break;
2589 }
2590 break;
2591 case MVT::i16:
2592 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2593 "Cannot sign extend from bool!");
2594 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2595 break;
2596 }
2597 return Result;
2598 }
2599
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002600 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002601 // Generate both result values.
2602 if (Result != 1)
2603 ExprMap[N.getValue(1)] = 1; // Generate the token
2604 else
2605 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2606
2607 // FIXME: We are currently ignoring the requested alignment for handling
2608 // greater than the stack alignment. This will need to be revisited at some
2609 // point. Align = N.getOperand(2);
2610
2611 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2612 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2613 std::cerr << "Cannot allocate stack object with greater alignment than"
2614 << " the stack alignment yet!";
2615 abort();
2616 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002617
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002618 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002619 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002620 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2621 .addImm(CN->getValue());
2622 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002623 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2624 Select(N.getOperand(0));
2625 Tmp1 = SelectExpr(N.getOperand(1));
2626 } else {
2627 Tmp1 = SelectExpr(N.getOperand(1));
2628 Select(N.getOperand(0));
2629 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002630
2631 // Subtract size from stack pointer, thereby allocating some space.
2632 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2633 }
2634
2635 // Put a pointer to the space into the result register, by copying the stack
2636 // pointer.
2637 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2638 return Result;
2639
2640 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002641 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002642 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002643
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002644 if (GlobalAddressSDNode *GASD =
2645 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002646 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002647 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2648 } else if (ExternalSymbolSDNode *ESSDN =
2649 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002650 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002651 BuildMI(BB, X86::CALLpcrel32,
2652 1).addExternalSymbol(ESSDN->getSymbol(), true);
2653 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002654 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2655 Select(N.getOperand(0));
2656 Tmp1 = SelectExpr(N.getOperand(1));
2657 } else {
2658 Tmp1 = SelectExpr(N.getOperand(1));
2659 Select(N.getOperand(0));
2660 }
2661
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002662 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2663 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002664 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002665 default: assert(0 && "Unknown value type for call result!");
2666 case MVT::Other: return 1;
2667 case MVT::i1:
2668 case MVT::i8:
2669 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2670 break;
2671 case MVT::i16:
2672 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2673 break;
2674 case MVT::i32:
2675 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002676 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002677 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2678 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002679 case MVT::f64: // Floating-point return values live in %ST(0)
2680 ContainsFPCode = true;
2681 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2682 break;
2683 }
2684 return Result+N.ResNo;
Chris Lattner966cdfb2005-05-09 21:17:38 +00002685 case ISD::READPORT:
2686 // First, determine that the size of the operand falls within the acceptable
2687 // range for this architecture.
2688 //
2689 if (Node->getOperand(1).getValueType() != MVT::i16) {
2690 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2691 exit(1);
2692 }
2693
2694 // Make sure we generate both values.
2695 if (Result != 1) { // Generate the token
2696 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2697 assert(0 && "readport already emitted!?");
2698 } else
2699 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2700
2701 Select(Node->getOperand(0)); // Select the chain.
2702
2703 // If the port is a single-byte constant, use the immediate form.
2704 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2705 if ((Port->getValue() & 255) == Port->getValue()) {
2706 switch (Node->getValueType(0)) {
2707 case MVT::i8:
2708 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2709 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2710 return Result;
2711 case MVT::i16:
2712 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2713 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2714 return Result;
2715 case MVT::i32:
2716 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2717 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2718 return Result;
2719 default: break;
2720 }
2721 }
2722
2723 // Now, move the I/O port address into the DX register and use the IN
2724 // instruction to get the input data.
2725 //
2726 Tmp1 = SelectExpr(Node->getOperand(1));
2727 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2728 switch (Node->getValueType(0)) {
2729 case MVT::i8:
2730 BuildMI(BB, X86::IN8rr, 0);
2731 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2732 return Result;
2733 case MVT::i16:
2734 BuildMI(BB, X86::IN16rr, 0);
2735 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2736 return Result;
2737 case MVT::i32:
2738 BuildMI(BB, X86::IN32rr, 0);
2739 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2740 return Result;
2741 default:
2742 std::cerr << "Cannot do input on this data type";
2743 exit(1);
2744 }
2745
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002746 }
2747
2748 return 0;
2749}
2750
Chris Lattnere10269b2005-01-17 19:25:26 +00002751/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2752/// load/op/store instruction. If successful return true.
2753bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2754 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2755 SDOperand Chain = Node->getOperand(0);
2756 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002757 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002758
2759 // The chain has to be a load, the stored value must be an integer binary
2760 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002761 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002762 MVT::isFloatingPoint(StVal.getValueType()))
2763 return false;
2764
Chris Lattner5c659812005-01-17 22:10:42 +00002765 // Token chain must either be a factor node or the load to fold.
2766 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2767 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002768
Chris Lattner5c659812005-01-17 22:10:42 +00002769 SDOperand TheLoad;
2770
2771 // Check to see if there is a load from the same pointer that we're storing
2772 // to in either operand of the binop.
2773 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2774 StVal.getOperand(0).getOperand(1) == StPtr)
2775 TheLoad = StVal.getOperand(0);
2776 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2777 StVal.getOperand(1).getOperand(1) == StPtr)
2778 TheLoad = StVal.getOperand(1);
2779 else
2780 return false; // No matching load operand.
2781
2782 // We can only fold the load if there are no intervening side-effecting
2783 // operations. This means that the store uses the load as its token chain, or
2784 // there are only token factor nodes in between the store and load.
2785 if (Chain != TheLoad.getValue(1)) {
2786 // Okay, the other option is that we have a store referring to (possibly
2787 // nested) token factor nodes. For now, just try peeking through one level
2788 // of token factors to see if this is the case.
2789 bool ChainOk = false;
2790 if (Chain.getOpcode() == ISD::TokenFactor) {
2791 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2792 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2793 ChainOk = true;
2794 break;
2795 }
2796 }
2797
2798 if (!ChainOk) return false;
2799 }
2800
2801 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002802 return false;
2803
2804 // Make sure that one of the operands of the binop is the load, and that the
2805 // load folds into the binop.
2806 if (((StVal.getOperand(0) != TheLoad ||
2807 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2808 (StVal.getOperand(1) != TheLoad ||
2809 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2810 return false;
2811
2812 // Finally, check to see if this is one of the ops we can handle!
2813 static const unsigned ADDTAB[] = {
2814 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2815 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2816 };
2817 static const unsigned SUBTAB[] = {
2818 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2819 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2820 };
2821 static const unsigned ANDTAB[] = {
2822 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2823 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2824 };
2825 static const unsigned ORTAB[] = {
2826 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2827 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2828 };
2829 static const unsigned XORTAB[] = {
2830 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2831 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2832 };
2833 static const unsigned SHLTAB[] = {
2834 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2835 /*Have to put the reg in CL*/0, 0, 0,
2836 };
2837 static const unsigned SARTAB[] = {
2838 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2839 /*Have to put the reg in CL*/0, 0, 0,
2840 };
2841 static const unsigned SHRTAB[] = {
2842 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2843 /*Have to put the reg in CL*/0, 0, 0,
2844 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002845
Chris Lattnere10269b2005-01-17 19:25:26 +00002846 const unsigned *TabPtr = 0;
2847 switch (StVal.getOpcode()) {
2848 default:
2849 std::cerr << "CANNOT [mem] op= val: ";
2850 StVal.Val->dump(); std::cerr << "\n";
2851 case ISD::MUL:
2852 case ISD::SDIV:
2853 case ISD::UDIV:
2854 case ISD::SREM:
2855 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002856
Chris Lattnere10269b2005-01-17 19:25:26 +00002857 case ISD::ADD: TabPtr = ADDTAB; break;
2858 case ISD::SUB: TabPtr = SUBTAB; break;
2859 case ISD::AND: TabPtr = ANDTAB; break;
2860 case ISD:: OR: TabPtr = ORTAB; break;
2861 case ISD::XOR: TabPtr = XORTAB; break;
2862 case ISD::SHL: TabPtr = SHLTAB; break;
2863 case ISD::SRA: TabPtr = SARTAB; break;
2864 case ISD::SRL: TabPtr = SHRTAB; break;
2865 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002866
Chris Lattnere10269b2005-01-17 19:25:26 +00002867 // Handle: [mem] op= CST
2868 SDOperand Op0 = StVal.getOperand(0);
2869 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002870 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002871 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2872 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2873 default: break;
2874 case MVT::i1:
2875 case MVT::i8: Opc = TabPtr[0]; break;
2876 case MVT::i16: Opc = TabPtr[1]; break;
2877 case MVT::i32: Opc = TabPtr[2]; break;
2878 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002879
Chris Lattnere10269b2005-01-17 19:25:26 +00002880 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002881 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2882 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002883 Select(Chain);
2884
Chris Lattnere10269b2005-01-17 19:25:26 +00002885 X86AddressMode AM;
2886 if (getRegPressure(TheLoad.getOperand(0)) >
2887 getRegPressure(TheLoad.getOperand(1))) {
2888 Select(TheLoad.getOperand(0));
2889 SelectAddress(TheLoad.getOperand(1), AM);
2890 } else {
2891 SelectAddress(TheLoad.getOperand(1), AM);
2892 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002893 }
Chris Lattner5c659812005-01-17 22:10:42 +00002894
2895 if (StVal.getOpcode() == ISD::ADD) {
2896 if (CN->getValue() == 1) {
2897 switch (Op0.getValueType()) {
2898 default: break;
2899 case MVT::i8:
2900 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2901 return true;
2902 case MVT::i16: Opc = TabPtr[1];
2903 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2904 return true;
2905 case MVT::i32: Opc = TabPtr[2];
2906 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2907 return true;
2908 }
2909 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2910 switch (Op0.getValueType()) {
2911 default: break;
2912 case MVT::i8:
2913 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2914 return true;
2915 case MVT::i16: Opc = TabPtr[1];
2916 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2917 return true;
2918 case MVT::i32: Opc = TabPtr[2];
2919 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2920 return true;
2921 }
2922 }
2923 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002924
Chris Lattnere10269b2005-01-17 19:25:26 +00002925 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2926 return true;
2927 }
2928 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002929
Chris Lattnere10269b2005-01-17 19:25:26 +00002930 // If we have [mem] = V op [mem], try to turn it into:
2931 // [mem] = [mem] op V.
2932 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2933 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2934 StVal.getOpcode() != ISD::SRL)
2935 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002936
Chris Lattnere10269b2005-01-17 19:25:26 +00002937 if (Op0 != TheLoad) return false;
2938
2939 switch (Op0.getValueType()) {
2940 default: return false;
2941 case MVT::i1:
2942 case MVT::i8: Opc = TabPtr[3]; break;
2943 case MVT::i16: Opc = TabPtr[4]; break;
2944 case MVT::i32: Opc = TabPtr[5]; break;
2945 }
Chris Lattner5c659812005-01-17 22:10:42 +00002946
Chris Lattnerb422aea2005-01-18 17:35:28 +00002947 // Table entry doesn't exist?
2948 if (Opc == 0) return false;
2949
Chris Lattner4a108662005-01-18 03:51:59 +00002950 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2951 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002952 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002953 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002954
Chris Lattnere10269b2005-01-17 19:25:26 +00002955 X86AddressMode AM;
2956 SelectAddress(TheLoad.getOperand(1), AM);
2957 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002958 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002959 return true;
2960}
2961
2962
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002963void ISel::Select(SDOperand N) {
2964 unsigned Tmp1, Tmp2, Opc;
2965
Nate Begeman85fdeb22005-03-24 04:39:54 +00002966 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002967 return; // Already selected.
2968
Chris Lattner989de032005-01-11 06:14:36 +00002969 SDNode *Node = N.Val;
2970
2971 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002972 default:
Chris Lattner989de032005-01-11 06:14:36 +00002973 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002974 assert(0 && "Node not handled yet!");
2975 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002976 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002977 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002978 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002979 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2980 Select(Node->getOperand(OneFirst));
2981 Select(Node->getOperand(!OneFirst));
2982 } else {
2983 std::vector<std::pair<unsigned, unsigned> > OpsP;
2984 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2985 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2986 std::sort(OpsP.begin(), OpsP.end());
2987 std::reverse(OpsP.begin(), OpsP.end());
2988 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2989 Select(Node->getOperand(OpsP[i].second));
2990 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002991 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002992 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002993 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2994 Select(N.getOperand(0));
2995 Tmp1 = SelectExpr(N.getOperand(1));
2996 } else {
2997 Tmp1 = SelectExpr(N.getOperand(1));
2998 Select(N.getOperand(0));
2999 }
Chris Lattner18c2f132005-01-13 20:50:02 +00003000 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003001
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003002 if (Tmp1 != Tmp2) {
3003 switch (N.getOperand(1).getValueType()) {
3004 default: assert(0 && "Invalid type for operation!");
3005 case MVT::i1:
3006 case MVT::i8: Opc = X86::MOV8rr; break;
3007 case MVT::i16: Opc = X86::MOV16rr; break;
3008 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003009 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003010 }
3011 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3012 }
3013 return;
3014 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003015 switch (N.getNumOperands()) {
3016 default:
3017 assert(0 && "Unknown return instruction!");
3018 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003019 assert(N.getOperand(1).getValueType() == MVT::i32 &&
3020 N.getOperand(2).getValueType() == MVT::i32 &&
3021 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00003022 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3023 Tmp1 = SelectExpr(N.getOperand(1));
3024 Tmp2 = SelectExpr(N.getOperand(2));
3025 } else {
3026 Tmp2 = SelectExpr(N.getOperand(2));
3027 Tmp1 = SelectExpr(N.getOperand(1));
3028 }
3029 Select(N.getOperand(0));
3030
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003031 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3032 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003033 break;
3034 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00003035 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3036 Select(N.getOperand(0));
3037 Tmp1 = SelectExpr(N.getOperand(1));
3038 } else {
3039 Tmp1 = SelectExpr(N.getOperand(1));
3040 Select(N.getOperand(0));
3041 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003042 switch (N.getOperand(1).getValueType()) {
3043 default: assert(0 && "All other types should have been promoted!!");
3044 case MVT::f64:
3045 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003046 break;
3047 case MVT::i32:
3048 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003049 break;
3050 }
3051 break;
3052 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00003053 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003054 break;
3055 }
3056 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3057 return;
3058 case ISD::BR: {
3059 Select(N.getOperand(0));
3060 MachineBasicBlock *Dest =
3061 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3062 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3063 return;
3064 }
3065
3066 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003067 MachineBasicBlock *Dest =
3068 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003069
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003070 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3071 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003072 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3073 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3074 Select(N.getOperand(0));
3075 Tmp1 = SelectExpr(N.getOperand(1));
3076 } else {
3077 Tmp1 = SelectExpr(N.getOperand(1));
3078 Select(N.getOperand(0));
3079 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003080 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3081 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3082 }
Chris Lattner11333092005-01-11 03:11:44 +00003083
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003084 return;
3085 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003086
Chris Lattner4df0de92005-01-17 00:00:33 +00003087 case ISD::LOAD:
3088 // If this load could be folded into the only using instruction, and if it
3089 // is safe to emit the instruction here, try to do so now.
3090 if (Node->hasNUsesOfValue(1, 0)) {
3091 SDOperand TheVal = N.getValue(0);
3092 SDNode *User = 0;
3093 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3094 assert(UI != Node->use_end() && "Didn't find use!");
3095 SDNode *UN = *UI;
3096 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3097 if (UN->getOperand(i) == TheVal) {
3098 User = UN;
3099 goto FoundIt;
3100 }
3101 }
3102 FoundIt:
3103 // Only handle unary operators right now.
3104 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003105 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003106 SelectExpr(SDOperand(User, 0));
3107 return;
3108 }
3109 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003110 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003111 SelectExpr(N);
3112 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00003113 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003114 case ISD::EXTLOAD:
3115 case ISD::SEXTLOAD:
3116 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003117 case ISD::CALL:
3118 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003119 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003120 SelectExpr(N);
3121 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003122
3123 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3124 // On X86, we can represent all types except for Bool and Float natively.
3125 X86AddressMode AM;
3126 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003127 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3128 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3129 && "Unsupported TRUNCSTORE for this target!");
3130
3131 if (StoredTy == MVT::i16) {
3132 // FIXME: This is here just to allow testing. X86 doesn't really have a
3133 // TRUNCSTORE i16 operation, but this is required for targets that do not
3134 // have 16-bit integer registers. We occasionally disable 16-bit integer
3135 // registers to test the promotion code.
3136 Select(N.getOperand(0));
3137 Tmp1 = SelectExpr(N.getOperand(1));
3138 SelectAddress(N.getOperand(2), AM);
3139
3140 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3141 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3142 return;
3143 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003144
3145 // Store of constant bool?
3146 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3147 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3148 Select(N.getOperand(0));
3149 SelectAddress(N.getOperand(2), AM);
3150 } else {
3151 SelectAddress(N.getOperand(2), AM);
3152 Select(N.getOperand(0));
3153 }
3154 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3155 return;
3156 }
3157
3158 switch (StoredTy) {
3159 default: assert(0 && "Cannot truncstore this type!");
3160 case MVT::i1: Opc = X86::MOV8mr; break;
3161 case MVT::f32: Opc = X86::FST32m; break;
3162 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003163
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003164 std::vector<std::pair<unsigned, unsigned> > RP;
3165 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3166 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3167 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3168 std::sort(RP.begin(), RP.end());
3169
Chris Lattner572dd082005-02-23 05:57:21 +00003170 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003171 for (unsigned i = 0; i != 3; ++i)
3172 switch (RP[2-i].second) {
3173 default: assert(0 && "Unknown operand number!");
3174 case 0: Select(N.getOperand(0)); break;
3175 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3176 case 2: SelectAddress(N.getOperand(2), AM); break;
3177 }
3178
3179 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3180 return;
3181 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003182 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003183 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003184
3185 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3186 Opc = 0;
3187 switch (CN->getValueType(0)) {
3188 default: assert(0 && "Invalid type for operation!");
3189 case MVT::i1:
3190 case MVT::i8: Opc = X86::MOV8mi; break;
3191 case MVT::i16: Opc = X86::MOV16mi; break;
3192 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003193 case MVT::f64: break;
3194 }
3195 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003196 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3197 Select(N.getOperand(0));
3198 SelectAddress(N.getOperand(2), AM);
3199 } else {
3200 SelectAddress(N.getOperand(2), AM);
3201 Select(N.getOperand(0));
3202 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003203 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3204 return;
3205 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003206 } else if (GlobalAddressSDNode *GA =
3207 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3208 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3209
3210 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3211 Select(N.getOperand(0));
3212 SelectAddress(N.getOperand(2), AM);
3213 } else {
3214 SelectAddress(N.getOperand(2), AM);
3215 Select(N.getOperand(0));
3216 }
3217 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3218 AM).addGlobalAddress(GA->getGlobal());
3219 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003220 }
Chris Lattner837caa72005-01-11 23:21:30 +00003221
3222 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003223 if (TryToFoldLoadOpStore(Node))
3224 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003225
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003226 switch (N.getOperand(1).getValueType()) {
3227 default: assert(0 && "Cannot store this type!");
3228 case MVT::i1:
3229 case MVT::i8: Opc = X86::MOV8mr; break;
3230 case MVT::i16: Opc = X86::MOV16mr; break;
3231 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003232 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003233 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003234
Chris Lattner11333092005-01-11 03:11:44 +00003235 std::vector<std::pair<unsigned, unsigned> > RP;
3236 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3237 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3238 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3239 std::sort(RP.begin(), RP.end());
3240
Chris Lattner572dd082005-02-23 05:57:21 +00003241 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003242 for (unsigned i = 0; i != 3; ++i)
3243 switch (RP[2-i].second) {
3244 default: assert(0 && "Unknown operand number!");
3245 case 0: Select(N.getOperand(0)); break;
3246 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003247 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003248 }
3249
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003250 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3251 return;
3252 }
3253 case ISD::ADJCALLSTACKDOWN:
3254 case ISD::ADJCALLSTACKUP:
3255 Select(N.getOperand(0));
3256 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003257
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003258 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3259 X86::ADJCALLSTACKUP;
3260 BuildMI(BB, Opc, 1).addImm(Tmp1);
3261 return;
Chris Lattner989de032005-01-11 06:14:36 +00003262 case ISD::MEMSET: {
3263 Select(N.getOperand(0)); // Select the chain.
3264 unsigned Align =
3265 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3266 if (Align == 0) Align = 1;
3267
3268 // Turn the byte code into # iterations
3269 unsigned CountReg;
3270 unsigned Opcode;
3271 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3272 unsigned Val = ValC->getValue() & 255;
3273
3274 // If the value is a constant, then we can potentially use larger sets.
3275 switch (Align & 3) {
3276 case 2: // WORD 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()/2);
3280 } else {
3281 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3282 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3283 }
3284 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3285 Opcode = X86::REP_STOSW;
3286 break;
3287 case 0: // DWORD aligned
3288 CountReg = MakeReg(MVT::i32);
3289 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3290 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3291 } else {
3292 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3293 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3294 }
3295 Val = (Val << 8) | Val;
3296 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3297 Opcode = X86::REP_STOSD;
3298 break;
3299 default: // BYTE aligned
3300 CountReg = SelectExpr(Node->getOperand(3));
3301 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3302 Opcode = X86::REP_STOSB;
3303 break;
3304 }
3305 } else {
3306 // If it's not a constant value we are storing, just fall back. We could
3307 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3308 unsigned ValReg = SelectExpr(Node->getOperand(2));
3309 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3310 CountReg = SelectExpr(Node->getOperand(3));
3311 Opcode = X86::REP_STOSB;
3312 }
3313
3314 // No matter what the alignment is, we put the source in ESI, the
3315 // destination in EDI, and the count in ECX.
3316 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3317 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3318 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3319 BuildMI(BB, Opcode, 0);
3320 return;
3321 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003322 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00003323 Select(N.getOperand(0)); // Select the chain.
3324 unsigned Align =
3325 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3326 if (Align == 0) Align = 1;
3327
3328 // Turn the byte code into # iterations
3329 unsigned CountReg;
3330 unsigned Opcode;
3331 switch (Align & 3) {
3332 case 2: // WORD aligned
3333 CountReg = MakeReg(MVT::i32);
3334 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3335 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3336 } else {
3337 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3338 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3339 }
3340 Opcode = X86::REP_MOVSW;
3341 break;
3342 case 0: // DWORD aligned
3343 CountReg = MakeReg(MVT::i32);
3344 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3345 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3346 } else {
3347 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3348 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3349 }
3350 Opcode = X86::REP_MOVSD;
3351 break;
3352 default: // BYTE aligned
3353 CountReg = SelectExpr(Node->getOperand(3));
3354 Opcode = X86::REP_MOVSB;
3355 break;
3356 }
3357
3358 // No matter what the alignment is, we put the source in ESI, the
3359 // destination in EDI, and the count in ECX.
3360 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3361 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3362 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3363 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3364 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3365 BuildMI(BB, Opcode, 0);
3366 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003367 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003368 case ISD::WRITEPORT:
3369 if (Node->getOperand(2).getValueType() != MVT::i16) {
3370 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3371 exit(1);
3372 }
3373 Select(Node->getOperand(0)); // Emit the chain.
3374
3375 Tmp1 = SelectExpr(Node->getOperand(1));
3376 switch (Node->getOperand(1).getValueType()) {
3377 case MVT::i8:
3378 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3379 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3380 break;
3381 case MVT::i16:
3382 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3383 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3384 break;
3385 case MVT::i32:
3386 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3387 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3388 break;
3389 default:
3390 std::cerr << "llvm.writeport: invalid data type for X86 target";
3391 exit(1);
3392 }
3393
3394 // If the port is a single-byte constant, use the immediate form.
3395 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3396 if ((CN->getValue() & 255) == CN->getValue()) {
3397 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3398 return;
3399 }
3400
3401 // Otherwise, move the I/O port address into the DX register.
3402 unsigned Reg = SelectExpr(Node->getOperand(2));
3403 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3404 BuildMI(BB, Opc, 0);
3405 return;
3406 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003407 assert(0 && "Should not be reached!");
3408}
3409
3410
3411/// createX86PatternInstructionSelector - This pass converts an LLVM function
3412/// into a machine code representation using pattern matching and a machine
3413/// description file.
3414///
3415FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003416 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003417}