blob: 45954a1c9a40343f04ccde9d2c4ea835f9d933f7 [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"
Brian Gaeke20244b72002-12-12 15:33:40 +000017#include "llvm/DerivedTypes.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000018#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000019#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000020#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/Target/TargetMachine.h"
Chris Lattner72614082002-10-25 22:55:53 +000023#include "llvm/Support/InstVisitor.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000024#include "llvm/Target/MRegisterInfo.h"
25#include <map>
Chris Lattner72614082002-10-25 22:55:53 +000026
Chris Lattner06925362002-11-17 21:56:38 +000027using namespace MOTy; // Get Use, Def, UseAndDef
28
Chris Lattner333b2fa2002-12-13 10:09:43 +000029
30/// BMI - A special BuildMI variant that takes an iterator to insert the
31/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000032/// this is the version for when you have a destination register in mind.
33inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Chris Lattner333b2fa2002-12-13 10:09:43 +000034 MachineBasicBlock::iterator &I,
35 MachineOpCode Opcode,
36 unsigned NumOperands,
37 unsigned DestReg) {
Chris Lattnerd7d38722002-12-13 13:04:04 +000038 assert(I >= MBB->begin() && I <= MBB->end() && "Bad iterator!");
Chris Lattner333b2fa2002-12-13 10:09:43 +000039 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
Chris Lattnere8f0d922002-12-24 00:03:11 +000040 I = MBB->insert(I, MI)+1;
Chris Lattner333b2fa2002-12-13 10:09:43 +000041 return MachineInstrBuilder(MI).addReg(DestReg, MOTy::Def);
42}
43
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000044/// BMI - A special BuildMI variant that takes an iterator to insert the
45/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000046inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000047 MachineBasicBlock::iterator &I,
48 MachineOpCode Opcode,
49 unsigned NumOperands) {
Chris Lattnerd7d38722002-12-13 13:04:04 +000050 assert(I > MBB->begin() && I <= MBB->end() && "Bad iterator!");
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000051 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
Chris Lattnere8f0d922002-12-24 00:03:11 +000052 I = MBB->insert(I, MI)+1;
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000053 return MachineInstrBuilder(MI);
54}
55
Chris Lattner333b2fa2002-12-13 10:09:43 +000056
Chris Lattner72614082002-10-25 22:55:53 +000057namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000058 struct ISel : public FunctionPass, InstVisitor<ISel> {
59 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000060 MachineFunction *F; // The function we are compiling into
61 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000062
63 unsigned CurReg;
64 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
65
Chris Lattner333b2fa2002-12-13 10:09:43 +000066 // MBBMap - Mapping between LLVM BB -> Machine BB
67 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
68
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000069 ISel(TargetMachine &tm)
70 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000071
72 /// runOnFunction - Top level implementation of instruction selection for
73 /// the entire function.
74 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000075 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000076 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +000077
78 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
79 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
80
Chris Lattner14aa7fe2002-12-16 22:54:46 +000081 // Emit instructions to load the arguments... The function's arguments
82 // look like this:
83 //
84 // [EBP] -- copy of old EBP
85 // [EBP + 4] -- return address
86 // [EBP + 8] -- first argument (leftmost lexically)
87 //
88 // So we want to start with counter = 2.
89 //
90 BB = &F->front();
91 unsigned ArgOffset = 8;
92 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E;
93 ++I, ArgOffset += 4) {
94 unsigned Reg = getReg(*I);
95
96 // Load it out of the stack frame at EBP + 4*argPos.
97
98 // FIXME: This should load the argument of the appropriate size!!
99 addRegOffset(BuildMI(BB, X86::MOVmr32, 4, Reg), X86::EBP, ArgOffset);
100 }
101
Chris Lattner333b2fa2002-12-13 10:09:43 +0000102 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000103 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104
105 // Select the PHI nodes
106 SelectPHINodes();
107
Chris Lattner72614082002-10-25 22:55:53 +0000108 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000109 MBBMap.clear();
Chris Lattner94e8ee22002-11-21 17:26:58 +0000110 CurReg = MRegisterInfo::FirstVirtualRegister;
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000111 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +0000112 return false; // We never modify the LLVM itself.
113 }
114
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000115 virtual const char *getPassName() const {
116 return "X86 Simple Instruction Selection";
117 }
118
Chris Lattner72614082002-10-25 22:55:53 +0000119 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000120 /// block. This simply creates a new MachineBasicBlock to emit code into
121 /// and adds it to the current MachineFunction. Subsequent visit* for
122 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000123 ///
124 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000125 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000126 }
127
Chris Lattner333b2fa2002-12-13 10:09:43 +0000128
129 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
130 /// because we have to generate our sources into the source basic blocks,
131 /// not the current one.
132 ///
133 void SelectPHINodes();
134
Chris Lattner72614082002-10-25 22:55:53 +0000135 // Visitation methods for various instructions. These methods simply emit
136 // fixed X86 code for each instruction.
137 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000138
139 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000140 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000141 void visitBranchInst(BranchInst &BI);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000142 void visitCallInst(CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000143
144 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000145 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000146 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
147 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattner8a307e82002-12-16 19:32:50 +0000148 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
149 unsigned destReg, const Type *resultType,
150 unsigned op0Reg, unsigned op1Reg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000151 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000152
Chris Lattnerf01729e2002-11-02 20:54:46 +0000153 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
154 void visitRem(BinaryOperator &B) { visitDivRem(B); }
155 void visitDivRem(BinaryOperator &B);
156
Chris Lattnere2954c82002-11-02 20:04:26 +0000157 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000158 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
159 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
160 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000161
162 // Binary comparison operators
Chris Lattner05093a52002-11-21 15:52:38 +0000163 void visitSetCCInst(SetCondInst &I, unsigned OpNum);
164 void visitSetEQ(SetCondInst &I) { visitSetCCInst(I, 0); }
165 void visitSetNE(SetCondInst &I) { visitSetCCInst(I, 1); }
166 void visitSetLT(SetCondInst &I) { visitSetCCInst(I, 2); }
167 void visitSetGT(SetCondInst &I) { visitSetCCInst(I, 3); }
168 void visitSetLE(SetCondInst &I) { visitSetCCInst(I, 4); }
169 void visitSetGE(SetCondInst &I) { visitSetCCInst(I, 5); }
Chris Lattner6fc3c522002-11-17 21:11:55 +0000170
171 // Memory Instructions
172 void visitLoadInst(LoadInst &I);
173 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000174 void visitGetElementPtrInst(GetElementPtrInst &I);
175 void visitMallocInst(MallocInst &I);
Brian Gaekee48ec012002-12-13 06:46:31 +0000176 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000177 void visitAllocaInst(AllocaInst &I);
178
Chris Lattnere2954c82002-11-02 20:04:26 +0000179 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000180 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000181 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000182 void visitCastInst(CastInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000183
184 void visitInstruction(Instruction &I) {
185 std::cerr << "Cannot instruction select: " << I;
186 abort();
187 }
188
Brian Gaeke95780cc2002-12-13 07:56:18 +0000189 /// promote32 - Make a value 32-bits wide, and put it somewhere.
190 void promote32 (const unsigned targetReg, Value *v);
191
192 // emitGEPOperation - Common code shared between visitGetElementPtrInst and
Chris Lattnerc0812d82002-12-13 06:56:29 +0000193 // constant expression GEP support.
194 //
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000195 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator&IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000196 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000197 User::op_iterator IdxEnd, unsigned TargetReg);
198
Chris Lattnerc5291f52002-10-27 21:16:59 +0000199 /// copyConstantToRegister - Output the instructions required to put the
200 /// specified constant into the specified register.
201 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000202 void copyConstantToRegister(MachineBasicBlock *MBB,
203 MachineBasicBlock::iterator &MBBI,
204 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000205
Brian Gaeke20244b72002-12-12 15:33:40 +0000206 /// makeAnotherReg - This method returns the next register number
207 /// we haven't yet used.
Chris Lattnerc0812d82002-12-13 06:56:29 +0000208 unsigned makeAnotherReg(const Type *Ty) {
209 // Add the mapping of regnumber => reg class to MachineFunction
210 F->addRegMap(CurReg, TM.getRegisterInfo()->getRegClassForType(Ty));
211 return CurReg++;
Brian Gaeke20244b72002-12-12 15:33:40 +0000212 }
213
Chris Lattner72614082002-10-25 22:55:53 +0000214 /// getReg - This method turns an LLVM value into a register number. This
215 /// is guaranteed to produce the same register number for a particular value
216 /// every time it is queried.
217 ///
218 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000219 unsigned getReg(Value *V) {
220 // Just append to the end of the current bb.
221 MachineBasicBlock::iterator It = BB->end();
222 return getReg(V, BB, It);
223 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000224 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000225 MachineBasicBlock::iterator &IPt) {
Chris Lattner72614082002-10-25 22:55:53 +0000226 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000227 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000228 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000229 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000230 }
Chris Lattner72614082002-10-25 22:55:53 +0000231
Chris Lattner6f8fd252002-10-27 21:23:43 +0000232 // If this operand is a constant, emit the code to copy the constant into
233 // the register here...
234 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000235 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner8a307e82002-12-16 19:32:50 +0000236 copyConstantToRegister(MBB, IPt, C, Reg);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000237 RegMap.erase(V); // Assign a new name to this constant if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000238 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
239 // Move the address of the global into the register
Brian Gaeke71794c02002-12-13 11:22:48 +0000240 BMI(MBB, IPt, X86::MOVir32, 1, Reg).addReg(GV);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000241 RegMap.erase(V); // Assign a new name to this address if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000242 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000243
Chris Lattner72614082002-10-25 22:55:53 +0000244 return Reg;
245 }
Chris Lattner72614082002-10-25 22:55:53 +0000246 };
247}
248
Chris Lattner43189d12002-11-17 20:07:45 +0000249/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
250/// Representation.
251///
252enum TypeClass {
253 cByte, cShort, cInt, cLong, cFloat, cDouble
254};
255
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000256/// getClass - Turn a primitive type into a "class" number which is based on the
257/// size of the type, and whether or not it is floating point.
258///
Chris Lattner43189d12002-11-17 20:07:45 +0000259static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000260 switch (Ty->getPrimitiveID()) {
261 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000262 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000263 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000264 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000265 case Type::IntTyID:
266 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000267 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000268
269 case Type::LongTyID:
Chris Lattnerc0812d82002-12-13 06:56:29 +0000270 case Type::ULongTyID: //return cLong; // Longs are class #3
271 return cInt; // FIXME: LONGS ARE TREATED AS INTS!
272
Chris Lattner43189d12002-11-17 20:07:45 +0000273 case Type::FloatTyID: return cFloat; // Float is class #4
274 case Type::DoubleTyID: return cDouble; // Doubles are class #5
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000275 default:
276 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000277 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000278 }
279}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000280
Chris Lattner6b993cc2002-12-15 08:02:15 +0000281// getClassB - Just like getClass, but treat boolean values as bytes.
282static inline TypeClass getClassB(const Type *Ty) {
283 if (Ty == Type::BoolTy) return cByte;
284 return getClass(Ty);
285}
286
Chris Lattner06925362002-11-17 21:56:38 +0000287
Chris Lattnerc5291f52002-10-27 21:16:59 +0000288/// copyConstantToRegister - Output the instructions required to put the
289/// specified constant into the specified register.
290///
Chris Lattner8a307e82002-12-16 19:32:50 +0000291void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
292 MachineBasicBlock::iterator &IP,
293 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000294 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
295 if (CE->getOpcode() == Instruction::GetElementPtr) {
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000296 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000297 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000298 return;
299 }
300
Brian Gaeke20244b72002-12-12 15:33:40 +0000301 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerc0812d82002-12-13 06:56:29 +0000302 assert (0 && "Constant expressions not yet handled!\n");
Brian Gaeke20244b72002-12-12 15:33:40 +0000303 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000304
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000305 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000306 unsigned Class = getClassB(C->getType());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000307 assert(Class != 3 && "Type not handled yet!");
308
309 static const unsigned IntegralOpcodeTab[] = {
310 X86::MOVir8, X86::MOVir16, X86::MOVir32
311 };
312
Chris Lattner6b993cc2002-12-15 08:02:15 +0000313 if (C->getType() == Type::BoolTy) {
314 BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True);
315 } else if (C->getType()->isSigned()) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000316 ConstantSInt *CSI = cast<ConstantSInt>(C);
Brian Gaeke71794c02002-12-13 11:22:48 +0000317 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addSImm(CSI->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000318 } else {
319 ConstantUInt *CUI = cast<ConstantUInt>(C);
Brian Gaeke71794c02002-12-13 11:22:48 +0000320 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CUI->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000321 }
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000322 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000323 // Copy zero (null pointer) to the register.
Brian Gaeke71794c02002-12-13 11:22:48 +0000324 BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000325 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000326 unsigned SrcReg = getReg(CPR->getValue(), MBB, IP);
Brian Gaeke71794c02002-12-13 11:22:48 +0000327 BMI(MBB, IP, X86::MOVrr32, 1, R).addReg(SrcReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000328 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000329 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000330 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000331 }
332}
333
Chris Lattner333b2fa2002-12-13 10:09:43 +0000334/// SelectPHINodes - Insert machine code to generate phis. This is tricky
335/// because we have to generate our sources into the source basic blocks, not
336/// the current one.
337///
338void ISel::SelectPHINodes() {
339 const Function &LF = *F->getFunction(); // The LLVM function...
340 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
341 const BasicBlock *BB = I;
342 MachineBasicBlock *MBB = MBBMap[I];
343
344 // Loop over all of the PHI nodes in the LLVM basic block...
345 unsigned NumPHIs = 0;
346 for (BasicBlock::const_iterator I = BB->begin();
347 PHINode *PN = (PHINode*)dyn_cast<PHINode>(&*I); ++I) {
348 // Create a new machine instr PHI node, and insert it.
349 MachineInstr *MI = BuildMI(X86::PHI, PN->getNumOperands(), getReg(*PN));
350 MBB->insert(MBB->begin()+NumPHIs++, MI); // Insert it at the top of the BB
351
352 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
353 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
354
355 // Get the incoming value into a virtual register. If it is not already
356 // available in a virtual register, insert the computation code into
357 // PredMBB
Chris Lattner92053632002-12-13 11:52:34 +0000358 //
359
360 MachineBasicBlock::iterator PI = PredMBB->begin();
361 while ((*PI)->getOpcode() == X86::PHI) ++PI;
362
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000363 MI->addRegOperand(getReg(PN->getIncomingValue(i), PredMBB, PI));
Chris Lattner6b993cc2002-12-15 08:02:15 +0000364 MI->addMachineBasicBlockOperand(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000365 }
366 }
367 }
368}
369
370
Chris Lattner06925362002-11-17 21:56:38 +0000371
Brian Gaeke1749d632002-11-07 17:59:21 +0000372/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
373/// register, then move it to wherever the result should be.
374/// We handle FP setcc instructions by pushing them, doing a
375/// compare-and-pop-twice, and then copying the concodes to the main
376/// processor's concodes (I didn't make this up, it's in the Intel manual)
377///
Chris Lattner05093a52002-11-21 15:52:38 +0000378void ISel::visitSetCCInst(SetCondInst &I, unsigned OpNum) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000379 // The arguments are already supposed to be of the same type.
Chris Lattner05093a52002-11-21 15:52:38 +0000380 const Type *CompTy = I.getOperand(0)->getType();
381 unsigned reg1 = getReg(I.getOperand(0));
382 unsigned reg2 = getReg(I.getOperand(1));
383
384 unsigned Class = getClass(CompTy);
385 switch (Class) {
386 // Emit: cmp <var1>, <var2> (do the comparison). We can
387 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
388 // 32-bit.
389 case cByte:
390 BuildMI (BB, X86::CMPrr8, 2).addReg (reg1).addReg (reg2);
391 break;
392 case cShort:
393 BuildMI (BB, X86::CMPrr16, 2).addReg (reg1).addReg (reg2);
394 break;
395 case cInt:
396 BuildMI (BB, X86::CMPrr32, 2).addReg (reg1).addReg (reg2);
397 break;
398
399 // Push the variables on the stack with fldl opcodes.
400 // FIXME: assuming var1, var2 are in memory, if not, spill to
401 // stack first
402 case cFloat: // Floats
Brian Gaeke20244b72002-12-12 15:33:40 +0000403 BuildMI (BB, X86::FLDr32, 1).addReg (reg1);
404 BuildMI (BB, X86::FLDr32, 1).addReg (reg2);
Chris Lattner05093a52002-11-21 15:52:38 +0000405 break;
406 case cDouble: // Doubles
Brian Gaeke20244b72002-12-12 15:33:40 +0000407 BuildMI (BB, X86::FLDr64, 1).addReg (reg1);
408 BuildMI (BB, X86::FLDr64, 1).addReg (reg2);
Chris Lattner05093a52002-11-21 15:52:38 +0000409 break;
410 case cLong:
411 default:
412 visitInstruction(I);
413 }
414
415 if (CompTy->isFloatingPoint()) {
416 // (Non-trapping) compare and pop twice.
417 BuildMI (BB, X86::FUCOMPP, 0);
418 // Move fp status word (concodes) to ax.
419 BuildMI (BB, X86::FNSTSWr8, 1, X86::AX);
420 // Load real concodes from ax.
421 BuildMI (BB, X86::SAHF, 1).addReg(X86::AH);
422 }
423
Brian Gaeke1749d632002-11-07 17:59:21 +0000424 // Emit setOp instruction (extract concode; clobbers ax),
425 // using the following mapping:
426 // LLVM -> X86 signed X86 unsigned
427 // ----- ----- -----
428 // seteq -> sete sete
429 // setne -> setne setne
430 // setlt -> setl setb
431 // setgt -> setg seta
432 // setle -> setle setbe
433 // setge -> setge setae
Chris Lattner05093a52002-11-21 15:52:38 +0000434
435 static const unsigned OpcodeTab[2][6] = {
Chris Lattner4b4e9dd2002-11-21 16:19:42 +0000436 {X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAr, X86::SETBEr, X86::SETAEr},
437 {X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGr, X86::SETLEr, X86::SETGEr},
Chris Lattner05093a52002-11-21 15:52:38 +0000438 };
439
Chris Lattner89fab072002-12-18 01:44:31 +0000440 BuildMI(BB, OpcodeTab[CompTy->isSigned()][OpNum], 0, getReg(I));
Brian Gaeke1749d632002-11-07 17:59:21 +0000441}
Chris Lattner51b49a92002-11-02 19:45:49 +0000442
Brian Gaekec2505982002-11-30 11:57:28 +0000443/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
444/// operand, in the specified target register.
445void
Chris Lattnerc0812d82002-12-13 06:56:29 +0000446ISel::promote32 (unsigned targetReg, Value *v)
Brian Gaekec2505982002-11-30 11:57:28 +0000447{
448 unsigned vReg = getReg (v);
449 unsigned Class = getClass (v->getType ());
450 bool isUnsigned = v->getType ()->isUnsigned ();
451 assert (((Class == cByte) || (Class == cShort) || (Class == cInt))
452 && "Unpromotable operand class in promote32");
453 switch (Class)
454 {
455 case cByte:
456 // Extend value into target register (8->32)
457 if (isUnsigned)
458 BuildMI (BB, X86::MOVZXr32r8, 1, targetReg).addReg (vReg);
459 else
460 BuildMI (BB, X86::MOVSXr32r8, 1, targetReg).addReg (vReg);
461 break;
462 case cShort:
463 // Extend value into target register (16->32)
464 if (isUnsigned)
465 BuildMI (BB, X86::MOVZXr32r16, 1, targetReg).addReg (vReg);
466 else
467 BuildMI (BB, X86::MOVSXr32r16, 1, targetReg).addReg (vReg);
468 break;
469 case cInt:
470 // Move value into target register (32->32)
471 BuildMI (BB, X86::MOVrr32, 1, targetReg).addReg (vReg);
472 break;
473 }
474}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000475
Chris Lattner72614082002-10-25 22:55:53 +0000476/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
477/// we have the following possibilities:
478///
479/// ret void: No return value, simply emit a 'ret' instruction
480/// ret sbyte, ubyte : Extend value into EAX and return
481/// ret short, ushort: Extend value into EAX and return
482/// ret int, uint : Move value into EAX and return
483/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000484/// ret long, ulong : Move value into EAX/EDX and return
485/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000486///
Brian Gaekec2505982002-11-30 11:57:28 +0000487void
488ISel::visitReturnInst (ReturnInst &I)
489{
490 if (I.getNumOperands () == 0)
491 {
492 // Emit a 'ret' instruction
493 BuildMI (BB, X86::RET, 0);
494 return;
495 }
496 Value *rv = I.getOperand (0);
497 unsigned Class = getClass (rv->getType ());
498 switch (Class)
499 {
500 // integral return values: extend or move into EAX and return.
501 case cByte:
502 case cShort:
503 case cInt:
504 promote32 (X86::EAX, rv);
505 break;
506 // ret float/double: top of FP stack
507 // FLD <val>
508 case cFloat: // Floats
Brian Gaeke20244b72002-12-12 15:33:40 +0000509 BuildMI (BB, X86::FLDr32, 1).addReg (getReg (rv));
Brian Gaekec2505982002-11-30 11:57:28 +0000510 break;
511 case cDouble: // Doubles
Brian Gaeke20244b72002-12-12 15:33:40 +0000512 BuildMI (BB, X86::FLDr64, 1).addReg (getReg (rv));
Brian Gaekec2505982002-11-30 11:57:28 +0000513 break;
514 case cLong:
515 // ret long: use EAX(least significant 32 bits)/EDX (most
516 // significant 32)...uh, I think so Brain, but how do i call
517 // up the two parts of the value from inside this mouse
518 // cage? *zort*
519 default:
520 visitInstruction (I);
521 }
Chris Lattner43189d12002-11-17 20:07:45 +0000522 // Emit a 'ret' instruction
Brian Gaekec2505982002-11-30 11:57:28 +0000523 BuildMI (BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000524}
525
Chris Lattner51b49a92002-11-02 19:45:49 +0000526/// visitBranchInst - Handle conditional and unconditional branches here. Note
527/// that since code layout is frozen at this point, that if we are trying to
528/// jump to a block that is the immediate successor of the current block, we can
529/// just make a fall-through. (but we don't currently).
530///
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000531void
532ISel::visitBranchInst (BranchInst & BI)
533{
534 if (BI.isConditional ())
535 {
536 BasicBlock *ifTrue = BI.getSuccessor (0);
537 BasicBlock *ifFalse = BI.getSuccessor (1); // this is really unobvious
Chris Lattner2df035b2002-11-02 19:27:56 +0000538
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000539 // simplest thing I can think of: compare condition with zero,
540 // followed by jump-if-equal to ifFalse, and jump-if-nonequal to
541 // ifTrue
542 unsigned int condReg = getReg (BI.getCondition ());
Chris Lattner97ad9e12002-11-21 01:59:50 +0000543 BuildMI (BB, X86::CMPri8, 2).addReg (condReg).addZImm (0);
Brian Gaekec03a0cb2002-11-19 09:08:47 +0000544 BuildMI (BB, X86::JNE, 1).addPCDisp (BI.getSuccessor (0));
545 BuildMI (BB, X86::JE, 1).addPCDisp (BI.getSuccessor (1));
546 }
547 else // unconditional branch
548 {
549 BuildMI (BB, X86::JMP, 1).addPCDisp (BI.getSuccessor (0));
550 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000551}
552
Brian Gaeke18a20212002-11-29 12:01:58 +0000553/// visitCallInst - Push args on stack and do a procedure call instruction.
554void
555ISel::visitCallInst (CallInst & CI)
556{
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000557 // keep a counter of how many bytes we pushed on the stack
558 unsigned bytesPushed = 0;
559
Brian Gaeke18a20212002-11-29 12:01:58 +0000560 // Push the arguments on the stack in reverse order, as specified by
561 // the ABI.
Chris Lattnerd852c152002-12-03 20:30:12 +0000562 for (unsigned i = CI.getNumOperands()-1; i >= 1; --i)
Brian Gaeke18a20212002-11-29 12:01:58 +0000563 {
564 Value *v = CI.getOperand (i);
Brian Gaeke18a20212002-11-29 12:01:58 +0000565 switch (getClass (v->getType ()))
566 {
Brian Gaekec2505982002-11-30 11:57:28 +0000567 case cByte:
568 case cShort:
Brian Gaekebb25f2f2002-12-03 00:51:09 +0000569 // Promote V to 32 bits wide, and move the result into EAX,
570 // then push EAX.
Brian Gaekec2505982002-11-30 11:57:28 +0000571 promote32 (X86::EAX, v);
572 BuildMI (BB, X86::PUSHr32, 1).addReg (X86::EAX);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000573 bytesPushed += 4;
Brian Gaekec2505982002-11-30 11:57:28 +0000574 break;
Brian Gaeke18a20212002-11-29 12:01:58 +0000575 case cInt:
Chris Lattner33ced562002-12-04 06:56:56 +0000576 case cFloat: {
577 unsigned Reg = getReg(v);
578 BuildMI (BB, X86::PUSHr32, 1).addReg(Reg);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000579 bytesPushed += 4;
Brian Gaeke18a20212002-11-29 12:01:58 +0000580 break;
Chris Lattner33ced562002-12-04 06:56:56 +0000581 }
Brian Gaeke18a20212002-11-29 12:01:58 +0000582 default:
Brian Gaekebb25f2f2002-12-03 00:51:09 +0000583 // FIXME: long/ulong/double args not handled.
Brian Gaeke18a20212002-11-29 12:01:58 +0000584 visitInstruction (CI);
585 break;
586 }
587 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +0000588
589 if (Function *F = CI.getCalledFunction()) {
590 // Emit a CALL instruction with PC-relative displacement.
591 BuildMI(BB, X86::CALLpcrel32, 1).addPCDisp(F);
592 } else {
593 unsigned Reg = getReg(CI.getCalledValue());
594 BuildMI(BB, X86::CALLr32, 1).addReg(Reg);
595 }
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000596
597 // Adjust the stack by `bytesPushed' amount if non-zero
598 if (bytesPushed > 0)
Chris Lattner3bd94092002-12-16 23:36:57 +0000599 BuildMI (BB, X86::ADDri32,2,X86::ESP).addReg(X86::ESP).addZImm(bytesPushed);
Chris Lattnera3243642002-12-04 23:45:28 +0000600
601 // If there is a return value, scavenge the result from the location the call
602 // leaves it in...
603 //
Chris Lattner4fa1acc2002-12-04 23:50:28 +0000604 if (CI.getType() != Type::VoidTy) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000605 unsigned resultTypeClass = getClass (CI.getType ());
606 switch (resultTypeClass) {
607 case cByte:
608 case cShort:
609 case cInt: {
610 // Integral results are in %eax, or the appropriate portion
611 // thereof.
612 static const unsigned regRegMove[] = {
613 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
614 };
615 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
616 BuildMI (BB, regRegMove[resultTypeClass], 1,
617 getReg (CI)).addReg (AReg[resultTypeClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +0000618 break;
Brian Gaeke20244b72002-12-12 15:33:40 +0000619 }
620 case cFloat:
621 // Floating-point return values live in %st(0) (i.e., the top of
622 // the FP stack.) The general way to approach this is to do a
623 // FSTP to save the top of the FP stack on the real stack, then
624 // do a MOV to load the top of the real stack into the target
625 // register.
626 visitInstruction (CI); // FIXME: add the right args for the calls below
627 // BuildMI (BB, X86::FSTPm32, 0);
628 // BuildMI (BB, X86::MOVmr32, 0);
629 break;
Chris Lattner4fa1acc2002-12-04 23:50:28 +0000630 default:
631 std::cerr << "Cannot get return value for call of type '"
632 << *CI.getType() << "'\n";
633 visitInstruction(CI);
634 }
Chris Lattnera3243642002-12-04 23:45:28 +0000635 }
Brian Gaekefa8d5712002-11-22 11:07:01 +0000636}
Chris Lattner2df035b2002-11-02 19:27:56 +0000637
Chris Lattner68aad932002-11-02 20:13:22 +0000638/// visitSimpleBinary - Implement simple binary operators for integral types...
639/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or,
640/// 4 for Xor.
641///
642void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
643 if (B.getType() == Type::BoolTy) // FIXME: Handle bools for logicals
Chris Lattnere2954c82002-11-02 20:04:26 +0000644 visitInstruction(B);
645
646 unsigned Class = getClass(B.getType());
647 if (Class > 2) // FIXME: Handle longs
648 visitInstruction(B);
649
650 static const unsigned OpcodeTab[][4] = {
Chris Lattner68aad932002-11-02 20:13:22 +0000651 // Arithmetic operators
652 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, 0 }, // ADD
653 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, 0 }, // SUB
654
655 // Bitwise operators
Chris Lattnere2954c82002-11-02 20:04:26 +0000656 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
657 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
658 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
659 };
660
661 unsigned Opcode = OpcodeTab[OperatorClass][Class];
662 unsigned Op0r = getReg(B.getOperand(0));
663 unsigned Op1r = getReg(B.getOperand(1));
664 BuildMI(BB, Opcode, 2, getReg(B)).addReg(Op0r).addReg(Op1r);
665}
666
Brian Gaeke20244b72002-12-12 15:33:40 +0000667/// doMultiply - Emit appropriate instructions to multiply together
668/// the registers op0Reg and op1Reg, and put the result in destReg.
669/// The type of the result should be given as resultType.
Chris Lattner8a307e82002-12-16 19:32:50 +0000670void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
671 unsigned destReg, const Type *resultType,
672 unsigned op0Reg, unsigned op1Reg) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000673 unsigned Class = getClass (resultType);
674
675 // FIXME:
676 assert (Class <= 2 && "Someday, we will learn how to multiply"
677 "longs and floating-point numbers. This is not that day.");
678
679 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
680 static const unsigned MulOpcode[]={ X86::MULrr8, X86::MULrr16, X86::MULrr32 };
681 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
682 unsigned Reg = Regs[Class];
683
684 // Emit a MOV to put the first operand into the appropriately-sized
685 // subreg of EAX.
Brian Gaeke71794c02002-12-13 11:22:48 +0000686 BMI(MBB, MBBI, MovOpcode[Class], 1, Reg).addReg (op0Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +0000687
688 // Emit the appropriate multiply instruction.
Brian Gaeke71794c02002-12-13 11:22:48 +0000689 BMI(MBB, MBBI, MulOpcode[Class], 1).addReg (op1Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +0000690
691 // Emit another MOV to put the result into the destination register.
Brian Gaeke71794c02002-12-13 11:22:48 +0000692 BMI(MBB, MBBI, MovOpcode[Class], 1, destReg).addReg (Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +0000693}
694
Chris Lattnerca9671d2002-11-02 20:28:58 +0000695/// visitMul - Multiplies are not simple binary operators because they must deal
696/// with the EAX register explicitly.
697///
698void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +0000699 unsigned DestReg = getReg(I);
700 unsigned Op0Reg = getReg(I.getOperand(0));
701 unsigned Op1Reg = getReg(I.getOperand(1));
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000702 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner8a307e82002-12-16 19:32:50 +0000703 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000704}
Chris Lattnerca9671d2002-11-02 20:28:58 +0000705
Chris Lattner06925362002-11-17 21:56:38 +0000706
Chris Lattnerf01729e2002-11-02 20:54:46 +0000707/// visitDivRem - Handle division and remainder instructions... these
708/// instruction both require the same instructions to be generated, they just
709/// select the result from a different register. Note that both of these
710/// instructions work differently for signed and unsigned operands.
711///
712void ISel::visitDivRem(BinaryOperator &I) {
713 unsigned Class = getClass(I.getType());
714 if (Class > 2) // FIXME: Handle longs
715 visitInstruction(I);
716
717 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
718 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Brian Gaeke6559bb92002-11-14 22:32:30 +0000719 static const unsigned ExtOpcode[]={ X86::CBW , X86::CWD , X86::CDQ };
Chris Lattnerf01729e2002-11-02 20:54:46 +0000720 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
721 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
722
723 static const unsigned DivOpcode[][4] = {
724 { X86::DIVrr8 , X86::DIVrr16 , X86::DIVrr32 , 0 }, // Unsigned division
725 { X86::IDIVrr8, X86::IDIVrr16, X86::IDIVrr32, 0 }, // Signed division
726 };
727
728 bool isSigned = I.getType()->isSigned();
729 unsigned Reg = Regs[Class];
730 unsigned ExtReg = ExtRegs[Class];
Chris Lattner6fc3c522002-11-17 21:11:55 +0000731 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +0000732 unsigned Op1Reg = getReg(I.getOperand(1));
733
734 // Put the first operand into one of the A registers...
735 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
736
737 if (isSigned) {
738 // Emit a sign extension instruction...
Chris Lattnera4978cc2002-12-01 23:24:58 +0000739 BuildMI(BB, ExtOpcode[Class], 0);
Chris Lattnerf01729e2002-11-02 20:54:46 +0000740 } else {
741 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
742 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
743 }
744
Chris Lattner06925362002-11-17 21:56:38 +0000745 // Emit the appropriate divide or remainder instruction...
Chris Lattner92845e32002-11-21 18:54:29 +0000746 BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +0000747
Chris Lattnerf01729e2002-11-02 20:54:46 +0000748 // Figure out which register we want to pick the result out of...
749 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
750
Chris Lattnerf01729e2002-11-02 20:54:46 +0000751 // Put the result into the destination register...
752 BuildMI(BB, MovOpcode[Class], 1, getReg(I)).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000753}
Chris Lattnere2954c82002-11-02 20:04:26 +0000754
Chris Lattner06925362002-11-17 21:56:38 +0000755
Brian Gaekea1719c92002-10-31 23:03:59 +0000756/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
757/// for constant immediate shift values, and for constant immediate
758/// shift values equal to 1. Even the general case is sort of special,
759/// because the shift amount has to be in CL, not just any old register.
760///
Chris Lattnerf01729e2002-11-02 20:54:46 +0000761void ISel::visitShiftInst (ShiftInst &I) {
762 unsigned Op0r = getReg (I.getOperand(0));
763 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +0000764 bool isLeftShift = I.getOpcode() == Instruction::Shl;
765 bool isOperandSigned = I.getType()->isUnsigned();
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000766 unsigned OperandClass = getClass(I.getType());
767
768 if (OperandClass > 2)
769 visitInstruction(I); // Can't handle longs yet!
Chris Lattner796df732002-11-02 00:44:25 +0000770
Brian Gaekea1719c92002-10-31 23:03:59 +0000771 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
772 {
Chris Lattner796df732002-11-02 00:44:25 +0000773 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
774 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
775 unsigned char shAmt = CUI->getValue();
776
Chris Lattnere9913f22002-11-02 01:41:55 +0000777 static const unsigned ConstantOperand[][4] = {
778 { X86::SHRir8, X86::SHRir16, X86::SHRir32, 0 }, // SHR
779 { X86::SARir8, X86::SARir16, X86::SARir32, 0 }, // SAR
780 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SHL
781 { X86::SHLir8, X86::SHLir16, X86::SHLir32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000782 };
783
Chris Lattnere9913f22002-11-02 01:41:55 +0000784 const unsigned *OpTab = // Figure out the operand table to use
785 ConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000786
Brian Gaekea1719c92002-10-31 23:03:59 +0000787 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000788 BuildMI(BB, OpTab[OperandClass], 2, DestReg).addReg(Op0r).addZImm(shAmt);
Brian Gaekea1719c92002-10-31 23:03:59 +0000789 }
790 else
791 {
792 // The shift amount is non-constant.
793 //
794 // In fact, you can only shift with a variable shift amount if
795 // that amount is already in the CL register, so we have to put it
796 // there first.
797 //
Chris Lattnere9913f22002-11-02 01:41:55 +0000798
Brian Gaekea1719c92002-10-31 23:03:59 +0000799 // Emit: move cl, shiftAmount (put the shift amount in CL.)
Chris Lattnerca9671d2002-11-02 20:28:58 +0000800 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000801
802 // This is a shift right (SHR).
Chris Lattnere9913f22002-11-02 01:41:55 +0000803 static const unsigned NonConstantOperand[][4] = {
804 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32, 0 }, // SHR
805 { X86::SARrr8, X86::SARrr16, X86::SARrr32, 0 }, // SAR
806 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SHL
807 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32, 0 }, // SAL = SHL
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000808 };
809
Chris Lattnere9913f22002-11-02 01:41:55 +0000810 const unsigned *OpTab = // Figure out the operand table to use
811 NonConstantOperand[isLeftShift*2+isOperandSigned];
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000812
Chris Lattner3a9a6932002-11-21 22:49:20 +0000813 BuildMI(BB, OpTab[OperandClass], 1, DestReg).addReg(Op0r);
Brian Gaekea1719c92002-10-31 23:03:59 +0000814 }
815}
816
Chris Lattner06925362002-11-17 21:56:38 +0000817
Chris Lattner6fc3c522002-11-17 21:11:55 +0000818/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +0000819/// instruction. The load and store instructions are the only place where we
820/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +0000821///
822void ISel::visitLoadInst(LoadInst &I) {
Chris Lattnere8f0d922002-12-24 00:03:11 +0000823 bool isLittleEndian = TM.getTargetData().isLittleEndian();
824 bool hasLongPointers = TM.getTargetData().getPointerSize() == 8;
825
Chris Lattner6fc3c522002-11-17 21:11:55 +0000826 unsigned Class = getClass(I.getType());
827 if (Class > 2) // FIXME: Handle longs and others...
828 visitInstruction(I);
829
830 static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
Chris Lattnere8f0d922002-12-24 00:03:11 +0000831 unsigned SrcAddrReg = getReg(I.getOperand(0));
Chris Lattner6fc3c522002-11-17 21:11:55 +0000832
Chris Lattnere8f0d922002-12-24 00:03:11 +0000833 // We need to adjust the input pointer if we are emulating a big-endian
834 // long-pointer target. On these systems, the pointer that we are interested
835 // in is in the upper part of the eight byte memory image of the pointer. It
836 // also happens to be byte-swapped, but this will be handled later.
837 //
838 if (!isLittleEndian && hasLongPointers && isa<PointerType>(I.getType())) {
839 unsigned R = makeAnotherReg(Type::UIntTy);
840 BuildMI(BB, X86::ADDri32, 2, R).addReg(SrcAddrReg).addZImm(4);
841 SrcAddrReg = R;
842 }
843 unsigned DestReg = getReg(I);
844 unsigned IReg = DestReg;
845 if (!isLittleEndian) { // If big endian we need an intermediate stage
846 IReg = makeAnotherReg(I.getType());
847 std::swap(IReg, DestReg);
848 }
849 addDirectMem(BuildMI(BB, Opcode[Class], 4, DestReg), SrcAddrReg);
850
851 if (!isLittleEndian) {
852 // Emit the byte swap instruction...
853 static const unsigned BSWAPOpcode[] = { X86::MOVrr8, X86::BSWAPr16, X86::BSWAPr32 };
854 BuildMI(BB, BSWAPOpcode[Class], 1, IReg).addReg(DestReg);
855 }
Chris Lattner6fc3c522002-11-17 21:11:55 +0000856}
857
Chris Lattner06925362002-11-17 21:56:38 +0000858
Chris Lattner6fc3c522002-11-17 21:11:55 +0000859/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
860/// instruction.
861///
862void ISel::visitStoreInst(StoreInst &I) {
Chris Lattnere8f0d922002-12-24 00:03:11 +0000863 bool isLittleEndian = TM.getTargetData().isLittleEndian();
864 bool hasLongPointers = TM.getTargetData().getPointerSize() == 8;
Chris Lattner6fc3c522002-11-17 21:11:55 +0000865 unsigned Class = getClass(I.getOperand(0)->getType());
866 if (Class > 2) // FIXME: Handle longs and others...
867 visitInstruction(I);
868
869 static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
870
871 unsigned ValReg = getReg(I.getOperand(0));
872 unsigned AddressReg = getReg(I.getOperand(1));
Chris Lattnere8f0d922002-12-24 00:03:11 +0000873
874 if (!isLittleEndian && hasLongPointers && isa<PointerType>(I.getOperand(0)->getType())) {
875 unsigned R = makeAnotherReg(Type::UIntTy);
876 BuildMI(BB, X86::ADDri32, 2, R).addReg(AddressReg).addZImm(4);
877 AddressReg = R;
878 }
879
880 if (!isLittleEndian && Class) {
881 // Emit the byte swap instruction...
882 static const unsigned BSWAPOpcode[] = { X86::MOVrr8, X86::BSWAPr16, X86::BSWAPr32 };
883 unsigned R = makeAnotherReg(I.getOperand(0)->getType());
884 BuildMI(BB, BSWAPOpcode[Class], 1, R).addReg(ValReg);
885 ValReg = R;
886 }
887
Chris Lattner6fc3c522002-11-17 21:11:55 +0000888 addDirectMem(BuildMI(BB, Opcode[Class], 1+4), AddressReg).addReg(ValReg);
889}
890
891
Brian Gaekec11232a2002-11-26 10:43:30 +0000892/// visitCastInst - Here we have various kinds of copying with or without
893/// sign extension going on.
Brian Gaekefa8d5712002-11-22 11:07:01 +0000894void
895ISel::visitCastInst (CastInst &CI)
896{
Chris Lattnerf18a36e2002-12-03 18:15:59 +0000897 const Type *targetType = CI.getType ();
Brian Gaeke07f02612002-12-03 07:36:03 +0000898 Value *operand = CI.getOperand (0);
899 unsigned int operandReg = getReg (operand);
Chris Lattnerf18a36e2002-12-03 18:15:59 +0000900 const Type *sourceType = operand->getType ();
Brian Gaeke07f02612002-12-03 07:36:03 +0000901 unsigned int destReg = getReg (CI);
Brian Gaeked474e9c2002-12-06 10:49:33 +0000902 //
903 // Currently we handle:
904 //
905 // 1) cast * to bool
906 //
907 // 2) cast {sbyte, ubyte} to {sbyte, ubyte}
908 // cast {short, ushort} to {ushort, short}
909 // cast {int, uint, ptr} to {int, uint, ptr}
910 //
911 // 3) cast {sbyte, ubyte} to {ushort, short}
912 // cast {sbyte, ubyte} to {int, uint, ptr}
913 // cast {short, ushort} to {int, uint, ptr}
914 //
915 // 4) cast {int, uint, ptr} to {short, ushort}
916 // cast {int, uint, ptr} to {sbyte, ubyte}
917 // cast {short, ushort} to {sbyte, ubyte}
Chris Lattner7d255892002-12-13 11:31:59 +0000918
Brian Gaeked474e9c2002-12-06 10:49:33 +0000919 // 1) Implement casts to bool by using compare on the operand followed
920 // by set if not zero on the result.
921 if (targetType == Type::BoolTy)
922 {
923 BuildMI (BB, X86::CMPri8, 2).addReg (operandReg).addZImm (0);
924 BuildMI (BB, X86::SETNEr, 1, destReg);
925 return;
926 }
Chris Lattner7d255892002-12-13 11:31:59 +0000927
Brian Gaeked474e9c2002-12-06 10:49:33 +0000928 // 2) Implement casts between values of the same type class (as determined
929 // by getClass) by using a register-to-register move.
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000930 unsigned srcClass = getClassB (sourceType);
Chris Lattner7d255892002-12-13 11:31:59 +0000931 unsigned targClass = getClass (targetType);
Brian Gaeked474e9c2002-12-06 10:49:33 +0000932 static const unsigned regRegMove[] = {
933 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
934 };
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000935 if ((srcClass < cLong) && (targClass < cLong) && (srcClass == targClass))
Brian Gaeked474e9c2002-12-06 10:49:33 +0000936 {
937 BuildMI (BB, regRegMove[srcClass], 1, destReg).addReg (operandReg);
938 return;
939 }
940 // 3) Handle cast of SMALLER int to LARGER int using a move with sign
941 // extension or zero extension, depending on whether the source type
942 // was signed.
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000943 if ((srcClass < cLong) && (targClass < cLong) && (srcClass < targClass))
Brian Gaeked474e9c2002-12-06 10:49:33 +0000944 {
945 static const unsigned ops[] = {
946 X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16,
947 X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16
948 };
949 unsigned srcSigned = sourceType->isSigned ();
950 BuildMI (BB, ops[3 * srcSigned + srcClass + targClass - 1], 1,
951 destReg).addReg (operandReg);
952 return;
953 }
954 // 4) Handle cast of LARGER int to SMALLER int using a move to EAX
955 // followed by a move out of AX or AL.
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000956 if ((srcClass < cLong) && (targClass < cLong) && (srcClass > targClass))
Brian Gaeked474e9c2002-12-06 10:49:33 +0000957 {
958 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
959 BuildMI (BB, regRegMove[srcClass], 1,
960 AReg[srcClass]).addReg (operandReg);
961 BuildMI (BB, regRegMove[targClass], 1, destReg).addReg (AReg[srcClass]);
962 return;
963 }
964 // Anything we haven't handled already, we can't (yet) handle at all.
Brian Gaeke20244b72002-12-12 15:33:40 +0000965 //
966 // FP to integral casts can be handled with FISTP to store onto the
967 // stack while converting to integer, followed by a MOV to load from
968 // the stack into the result register. Integral to FP casts can be
969 // handled with MOV to store onto the stack, followed by a FILD to
970 // load from the stack while converting to FP. For the moment, I
971 // can't quite get straight in my head how to borrow myself some
972 // stack space and write on it. Otherwise, this would be trivial.
Brian Gaekefa8d5712002-11-22 11:07:01 +0000973 visitInstruction (CI);
974}
Brian Gaekea1719c92002-10-31 23:03:59 +0000975
Chris Lattner8a307e82002-12-16 19:32:50 +0000976// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
977// returns zero when the input is not exactly a power of two.
978static unsigned ExactLog2(unsigned Val) {
979 if (Val == 0) return 0;
980 unsigned Count = 0;
981 while (Val != 1) {
982 if (Val & 1) return 0;
983 Val >>= 1;
984 ++Count;
985 }
986 return Count+1;
987}
988
Brian Gaeke20244b72002-12-12 15:33:40 +0000989/// visitGetElementPtrInst - I don't know, most programs don't have
990/// getelementptr instructions, right? That means we can put off
991/// implementing this, right? Right. This method emits machine
992/// instructions to perform type-safe pointer arithmetic. I am
993/// guessing this could be cleaned up somewhat to use fewer temporary
994/// registers.
995void
996ISel::visitGetElementPtrInst (GetElementPtrInst &I)
997{
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000998 unsigned outputReg = getReg (I);
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000999 MachineBasicBlock::iterator MI = BB->end();
1000 emitGEPOperation(BB, MI, I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001001 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001002}
1003
Brian Gaeke71794c02002-12-13 11:22:48 +00001004void ISel::emitGEPOperation(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001005 MachineBasicBlock::iterator &IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +00001006 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +00001007 User::op_iterator IdxEnd, unsigned TargetReg) {
1008 const TargetData &TD = TM.getTargetData();
1009 const Type *Ty = Src->getType();
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001010 unsigned basePtrReg = getReg(Src, MBB, IP);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001011
Brian Gaeke20244b72002-12-12 15:33:40 +00001012 // GEPs have zero or more indices; we must perform a struct access
1013 // or array access for each one.
Chris Lattnerc0812d82002-12-13 06:56:29 +00001014 for (GetElementPtrInst::op_iterator oi = IdxBegin,
1015 oe = IdxEnd; oi != oe; ++oi) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001016 Value *idx = *oi;
Chris Lattnerc0812d82002-12-13 06:56:29 +00001017 unsigned nextBasePtrReg = makeAnotherReg(Type::UIntTy);
Brian Gaeke20244b72002-12-12 15:33:40 +00001018 if (const StructType *StTy = dyn_cast <StructType> (Ty)) {
1019 // It's a struct access. idx is the index into the structure,
1020 // which names the field. This index must have ubyte type.
1021 const ConstantUInt *CUI = cast <ConstantUInt> (idx);
1022 assert (CUI->getType () == Type::UByteTy
1023 && "Funny-looking structure index in GEP");
1024 // Use the TargetData structure to pick out what the layout of
1025 // the structure is in memory. Since the structure index must
1026 // be constant, we can get its value and use it to find the
1027 // right byte offset from the StructLayout class's list of
1028 // structure member offsets.
Chris Lattnere8f0d922002-12-24 00:03:11 +00001029 unsigned idxValue = CUI->getValue();
Brian Gaeke20244b72002-12-12 15:33:40 +00001030 unsigned memberOffset =
1031 TD.getStructLayout (StTy)->MemberOffsets[idxValue];
1032 // Emit an ADD to add memberOffset to the basePtr.
Brian Gaeke71794c02002-12-13 11:22:48 +00001033 BMI(MBB, IP, X86::ADDri32, 2,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001034 nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset);
Brian Gaeke20244b72002-12-12 15:33:40 +00001035 // The next type is the member of the structure selected by the
1036 // index.
1037 Ty = StTy->getElementTypes ()[idxValue];
Chris Lattner8a307e82002-12-16 19:32:50 +00001038 } else if (const SequentialType *SqTy = cast <SequentialType>(Ty)) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001039 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner8a307e82002-12-16 19:32:50 +00001040
Brian Gaeke20244b72002-12-12 15:33:40 +00001041 // idx is the index into the array. Unlike with structure
1042 // indices, we may not know its actual value at code-generation
1043 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00001044 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
1045
1046 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
1047 // must find the size of the pointed-to type (Not coincidentally, the next
1048 // type is the type of the elements in the array).
1049 Ty = SqTy->getElementType();
1050 unsigned elementSize = TD.getTypeSize(Ty);
1051
1052 // If idxReg is a constant, we don't need to perform the multiply!
1053 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
1054 if (CSI->isNullValue()) {
1055 BMI(MBB, IP, X86::MOVrr32, 1, nextBasePtrReg).addReg(basePtrReg);
1056 } else {
1057 unsigned Offset = elementSize*CSI->getValue();
1058
1059 BMI(MBB, IP, X86::ADDri32, 2,
1060 nextBasePtrReg).addReg(basePtrReg).addZImm(Offset);
1061 }
1062 } else if (elementSize == 1) {
1063 // If the element size is 1, we don't have to multiply, just add
1064 unsigned idxReg = getReg(idx, MBB, IP);
1065 BMI(MBB, IP, X86::ADDrr32, 2,
1066 nextBasePtrReg).addReg(basePtrReg).addReg(idxReg);
1067 } else {
1068 unsigned idxReg = getReg(idx, MBB, IP);
1069 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
1070 if (unsigned Shift = ExactLog2(elementSize)) {
1071 // If the element size is exactly a power of 2, use a shift to get it.
1072
1073 BMI(MBB, IP, X86::SHLir32, 2,
1074 OffsetReg).addReg(idxReg).addZImm(Shift-1);
1075 } else {
1076 // Most general case, emit a multiply...
1077 unsigned elementSizeReg = makeAnotherReg(Type::LongTy);
1078 BMI(MBB, IP, X86::MOVir32, 1, elementSizeReg).addZImm(elementSize);
1079
1080 // Emit a MUL to multiply the register holding the index by
1081 // elementSize, putting the result in OffsetReg.
1082 doMultiply(MBB, IP, OffsetReg, Type::LongTy, idxReg, elementSizeReg);
1083 }
1084 // Emit an ADD to add OffsetReg to the basePtr.
1085 BMI(MBB, IP, X86::ADDrr32, 2,
1086 nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg);
1087 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001088 }
1089 // Now that we are here, further indices refer to subtypes of this
1090 // one, so we don't need to worry about basePtrReg itself, anymore.
1091 basePtrReg = nextBasePtrReg;
1092 }
1093 // After we have processed all the indices, the result is left in
1094 // basePtrReg. Move it to the register where we were expected to
1095 // put the answer. A 32-bit move should do it, because we are in
1096 // ILP32 land.
Brian Gaeke71794c02002-12-13 11:22:48 +00001097 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg (basePtrReg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001098}
1099
1100
1101/// visitMallocInst - I know that personally, whenever I want to remember
1102/// something, I have to clear off some space in my brain.
1103void
1104ISel::visitMallocInst (MallocInst &I)
1105{
Brian Gaekee48ec012002-12-13 06:46:31 +00001106 // We assume that by this point, malloc instructions have been
1107 // lowered to calls, and dlsym will magically find malloc for us.
1108 // So we do not want to see malloc instructions here.
1109 visitInstruction (I);
1110}
1111
1112
1113/// visitFreeInst - same story as MallocInst
1114void
1115ISel::visitFreeInst (FreeInst &I)
1116{
1117 // We assume that by this point, free instructions have been
1118 // lowered to calls, and dlsym will magically find free for us.
1119 // So we do not want to see free instructions here.
Brian Gaeke20244b72002-12-12 15:33:40 +00001120 visitInstruction (I);
1121}
1122
1123
1124/// visitAllocaInst - I want some stack space. Come on, man, I said I
1125/// want some freakin' stack space.
1126void
1127ISel::visitAllocaInst (AllocaInst &I)
1128{
Brian Gaekee48ec012002-12-13 06:46:31 +00001129 // Find the data size of the alloca inst's getAllocatedType.
1130 const Type *allocatedType = I.getAllocatedType ();
1131 const TargetData &TD = TM.DataLayout;
1132 unsigned allocatedTypeSize = TD.getTypeSize (allocatedType);
1133 // Keep stack 32-bit aligned.
1134 unsigned int allocatedTypeWords = allocatedTypeSize / 4;
1135 if (allocatedTypeSize % 4 != 0) { allocatedTypeWords++; }
1136 // Subtract size from stack pointer, thereby allocating some space.
Chris Lattner4863fe12002-12-16 22:29:06 +00001137 BuildMI(BB, X86::SUBri32, 2,
1138 X86::ESP).addReg(X86::ESP).addZImm(allocatedTypeWords * 4);
Brian Gaekee48ec012002-12-13 06:46:31 +00001139 // Put a pointer to the space into the result register, by copying
1140 // the stack pointer.
1141 BuildMI (BB, X86::MOVrr32, 1, getReg (I)).addReg (X86::ESP);
Brian Gaeke20244b72002-12-12 15:33:40 +00001142}
1143
1144
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001145/// createSimpleX86InstructionSelector - This pass converts an LLVM function
1146/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00001147/// generated code sucks but the implementation is nice and simple.
1148///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001149Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
1150 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00001151}