blob: e1515672648d670c5cd61af5eaa7c9eb8c2cb3e9 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3e130a22003-01-13 00:32:26 +000010// This file defines a simple peephole instruction selector for the x86 target
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
Chris Lattner055c9652002-10-29 21:05:24 +000015#include "X86InstrInfo.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000016#include "X86InstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000017#include "llvm/Function.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000018#include "llvm/Instructions.h"
Brian Gaeke20244b72002-12-12 15:33:40 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000020#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000021#include "llvm/Pass.h"
Chris Lattnereca195e2003-05-08 19:44:13 +000022#include "llvm/Intrinsics.h"
Chris Lattner341a9372002-10-29 17:43:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner94af4142002-12-25 05:13:53 +000025#include "llvm/CodeGen/SSARegMap.h"
Chris Lattneraa09b752002-12-28 21:08:28 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e130a22003-01-13 00:32:26 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000028#include "llvm/Target/TargetMachine.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000029#include "llvm/Target/MRegisterInfo.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000030#include "llvm/Support/InstVisitor.h"
Chris Lattner72614082002-10-25 22:55:53 +000031
Chris Lattner333b2fa2002-12-13 10:09:43 +000032/// BMI - A special BuildMI variant that takes an iterator to insert the
Chris Lattner8bdd1292003-04-25 21:58:54 +000033/// instruction at as well as a basic block. This is the version for when you
34/// have a destination register in mind.
Brian Gaeke71794c02002-12-13 11:22:48 +000035inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Chris Lattner333b2fa2002-12-13 10:09:43 +000036 MachineBasicBlock::iterator &I,
Chris Lattner8cc72d22003-06-03 15:41:58 +000037 int Opcode, unsigned NumOperands,
Chris Lattner333b2fa2002-12-13 10:09:43 +000038 unsigned DestReg) {
Chris Lattnerd7d38722002-12-13 13:04:04 +000039 assert(I >= MBB->begin() && I <= MBB->end() && "Bad iterator!");
Chris Lattner333b2fa2002-12-13 10:09:43 +000040 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
Chris Lattnere8f0d922002-12-24 00:03:11 +000041 I = MBB->insert(I, MI)+1;
Chris Lattner333b2fa2002-12-13 10:09:43 +000042 return MachineInstrBuilder(MI).addReg(DestReg, MOTy::Def);
43}
44
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000045/// BMI - A special BuildMI variant that takes an iterator to insert the
46/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000047inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000048 MachineBasicBlock::iterator &I,
Chris Lattner8cc72d22003-06-03 15:41:58 +000049 int Opcode, unsigned NumOperands) {
Chris Lattner8bdd1292003-04-25 21:58:54 +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 Lattnereca195e2003-05-08 19:44:13 +000060 MachineFunction *F; // The function we are compiling into
61 MachineBasicBlock *BB; // The current MBB we are compiling
62 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner72614082002-10-25 22:55:53 +000063
Chris Lattner72614082002-10-25 22:55:53 +000064 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 Lattner3e130a22003-01-13 00:32:26 +000069 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000070
71 /// runOnFunction - Top level implementation of instruction selection for
72 /// the entire function.
73 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000074 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000075 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +000076
Chris Lattner065faeb2002-12-28 20:24:02 +000077 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +000078 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 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +000082
Chris Lattnerdbd73722003-05-06 21:32:22 +000083 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +000084 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +000085
Chris Lattner333b2fa2002-12-13 10:09:43 +000086 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000087 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +000088
89 // Select the PHI nodes
90 SelectPHINodes();
91
Chris Lattner72614082002-10-25 22:55:53 +000092 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +000093 MBBMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000094 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +000095 // We always build a machine code representation for the function
96 return true;
Chris Lattner72614082002-10-25 22:55:53 +000097 }
98
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000099 virtual const char *getPassName() const {
100 return "X86 Simple Instruction Selection";
101 }
102
Chris Lattner72614082002-10-25 22:55:53 +0000103 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000104 /// block. This simply creates a new MachineBasicBlock to emit code into
105 /// and adds it to the current MachineFunction. Subsequent visit* for
106 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000107 ///
108 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000109 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000110 }
111
Chris Lattner065faeb2002-12-28 20:24:02 +0000112 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
113 /// from the stack into virtual registers.
114 ///
115 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000116
117 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
118 /// because we have to generate our sources into the source basic blocks,
119 /// not the current one.
120 ///
121 void SelectPHINodes();
122
Chris Lattner72614082002-10-25 22:55:53 +0000123 // Visitation methods for various instructions. These methods simply emit
124 // fixed X86 code for each instruction.
125 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000126
127 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000128 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000129 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000130
131 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000132 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000133 unsigned Reg;
134 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000135 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
136 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000137 };
138 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
139 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000140 void visitCallInst(CallInst &I);
Chris Lattnereca195e2003-05-08 19:44:13 +0000141 void visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000142
143 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000144 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000145 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
146 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattner8a307e82002-12-16 19:32:50 +0000147 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +0000148 unsigned DestReg, const Type *DestTy,
149 unsigned Op0Reg, unsigned Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000150 void doMultiplyConst(MachineBasicBlock *MBB,
151 MachineBasicBlock::iterator &MBBI,
152 unsigned DestReg, const Type *DestTy,
153 unsigned Op0Reg, unsigned Op1Val);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000154 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000155
Chris Lattnerf01729e2002-11-02 20:54:46 +0000156 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
157 void visitRem(BinaryOperator &B) { visitDivRem(B); }
158 void visitDivRem(BinaryOperator &B);
159
Chris Lattnere2954c82002-11-02 20:04:26 +0000160 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000161 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
162 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
163 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000164
Chris Lattner6d40c192003-01-16 16:43:00 +0000165 // Comparison operators...
166 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000167 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
168 MachineBasicBlock *MBB,
169 MachineBasicBlock::iterator &MBBI);
170
Chris Lattner6fc3c522002-11-17 21:11:55 +0000171 // Memory Instructions
172 void visitLoadInst(LoadInst &I);
173 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000174 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000175 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000176 void visitMallocInst(MallocInst &I);
177 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000178
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 Lattner73815062003-10-18 05:56:40 +0000183 void visitVANextInst(VANextInst &I);
184 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000185
186 void visitInstruction(Instruction &I) {
187 std::cerr << "Cannot instruction select: " << I;
188 abort();
189 }
190
Brian Gaeke95780cc2002-12-13 07:56:18 +0000191 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000192 ///
193 void promote32(unsigned targetReg, const ValueRecord &VR);
194
195 /// EmitByteSwap - Byteswap SrcReg into DestReg.
196 ///
197 void EmitByteSwap(unsigned DestReg, unsigned SrcReg, unsigned Class);
Brian Gaeke95780cc2002-12-13 07:56:18 +0000198
Chris Lattner3e130a22003-01-13 00:32:26 +0000199 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
200 /// constant expression GEP support.
201 ///
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000202 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator&IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000203 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000204 User::op_iterator IdxEnd, unsigned TargetReg);
205
Chris Lattner548f61d2003-04-23 17:22:12 +0000206 /// emitCastOperation - Common code shared between visitCastInst and
207 /// constant expression cast support.
208 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator&IP,
209 Value *Src, const Type *DestTy, unsigned TargetReg);
210
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000211 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
212 /// and constant expression support.
213 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
214 MachineBasicBlock::iterator &IP,
215 Value *Op0, Value *Op1,
216 unsigned OperatorClass, unsigned TargetReg);
217
Chris Lattner58c41fe2003-08-24 19:19:47 +0000218 /// emitSetCCOperation - Common code shared between visitSetCondInst and
219 /// constant expression support.
220 void emitSetCCOperation(MachineBasicBlock *BB,
221 MachineBasicBlock::iterator &IP,
222 Value *Op0, Value *Op1, unsigned Opcode,
223 unsigned TargetReg);
224
225
Chris Lattnerc5291f52002-10-27 21:16:59 +0000226 /// copyConstantToRegister - Output the instructions required to put the
227 /// specified constant into the specified register.
228 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000229 void copyConstantToRegister(MachineBasicBlock *MBB,
230 MachineBasicBlock::iterator &MBBI,
231 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000232
Chris Lattner3e130a22003-01-13 00:32:26 +0000233 /// makeAnotherReg - This method returns the next register number we haven't
234 /// yet used.
235 ///
236 /// Long values are handled somewhat specially. They are always allocated
237 /// as pairs of 32 bit integer values. The register number returned is the
238 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
239 /// of the long value.
240 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000241 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000242 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
243 "Current target doesn't have X86 reg info??");
244 const X86RegisterInfo *MRI =
245 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000246 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000247 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
Chris Lattner3e130a22003-01-13 00:32:26 +0000248 // Create the lower part
249 F->getSSARegMap()->createVirtualRegister(RC);
250 // Create the upper part.
251 return F->getSSARegMap()->createVirtualRegister(RC)-1;
252 }
253
Chris Lattnerc0812d82002-12-13 06:56:29 +0000254 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000255 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000256 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000257 }
258
Chris Lattner72614082002-10-25 22:55:53 +0000259 /// getReg - This method turns an LLVM value into a register number. This
260 /// is guaranteed to produce the same register number for a particular value
261 /// every time it is queried.
262 ///
263 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000264 unsigned getReg(Value *V) {
265 // Just append to the end of the current bb.
266 MachineBasicBlock::iterator It = BB->end();
267 return getReg(V, BB, It);
268 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000269 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000270 MachineBasicBlock::iterator &IPt) {
Chris Lattner72614082002-10-25 22:55:53 +0000271 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000272 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000273 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000274 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000275 }
Chris Lattner72614082002-10-25 22:55:53 +0000276
Chris Lattner6f8fd252002-10-27 21:23:43 +0000277 // If this operand is a constant, emit the code to copy the constant into
278 // the register here...
279 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000280 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner8a307e82002-12-16 19:32:50 +0000281 copyConstantToRegister(MBB, IPt, C, Reg);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000282 RegMap.erase(V); // Assign a new name to this constant if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000283 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
284 // Move the address of the global into the register
Chris Lattner3e130a22003-01-13 00:32:26 +0000285 BMI(MBB, IPt, X86::MOVir32, 1, Reg).addGlobalAddress(GV);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000286 RegMap.erase(V); // Assign a new name to this address if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000287 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000288
Chris Lattner72614082002-10-25 22:55:53 +0000289 return Reg;
290 }
Chris Lattner72614082002-10-25 22:55:53 +0000291 };
292}
293
Chris Lattner43189d12002-11-17 20:07:45 +0000294/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
295/// Representation.
296///
297enum TypeClass {
Chris Lattner94af4142002-12-25 05:13:53 +0000298 cByte, cShort, cInt, cFP, cLong
Chris Lattner43189d12002-11-17 20:07:45 +0000299};
300
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000301/// getClass - Turn a primitive type into a "class" number which is based on the
302/// size of the type, and whether or not it is floating point.
303///
Chris Lattner43189d12002-11-17 20:07:45 +0000304static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000305 switch (Ty->getPrimitiveID()) {
306 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000307 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000308 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000309 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000310 case Type::IntTyID:
311 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000312 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000313
Chris Lattner94af4142002-12-25 05:13:53 +0000314 case Type::FloatTyID:
315 case Type::DoubleTyID: return cFP; // Floating Point is #3
Chris Lattner3e130a22003-01-13 00:32:26 +0000316
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000317 case Type::LongTyID:
Chris Lattner3e130a22003-01-13 00:32:26 +0000318 case Type::ULongTyID: return cLong; // Longs are class #4
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000319 default:
320 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000321 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000322 }
323}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000324
Chris Lattner6b993cc2002-12-15 08:02:15 +0000325// getClassB - Just like getClass, but treat boolean values as bytes.
326static inline TypeClass getClassB(const Type *Ty) {
327 if (Ty == Type::BoolTy) return cByte;
328 return getClass(Ty);
329}
330
Chris Lattner06925362002-11-17 21:56:38 +0000331
Chris Lattnerc5291f52002-10-27 21:16:59 +0000332/// copyConstantToRegister - Output the instructions required to put the
333/// specified constant into the specified register.
334///
Chris Lattner8a307e82002-12-16 19:32:50 +0000335void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
336 MachineBasicBlock::iterator &IP,
337 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000338 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000339 unsigned Class = 0;
340 switch (CE->getOpcode()) {
341 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000342 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000343 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000344 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000345 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000346 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000347 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000348
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000349 case Instruction::Xor: ++Class; // FALL THROUGH
350 case Instruction::Or: ++Class; // FALL THROUGH
351 case Instruction::And: ++Class; // FALL THROUGH
352 case Instruction::Sub: ++Class; // FALL THROUGH
353 case Instruction::Add:
354 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
355 Class, R);
356 return;
357
Chris Lattner58c41fe2003-08-24 19:19:47 +0000358 case Instruction::SetNE:
359 case Instruction::SetEQ:
360 case Instruction::SetLT:
361 case Instruction::SetGT:
362 case Instruction::SetLE:
363 case Instruction::SetGE:
364 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
365 CE->getOpcode(), R);
366 return;
367
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000368 default:
369 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000370 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000371 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000372 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000373
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000374 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000375 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000376
377 if (Class == cLong) {
378 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000379 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Chris Lattner3e130a22003-01-13 00:32:26 +0000380 BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(Val & 0xFFFFFFFF);
381 BMI(MBB, IP, X86::MOVir32, 1, R+1).addZImm(Val >> 32);
382 return;
383 }
384
Chris Lattner94af4142002-12-25 05:13:53 +0000385 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000386
387 static const unsigned IntegralOpcodeTab[] = {
388 X86::MOVir8, X86::MOVir16, X86::MOVir32
389 };
390
Chris Lattner6b993cc2002-12-15 08:02:15 +0000391 if (C->getType() == Type::BoolTy) {
392 BMI(MBB, IP, X86::MOVir8, 1, R).addZImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000393 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000394 ConstantInt *CI = cast<ConstantInt>(C);
395 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000396 }
Chris Lattner94af4142002-12-25 05:13:53 +0000397 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
398 double Value = CFP->getValue();
399 if (Value == +0.0)
400 BMI(MBB, IP, X86::FLD0, 0, R);
401 else if (Value == +1.0)
402 BMI(MBB, IP, X86::FLD1, 0, R);
403 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000404 // Otherwise we need to spill the constant to memory...
405 MachineConstantPool *CP = F->getConstantPool();
406 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000407 const Type *Ty = CFP->getType();
408
409 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
410 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLDr32 : X86::FLDr64;
411 addConstantPoolReference(BMI(MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000412 }
413
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000414 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000415 // Copy zero (null pointer) to the register.
Brian Gaeke71794c02002-12-13 11:22:48 +0000416 BMI(MBB, IP, X86::MOVir32, 1, R).addZImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000417 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000418 unsigned SrcReg = getReg(CPR->getValue(), MBB, IP);
Brian Gaeke71794c02002-12-13 11:22:48 +0000419 BMI(MBB, IP, X86::MOVrr32, 1, R).addReg(SrcReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000420 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000421 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000422 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000423 }
424}
425
Chris Lattner065faeb2002-12-28 20:24:02 +0000426/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
427/// the stack into virtual registers.
428///
429void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
430 // Emit instructions to load the arguments... On entry to a function on the
431 // X86, the stack frame looks like this:
432 //
433 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000434 // [ESP + 4] -- first argument (leftmost lexically)
435 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000436 // ...
437 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000438 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000439 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000440
441 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
442 unsigned Reg = getReg(*I);
443
Chris Lattner065faeb2002-12-28 20:24:02 +0000444 int FI; // Frame object index
Chris Lattner065faeb2002-12-28 20:24:02 +0000445 switch (getClassB(I->getType())) {
446 case cByte:
Chris Lattneraa09b752002-12-28 21:08:28 +0000447 FI = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000448 addFrameReference(BuildMI(BB, X86::MOVmr8, 4, Reg), FI);
449 break;
450 case cShort:
Chris Lattneraa09b752002-12-28 21:08:28 +0000451 FI = MFI->CreateFixedObject(2, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000452 addFrameReference(BuildMI(BB, X86::MOVmr16, 4, Reg), FI);
453 break;
454 case cInt:
Chris Lattneraa09b752002-12-28 21:08:28 +0000455 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000456 addFrameReference(BuildMI(BB, X86::MOVmr32, 4, Reg), FI);
457 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000458 case cLong:
459 FI = MFI->CreateFixedObject(8, ArgOffset);
460 addFrameReference(BuildMI(BB, X86::MOVmr32, 4, Reg), FI);
461 addFrameReference(BuildMI(BB, X86::MOVmr32, 4, Reg+1), FI, 4);
462 ArgOffset += 4; // longs require 4 additional bytes
463 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000464 case cFP:
465 unsigned Opcode;
466 if (I->getType() == Type::FloatTy) {
467 Opcode = X86::FLDr32;
Chris Lattneraa09b752002-12-28 21:08:28 +0000468 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000469 } else {
470 Opcode = X86::FLDr64;
Chris Lattneraa09b752002-12-28 21:08:28 +0000471 FI = MFI->CreateFixedObject(8, ArgOffset);
Chris Lattner3e130a22003-01-13 00:32:26 +0000472 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000473 }
474 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
475 break;
476 default:
477 assert(0 && "Unhandled argument type!");
478 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000479 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000480 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000481
482 // If the function takes variable number of arguments, add a frame offset for
483 // the start of the first vararg value... this is used to expand
484 // llvm.va_start.
485 if (Fn.getFunctionType()->isVarArg())
486 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000487}
488
489
Chris Lattner333b2fa2002-12-13 10:09:43 +0000490/// SelectPHINodes - Insert machine code to generate phis. This is tricky
491/// because we have to generate our sources into the source basic blocks, not
492/// the current one.
493///
494void ISel::SelectPHINodes() {
Chris Lattner3501fea2003-01-14 22:00:31 +0000495 const TargetInstrInfo &TII = TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000496 const Function &LF = *F->getFunction(); // The LLVM function...
497 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
498 const BasicBlock *BB = I;
499 MachineBasicBlock *MBB = MBBMap[I];
500
501 // Loop over all of the PHI nodes in the LLVM basic block...
502 unsigned NumPHIs = 0;
503 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000504 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000505
Chris Lattner333b2fa2002-12-13 10:09:43 +0000506 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000507 unsigned PHIReg = getReg(*PN);
508 MachineInstr *PhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg);
509 MBB->insert(MBB->begin()+NumPHIs++, PhiMI);
510
511 MachineInstr *LongPhiMI = 0;
512 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) {
513 LongPhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg+1);
514 MBB->insert(MBB->begin()+NumPHIs++, LongPhiMI);
515 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000516
Chris Lattnera6e73f12003-05-12 14:22:21 +0000517 // PHIValues - Map of blocks to incoming virtual registers. We use this
518 // so that we only initialize one incoming value for a particular block,
519 // even if the block has multiple entries in the PHI node.
520 //
521 std::map<MachineBasicBlock*, unsigned> PHIValues;
522
Chris Lattner333b2fa2002-12-13 10:09:43 +0000523 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
524 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000525 unsigned ValReg;
526 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
527 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000528
Chris Lattnera6e73f12003-05-12 14:22:21 +0000529 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
530 // We already inserted an initialization of the register for this
531 // predecessor. Recycle it.
532 ValReg = EntryIt->second;
533
534 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000535 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000536 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000537 Value *Val = PN->getIncomingValue(i);
538
539 // If this is a constant or GlobalValue, we may have to insert code
540 // into the basic block to compute it into a virtual register.
541 if (isa<Constant>(Val) || isa<GlobalValue>(Val)) {
542 // Because we don't want to clobber any values which might be in
543 // physical registers with the computation of this constant (which
544 // might be arbitrarily complex if it is a constant expression),
545 // just insert the computation at the top of the basic block.
546 MachineBasicBlock::iterator PI = PredMBB->begin();
547
548 // Skip over any PHI nodes though!
549 while (PI != PredMBB->end() && (*PI)->getOpcode() == X86::PHI)
550 ++PI;
551
552 ValReg = getReg(Val, PredMBB, PI);
553 } else {
554 ValReg = getReg(Val);
555 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000556
557 // Remember that we inserted a value for this PHI for this predecessor
558 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
559 }
560
Chris Lattner3e130a22003-01-13 00:32:26 +0000561 PhiMI->addRegOperand(ValReg);
562 PhiMI->addMachineBasicBlockOperand(PredMBB);
563 if (LongPhiMI) {
564 LongPhiMI->addRegOperand(ValReg+1);
565 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
566 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000567 }
568 }
569 }
570}
571
Chris Lattner6d40c192003-01-16 16:43:00 +0000572// canFoldSetCCIntoBranch - Return the setcc instruction if we can fold it into
573// the conditional branch instruction which is the only user of the cc
574// instruction. This is the case if the conditional branch is the only user of
575// the setcc, and if the setcc is in the same basic block as the conditional
576// branch. We also don't handle long arguments below, so we reject them here as
577// well.
578//
579static SetCondInst *canFoldSetCCIntoBranch(Value *V) {
580 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattnerfd059242003-10-15 16:48:29 +0000581 if (SCI->hasOneUse() && isa<BranchInst>(SCI->use_back()) &&
Chris Lattner6d40c192003-01-16 16:43:00 +0000582 SCI->getParent() == cast<BranchInst>(SCI->use_back())->getParent()) {
583 const Type *Ty = SCI->getOperand(0)->getType();
584 if (Ty != Type::LongTy && Ty != Type::ULongTy)
585 return SCI;
586 }
587 return 0;
588}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000589
Chris Lattner6d40c192003-01-16 16:43:00 +0000590// Return a fixed numbering for setcc instructions which does not depend on the
591// order of the opcodes.
592//
593static unsigned getSetCCNumber(unsigned Opcode) {
594 switch(Opcode) {
595 default: assert(0 && "Unknown setcc instruction!");
596 case Instruction::SetEQ: return 0;
597 case Instruction::SetNE: return 1;
598 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000599 case Instruction::SetGE: return 3;
600 case Instruction::SetGT: return 4;
601 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000602 }
603}
Chris Lattner06925362002-11-17 21:56:38 +0000604
Chris Lattner6d40c192003-01-16 16:43:00 +0000605// LLVM -> X86 signed X86 unsigned
606// ----- ---------- ------------
607// seteq -> sete sete
608// setne -> setne setne
609// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000610// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000611// setgt -> setg seta
612// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000613// ----
614// sets // Used by comparison with 0 optimization
615// setns
616static const unsigned SetCCOpcodeTab[2][8] = {
617 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
618 0, 0 },
619 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
620 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000621};
622
Chris Lattnerb2acc512003-10-19 21:09:10 +0000623// EmitComparison - This function emits a comparison of the two operands,
624// returning the extended setcc code to use.
625unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
626 MachineBasicBlock *MBB,
627 MachineBasicBlock::iterator &IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000628 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000629 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000630 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000631 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000632
633 // Special case handling of: cmp R, i
634 if (Class == cByte || Class == cShort || Class == cInt)
635 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000636 uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue();
637
Chris Lattner333864d2003-06-05 19:30:30 +0000638 // Mask off any upper bits of the constant, if there are any...
639 Op1v &= (1ULL << (8 << Class)) - 1;
640
Chris Lattnerb2acc512003-10-19 21:09:10 +0000641 // If this is a comparison against zero, emit more efficient code. We
642 // can't handle unsigned comparisons against zero unless they are == or
643 // !=. These should have been strength reduced already anyway.
644 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
645 static const unsigned TESTTab[] = {
646 X86::TESTrr8, X86::TESTrr16, X86::TESTrr32
647 };
648 BMI(MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
649
650 if (OpNum == 2) return 6; // Map jl -> js
651 if (OpNum == 3) return 7; // Map jg -> jns
652 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000653 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000654
655 static const unsigned CMPTab[] = {
656 X86::CMPri8, X86::CMPri16, X86::CMPri32
657 };
658
659 BMI(MBB, IP, CMPTab[Class], 2).addReg(Op0r).addZImm(Op1v);
660 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000661 }
662
Chris Lattner58c41fe2003-08-24 19:19:47 +0000663 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000664 switch (Class) {
665 default: assert(0 && "Unknown type class!");
666 // Emit: cmp <var1>, <var2> (do the comparison). We can
667 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
668 // 32-bit.
669 case cByte:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000670 BMI(MBB, IP, X86::CMPrr8, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000671 break;
672 case cShort:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000673 BMI(MBB, IP, X86::CMPrr16, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000674 break;
675 case cInt:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000676 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000677 break;
678 case cFP:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000679 BMI(MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
680 BMI(MBB, IP, X86::FNSTSWr8, 0);
681 BMI(MBB, IP, X86::SAHF, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +0000682 break;
683
684 case cLong:
685 if (OpNum < 2) { // seteq, setne
686 unsigned LoTmp = makeAnotherReg(Type::IntTy);
687 unsigned HiTmp = makeAnotherReg(Type::IntTy);
688 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000689 BMI(MBB, IP, X86::XORrr32, 2, LoTmp).addReg(Op0r).addReg(Op1r);
690 BMI(MBB, IP, X86::XORrr32, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
691 BMI(MBB, IP, X86::ORrr32, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000692 break; // Allow the sete or setne to be generated from flags set by OR
693 } else {
694 // Emit a sequence of code which compares the high and low parts once
695 // each, then uses a conditional move to handle the overflow case. For
696 // example, a setlt for long would generate code like this:
697 //
698 // AL = lo(op1) < lo(op2) // Signedness depends on operands
699 // BL = hi(op1) < hi(op2) // Always unsigned comparison
700 // dest = hi(op1) == hi(op2) ? AL : BL;
701 //
702
Chris Lattner6d40c192003-01-16 16:43:00 +0000703 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000704 // classes! Until then, hardcode registers so that we can deal with their
705 // aliases (because we don't have conditional byte moves).
706 //
Chris Lattner58c41fe2003-08-24 19:19:47 +0000707 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r);
708 BMI(MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
709 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000710 BMI(MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000711 BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
712 BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
713 BMI(MBB, IP, X86::CMOVErr16, 2, X86::BX).addReg(X86::BX).addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000714 // NOTE: visitSetCondInst knows that the value is dumped into the BL
715 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000716 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000717 }
718 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000719 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000720}
Chris Lattner3e130a22003-01-13 00:32:26 +0000721
Chris Lattner6d40c192003-01-16 16:43:00 +0000722
723/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
724/// register, then move it to wherever the result should be.
725///
726void ISel::visitSetCondInst(SetCondInst &I) {
727 if (canFoldSetCCIntoBranch(&I)) return; // Fold this into a branch...
728
Chris Lattner6d40c192003-01-16 16:43:00 +0000729 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000730 MachineBasicBlock::iterator MII = BB->end();
731 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
732 DestReg);
733}
Chris Lattner6d40c192003-01-16 16:43:00 +0000734
Chris Lattner58c41fe2003-08-24 19:19:47 +0000735/// emitSetCCOperation - Common code shared between visitSetCondInst and
736/// constant expression support.
737void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
738 MachineBasicBlock::iterator &IP,
739 Value *Op0, Value *Op1, unsigned Opcode,
740 unsigned TargetReg) {
741 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000742 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000743
Chris Lattnerb2acc512003-10-19 21:09:10 +0000744 const Type *CompTy = Op0->getType();
745 unsigned CompClass = getClassB(CompTy);
746 bool isSigned = CompTy->isSigned() && CompClass != cFP;
747
748 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000749 // Handle normal comparisons with a setcc instruction...
Chris Lattner58c41fe2003-08-24 19:19:47 +0000750 BMI(MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +0000751 } else {
752 // Handle long comparisons by copying the value which is already in BL into
753 // the register we want...
Chris Lattner58c41fe2003-08-24 19:19:47 +0000754 BMI(MBB, IP, X86::MOVrr8, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +0000755 }
Brian Gaeke1749d632002-11-07 17:59:21 +0000756}
Chris Lattner51b49a92002-11-02 19:45:49 +0000757
Chris Lattner58c41fe2003-08-24 19:19:47 +0000758
759
760
Brian Gaekec2505982002-11-30 11:57:28 +0000761/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
762/// operand, in the specified target register.
Chris Lattner3e130a22003-01-13 00:32:26 +0000763void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
764 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000765
766 // Make sure we have the register number for this value...
767 unsigned Reg = VR.Val ? getReg(VR.Val) : VR.Reg;
768
Chris Lattner3e130a22003-01-13 00:32:26 +0000769 switch (getClassB(VR.Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +0000770 case cByte:
771 // Extend value into target register (8->32)
772 if (isUnsigned)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000773 BuildMI(BB, X86::MOVZXr32r8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000774 else
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000775 BuildMI(BB, X86::MOVSXr32r8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000776 break;
777 case cShort:
778 // Extend value into target register (16->32)
779 if (isUnsigned)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000780 BuildMI(BB, X86::MOVZXr32r16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000781 else
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000782 BuildMI(BB, X86::MOVSXr32r16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000783 break;
784 case cInt:
785 // Move value into target register (32->32)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000786 BuildMI(BB, X86::MOVrr32, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000787 break;
788 default:
789 assert(0 && "Unpromotable operand class in promote32");
790 }
Brian Gaekec2505982002-11-30 11:57:28 +0000791}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000792
Chris Lattner72614082002-10-25 22:55:53 +0000793/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
794/// we have the following possibilities:
795///
796/// ret void: No return value, simply emit a 'ret' instruction
797/// ret sbyte, ubyte : Extend value into EAX and return
798/// ret short, ushort: Extend value into EAX and return
799/// ret int, uint : Move value into EAX and return
800/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000801/// ret long, ulong : Move value into EAX/EDX and return
802/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000803///
Chris Lattner3e130a22003-01-13 00:32:26 +0000804void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +0000805 if (I.getNumOperands() == 0) {
806 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
807 return;
808 }
809
810 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +0000811 unsigned RetReg = getReg(RetVal);
812 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +0000813 case cByte: // integral return values: extend or move into EAX and return
814 case cShort:
815 case cInt:
Chris Lattner3e130a22003-01-13 00:32:26 +0000816 promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType()));
Chris Lattnerdbd73722003-05-06 21:32:22 +0000817 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000818 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +0000819 break;
820 case cFP: // Floats & Doubles: Return in ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +0000821 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +0000822 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000823 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +0000824 break;
825 case cLong:
Chris Lattner3e130a22003-01-13 00:32:26 +0000826 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(RetReg);
827 BuildMI(BB, X86::MOVrr32, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +0000828 // Declare that EAX & EDX are live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000829 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX).addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000830 break;
Chris Lattner94af4142002-12-25 05:13:53 +0000831 default:
Chris Lattner3e130a22003-01-13 00:32:26 +0000832 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +0000833 }
Chris Lattner43189d12002-11-17 20:07:45 +0000834 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +0000835 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000836}
837
Chris Lattner55f6fab2003-01-16 18:07:23 +0000838// getBlockAfter - Return the basic block which occurs lexically after the
839// specified one.
840static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
841 Function::iterator I = BB; ++I; // Get iterator to next block
842 return I != BB->getParent()->end() ? &*I : 0;
843}
844
Chris Lattner51b49a92002-11-02 19:45:49 +0000845/// visitBranchInst - Handle conditional and unconditional branches here. Note
846/// that since code layout is frozen at this point, that if we are trying to
847/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +0000848/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +0000849///
Chris Lattner94af4142002-12-25 05:13:53 +0000850void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +0000851 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
852
853 if (!BI.isConditional()) { // Unconditional branch?
854 if (BI.getSuccessor(0) != NextBB)
855 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +0000856 return;
857 }
858
859 // See if we can fold the setcc into the branch itself...
860 SetCondInst *SCI = canFoldSetCCIntoBranch(BI.getCondition());
861 if (SCI == 0) {
862 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
863 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +0000864 unsigned condReg = getReg(BI.getCondition());
Chris Lattner94af4142002-12-25 05:13:53 +0000865 BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0);
Chris Lattner55f6fab2003-01-16 18:07:23 +0000866 if (BI.getSuccessor(1) == NextBB) {
867 if (BI.getSuccessor(0) != NextBB)
868 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
869 } else {
870 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
871
872 if (BI.getSuccessor(0) != NextBB)
873 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
874 }
Chris Lattner6d40c192003-01-16 16:43:00 +0000875 return;
Chris Lattner94af4142002-12-25 05:13:53 +0000876 }
Chris Lattner6d40c192003-01-16 16:43:00 +0000877
878 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +0000879 MachineBasicBlock::iterator MII = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +0000880 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB, MII);
881
882 const Type *CompTy = SCI->getOperand(0)->getType();
883 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +0000884
Chris Lattnerb2acc512003-10-19 21:09:10 +0000885
Chris Lattner6d40c192003-01-16 16:43:00 +0000886 // LLVM -> X86 signed X86 unsigned
887 // ----- ---------- ------------
888 // seteq -> je je
889 // setne -> jne jne
890 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000891 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +0000892 // setgt -> jg ja
893 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000894 // ----
895 // js // Used by comparison with 0 optimization
896 // jns
897
898 static const unsigned OpcodeTab[2][8] = {
899 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
900 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
901 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +0000902 };
903
Chris Lattner55f6fab2003-01-16 18:07:23 +0000904 if (BI.getSuccessor(0) != NextBB) {
905 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
906 if (BI.getSuccessor(1) != NextBB)
907 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
908 } else {
909 // Change to the inverse condition...
910 if (BI.getSuccessor(1) != NextBB) {
911 OpNum ^= 1;
912 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
913 }
914 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000915}
916
Chris Lattner3e130a22003-01-13 00:32:26 +0000917
918/// doCall - This emits an abstract call instruction, setting up the arguments
919/// and the return value as appropriate. For the actual function call itself,
920/// it inserts the specified CallMI instruction into the stream.
921///
922void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
923 const std::vector<ValueRecord> &Args) {
924
Chris Lattner065faeb2002-12-28 20:24:02 +0000925 // Count how many bytes are to be pushed on the stack...
926 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000927
Chris Lattner3e130a22003-01-13 00:32:26 +0000928 if (!Args.empty()) {
929 for (unsigned i = 0, e = Args.size(); i != e; ++i)
930 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000931 case cByte: case cShort: case cInt:
Chris Lattner3e130a22003-01-13 00:32:26 +0000932 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000933 case cLong:
Chris Lattner3e130a22003-01-13 00:32:26 +0000934 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000935 case cFP:
Chris Lattner3e130a22003-01-13 00:32:26 +0000936 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
Chris Lattner065faeb2002-12-28 20:24:02 +0000937 break;
938 default: assert(0 && "Unknown class!");
939 }
940
941 // Adjust the stack pointer for the new arguments...
942 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(NumBytes);
943
944 // Arguments go on the stack in reverse order, as specified by the ABI.
945 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +0000946 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000947 unsigned ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Chris Lattner3e130a22003-01-13 00:32:26 +0000948 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000949 case cByte:
950 case cShort: {
951 // Promote arg to 32 bits wide into a temporary register...
952 unsigned R = makeAnotherReg(Type::UIntTy);
Chris Lattner3e130a22003-01-13 00:32:26 +0000953 promote32(R, Args[i]);
Chris Lattner065faeb2002-12-28 20:24:02 +0000954 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
955 X86::ESP, ArgOffset).addReg(R);
956 break;
957 }
958 case cInt:
959 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
Chris Lattner3e130a22003-01-13 00:32:26 +0000960 X86::ESP, ArgOffset).addReg(ArgReg);
Chris Lattner065faeb2002-12-28 20:24:02 +0000961 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000962 case cLong:
963 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
964 X86::ESP, ArgOffset).addReg(ArgReg);
965 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
966 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
967 ArgOffset += 4; // 8 byte entry, not 4.
968 break;
969
Chris Lattner065faeb2002-12-28 20:24:02 +0000970 case cFP:
Chris Lattner3e130a22003-01-13 00:32:26 +0000971 if (Args[i].Ty == Type::FloatTy) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000972 addRegOffset(BuildMI(BB, X86::FSTr32, 5),
Chris Lattner3e130a22003-01-13 00:32:26 +0000973 X86::ESP, ArgOffset).addReg(ArgReg);
Chris Lattner065faeb2002-12-28 20:24:02 +0000974 } else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000975 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
976 addRegOffset(BuildMI(BB, X86::FSTr64, 5),
977 X86::ESP, ArgOffset).addReg(ArgReg);
978 ArgOffset += 4; // 8 byte entry, not 4.
Chris Lattner065faeb2002-12-28 20:24:02 +0000979 }
980 break;
981
Chris Lattner3e130a22003-01-13 00:32:26 +0000982 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +0000983 }
984 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +0000985 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000986 } else {
987 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +0000988 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +0000989
Chris Lattner3e130a22003-01-13 00:32:26 +0000990 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000991
Chris Lattner065faeb2002-12-28 20:24:02 +0000992 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addZImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +0000993
994 // If there is a return value, scavenge the result from the location the call
995 // leaves it in...
996 //
Chris Lattner3e130a22003-01-13 00:32:26 +0000997 if (Ret.Ty != Type::VoidTy) {
998 unsigned DestClass = getClassB(Ret.Ty);
999 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001000 case cByte:
1001 case cShort:
1002 case cInt: {
1003 // Integral results are in %eax, or the appropriate portion
1004 // thereof.
1005 static const unsigned regRegMove[] = {
1006 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
1007 };
1008 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001009 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001010 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001011 }
Chris Lattner94af4142002-12-25 05:13:53 +00001012 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001013 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001014 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001015 case cLong: // Long values are left in EDX:EAX
1016 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg).addReg(X86::EAX);
1017 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg+1).addReg(X86::EDX);
1018 break;
1019 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001020 }
Chris Lattnera3243642002-12-04 23:45:28 +00001021 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001022}
Chris Lattner2df035b2002-11-02 19:27:56 +00001023
Chris Lattner3e130a22003-01-13 00:32:26 +00001024
1025/// visitCallInst - Push args on stack and do a procedure call instruction.
1026void ISel::visitCallInst(CallInst &CI) {
1027 MachineInstr *TheCall;
1028 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001029 // Is it an intrinsic function call?
1030 if (LLVMIntrinsic::ID ID = (LLVMIntrinsic::ID)F->getIntrinsicID()) {
1031 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1032 return;
1033 }
1034
Chris Lattner3e130a22003-01-13 00:32:26 +00001035 // Emit a CALL instruction with PC-relative displacement.
1036 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1037 } else { // Emit an indirect call...
1038 unsigned Reg = getReg(CI.getCalledValue());
1039 TheCall = BuildMI(X86::CALLr32, 1).addReg(Reg);
1040 }
1041
1042 std::vector<ValueRecord> Args;
1043 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001044 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001045
1046 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1047 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
1048}
1049
Chris Lattneraeb54b82003-08-28 21:23:43 +00001050
Chris Lattnereca195e2003-05-08 19:44:13 +00001051void ISel::visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &CI) {
1052 unsigned TmpReg1, TmpReg2;
1053 switch (ID) {
1054 case LLVMIntrinsic::va_start:
1055 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001056 TmpReg1 = getReg(CI);
Chris Lattnereca195e2003-05-08 19:44:13 +00001057 addFrameReference(BuildMI(BB, X86::LEAr32, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001058 return;
1059
Chris Lattnereca195e2003-05-08 19:44:13 +00001060 case LLVMIntrinsic::va_copy:
Chris Lattner73815062003-10-18 05:56:40 +00001061 TmpReg1 = getReg(CI);
1062 TmpReg2 = getReg(CI.getOperand(1));
1063 BuildMI(BB, X86::MOVrr32, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001064 return;
Chris Lattner73815062003-10-18 05:56:40 +00001065 case LLVMIntrinsic::va_end: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001066
Chris Lattnerc151e4f2003-06-29 16:42:32 +00001067 case LLVMIntrinsic::longjmp:
Chris Lattner72af6b82003-08-18 16:06:09 +00001068 case LLVMIntrinsic::siglongjmp:
Chris Lattneraeb54b82003-08-28 21:23:43 +00001069 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("abort", true);
Brian Gaeked4615052003-07-18 20:23:43 +00001070 return;
1071
Chris Lattnerc151e4f2003-06-29 16:42:32 +00001072 case LLVMIntrinsic::setjmp:
Chris Lattner72af6b82003-08-18 16:06:09 +00001073 case LLVMIntrinsic::sigsetjmp:
Chris Lattnereb093fb2003-06-30 19:35:54 +00001074 // Setjmp always returns zero...
1075 BuildMI(BB, X86::MOVir32, 1, getReg(CI)).addZImm(0);
Chris Lattnerc151e4f2003-06-29 16:42:32 +00001076 return;
Chris Lattnereca195e2003-05-08 19:44:13 +00001077 default: assert(0 && "Unknown intrinsic for X86!");
1078 }
1079}
1080
1081
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001082/// visitSimpleBinary - Implement simple binary operators for integral types...
1083/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1084/// Xor.
1085void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1086 unsigned DestReg = getReg(B);
1087 MachineBasicBlock::iterator MI = BB->end();
1088 emitSimpleBinaryOperation(BB, MI, B.getOperand(0), B.getOperand(1),
1089 OperatorClass, DestReg);
1090}
Chris Lattner3e130a22003-01-13 00:32:26 +00001091
Chris Lattnerb2acc512003-10-19 21:09:10 +00001092/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1093/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1094/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001095///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001096/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1097/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001098///
1099void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001100 MachineBasicBlock::iterator &IP,
1101 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001102 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001103 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001104
1105 // sub 0, X -> neg X
1106 if (OperatorClass == 1 && Class != cLong)
1107 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
1108 if (CI->isNullValue()) {
1109 unsigned op1Reg = getReg(Op1, MBB, IP);
1110 switch (Class) {
1111 default: assert(0 && "Unknown class for this function!");
1112 case cByte:
1113 BMI(MBB, IP, X86::NEGr8, 1, DestReg).addReg(op1Reg);
1114 return;
1115 case cShort:
1116 BMI(MBB, IP, X86::NEGr16, 1, DestReg).addReg(op1Reg);
1117 return;
1118 case cInt:
1119 BMI(MBB, IP, X86::NEGr32, 1, DestReg).addReg(op1Reg);
1120 return;
1121 }
1122 }
1123
Chris Lattner35333e12003-06-05 18:28:55 +00001124 if (!isa<ConstantInt>(Op1) || Class == cLong) {
1125 static const unsigned OpcodeTab[][4] = {
1126 // Arithmetic operators
1127 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD
1128 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB
1129
1130 // Bitwise operators
1131 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
1132 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
1133 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
Chris Lattner3e130a22003-01-13 00:32:26 +00001134 };
Chris Lattner35333e12003-06-05 18:28:55 +00001135
1136 bool isLong = false;
1137 if (Class == cLong) {
1138 isLong = true;
1139 Class = cInt; // Bottom 32 bits are handled just like ints
1140 }
1141
1142 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1143 assert(Opcode && "Floating point arguments to logical inst?");
Chris Lattnerb2acc512003-10-19 21:09:10 +00001144 unsigned Op0r = getReg(Op0, MBB, IP);
1145 unsigned Op1r = getReg(Op1, MBB, IP);
1146 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner35333e12003-06-05 18:28:55 +00001147
1148 if (isLong) { // Handle the upper 32 bits of long values...
1149 static const unsigned TopTab[] = {
1150 X86::ADCrr32, X86::SBBrr32, X86::ANDrr32, X86::ORrr32, X86::XORrr32
1151 };
Chris Lattnerb2acc512003-10-19 21:09:10 +00001152 BMI(MBB, IP, TopTab[OperatorClass], 2,
1153 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattner35333e12003-06-05 18:28:55 +00001154 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001155 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001156 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001157
1158 // Special case: op Reg, <const>
1159 ConstantInt *Op1C = cast<ConstantInt>(Op1);
1160 unsigned Op0r = getReg(Op0, MBB, IP);
1161
1162 // xor X, -1 -> not X
1163 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1164 static unsigned const NOTTab[] = { X86::NOTr8, X86::NOTr16, X86::NOTr32 };
1165 BMI(MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
1166 return;
1167 }
1168
1169 // add X, -1 -> dec X
1170 if (OperatorClass == 0 && Op1C->isAllOnesValue()) {
1171 static unsigned const DECTab[] = { X86::DECr8, X86::DECr16, X86::DECr32 };
1172 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1173 return;
1174 }
1175
1176 // add X, 1 -> inc X
1177 if (OperatorClass == 0 && Op1C->equalsInt(1)) {
1178 static unsigned const DECTab[] = { X86::INCr8, X86::INCr16, X86::INCr32 };
1179 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1180 return;
1181 }
1182
1183 static const unsigned OpcodeTab[][3] = {
1184 // Arithmetic operators
1185 { X86::ADDri8, X86::ADDri16, X86::ADDri32 }, // ADD
1186 { X86::SUBri8, X86::SUBri16, X86::SUBri32 }, // SUB
1187
1188 // Bitwise operators
1189 { X86::ANDri8, X86::ANDri16, X86::ANDri32 }, // AND
1190 { X86:: ORri8, X86:: ORri16, X86:: ORri32 }, // OR
1191 { X86::XORri8, X86::XORri16, X86::XORri32 }, // XOR
1192 };
1193
1194 assert(Class < 3 && "General code handles 64-bit integer types!");
1195 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1196 uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
1197
1198 // Mask off any upper bits of the constant, if there are any...
1199 Op1v &= (1ULL << (8 << Class)) - 1;
1200 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addZImm(Op1v);
Chris Lattnere2954c82002-11-02 20:04:26 +00001201}
1202
Chris Lattner3e130a22003-01-13 00:32:26 +00001203/// doMultiply - Emit appropriate instructions to multiply together the
1204/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
1205/// result should be given as DestTy.
1206///
Chris Lattner8a307e82002-12-16 19:32:50 +00001207void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00001208 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00001209 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001210 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00001211 switch (Class) {
1212 case cFP: // Floating point multiply
Chris Lattner3e130a22003-01-13 00:32:26 +00001213 BMI(BB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001214 return;
Chris Lattner0f1c4612003-06-21 17:16:58 +00001215 case cInt:
1216 case cShort:
Chris Lattnerc01d1232003-10-20 03:42:58 +00001217 BMI(BB, MBBI, Class == cInt ? X86::IMULrr32 : X86::IMULrr16, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00001218 .addReg(op0Reg).addReg(op1Reg);
1219 return;
1220 case cByte:
1221 // Must use the MUL instruction, which forces use of AL...
1222 BMI(MBB, MBBI, X86::MOVrr8, 1, X86::AL).addReg(op0Reg);
1223 BMI(MBB, MBBI, X86::MULr8, 1).addReg(op1Reg);
1224 BMI(MBB, MBBI, X86::MOVrr8, 1, DestReg).addReg(X86::AL);
1225 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001226 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001227 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00001228 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001229}
1230
Chris Lattnerb2acc512003-10-19 21:09:10 +00001231// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1232// returns zero when the input is not exactly a power of two.
1233static unsigned ExactLog2(unsigned Val) {
1234 if (Val == 0) return 0;
1235 unsigned Count = 0;
1236 while (Val != 1) {
1237 if (Val & 1) return 0;
1238 Val >>= 1;
1239 ++Count;
1240 }
1241 return Count+1;
1242}
1243
1244void ISel::doMultiplyConst(MachineBasicBlock *MBB,
1245 MachineBasicBlock::iterator &IP,
1246 unsigned DestReg, const Type *DestTy,
1247 unsigned op0Reg, unsigned ConstRHS) {
1248 unsigned Class = getClass(DestTy);
1249
1250 // If the element size is exactly a power of 2, use a shift to get it.
1251 if (unsigned Shift = ExactLog2(ConstRHS)) {
1252 switch (Class) {
1253 default: assert(0 && "Unknown class for this function!");
1254 case cByte:
1255 BMI(MBB, IP, X86::SHLir32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
1256 return;
1257 case cShort:
1258 BMI(MBB, IP, X86::SHLir32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
1259 return;
1260 case cInt:
1261 BMI(MBB, IP, X86::SHLir32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
1262 return;
1263 }
1264 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00001265
1266 if (Class == cShort) {
1267 BMI(MBB, IP, X86::IMULri16, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
1268 return;
1269 } else if (Class == cInt) {
1270 BMI(MBB, IP, X86::IMULri32, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
1271 return;
1272 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001273
1274 // Most general case, emit a normal multiply...
1275 static const unsigned MOVirTab[] = {
1276 X86::MOVir8, X86::MOVir16, X86::MOVir32
1277 };
1278
1279 unsigned TmpReg = makeAnotherReg(DestTy);
1280 BMI(MBB, IP, MOVirTab[Class], 1, TmpReg).addZImm(ConstRHS);
1281
1282 // Emit a MUL to multiply the register holding the index by
1283 // elementSize, putting the result in OffsetReg.
1284 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
1285}
1286
Chris Lattnerca9671d2002-11-02 20:28:58 +00001287/// visitMul - Multiplies are not simple binary operators because they must deal
1288/// with the EAX register explicitly.
1289///
1290void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +00001291 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00001292 unsigned DestReg = getReg(I);
1293
1294 // Simple scalar multiply?
1295 if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001296 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
1297 unsigned Val = (unsigned)CI->getRawValue(); // Cannot be 64-bit constant
1298 MachineBasicBlock::iterator MBBI = BB->end();
1299 doMultiplyConst(BB, MBBI, DestReg, I.getType(), Op0Reg, Val);
1300 } else {
1301 unsigned Op1Reg = getReg(I.getOperand(1));
1302 MachineBasicBlock::iterator MBBI = BB->end();
1303 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
1304 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001305 } else {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001306 unsigned Op1Reg = getReg(I.getOperand(1));
1307
Chris Lattner3e130a22003-01-13 00:32:26 +00001308 // Long value. We have to do things the hard way...
1309 // Multiply the two low parts... capturing carry into EDX
1310 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(Op0Reg);
1311 BuildMI(BB, X86::MULr32, 1).addReg(Op1Reg); // AL*BL
1312
1313 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
1314 BuildMI(BB, X86::MOVrr32, 1, DestReg).addReg(X86::EAX); // AL*BL
1315 BuildMI(BB, X86::MOVrr32, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32
1316
1317 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001318 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
Chris Lattnerc01d1232003-10-20 03:42:58 +00001319 BMI(BB, MBBI, X86::IMULrr32, 2, AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001320
1321 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
1322 BuildMI(BB, X86::ADDrr32, 2, // AH*BL+(AL*BL >> 32)
1323 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
1324
1325 MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001326 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattnerc01d1232003-10-20 03:42:58 +00001327 BMI(BB, MBBI, X86::IMULrr32, 2, ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001328
1329 BuildMI(BB, X86::ADDrr32, 2, // AL*BH + AH*BL + (AL*BL >> 32)
1330 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
1331 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001332}
Chris Lattnerca9671d2002-11-02 20:28:58 +00001333
Chris Lattner06925362002-11-17 21:56:38 +00001334
Chris Lattnerf01729e2002-11-02 20:54:46 +00001335/// visitDivRem - Handle division and remainder instructions... these
1336/// instruction both require the same instructions to be generated, they just
1337/// select the result from a different register. Note that both of these
1338/// instructions work differently for signed and unsigned operands.
1339///
1340void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001341 unsigned Class = getClass(I.getType());
1342 unsigned Op0Reg, Op1Reg, ResultReg = getReg(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001343
1344 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001345 case cFP: // Floating point divide
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001346 if (I.getOpcode() == Instruction::Div) {
1347 Op0Reg = getReg(I.getOperand(0));
1348 Op1Reg = getReg(I.getOperand(1));
Chris Lattner94af4142002-12-25 05:13:53 +00001349 BuildMI(BB, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001350 } else { // Floating point remainder...
Chris Lattner3e130a22003-01-13 00:32:26 +00001351 MachineInstr *TheCall =
1352 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
1353 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001354 Args.push_back(ValueRecord(I.getOperand(0)));
1355 Args.push_back(ValueRecord(I.getOperand(1)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001356 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
1357 }
Chris Lattner94af4142002-12-25 05:13:53 +00001358 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001359 case cLong: {
1360 static const char *FnName[] =
1361 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
1362
1363 unsigned NameIdx = I.getType()->isUnsigned()*2;
1364 NameIdx += I.getOpcode() == Instruction::Div;
1365 MachineInstr *TheCall =
1366 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
1367
1368 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001369 Args.push_back(ValueRecord(I.getOperand(0)));
1370 Args.push_back(ValueRecord(I.getOperand(1)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001371 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
1372 return;
1373 }
1374 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00001375 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00001376 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00001377 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001378
1379 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
1380 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Chris Lattner7b52c032003-06-22 03:31:18 +00001381 static const unsigned SarOpcode[]={ X86::SARir8, X86::SARir16, X86::SARir32 };
Chris Lattnerf01729e2002-11-02 20:54:46 +00001382 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
1383 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
1384
1385 static const unsigned DivOpcode[][4] = {
Chris Lattner3e130a22003-01-13 00:32:26 +00001386 { X86::DIVr8 , X86::DIVr16 , X86::DIVr32 , 0 }, // Unsigned division
1387 { X86::IDIVr8, X86::IDIVr16, X86::IDIVr32, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00001388 };
1389
1390 bool isSigned = I.getType()->isSigned();
1391 unsigned Reg = Regs[Class];
1392 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00001393
1394 // Put the first operand into one of the A registers...
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001395 Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +00001396 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
1397
1398 if (isSigned) {
1399 // Emit a sign extension instruction...
Chris Lattner7b52c032003-06-22 03:31:18 +00001400 unsigned ShiftResult = makeAnotherReg(I.getType());
1401 BuildMI(BB, SarOpcode[Class], 2, ShiftResult).addReg(Op0Reg).addZImm(31);
1402 BuildMI(BB, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001403 } else {
1404 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
1405 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
1406 }
1407
Chris Lattner06925362002-11-17 21:56:38 +00001408 // Emit the appropriate divide or remainder instruction...
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001409 Op1Reg = getReg(I.getOperand(1));
Chris Lattner92845e32002-11-21 18:54:29 +00001410 BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00001411
Chris Lattnerf01729e2002-11-02 20:54:46 +00001412 // Figure out which register we want to pick the result out of...
1413 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
1414
Chris Lattnerf01729e2002-11-02 20:54:46 +00001415 // Put the result into the destination register...
Chris Lattner94af4142002-12-25 05:13:53 +00001416 BuildMI(BB, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00001417}
Chris Lattnere2954c82002-11-02 20:04:26 +00001418
Chris Lattner06925362002-11-17 21:56:38 +00001419
Brian Gaekea1719c92002-10-31 23:03:59 +00001420/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1421/// for constant immediate shift values, and for constant immediate
1422/// shift values equal to 1. Even the general case is sort of special,
1423/// because the shift amount has to be in CL, not just any old register.
1424///
Chris Lattner3e130a22003-01-13 00:32:26 +00001425void ISel::visitShiftInst(ShiftInst &I) {
1426 unsigned SrcReg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +00001427 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +00001428 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Chris Lattner3e130a22003-01-13 00:32:26 +00001429 bool isSigned = I.getType()->isSigned();
1430 unsigned Class = getClass(I.getType());
1431
1432 static const unsigned ConstantOperand[][4] = {
1433 { X86::SHRir8, X86::SHRir16, X86::SHRir32, X86::SHRDir32 }, // SHR
1434 { X86::SARir8, X86::SARir16, X86::SARir32, X86::SHRDir32 }, // SAR
1435 { X86::SHLir8, X86::SHLir16, X86::SHLir32, X86::SHLDir32 }, // SHL
1436 { X86::SHLir8, X86::SHLir16, X86::SHLir32, X86::SHLDir32 }, // SAL = SHL
1437 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001438
Chris Lattner3e130a22003-01-13 00:32:26 +00001439 static const unsigned NonConstantOperand[][4] = {
1440 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32 }, // SHR
1441 { X86::SARrr8, X86::SARrr16, X86::SARrr32 }, // SAR
1442 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SHL
1443 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SAL = SHL
1444 };
Chris Lattner796df732002-11-02 00:44:25 +00001445
Chris Lattner3e130a22003-01-13 00:32:26 +00001446 // Longs, as usual, are handled specially...
1447 if (Class == cLong) {
1448 // If we have a constant shift, we can generate much more efficient code
1449 // than otherwise...
1450 //
1451 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getOperand(1))) {
1452 unsigned Amount = CUI->getValue();
1453 if (Amount < 32) {
1454 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
1455 if (isLeftShift) {
1456 BuildMI(BB, Opc[3], 3,
1457 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addZImm(Amount);
1458 BuildMI(BB, Opc[2], 2, DestReg).addReg(SrcReg).addZImm(Amount);
1459 } else {
1460 BuildMI(BB, Opc[3], 3,
1461 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addZImm(Amount);
1462 BuildMI(BB, Opc[2], 2, DestReg+1).addReg(SrcReg+1).addZImm(Amount);
1463 }
1464 } else { // Shifting more than 32 bits
1465 Amount -= 32;
1466 if (isLeftShift) {
1467 BuildMI(BB, X86::SHLir32, 2,DestReg+1).addReg(SrcReg).addZImm(Amount);
1468 BuildMI(BB, X86::MOVir32, 1,DestReg ).addZImm(0);
1469 } else {
1470 unsigned Opcode = isSigned ? X86::SARir32 : X86::SHRir32;
1471 BuildMI(BB, Opcode, 2, DestReg).addReg(SrcReg+1).addZImm(Amount);
1472 BuildMI(BB, X86::MOVir32, 1, DestReg+1).addZImm(0);
1473 }
1474 }
1475 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00001476 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1477
1478 if (!isLeftShift && isSigned) {
1479 // If this is a SHR of a Long, then we need to do funny sign extension
1480 // stuff. TmpReg gets the value to use as the high-part if we are
1481 // shifting more than 32 bits.
1482 BuildMI(BB, X86::SARir32, 2, TmpReg).addReg(SrcReg).addZImm(31);
1483 } else {
1484 // Other shifts use a fixed zero value if the shift is more than 32
1485 // bits.
1486 BuildMI(BB, X86::MOVir32, 1, TmpReg).addZImm(0);
1487 }
1488
1489 // Initialize CL with the shift amount...
1490 unsigned ShiftAmount = getReg(I.getOperand(1));
1491 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmount);
1492
1493 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
1494 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
1495 if (isLeftShift) {
1496 // TmpReg2 = shld inHi, inLo
1497 BuildMI(BB, X86::SHLDrr32, 2, TmpReg2).addReg(SrcReg+1).addReg(SrcReg);
1498 // TmpReg3 = shl inLo, CL
1499 BuildMI(BB, X86::SHLrr32, 1, TmpReg3).addReg(SrcReg);
1500
1501 // Set the flags to indicate whether the shift was by more than 32 bits.
1502 BuildMI(BB, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
1503
1504 // DestHi = (>32) ? TmpReg3 : TmpReg2;
1505 BuildMI(BB, X86::CMOVNErr32, 2,
1506 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
1507 // DestLo = (>32) ? TmpReg : TmpReg3;
1508 BuildMI(BB, X86::CMOVNErr32, 2, DestReg).addReg(TmpReg3).addReg(TmpReg);
1509 } else {
1510 // TmpReg2 = shrd inLo, inHi
1511 BuildMI(BB, X86::SHRDrr32, 2, TmpReg2).addReg(SrcReg).addReg(SrcReg+1);
1512 // TmpReg3 = s[ah]r inHi, CL
1513 BuildMI(BB, isSigned ? X86::SARrr32 : X86::SHRrr32, 1, TmpReg3)
1514 .addReg(SrcReg+1);
1515
1516 // Set the flags to indicate whether the shift was by more than 32 bits.
1517 BuildMI(BB, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
1518
1519 // DestLo = (>32) ? TmpReg3 : TmpReg2;
1520 BuildMI(BB, X86::CMOVNErr32, 2,
1521 DestReg).addReg(TmpReg2).addReg(TmpReg3);
1522
1523 // DestHi = (>32) ? TmpReg : TmpReg3;
1524 BuildMI(BB, X86::CMOVNErr32, 2,
1525 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
1526 }
Brian Gaekea1719c92002-10-31 23:03:59 +00001527 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001528 return;
1529 }
Chris Lattnere9913f22002-11-02 01:41:55 +00001530
Chris Lattner3e130a22003-01-13 00:32:26 +00001531 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getOperand(1))) {
1532 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
1533 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001534
Chris Lattner3e130a22003-01-13 00:32:26 +00001535 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
1536 BuildMI(BB, Opc[Class], 2, DestReg).addReg(SrcReg).addZImm(CUI->getValue());
1537 } else { // The shift amount is non-constant.
1538 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001539
Chris Lattner3e130a22003-01-13 00:32:26 +00001540 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
1541 BuildMI(BB, Opc[Class], 1, DestReg).addReg(SrcReg);
1542 }
1543}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001544
Chris Lattner3e130a22003-01-13 00:32:26 +00001545
Chris Lattner3e130a22003-01-13 00:32:26 +00001546/// EmitByteSwap - Byteswap SrcReg into DestReg.
1547///
1548void ISel::EmitByteSwap(unsigned DestReg, unsigned SrcReg, unsigned Class) {
1549 // Emit the byte swap instruction...
1550 switch (Class) {
1551 case cByte:
Misha Brukmanbaf06072003-04-22 17:54:23 +00001552 // No byteswap necessary for 8 bit value...
Chris Lattner3e130a22003-01-13 00:32:26 +00001553 BuildMI(BB, X86::MOVrr8, 1, DestReg).addReg(SrcReg);
1554 break;
1555 case cInt:
1556 // Use the 32 bit bswap instruction to do a 32 bit swap...
1557 BuildMI(BB, X86::BSWAPr32, 1, DestReg).addReg(SrcReg);
1558 break;
1559
1560 case cShort:
1561 // For 16 bit we have to use an xchg instruction, because there is no
Misha Brukmanbaf06072003-04-22 17:54:23 +00001562 // 16-bit bswap. XCHG is necessarily not in SSA form, so we force things
Chris Lattner3e130a22003-01-13 00:32:26 +00001563 // into AX to do the xchg.
1564 //
1565 BuildMI(BB, X86::MOVrr16, 1, X86::AX).addReg(SrcReg);
1566 BuildMI(BB, X86::XCHGrr8, 2).addReg(X86::AL, MOTy::UseAndDef)
1567 .addReg(X86::AH, MOTy::UseAndDef);
1568 BuildMI(BB, X86::MOVrr16, 1, DestReg).addReg(X86::AX);
1569 break;
1570 default: assert(0 && "Cannot byteswap this class!");
1571 }
Brian Gaekea1719c92002-10-31 23:03:59 +00001572}
1573
Chris Lattner06925362002-11-17 21:56:38 +00001574
Chris Lattner6fc3c522002-11-17 21:11:55 +00001575/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00001576/// instruction. The load and store instructions are the only place where we
1577/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00001578///
1579void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001580 unsigned SrcAddrReg = getReg(I.getOperand(0));
1581 unsigned DestReg = getReg(I);
Chris Lattnere8f0d922002-12-24 00:03:11 +00001582
Brian Gaekebfedb912003-07-17 21:30:06 +00001583 unsigned Class = getClassB(I.getType());
Chris Lattner6ac1d712003-10-20 04:48:06 +00001584
1585 if (Class == cLong) {
1586 addDirectMem(BuildMI(BB, X86::MOVmr32, 4, DestReg), SrcAddrReg);
1587 addRegOffset(BuildMI(BB, X86::MOVmr32, 4, DestReg+1), SrcAddrReg, 4);
Chris Lattner94af4142002-12-25 05:13:53 +00001588 return;
1589 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00001590
Chris Lattner6ac1d712003-10-20 04:48:06 +00001591 static const unsigned Opcodes[] = {
1592 X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FLDr32
Chris Lattner3e130a22003-01-13 00:32:26 +00001593 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00001594 unsigned Opcode = Opcodes[Class];
1595 if (I.getType() == Type::DoubleTy) Opcode = X86::FLDr64;
1596 addDirectMem(BuildMI(BB, Opcode, 4, DestReg), SrcAddrReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001597}
1598
Chris Lattner6fc3c522002-11-17 21:11:55 +00001599/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
1600/// instruction.
1601///
1602void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001603 unsigned ValReg = getReg(I.getOperand(0));
1604 unsigned AddressReg = getReg(I.getOperand(1));
Chris Lattner6c09db22003-10-20 04:11:23 +00001605
1606 const Type *ValTy = I.getOperand(0)->getType();
1607 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00001608
1609 if (Class == cLong) {
Chris Lattner6c09db22003-10-20 04:11:23 +00001610 addDirectMem(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg).addReg(ValReg);
1611 addRegOffset(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg,4).addReg(ValReg+1);
Chris Lattner94af4142002-12-25 05:13:53 +00001612 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001613 }
1614
Chris Lattner6ac1d712003-10-20 04:48:06 +00001615 static const unsigned Opcodes[] = {
1616 X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FSTr32
1617 };
1618 unsigned Opcode = Opcodes[Class];
1619 if (ValTy == Type::DoubleTy) Opcode = X86::FSTr64;
1620 addDirectMem(BuildMI(BB, Opcode, 1+4), AddressReg).addReg(ValReg);
Chris Lattner6fc3c522002-11-17 21:11:55 +00001621}
1622
1623
Brian Gaekec11232a2002-11-26 10:43:30 +00001624/// visitCastInst - Here we have various kinds of copying with or without
1625/// sign extension going on.
Chris Lattner3e130a22003-01-13 00:32:26 +00001626void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00001627 Value *Op = CI.getOperand(0);
1628 // If this is a cast from a 32-bit integer to a Long type, and the only uses
1629 // of the case are GEP instructions, then the cast does not need to be
1630 // generated explicitly, it will be folded into the GEP.
1631 if (CI.getType() == Type::LongTy &&
1632 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
1633 bool AllUsesAreGEPs = true;
1634 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
1635 if (!isa<GetElementPtrInst>(*I)) {
1636 AllUsesAreGEPs = false;
1637 break;
1638 }
1639
1640 // No need to codegen this cast if all users are getelementptr instrs...
1641 if (AllUsesAreGEPs) return;
1642 }
1643
Chris Lattner548f61d2003-04-23 17:22:12 +00001644 unsigned DestReg = getReg(CI);
1645 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00001646 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00001647}
1648
1649/// emitCastOperation - Common code shared between visitCastInst and
1650/// constant expression cast support.
1651void ISel::emitCastOperation(MachineBasicBlock *BB,
1652 MachineBasicBlock::iterator &IP,
1653 Value *Src, const Type *DestTy,
1654 unsigned DestReg) {
Chris Lattner3907d112003-04-23 17:57:48 +00001655 unsigned SrcReg = getReg(Src, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001656 const Type *SrcTy = Src->getType();
1657 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00001658 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00001659
Chris Lattner3e130a22003-01-13 00:32:26 +00001660 // Implement casts to bool by using compare on the operand followed by set if
1661 // not zero on the result.
1662 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00001663 switch (SrcClass) {
1664 case cByte:
1665 BMI(BB, IP, X86::TESTrr8, 2).addReg(SrcReg).addReg(SrcReg);
1666 break;
1667 case cShort:
1668 BMI(BB, IP, X86::TESTrr16, 2).addReg(SrcReg).addReg(SrcReg);
1669 break;
1670 case cInt:
1671 BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg).addReg(SrcReg);
1672 break;
1673 case cLong: {
1674 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1675 BMI(BB, IP, X86::ORrr32, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
1676 break;
1677 }
1678 case cFP:
1679 assert(0 && "FIXME: implement cast FP to bool");
1680 abort();
1681 }
1682
1683 // If the zero flag is not set, then the value is true, set the byte to
1684 // true.
Chris Lattner548f61d2003-04-23 17:22:12 +00001685 BMI(BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00001686 return;
1687 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001688
1689 static const unsigned RegRegMove[] = {
1690 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV, X86::MOVrr32
1691 };
1692
1693 // Implement casts between values of the same type class (as determined by
1694 // getClass) by using a register-to-register move.
1695 if (SrcClass == DestClass) {
1696 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001697 BMI(BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001698 } else if (SrcClass == cFP) {
1699 if (SrcTy == Type::FloatTy) { // double -> float
1700 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattner548f61d2003-04-23 17:22:12 +00001701 BMI(BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001702 } else { // float -> double
1703 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
1704 "Unknown cFP member!");
1705 // Truncate from double to float by storing to memory as short, then
1706 // reading it back.
1707 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
1708 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Chris Lattner548f61d2003-04-23 17:22:12 +00001709 addFrameReference(BMI(BB, IP, X86::FSTr32, 5), FrameIdx).addReg(SrcReg);
1710 addFrameReference(BMI(BB, IP, X86::FLDr32, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001711 }
1712 } else if (SrcClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001713 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
1714 BMI(BB, IP, X86::MOVrr32, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001715 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00001716 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00001717 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00001718 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001719 return;
1720 }
1721
1722 // Handle cast of SMALLER int to LARGER int using a move with sign extension
1723 // or zero extension, depending on whether the source type was signed.
1724 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
1725 SrcClass < DestClass) {
1726 bool isLong = DestClass == cLong;
1727 if (isLong) DestClass = cInt;
1728
1729 static const unsigned Opc[][4] = {
1730 { X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, X86::MOVrr32 }, // s
1731 { X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16, X86::MOVrr32 } // u
1732 };
1733
1734 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattner548f61d2003-04-23 17:22:12 +00001735 BMI(BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
1736 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001737
1738 if (isLong) { // Handle upper 32 bits as appropriate...
1739 if (isUnsigned) // Zero out top bits...
Chris Lattner548f61d2003-04-23 17:22:12 +00001740 BMI(BB, IP, X86::MOVir32, 1, DestReg+1).addZImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001741 else // Sign extend bottom half...
Chris Lattner548f61d2003-04-23 17:22:12 +00001742 BMI(BB, IP, X86::SARir32, 2, DestReg+1).addReg(DestReg).addZImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00001743 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001744 return;
1745 }
1746
1747 // Special case long -> int ...
1748 if (SrcClass == cLong && DestClass == cInt) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001749 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001750 return;
1751 }
1752
1753 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
1754 // move out of AX or AL.
1755 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
1756 && SrcClass > DestClass) {
1757 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattner548f61d2003-04-23 17:22:12 +00001758 BMI(BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
1759 BMI(BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00001760 return;
1761 }
1762
1763 // Handle casts from integer to floating point now...
1764 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00001765 // Promote the integer to a type supported by FLD. We do this because there
1766 // are no unsigned FLD instructions, so we must promote an unsigned value to
1767 // a larger signed value, then use FLD on the larger value.
1768 //
1769 const Type *PromoteType = 0;
1770 unsigned PromoteOpcode;
1771 switch (SrcTy->getPrimitiveID()) {
1772 case Type::BoolTyID:
1773 case Type::SByteTyID:
1774 // We don't have the facilities for directly loading byte sized data from
1775 // memory (even signed). Promote it to 16 bits.
1776 PromoteType = Type::ShortTy;
1777 PromoteOpcode = X86::MOVSXr16r8;
1778 break;
1779 case Type::UByteTyID:
1780 PromoteType = Type::ShortTy;
1781 PromoteOpcode = X86::MOVZXr16r8;
1782 break;
1783 case Type::UShortTyID:
1784 PromoteType = Type::IntTy;
1785 PromoteOpcode = X86::MOVZXr32r16;
1786 break;
1787 case Type::UIntTyID: {
1788 // Make a 64 bit temporary... and zero out the top of it...
1789 unsigned TmpReg = makeAnotherReg(Type::LongTy);
1790 BMI(BB, IP, X86::MOVrr32, 1, TmpReg).addReg(SrcReg);
1791 BMI(BB, IP, X86::MOVir32, 1, TmpReg+1).addZImm(0);
1792 SrcTy = Type::LongTy;
1793 SrcClass = cLong;
1794 SrcReg = TmpReg;
1795 break;
1796 }
1797 case Type::ULongTyID:
1798 assert("FIXME: not implemented: cast ulong X to fp type!");
1799 default: // No promotion needed...
1800 break;
1801 }
1802
1803 if (PromoteType) {
1804 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner548f61d2003-04-23 17:22:12 +00001805 BMI(BB, IP, SrcTy->isSigned() ? X86::MOVSXr16r8 : X86::MOVZXr16r8,
1806 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00001807 SrcTy = PromoteType;
1808 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00001809 SrcReg = TmpReg;
1810 }
1811
1812 // Spill the integer to memory and reload it from there...
1813 int FrameIdx =
1814 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
1815
1816 if (SrcClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001817 addFrameReference(BMI(BB, IP, X86::MOVrm32, 5), FrameIdx).addReg(SrcReg);
1818 addFrameReference(BMI(BB, IP, X86::MOVrm32, 5),
Chris Lattner3e130a22003-01-13 00:32:26 +00001819 FrameIdx, 4).addReg(SrcReg+1);
1820 } else {
1821 static const unsigned Op1[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001822 addFrameReference(BMI(BB, IP, Op1[SrcClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001823 }
1824
1825 static const unsigned Op2[] =
Chris Lattner4d5a50a2003-05-12 20:36:13 +00001826 { 0/*byte*/, X86::FILDr16, X86::FILDr32, 0/*FP*/, X86::FILDr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001827 addFrameReference(BMI(BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001828 return;
1829 }
1830
1831 // Handle casts from floating point to integer now...
1832 if (SrcClass == cFP) {
1833 // Change the floating point control register to use "round towards zero"
1834 // mode when truncating to an integer value.
1835 //
1836 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Chris Lattner548f61d2003-04-23 17:22:12 +00001837 addFrameReference(BMI(BB, IP, X86::FNSTCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001838
1839 // Load the old value of the high byte of the control word...
1840 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Chris Lattner548f61d2003-04-23 17:22:12 +00001841 addFrameReference(BMI(BB, IP, X86::MOVmr8, 4, HighPartOfCW), CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001842
1843 // Set the high part to be round to zero...
Chris Lattner548f61d2003-04-23 17:22:12 +00001844 addFrameReference(BMI(BB, IP, X86::MOVim8, 5), CWFrameIdx, 1).addZImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00001845
1846 // Reload the modified control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00001847 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001848
1849 // Restore the memory image of control word to original value
Chris Lattner548f61d2003-04-23 17:22:12 +00001850 addFrameReference(BMI(BB, IP, X86::MOVrm8, 5),
Chris Lattner3e130a22003-01-13 00:32:26 +00001851 CWFrameIdx, 1).addReg(HighPartOfCW);
1852
1853 // We don't have the facilities for directly storing byte sized data to
1854 // memory. Promote it to 16 bits. We also must promote unsigned values to
1855 // larger classes because we only have signed FP stores.
1856 unsigned StoreClass = DestClass;
1857 const Type *StoreTy = DestTy;
1858 if (StoreClass == cByte || DestTy->isUnsigned())
1859 switch (StoreClass) {
1860 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
1861 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
1862 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00001863 // The following treatment of cLong may not be perfectly right,
1864 // but it survives chains of casts of the form
1865 // double->ulong->double.
1866 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001867 default: assert(0 && "Unknown store class!");
1868 }
1869
1870 // Spill the integer to memory and reload it from there...
1871 int FrameIdx =
1872 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
1873
1874 static const unsigned Op1[] =
1875 { 0, X86::FISTr16, X86::FISTr32, 0, X86::FISTPr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001876 addFrameReference(BMI(BB, IP, Op1[StoreClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001877
1878 if (DestClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001879 addFrameReference(BMI(BB, IP, X86::MOVmr32, 4, DestReg), FrameIdx);
1880 addFrameReference(BMI(BB, IP, X86::MOVmr32, 4, DestReg+1), FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00001881 } else {
1882 static const unsigned Op2[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001883 addFrameReference(BMI(BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001884 }
1885
1886 // Reload the original control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00001887 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001888 return;
1889 }
1890
Brian Gaeked474e9c2002-12-06 10:49:33 +00001891 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00001892 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00001893 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00001894}
Brian Gaekea1719c92002-10-31 23:03:59 +00001895
Chris Lattner73815062003-10-18 05:56:40 +00001896/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00001897///
Chris Lattner73815062003-10-18 05:56:40 +00001898void ISel::visitVANextInst(VANextInst &I) {
1899 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00001900 unsigned DestReg = getReg(I);
1901
Chris Lattnereca195e2003-05-08 19:44:13 +00001902 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00001903 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001904 default:
1905 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00001906 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001907 return;
1908 case Type::PointerTyID:
1909 case Type::UIntTyID:
1910 case Type::IntTyID:
1911 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00001912 break;
1913 case Type::ULongTyID:
1914 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00001915 case Type::DoubleTyID:
1916 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00001917 break;
1918 }
1919
1920 // Increment the VAList pointer...
Chris Lattner73815062003-10-18 05:56:40 +00001921 BuildMI(BB, X86::ADDri32, 2, DestReg).addReg(VAList).addZImm(Size);
1922}
Chris Lattnereca195e2003-05-08 19:44:13 +00001923
Chris Lattner73815062003-10-18 05:56:40 +00001924void ISel::visitVAArgInst(VAArgInst &I) {
1925 unsigned VAList = getReg(I.getOperand(0));
1926 unsigned DestReg = getReg(I);
1927
1928 switch (I.getType()->getPrimitiveID()) {
1929 default:
1930 std::cerr << I;
1931 assert(0 && "Error: bad type for va_next instruction!");
1932 return;
1933 case Type::PointerTyID:
1934 case Type::UIntTyID:
1935 case Type::IntTyID:
1936 addDirectMem(BuildMI(BB, X86::MOVmr32, 4, DestReg), VAList);
1937 break;
1938 case Type::ULongTyID:
1939 case Type::LongTyID:
1940 addDirectMem(BuildMI(BB, X86::MOVmr32, 4, DestReg), VAList);
1941 addRegOffset(BuildMI(BB, X86::MOVmr32, 4, DestReg+1), VAList, 4);
1942 break;
1943 case Type::DoubleTyID:
1944 addDirectMem(BuildMI(BB, X86::FLDr64, 4, DestReg), VAList);
1945 break;
1946 }
Chris Lattnereca195e2003-05-08 19:44:13 +00001947}
1948
1949
Chris Lattner3e130a22003-01-13 00:32:26 +00001950void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
1951 unsigned outputReg = getReg(I);
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001952 MachineBasicBlock::iterator MI = BB->end();
1953 emitGEPOperation(BB, MI, I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001954 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001955}
1956
Brian Gaeke71794c02002-12-13 11:22:48 +00001957void ISel::emitGEPOperation(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001958 MachineBasicBlock::iterator &IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +00001959 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +00001960 User::op_iterator IdxEnd, unsigned TargetReg) {
1961 const TargetData &TD = TM.getTargetData();
1962 const Type *Ty = Src->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +00001963 unsigned BaseReg = getReg(Src, MBB, IP);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001964
Brian Gaeke20244b72002-12-12 15:33:40 +00001965 // GEPs have zero or more indices; we must perform a struct access
1966 // or array access for each one.
Chris Lattnerc0812d82002-12-13 06:56:29 +00001967 for (GetElementPtrInst::op_iterator oi = IdxBegin,
1968 oe = IdxEnd; oi != oe; ++oi) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001969 Value *idx = *oi;
Chris Lattner3e130a22003-01-13 00:32:26 +00001970 unsigned NextReg = BaseReg;
Chris Lattner065faeb2002-12-28 20:24:02 +00001971 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001972 // It's a struct access. idx is the index into the structure,
1973 // which names the field. This index must have ubyte type.
Chris Lattner065faeb2002-12-28 20:24:02 +00001974 const ConstantUInt *CUI = cast<ConstantUInt>(idx);
1975 assert(CUI->getType() == Type::UByteTy
Brian Gaeke20244b72002-12-12 15:33:40 +00001976 && "Funny-looking structure index in GEP");
1977 // Use the TargetData structure to pick out what the layout of
1978 // the structure is in memory. Since the structure index must
1979 // be constant, we can get its value and use it to find the
1980 // right byte offset from the StructLayout class's list of
1981 // structure member offsets.
Chris Lattnere8f0d922002-12-24 00:03:11 +00001982 unsigned idxValue = CUI->getValue();
Chris Lattner3e130a22003-01-13 00:32:26 +00001983 unsigned FieldOff = TD.getStructLayout(StTy)->MemberOffsets[idxValue];
1984 if (FieldOff) {
1985 NextReg = makeAnotherReg(Type::UIntTy);
1986 // Emit an ADD to add FieldOff to the basePtr.
1987 BMI(MBB, IP, X86::ADDri32, 2,NextReg).addReg(BaseReg).addZImm(FieldOff);
1988 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001989 // The next type is the member of the structure selected by the
1990 // index.
Chris Lattner065faeb2002-12-28 20:24:02 +00001991 Ty = StTy->getElementTypes()[idxValue];
1992 } else if (const SequentialType *SqTy = cast<SequentialType>(Ty)) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001993 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner8a307e82002-12-16 19:32:50 +00001994
Brian Gaeke20244b72002-12-12 15:33:40 +00001995 // idx is the index into the array. Unlike with structure
1996 // indices, we may not know its actual value at code-generation
1997 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00001998 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
1999
Chris Lattnerf5854472003-06-21 16:01:24 +00002000 // Most GEP instructions use a [cast (int/uint) to LongTy] as their
2001 // operand on X86. Handle this case directly now...
2002 if (CastInst *CI = dyn_cast<CastInst>(idx))
2003 if (CI->getOperand(0)->getType() == Type::IntTy ||
2004 CI->getOperand(0)->getType() == Type::UIntTy)
2005 idx = CI->getOperand(0);
2006
Chris Lattner3e130a22003-01-13 00:32:26 +00002007 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00002008 // must find the size of the pointed-to type (Not coincidentally, the next
2009 // type is the type of the elements in the array).
2010 Ty = SqTy->getElementType();
2011 unsigned elementSize = TD.getTypeSize(Ty);
2012
2013 // If idxReg is a constant, we don't need to perform the multiply!
2014 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002015 if (!CSI->isNullValue()) {
Chris Lattner8a307e82002-12-16 19:32:50 +00002016 unsigned Offset = elementSize*CSI->getValue();
Chris Lattner3e130a22003-01-13 00:32:26 +00002017 NextReg = makeAnotherReg(Type::UIntTy);
2018 BMI(MBB, IP, X86::ADDri32, 2,NextReg).addReg(BaseReg).addZImm(Offset);
Chris Lattner8a307e82002-12-16 19:32:50 +00002019 }
2020 } else if (elementSize == 1) {
2021 // If the element size is 1, we don't have to multiply, just add
2022 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002023 NextReg = makeAnotherReg(Type::UIntTy);
2024 BMI(MBB, IP, X86::ADDrr32, 2, NextReg).addReg(BaseReg).addReg(idxReg);
Chris Lattner8a307e82002-12-16 19:32:50 +00002025 } else {
2026 unsigned idxReg = getReg(idx, MBB, IP);
2027 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002028
2029 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
2030
Chris Lattner8a307e82002-12-16 19:32:50 +00002031 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3e130a22003-01-13 00:32:26 +00002032 NextReg = makeAnotherReg(Type::UIntTy);
2033 BMI(MBB, IP, X86::ADDrr32, 2,NextReg).addReg(BaseReg).addReg(OffsetReg);
Chris Lattner8a307e82002-12-16 19:32:50 +00002034 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002035 }
2036 // Now that we are here, further indices refer to subtypes of this
Chris Lattner3e130a22003-01-13 00:32:26 +00002037 // one, so we don't need to worry about BaseReg itself, anymore.
2038 BaseReg = NextReg;
Brian Gaeke20244b72002-12-12 15:33:40 +00002039 }
2040 // After we have processed all the indices, the result is left in
Chris Lattner3e130a22003-01-13 00:32:26 +00002041 // BaseReg. Move it to the register where we were expected to
Brian Gaeke20244b72002-12-12 15:33:40 +00002042 // put the answer. A 32-bit move should do it, because we are in
2043 // ILP32 land.
Chris Lattner3e130a22003-01-13 00:32:26 +00002044 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg);
Brian Gaeke20244b72002-12-12 15:33:40 +00002045}
2046
2047
Chris Lattner065faeb2002-12-28 20:24:02 +00002048/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2049/// frame manager, otherwise do it the hard way.
2050///
2051void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00002052 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00002053 const Type *Ty = I.getAllocatedType();
2054 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2055
2056 // If this is a fixed size alloca in the entry block for the function,
2057 // statically stack allocate the space.
2058 //
2059 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
2060 if (I.getParent() == I.getParent()->getParent()->begin()) {
2061 TySize *= CUI->getValue(); // Get total allocated size...
2062 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
2063
2064 // Create a new stack object using the frame manager...
2065 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
2066 addFrameReference(BuildMI(BB, X86::LEAr32, 5, getReg(I)), FrameIdx);
2067 return;
2068 }
2069 }
2070
2071 // Create a register to hold the temporary result of multiplying the type size
2072 // constant by the variable amount.
2073 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2074 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00002075
2076 // TotalSizeReg = mul <numelements>, <TypeSize>
2077 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002078 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002079
2080 // AddedSize = add <TotalSizeReg>, 15
2081 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2082 BuildMI(BB, X86::ADDri32, 2, AddedSizeReg).addReg(TotalSizeReg).addZImm(15);
2083
2084 // AlignedSize = and <AddedSize>, ~15
2085 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2086 BuildMI(BB, X86::ANDri32, 2, AlignedSize).addReg(AddedSizeReg).addZImm(~15);
2087
Brian Gaekee48ec012002-12-13 06:46:31 +00002088 // Subtract size from stack pointer, thereby allocating some space.
Chris Lattner3e130a22003-01-13 00:32:26 +00002089 BuildMI(BB, X86::SUBrr32, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002090
Brian Gaekee48ec012002-12-13 06:46:31 +00002091 // Put a pointer to the space into the result register, by copying
2092 // the stack pointer.
Chris Lattner065faeb2002-12-28 20:24:02 +00002093 BuildMI(BB, X86::MOVrr32, 1, getReg(I)).addReg(X86::ESP);
2094
Misha Brukman48196b32003-05-03 02:18:17 +00002095 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00002096 // object.
2097 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00002098}
Chris Lattner3e130a22003-01-13 00:32:26 +00002099
2100/// visitMallocInst - Malloc instructions are code generated into direct calls
2101/// to the library malloc.
2102///
2103void ISel::visitMallocInst(MallocInst &I) {
2104 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2105 unsigned Arg;
2106
2107 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2108 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2109 } else {
2110 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002111 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00002112 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002113 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00002114 }
2115
2116 std::vector<ValueRecord> Args;
2117 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2118 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
2119 1).addExternalSymbol("malloc", true);
2120 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
2121}
2122
2123
2124/// visitFreeInst - Free instructions are code gen'd to call the free libc
2125/// function.
2126///
2127void ISel::visitFreeInst(FreeInst &I) {
2128 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002129 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00002130 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
2131 1).addExternalSymbol("free", true);
2132 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
2133}
2134
Brian Gaeke20244b72002-12-12 15:33:40 +00002135
Chris Lattnerd281de22003-07-26 23:49:58 +00002136/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002137/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00002138/// generated code sucks but the implementation is nice and simple.
2139///
Brian Gaeke19df3872003-08-13 18:18:15 +00002140FunctionPass *createX86SimpleInstructionSelector(TargetMachine &TM) {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002141 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00002142}