blob: 27c5d061c266e7c5593a25fabe34c6e5d775f1ea [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
2//
3// This file defines a simple peephole instruction selector for the x86 platform
4//
5//===----------------------------------------------------------------------===//
6
7#include "X86.h"
Chris Lattner055c9652002-10-29 21:05:24 +00008#include "X86InstrInfo.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +00009#include "X86InstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000010#include "llvm/Function.h"
11#include "llvm/iTerminators.h"
Brian Gaeke1749d632002-11-07 17:59:21 +000012#include "llvm/iOperators.h"
Brian Gaekea1719c92002-10-31 23:03:59 +000013#include "llvm/iOther.h"
Chris Lattner51b49a92002-11-02 19:45:49 +000014#include "llvm/iPHINode.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "llvm/iMemory.h"
Chris Lattner72614082002-10-25 22:55:53 +000016#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000017#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000018#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/Target/TargetMachine.h"
Chris Lattner72614082002-10-25 22:55:53 +000022#include "llvm/Support/InstVisitor.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000023#include "llvm/Target/MRegisterInfo.h"
24#include <map>
Chris Lattner72614082002-10-25 22:55:53 +000025
Chris Lattner06925362002-11-17 21:56:38 +000026using namespace MOTy; // Get Use, Def, UseAndDef
27
Chris Lattner72614082002-10-25 22:55:53 +000028namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000029 struct ISel : public FunctionPass, InstVisitor<ISel> {
30 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000031 MachineFunction *F; // The function we are compiling into
32 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000033
34 unsigned CurReg;
35 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
36
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000037 ISel(TargetMachine &tm)
38 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000039
40 /// runOnFunction - Top level implementation of instruction selection for
41 /// the entire function.
42 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000043 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000044 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000045 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000046 RegMap.clear();
Chris Lattner94e8ee22002-11-21 17:26:58 +000047 CurReg = MRegisterInfo::FirstVirtualRegister;
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000048 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000049 return false; // We never modify the LLVM itself.
50 }
51
52 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000053 /// block. This simply creates a new MachineBasicBlock to emit code into
54 /// and adds it to the current MachineFunction. Subsequent visit* for
55 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000056 ///
57 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000058 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000059 // FIXME: Use the auto-insert form when it's available
60 F->getBasicBlockList().push_back(BB);
61 }
62
63 // Visitation methods for various instructions. These methods simply emit
64 // fixed X86 code for each instruction.
65 //
Brian Gaekefa8d5712002-11-22 11:07:01 +000066
67 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +000068 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +000069 void visitBranchInst(BranchInst &BI);
Brian Gaekefa8d5712002-11-22 11:07:01 +000070 void visitCallInst(CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000071
72 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +000073 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +000074 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
75 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +000076 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +000077
Chris Lattnerf01729e2002-11-02 20:54:46 +000078 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
79 void visitRem(BinaryOperator &B) { visitDivRem(B); }
80 void visitDivRem(BinaryOperator &B);
81
Chris Lattnere2954c82002-11-02 20:04:26 +000082 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +000083 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
84 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
85 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +000086
87 // Binary comparison operators
Chris Lattner05093a52002-11-21 15:52:38 +000088 void visitSetCCInst(SetCondInst &I, unsigned OpNum);
89 void visitSetEQ(SetCondInst &I) { visitSetCCInst(I, 0); }
90 void visitSetNE(SetCondInst &I) { visitSetCCInst(I, 1); }
91 void visitSetLT(SetCondInst &I) { visitSetCCInst(I, 2); }
92 void visitSetGT(SetCondInst &I) { visitSetCCInst(I, 3); }
93 void visitSetLE(SetCondInst &I) { visitSetCCInst(I, 4); }
94 void visitSetGE(SetCondInst &I) { visitSetCCInst(I, 5); }
Chris Lattner6fc3c522002-11-17 21:11:55 +000095
96 // Memory Instructions
97 void visitLoadInst(LoadInst &I);
98 void visitStoreInst(StoreInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +000099
100 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000101 void visitShiftInst(ShiftInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000102 void visitPHINode(PHINode &I);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000103 void visitCastInst(CastInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000104
105 void visitInstruction(Instruction &I) {
106 std::cerr << "Cannot instruction select: " << I;
107 abort();
108 }
109
Brian Gaekec2505982002-11-30 11:57:28 +0000110 void promote32 (const unsigned targetReg, Value *v);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000111
112 /// copyConstantToRegister - Output the instructions required to put the
113 /// specified constant into the specified register.
114 ///
115 void copyConstantToRegister(Constant *C, unsigned Reg);
116
Chris Lattner72614082002-10-25 22:55:53 +0000117 /// getReg - This method turns an LLVM value into a register number. This
118 /// is guaranteed to produce the same register number for a particular value
119 /// every time it is queried.
120 ///
121 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
122 unsigned getReg(Value *V) {
123 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000124 if (Reg == 0) {
Chris Lattner72614082002-10-25 22:55:53 +0000125 Reg = CurReg++;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000126 RegMap[V] = Reg;
127
128 // Add the mapping of regnumber => reg class to MachineFunction
129 F->addRegMap(Reg,
130 TM.getRegisterInfo()->getRegClassForType(V->getType()));
131 }
Chris Lattner72614082002-10-25 22:55:53 +0000132
Chris Lattner6f8fd252002-10-27 21:23:43 +0000133 // If this operand is a constant, emit the code to copy the constant into
134 // the register here...
135 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000136 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerc5291f52002-10-27 21:16:59 +0000137 copyConstantToRegister(C, Reg);
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000138 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
139 // Move the address of the global into the register
140 BuildMI(BB, X86::MOVir32, 1, Reg).addReg(GV);
Chris Lattnerd6c4cfa2002-12-04 17:15:34 +0000141 } else if (Argument *A = dyn_cast<Argument>(V)) {
142 std::cerr << "ERROR: Arguments not implemented in SimpleInstSel\n";
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000143 } else {
144 assert(0 && "Don't know how to handle a value of this type!");
145 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000146
Chris Lattner72614082002-10-25 22:55:53 +0000147 return Reg;
148 }
Chris Lattner72614082002-10-25 22:55:53 +0000149 };
150}
151
Chris Lattner43189d12002-11-17 20:07:45 +0000152/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
153/// Representation.
154///
155enum TypeClass {
156 cByte, cShort, cInt, cLong, cFloat, cDouble
157};
158
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000159/// getClass - Turn a primitive type into a "class" number which is based on the
160/// size of the type, and whether or not it is floating point.
161///
Chris Lattner43189d12002-11-17 20:07:45 +0000162static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000163 switch (Ty->getPrimitiveID()) {
164 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000165 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000166 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000167 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000168 case Type::IntTyID:
169 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000170 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000171
172 case Type::LongTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000173 case Type::ULongTyID: return cLong; // Longs are class #3
174 case Type::FloatTyID: return cFloat; // Float is class #4
175 case Type::DoubleTyID: return cDouble; // Doubles are class #5
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000176 default:
177 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000178 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000179 }
180}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000181
Chris Lattner06925362002-11-17 21:56:38 +0000182
Chris Lattnerc5291f52002-10-27 21:16:59 +0000183/// copyConstantToRegister - Output the instructions required to put the
184/// specified constant into the specified register.
185///
186void ISel::copyConstantToRegister(Constant *C, unsigned R) {
187 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
188
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000189 if (C->getType()->isIntegral()) {
190 unsigned Class = getClass(C->getType());
191 assert(Class != 3 && "Type not handled yet!");
192
193 static const unsigned IntegralOpcodeTab[] = {
194 X86::MOVir8, X86::MOVir16, X86::MOVir32
195 };
196
197 if (C->getType()->isSigned()) {
198 ConstantSInt *CSI = cast<ConstantSInt>(C);
199 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
200 } else {
201 ConstantUInt *CUI = cast<ConstantUInt>(C);
202 BuildMI(BB, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
203 }
204 } else {
205 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000206 }
207}
208
Chris Lattner06925362002-11-17 21:56:38 +0000209
Brian Gaeke1749d632002-11-07 17:59:21 +0000210/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
211/// register, then move it to wherever the result should be.
212/// We handle FP setcc instructions by pushing them, doing a
213/// compare-and-pop-twice, and then copying the concodes to the main
214/// processor's concodes (I didn't make this up, it's in the Intel manual)
215///
Chris Lattner05093a52002-11-21 15:52:38 +0000216void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000217 // The arguments are already supposed to be of the same type.
Chris Lattner05093a52002-11-21 15:52:38 +0000218 const Type *CompTy = I.getOperand(0)->getType();
219 unsigned reg1 = getReg(I.getOperand(0));
220 unsigned reg2 = getReg(I.getOperand(1));
221
222 unsigned Class = getClass(CompTy);
223 switch (Class) {
224 // Emit: cmp <var1>, <var2> (do the comparison). We can
225 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
226 // 32-bit.
227 case cByte:
228 BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2);
229 break;
230 case cShort:
231 BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2);
232 break;
233 case cInt:
234 BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2);
235 break;
236
237 // Push the variables on the stack with fldl opcodes.
238 // FIXME: assuming var1, var2 are in memory, if not, spill to
239 // stack first
240 case cFloat: // Floats
Chris Lattner3a9a6932002-11-21 22:49:20 +0000241 BuildMI (BB, X86::FLDr4, 1).addReg (reg1);
242 BuildMI (BB, X86::FLDr4, 1).addReg (reg2);
Chris Lattner05093a52002-11-21 15:52:38 +0000243 break;
244 case cDouble: // Doubles
Chris Lattner3a9a6932002-11-21 22:49:20 +0000245 BuildMI (BB, X86::FLDr8, 1).addReg (reg1);
246 BuildMI (BB, X86::FLDr8, 1).addReg (reg2);
Chris Lattner05093a52002-11-21 15:52:38 +0000247 break;
248 case cLong:
249 default:
250 visitInstruction(I);
251 }
252
253 if (CompTy->isFloatingPoint()) {
254 // (Non-trapping) compare and pop twice.
255 BuildMI (BB, X86::FUCOMPP, 0);
256 // Move fp status word (concodes) to ax.
257 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
258 // Load real concodes from ax.
259 BuildMI (BB, X86::SAHF, 1).addReg(X86::AH);
260 }
261
Brian Gaeke1749d632002-11-07 17:59:21 +0000262 // Emit setOp instruction (extract concode; clobbers ax),
263 // using the following mapping:
264 // LLVM -> X86 signed X86 unsigned
265 // ----- ----- -----
266 // seteq -> sete sete
267 // setne -> setne setne
268 // setlt -> setl setb
269 // setgt -> setg seta
270 // setle -> setle setbe
271 // setge -> setge setae
Chris Lattner05093a52002-11-21 15:52:38 +0000272
273 static const unsigned OpcodeTab[2][6] = {
Chris Lattner4b4e9dd2002-11-21 16:19:42 +0000274 {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAr, X86::SETBEr, X86::SETAEr},
275 {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGr, X86::SETLEr, X86::SETGEr},
Chris Lattner05093a52002-11-21 15:52:38 +0000276 };
277
278 BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, X86::AL);
279
Brian Gaeke1749d632002-11-07 17:59:21 +0000280 // Put it in the result using a move.
Chris Lattner05093a52002-11-21 15:52:38 +0000281 BuildMI (BB, X86::MOVrr8, 1, getReg(I)).addReg(X86::AL);
Brian Gaeke1749d632002-11-07 17:59:21 +0000282}
Chris Lattner51b49a92002-11-02 19:45:49 +0000283
Brian Gaekec2505982002-11-30 11:57:28 +0000284/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
285/// operand, in the specified target register.
286void
287ISel::promote32 (const unsigned targetReg, Value *v)
288{
289 unsigned vReg = getReg (v);
290 unsigned Class = getClass (v->getType ());
291 bool isUnsigned = v->getType ()->isUnsigned ();
292 assert (((Class == cByte) || (Class == cShort) || (Class == cInt))
293 && "Unpromotable operand class in promote32");
294 switch (Class)
295 {
296 case cByte:
297 // Extend value into target register (8->32)
298 if (isUnsigned)
299 BuildMI (BB, X86::MOVZXr32r8, 1, targetReg).addReg (vReg);
300 else
301 BuildMI (BB, X86::MOVSXr32r8, 1, targetReg).addReg (vReg);
302 break;
303 case cShort:
304 // Extend value into target register (16->32)
305 if (isUnsigned)
306 BuildMI (BB, X86::MOVZXr32r16, 1, targetReg).addReg (vReg);
307 else
308 BuildMI (BB, X86::MOVSXr32r16, 1, targetReg).addReg (vReg);
309 break;
310 case cInt:
311 // Move value into target register (32->32)
312 BuildMI (BB, X86::MOVrr32, 1, targetReg).addReg (vReg);
313 break;
314 }
315}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000316
Chris Lattner72614082002-10-25 22:55:53 +0000317/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
318/// we have the following possibilities:
319///
320/// ret void: No return value, simply emit a 'ret' instruction
321/// ret sbyte, ubyte : Extend value into EAX and return
322/// ret short, ushort: Extend value into EAX and return
323/// ret int, uint : Move value into EAX and return
324/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000325/// ret long, ulong : Move value into EAX/EDX and return
326/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000327///
Brian Gaekec2505982002-11-30 11:57:28 +0000328void
329ISel::visitReturnInst (ReturnInst &I)
330{
331 if (I.getNumOperands () == 0)
332 {
333 // Emit a 'ret' instruction
334 BuildMI (BB, X86::RET, 0);
335 return;
336 }
337 Value *rv = I.getOperand (0);
338 unsigned Class = getClass (rv->getType ());
339 switch (Class)
340 {
341 // integral return values: extend or move into EAX and return.
342 case cByte:
343 case cShort:
344 case cInt:
345 promote32 (X86::EAX, rv);
346 break;
347 // ret float/double: top of FP stack
348 // FLD <val>
349 case cFloat: // Floats
350 BuildMI (BB, X86::FLDr4, 1).addReg (getReg (rv));
351 break;
352 case cDouble: // Doubles
353 BuildMI (BB, X86::FLDr8, 1).addReg (getReg (rv));
354 break;
355 case cLong:
356 // ret long: use EAX(least significant 32 bits)/EDX (most
357 // significant 32)...uh, I think so Brain, but how do i call
358 // up the two parts of the value from inside this mouse
359 // cage? *zort*
360 default:
361 visitInstruction (I);
362 }
Chris Lattner43189d12002-11-17 20:07:45 +0000363 // Emit a 'ret' instruction
Brian Gaekec2505982002-11-30 11:57:28 +0000364 BuildMI (BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000365}
366
Chris Lattner51b49a92002-11-02 19:45:49 +0000367/// visitBranchInst - Handle conditional and unconditional branches here. Note
368/// that since code layout is frozen at this point, that if we are trying to
369/// jump to a block that is the immediate successor of the current block, we can
370/// just make a fall-through. (but we don't currently).
371///
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000372void
373ISel::visitBranchInst (BranchInst & BI)
374{
375 if (BI.isConditional ())
376 {
377 BasicBlock *ifTrue = BI.getSuccessor (0);
378 BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious
Chris Lattner2df035b2002-11-02 19:27:56 +0000379
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000380 // simplest thing I can think of: compare condition with zero,
381 // followed by jump-if-equal to ifFalse, and jump-if-nonequal to
382 // ifTrue
383 unsigned int condReg = getReg (BI.getCondition ());
Chris Lattner97ad9e12002-11-21 01:59:50 +0000384 BuildMI (BB, X86::CMPri8, 2).addReg (condReg).addZImm (0);
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000385 BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0));
386 BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1));
387 }
388 else // unconditional branch
389 {
390 BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0));
391 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000392}
393
Brian Gaeke18a20212002-11-29 12:01:58 +0000394/// visitCallInst - Push args on stack and do a procedure call instruction.
395void
396ISel::visitCallInst (CallInst & CI)
397{
398 // Push the arguments on the stack in reverse order, as specified by
399 // the ABI.
Chris Lattnerd852c152002-12-03 20:30:12 +0000400 for (unsigned i = CI.getNumOperands()-1; i >= 1; --i)
Brian Gaeke18a20212002-11-29 12:01:58 +0000401 {
402 Value *v = CI.getOperand (i);
Brian Gaeke18a20212002-11-29 12:01:58 +0000403 switch (getClass (v->getType ()))
404 {
Brian Gaekec2505982002-11-30 11:57:28 +0000405 case cByte:
406 case cShort:
Brian Gaekebb25f2f2002-12-03 00:51:09 +0000407 // Promote V to 32 bits wide, and move the result into EAX,
408 // then push EAX.
Brian Gaekec2505982002-11-30 11:57:28 +0000409 promote32 (X86::EAX, v);
410 BuildMI (BB, X86::PUSHr32, 1).addReg (X86::EAX);
411 break;
Brian Gaeke18a20212002-11-29 12:01:58 +0000412 case cInt:
Chris Lattner33ced562002-12-04 06:56:56 +0000413 case cFloat: {
414 unsigned Reg = getReg(v);
415 BuildMI (BB, X86::PUSHr32, 1).addReg(Reg);
Brian Gaeke18a20212002-11-29 12:01:58 +0000416 break;
Chris Lattner33ced562002-12-04 06:56:56 +0000417 }
Brian Gaeke18a20212002-11-29 12:01:58 +0000418 default:
Brian Gaekebb25f2f2002-12-03 00:51:09 +0000419 // FIXME: long/ulong/double args not handled.
Brian Gaeke18a20212002-11-29 12:01:58 +0000420 visitInstruction (CI);
421 break;
422 }
423 }
424 // Emit a CALL instruction with PC-relative displacement.
425 BuildMI (BB, X86::CALLpcrel32, 1).addPCDisp (CI.getCalledValue ());
Brian Gaekefa8d5712002-11-22 11:07:01 +0000426}
Chris Lattner2df035b2002-11-02 19:27:56 +0000427
Chris Lattner68aad932002-11-02 20:13:22 +0000428/// visitSimpleBinary - Implement simple binary operators for integral types...
429/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
430/// 4 for Xor.
431///
432void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
433 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000434 visitInstruction(B);
435
436 unsigned Class = getClass(B.getType());
437 if (Class > 2) // FIXME: Handle longs
438 visitInstruction(B);
439
440 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000441 // Arithmetic operators
442 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
443 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
444
445 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000446 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
447 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
448 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
449 };
450
451 unsigned Opcode = OpcodeTab[OperatorClass][Class];
452 unsigned Op0r = getReg(B.getOperand(0));
453 unsigned Op1r = getReg(B.getOperand(1));
454 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
455}
456
Chris Lattnerca9671d2002-11-02 20:28:58 +0000457/// visitMul - Multiplies are not simple binary operators because they must deal
458/// with the EAX register explicitly.
459///
460void ISel::visitMul(BinaryOperator &I) {
461 unsigned Class = getClass(I.getType());
462 if (Class > 2) // FIXME: Handle longs
463 visitInstruction(I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000464
Chris Lattnerca9671d2002-11-02 20:28:58 +0000465 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
466 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
467 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
468
Chris Lattner06925362002-11-17 21:56:38 +0000469 unsigned Reg = Regs[Class];
Chris Lattner06925362002-11-17 21:56:38 +0000470 unsigned Op0Reg = getReg(I.getOperand(0));
471 unsigned Op1Reg = getReg(I.getOperand(1));
Chris Lattnerca9671d2002-11-02 20:28:58 +0000472
473 // Put the first operand into one of the A registers...
474 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
475
Chris Lattner06925362002-11-17 21:56:38 +0000476 // Emit the appropriate multiply instruction...
Chris Lattner92845e32002-11-21 18:54:29 +0000477 BuildMI(BB, MulOpcode[Class], 1).addReg(Op1Reg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000478
479 // Put the result into the destination register...
480 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000481}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000482
Chris Lattner06925362002-11-17 21:56:38 +0000483
Chris Lattnerf01729e2002-11-02 20:54:46 +0000484/// visitDivRem - Handle division and remainder instructions... these
485/// instruction both require the same instructions to be generated, they just
486/// select the result from a different register. Note that both of these
487/// instructions work differently for signed and unsigned operands.
488///
489void ISel::visitDivRem(BinaryOperator &I) {
490 unsigned Class = getClass(I.getType());
491 if (Class > 2) // FIXME: Handle longs
492 visitInstruction(I);
493
494 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
495 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Brian Gaeke6559bb92002-11-14 22:32:30 +0000496 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +0000497 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
498 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
499
500 static const unsigned DivOpcode[][4] = {
501 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
502 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
503 };
504
505 bool isSigned = I.getType()->isSigned();
506 unsigned Reg = Regs[Class];
507 unsigned ExtReg = ExtRegs[Class];
Chris Lattner6fc3c522002-11-17 21:11:55 +0000508 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +0000509 unsigned Op1Reg = getReg(I.getOperand(1));
510
511 // Put the first operand into one of the A registers...
512 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
513
514 if (isSigned) {
515 // Emit a sign extension instruction...
Chris Lattnera4978cc2002-12-01 23:24:58 +0000516 BuildMI(BB, ExtOpcode[Class], 0);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000517 } else {
518 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
519 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
520 }
521
Chris Lattner06925362002-11-17 21:56:38 +0000522 // Emit the appropriate divide or remainder instruction...
Chris Lattner92845e32002-11-21 18:54:29 +0000523 BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +0000524
Chris Lattnerf01729e2002-11-02 20:54:46 +0000525 // Figure out which register we want to pick the result out of...
526 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
527
Chris Lattnerf01729e2002-11-02 20:54:46 +0000528 // Put the result into the destination register...
529 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000530}
Chris Lattnere2954c82002-11-02 20:04:26 +0000531
Chris Lattner06925362002-11-17 21:56:38 +0000532
Brian Gaekea1719c92002-10-31 23:03:59 +0000533/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
534/// for constant immediate shift values, and for constant immediate
535/// shift values equal to 1. Even the general case is sort of special,
536/// because the shift amount has to be in CL, not just any old register.
537///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000538void ISel::visitShiftInst (ShiftInst &I) {
539 unsigned Op0r = getReg (I.getOperand(0));
540 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000541 bool isLeftShift = I.getOpcode() == Instruction::Shl;
542 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000543 unsigned OperandClass = getClass(I.getType());
544
545 if (OperandClass > 2)
546 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000547
Brian Gaekea1719c92002-10-31 23:03:59 +0000548 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
549 {
Chris Lattner796df732002-11-02 00:44:25 +0000550 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
551 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
552 unsigned char shAmt = CUI->getValue();
553
Chris Lattnere9913f22002-11-02 01:41:55 +0000554 static const unsigned ConstantOperand[][4] = {
555 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
556 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
557 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
558 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000559 };
560
Chris Lattnere9913f22002-11-02 01:41:55 +0000561 const unsigned *OpTab = // Figure out the operand table to use
562 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000563
Brian Gaekea1719c92002-10-31 23:03:59 +0000564 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000565 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000566 }
567 else
568 {
569 // The shift amount is non-constant.
570 //
571 // In fact, you can only shift with a variable shift amount if
572 // that amount is already in the CL register, so we have to put it
573 // there first.
574 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000575
Brian Gaekea1719c92002-10-31 23:03:59 +0000576 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000577 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000578
579 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000580 static const unsigned NonConstantOperand[][4] = {
581 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
582 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
583 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
584 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000585 };
586
Chris Lattnere9913f22002-11-02 01:41:55 +0000587 const unsigned *OpTab = // Figure out the operand table to use
588 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000589
Chris Lattner3a9a6932002-11-21 22:49:20 +0000590 BuildMI(BB, OpTab[OperandClass], 1, DestReg).addReg(Op0r);
Brian Gaekea1719c92002-10-31 23:03:59 +0000591 }
592}
593
Chris Lattner06925362002-11-17 21:56:38 +0000594
Chris Lattner6fc3c522002-11-17 21:11:55 +0000595/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
596/// instruction.
597///
598void ISel::visitLoadInst(LoadInst &I) {
599 unsigned Class = getClass(I.getType());
600 if (Class > 2) // FIXME: Handle longs and others...
601 visitInstruction(I);
602
603 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
604
605 unsigned AddressReg = getReg(I.getOperand(0));
606 addDirectMem(BuildMI(BB, Opcode[Class], 4, getReg(I)), AddressReg);
607}
608
Chris Lattner06925362002-11-17 21:56:38 +0000609
Chris Lattner6fc3c522002-11-17 21:11:55 +0000610/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
611/// instruction.
612///
613void ISel::visitStoreInst(StoreInst &I) {
614 unsigned Class = getClass(I.getOperand(0)->getType());
615 if (Class > 2) // FIXME: Handle longs and others...
616 visitInstruction(I);
617
618 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
619
620 unsigned ValReg = getReg(I.getOperand(0));
621 unsigned AddressReg = getReg(I.getOperand(1));
622 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
623}
624
625
Chris Lattnere2954c82002-11-02 20:04:26 +0000626/// visitPHINode - Turn an LLVM PHI node into an X86 PHI node...
627///
628void ISel::visitPHINode(PHINode &PN) {
629 MachineInstr *MI = BuildMI(BB, X86::PHI, PN.getNumOperands(), getReg(PN));
Chris Lattner72614082002-10-25 22:55:53 +0000630
Chris Lattnere2954c82002-11-02 20:04:26 +0000631 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
632 // FIXME: This will put constants after the PHI nodes in the block, which
633 // is invalid. They should be put inline into the PHI node eventually.
634 //
635 MI->addRegOperand(getReg(PN.getIncomingValue(i)));
636 MI->addPCDispOperand(PN.getIncomingBlock(i));
637 }
Chris Lattner72614082002-10-25 22:55:53 +0000638}
639
Brian Gaekec11232a2002-11-26 10:43:30 +0000640/// visitCastInst - Here we have various kinds of copying with or without
641/// sign extension going on.
Brian Gaekefa8d5712002-11-22 11:07:01 +0000642void
643ISel::visitCastInst (CastInst &CI)
644{
Brian Gaekec11232a2002-11-26 10:43:30 +0000645//> cast larger int to smaller int --> copy least significant byte/word w/ mov?
646//
647//I'm not really sure what to do with this. We could insert a pseudo-op
648//that says take the low X bits of a Y bit register, but for now we can just
649//force the value into, say, EAX, then rip out AL or AX. The advantage of
650//the former is that the register allocator could use any register it wants,
651//but for now this obviously doesn't matter. :)
652
Chris Lattnerf18a36e2002-12-03 18:15:59 +0000653 const Type *targetType = CI.getType ();
Brian Gaeke07f02612002-12-03 07:36:03 +0000654 Value *operand = CI.getOperand (0);
655 unsigned int operandReg = getReg (operand);
Chris Lattnerf18a36e2002-12-03 18:15:59 +0000656 const Type *sourceType = operand->getType ();
Brian Gaeke07f02612002-12-03 07:36:03 +0000657 unsigned int destReg = getReg (CI);
658
659 // cast to bool:
660 if (targetType == Type::BoolTy) {
661 // Emit Compare
662 BuildMI (BB, X86::CMPri8, 2).addReg (operandReg).addZImm (0);
663 // Emit Set-if-not-zero
664 BuildMI (BB, X86::SETNEr, 1, destReg);
665 return;
666 }
Brian Gaekec11232a2002-11-26 10:43:30 +0000667
668// if size of target type == size of source type
669// Emit Mov reg(target) <- reg(source)
670
671// if size of target type > size of source type
672// if both types are integer types
673// if source type is signed
674// sbyte to short, ushort: Emit movsx 8->16
675// sbyte to int, uint: Emit movsx 8->32
676// short to int, uint: Emit movsx 16->32
677// else if source type is unsigned
678// ubyte to short, ushort: Emit movzx 8->16
679// ubyte to int, uint: Emit movzx 8->32
680// ushort to int, uint: Emit movzx 16->32
681// if both types are fp types
682// float to double: Emit fstp, fld (???)
683
Brian Gaekefa8d5712002-11-22 11:07:01 +0000684 visitInstruction (CI);
685}
Brian Gaekea1719c92002-10-31 23:03:59 +0000686
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000687/// createSimpleX86InstructionSelector - This pass converts an LLVM function
688/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000689/// generated code sucks but the implementation is nice and simple.
690///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000691Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
692 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000693}