blob: 3d06163dc6aa1cb535c2b9071711bc15ff729768 [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 Lattner6fc3c522002-11-17 21:11:55 +000015#include "X86InstrBuilder.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000016#include "X86InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Function.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000020#include "llvm/Instructions.h"
Chris Lattnereca195e2003-05-08 19:44:13 +000021#include "llvm/Intrinsics.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000022#include "llvm/Pass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner341a9372002-10-29 17:43:55 +000025#include "llvm/CodeGen/MachineFunction.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner94af4142002-12-25 05:13:53 +000027#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000028#include "llvm/Target/MRegisterInfo.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000029#include "llvm/Target/TargetMachine.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,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000139 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,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000149 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) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000247 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
248 // Create the lower part
249 F->getSSARegMap()->createVirtualRegister(RC);
250 // Create the upper part.
251 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000252 }
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) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000467 Opcode = X86::FLDr32;
468 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000469 } else {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000470 Opcode = X86::FLDr64;
471 FI = MFI->CreateFixedObject(8, ArgOffset);
472 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) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000513 LongPhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg+1);
514 MBB->insert(MBB->begin()+NumPHIs++, LongPhiMI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000515 }
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
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000561 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000562 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000563 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
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000829 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
830 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000831 break;
Chris Lattner94af4142002-12-25 05:13:53 +0000832 default:
Chris Lattner3e130a22003-01-13 00:32:26 +0000833 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +0000834 }
Chris Lattner43189d12002-11-17 20:07:45 +0000835 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +0000836 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000837}
838
Chris Lattner55f6fab2003-01-16 18:07:23 +0000839// getBlockAfter - Return the basic block which occurs lexically after the
840// specified one.
841static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
842 Function::iterator I = BB; ++I; // Get iterator to next block
843 return I != BB->getParent()->end() ? &*I : 0;
844}
845
Chris Lattner51b49a92002-11-02 19:45:49 +0000846/// visitBranchInst - Handle conditional and unconditional branches here. Note
847/// that since code layout is frozen at this point, that if we are trying to
848/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +0000849/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +0000850///
Chris Lattner94af4142002-12-25 05:13:53 +0000851void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +0000852 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
853
854 if (!BI.isConditional()) { // Unconditional branch?
855 if (BI.getSuccessor(0) != NextBB)
856 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +0000857 return;
858 }
859
860 // See if we can fold the setcc into the branch itself...
861 SetCondInst *SCI = canFoldSetCCIntoBranch(BI.getCondition());
862 if (SCI == 0) {
863 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
864 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +0000865 unsigned condReg = getReg(BI.getCondition());
Chris Lattner94af4142002-12-25 05:13:53 +0000866 BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0);
Chris Lattner55f6fab2003-01-16 18:07:23 +0000867 if (BI.getSuccessor(1) == NextBB) {
868 if (BI.getSuccessor(0) != NextBB)
869 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
870 } else {
871 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
872
873 if (BI.getSuccessor(0) != NextBB)
874 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
875 }
Chris Lattner6d40c192003-01-16 16:43:00 +0000876 return;
Chris Lattner94af4142002-12-25 05:13:53 +0000877 }
Chris Lattner6d40c192003-01-16 16:43:00 +0000878
879 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +0000880 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000881 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000882
883 const Type *CompTy = SCI->getOperand(0)->getType();
884 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +0000885
Chris Lattnerb2acc512003-10-19 21:09:10 +0000886
Chris Lattner6d40c192003-01-16 16:43:00 +0000887 // LLVM -> X86 signed X86 unsigned
888 // ----- ---------- ------------
889 // seteq -> je je
890 // setne -> jne jne
891 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000892 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +0000893 // setgt -> jg ja
894 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000895 // ----
896 // js // Used by comparison with 0 optimization
897 // jns
898
899 static const unsigned OpcodeTab[2][8] = {
900 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
901 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
902 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +0000903 };
904
Chris Lattner55f6fab2003-01-16 18:07:23 +0000905 if (BI.getSuccessor(0) != NextBB) {
906 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
907 if (BI.getSuccessor(1) != NextBB)
908 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
909 } else {
910 // Change to the inverse condition...
911 if (BI.getSuccessor(1) != NextBB) {
912 OpNum ^= 1;
913 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
914 }
915 }
Chris Lattner2df035b2002-11-02 19:27:56 +0000916}
917
Chris Lattner3e130a22003-01-13 00:32:26 +0000918
919/// doCall - This emits an abstract call instruction, setting up the arguments
920/// and the return value as appropriate. For the actual function call itself,
921/// it inserts the specified CallMI instruction into the stream.
922///
923void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000924 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000925
Chris Lattner065faeb2002-12-28 20:24:02 +0000926 // Count how many bytes are to be pushed on the stack...
927 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000928
Chris Lattner3e130a22003-01-13 00:32:26 +0000929 if (!Args.empty()) {
930 for (unsigned i = 0, e = Args.size(); i != e; ++i)
931 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000932 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000933 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000934 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000935 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000936 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000937 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
938 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000939 default: assert(0 && "Unknown class!");
940 }
941
942 // Adjust the stack pointer for the new arguments...
943 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(NumBytes);
944
945 // Arguments go on the stack in reverse order, as specified by the ABI.
946 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +0000947 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000948 unsigned ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Chris Lattner3e130a22003-01-13 00:32:26 +0000949 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000950 case cByte:
951 case cShort: {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000952 // Promote arg to 32 bits wide into a temporary register...
953 unsigned R = makeAnotherReg(Type::UIntTy);
954 promote32(R, Args[i]);
955 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
956 X86::ESP, ArgOffset).addReg(R);
957 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000958 }
959 case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000960 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
961 X86::ESP, ArgOffset).addReg(ArgReg);
962 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000963 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000964 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
965 X86::ESP, ArgOffset).addReg(ArgReg);
966 addRegOffset(BuildMI(BB, X86::MOVrm32, 5),
967 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
968 ArgOffset += 4; // 8 byte entry, not 4.
969 break;
970
Chris Lattner065faeb2002-12-28 20:24:02 +0000971 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000972 if (Args[i].Ty == Type::FloatTy) {
973 addRegOffset(BuildMI(BB, X86::FSTr32, 5),
974 X86::ESP, ArgOffset).addReg(ArgReg);
975 } else {
976 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
977 addRegOffset(BuildMI(BB, X86::FSTr64, 5),
978 X86::ESP, ArgOffset).addReg(ArgReg);
979 ArgOffset += 4; // 8 byte entry, not 4.
980 }
981 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000982
Chris Lattner3e130a22003-01-13 00:32:26 +0000983 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +0000984 }
985 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +0000986 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000987 } else {
988 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +0000989 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +0000990
Chris Lattner3e130a22003-01-13 00:32:26 +0000991 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +0000992
Chris Lattner065faeb2002-12-28 20:24:02 +0000993 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addZImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +0000994
995 // If there is a return value, scavenge the result from the location the call
996 // leaves it in...
997 //
Chris Lattner3e130a22003-01-13 00:32:26 +0000998 if (Ret.Ty != Type::VoidTy) {
999 unsigned DestClass = getClassB(Ret.Ty);
1000 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001001 case cByte:
1002 case cShort:
1003 case cInt: {
1004 // Integral results are in %eax, or the appropriate portion
1005 // thereof.
1006 static const unsigned regRegMove[] = {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001007 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
Brian Gaeke20244b72002-12-12 15:33:40 +00001008 };
1009 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001010 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001011 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001012 }
Chris Lattner94af4142002-12-25 05:13:53 +00001013 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001014 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001015 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001016 case cLong: // Long values are left in EDX:EAX
1017 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg).addReg(X86::EAX);
1018 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg+1).addReg(X86::EDX);
1019 break;
1020 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001021 }
Chris Lattnera3243642002-12-04 23:45:28 +00001022 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001023}
Chris Lattner2df035b2002-11-02 19:27:56 +00001024
Chris Lattner3e130a22003-01-13 00:32:26 +00001025
1026/// visitCallInst - Push args on stack and do a procedure call instruction.
1027void ISel::visitCallInst(CallInst &CI) {
1028 MachineInstr *TheCall;
1029 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001030 // Is it an intrinsic function call?
1031 if (LLVMIntrinsic::ID ID = (LLVMIntrinsic::ID)F->getIntrinsicID()) {
1032 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1033 return;
1034 }
1035
Chris Lattner3e130a22003-01-13 00:32:26 +00001036 // Emit a CALL instruction with PC-relative displacement.
1037 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1038 } else { // Emit an indirect call...
1039 unsigned Reg = getReg(CI.getCalledValue());
1040 TheCall = BuildMI(X86::CALLr32, 1).addReg(Reg);
1041 }
1042
1043 std::vector<ValueRecord> Args;
1044 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001045 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001046
1047 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1048 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001049}
Chris Lattner3e130a22003-01-13 00:32:26 +00001050
Chris Lattneraeb54b82003-08-28 21:23:43 +00001051
Chris Lattnereca195e2003-05-08 19:44:13 +00001052void ISel::visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &CI) {
1053 unsigned TmpReg1, TmpReg2;
1054 switch (ID) {
1055 case LLVMIntrinsic::va_start:
1056 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001057 TmpReg1 = getReg(CI);
Chris Lattnereca195e2003-05-08 19:44:13 +00001058 addFrameReference(BuildMI(BB, X86::LEAr32, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001059 return;
1060
Chris Lattnereca195e2003-05-08 19:44:13 +00001061 case LLVMIntrinsic::va_copy:
Chris Lattner73815062003-10-18 05:56:40 +00001062 TmpReg1 = getReg(CI);
1063 TmpReg2 = getReg(CI.getOperand(1));
1064 BuildMI(BB, X86::MOVrr32, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001065 return;
Chris Lattner73815062003-10-18 05:56:40 +00001066 case LLVMIntrinsic::va_end: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001067
Chris Lattnerc151e4f2003-06-29 16:42:32 +00001068 case LLVMIntrinsic::longjmp:
Chris Lattner72af6b82003-08-18 16:06:09 +00001069 case LLVMIntrinsic::siglongjmp:
Chris Lattneraeb54b82003-08-28 21:23:43 +00001070 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("abort", true);
Brian Gaeked4615052003-07-18 20:23:43 +00001071 return;
1072
Chris Lattnerc151e4f2003-06-29 16:42:32 +00001073 case LLVMIntrinsic::setjmp:
Chris Lattner72af6b82003-08-18 16:06:09 +00001074 case LLVMIntrinsic::sigsetjmp:
Chris Lattnereb093fb2003-06-30 19:35:54 +00001075 // Setjmp always returns zero...
1076 BuildMI(BB, X86::MOVir32, 1, getReg(CI)).addZImm(0);
Chris Lattnerc151e4f2003-06-29 16:42:32 +00001077 return;
Chris Lattnereca195e2003-05-08 19:44:13 +00001078 default: assert(0 && "Unknown intrinsic for X86!");
1079 }
1080}
1081
1082
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001083/// visitSimpleBinary - Implement simple binary operators for integral types...
1084/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1085/// Xor.
1086void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1087 unsigned DestReg = getReg(B);
1088 MachineBasicBlock::iterator MI = BB->end();
1089 emitSimpleBinaryOperation(BB, MI, B.getOperand(0), B.getOperand(1),
1090 OperatorClass, DestReg);
1091}
Chris Lattner3e130a22003-01-13 00:32:26 +00001092
Chris Lattnerb2acc512003-10-19 21:09:10 +00001093/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1094/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1095/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001096///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001097/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1098/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001099///
1100void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001101 MachineBasicBlock::iterator &IP,
1102 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001103 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001104 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001105
1106 // sub 0, X -> neg X
1107 if (OperatorClass == 1 && Class != cLong)
1108 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
1109 if (CI->isNullValue()) {
1110 unsigned op1Reg = getReg(Op1, MBB, IP);
1111 switch (Class) {
1112 default: assert(0 && "Unknown class for this function!");
1113 case cByte:
1114 BMI(MBB, IP, X86::NEGr8, 1, DestReg).addReg(op1Reg);
1115 return;
1116 case cShort:
1117 BMI(MBB, IP, X86::NEGr16, 1, DestReg).addReg(op1Reg);
1118 return;
1119 case cInt:
1120 BMI(MBB, IP, X86::NEGr32, 1, DestReg).addReg(op1Reg);
1121 return;
1122 }
1123 }
1124
Chris Lattner35333e12003-06-05 18:28:55 +00001125 if (!isa<ConstantInt>(Op1) || Class == cLong) {
1126 static const unsigned OpcodeTab[][4] = {
1127 // Arithmetic operators
1128 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD
1129 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB
1130
1131 // Bitwise operators
1132 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
1133 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
1134 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
Chris Lattner3e130a22003-01-13 00:32:26 +00001135 };
Chris Lattner35333e12003-06-05 18:28:55 +00001136
1137 bool isLong = false;
1138 if (Class == cLong) {
1139 isLong = true;
1140 Class = cInt; // Bottom 32 bits are handled just like ints
1141 }
1142
1143 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1144 assert(Opcode && "Floating point arguments to logical inst?");
Chris Lattnerb2acc512003-10-19 21:09:10 +00001145 unsigned Op0r = getReg(Op0, MBB, IP);
1146 unsigned Op1r = getReg(Op1, MBB, IP);
1147 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner35333e12003-06-05 18:28:55 +00001148
1149 if (isLong) { // Handle the upper 32 bits of long values...
1150 static const unsigned TopTab[] = {
1151 X86::ADCrr32, X86::SBBrr32, X86::ANDrr32, X86::ORrr32, X86::XORrr32
1152 };
Chris Lattnerb2acc512003-10-19 21:09:10 +00001153 BMI(MBB, IP, TopTab[OperatorClass], 2,
1154 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattner35333e12003-06-05 18:28:55 +00001155 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001156 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001157 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001158
1159 // Special case: op Reg, <const>
1160 ConstantInt *Op1C = cast<ConstantInt>(Op1);
1161 unsigned Op0r = getReg(Op0, MBB, IP);
1162
1163 // xor X, -1 -> not X
1164 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1165 static unsigned const NOTTab[] = { X86::NOTr8, X86::NOTr16, X86::NOTr32 };
1166 BMI(MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
1167 return;
1168 }
1169
1170 // add X, -1 -> dec X
1171 if (OperatorClass == 0 && Op1C->isAllOnesValue()) {
1172 static unsigned const DECTab[] = { X86::DECr8, X86::DECr16, X86::DECr32 };
1173 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1174 return;
1175 }
1176
1177 // add X, 1 -> inc X
1178 if (OperatorClass == 0 && Op1C->equalsInt(1)) {
1179 static unsigned const DECTab[] = { X86::INCr8, X86::INCr16, X86::INCr32 };
1180 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1181 return;
1182 }
1183
1184 static const unsigned OpcodeTab[][3] = {
1185 // Arithmetic operators
1186 { X86::ADDri8, X86::ADDri16, X86::ADDri32 }, // ADD
1187 { X86::SUBri8, X86::SUBri16, X86::SUBri32 }, // SUB
1188
1189 // Bitwise operators
1190 { X86::ANDri8, X86::ANDri16, X86::ANDri32 }, // AND
1191 { X86:: ORri8, X86:: ORri16, X86:: ORri32 }, // OR
1192 { X86::XORri8, X86::XORri16, X86::XORri32 }, // XOR
1193 };
1194
1195 assert(Class < 3 && "General code handles 64-bit integer types!");
1196 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1197 uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
1198
1199 // Mask off any upper bits of the constant, if there are any...
1200 Op1v &= (1ULL << (8 << Class)) - 1;
1201 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addZImm(Op1v);
Chris Lattnere2954c82002-11-02 20:04:26 +00001202}
1203
Chris Lattner3e130a22003-01-13 00:32:26 +00001204/// doMultiply - Emit appropriate instructions to multiply together the
1205/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
1206/// result should be given as DestTy.
1207///
Chris Lattner8a307e82002-12-16 19:32:50 +00001208void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator &MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00001209 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00001210 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001211 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00001212 switch (Class) {
1213 case cFP: // Floating point multiply
Chris Lattner3e130a22003-01-13 00:32:26 +00001214 BMI(BB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001215 return;
Chris Lattner0f1c4612003-06-21 17:16:58 +00001216 case cInt:
1217 case cShort:
Chris Lattnerc01d1232003-10-20 03:42:58 +00001218 BMI(BB, MBBI, Class == cInt ? X86::IMULrr32 : X86::IMULrr16, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00001219 .addReg(op0Reg).addReg(op1Reg);
1220 return;
1221 case cByte:
1222 // Must use the MUL instruction, which forces use of AL...
1223 BMI(MBB, MBBI, X86::MOVrr8, 1, X86::AL).addReg(op0Reg);
1224 BMI(MBB, MBBI, X86::MULr8, 1).addReg(op1Reg);
1225 BMI(MBB, MBBI, X86::MOVrr8, 1, DestReg).addReg(X86::AL);
1226 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001227 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001228 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00001229 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001230}
1231
Chris Lattnerb2acc512003-10-19 21:09:10 +00001232// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1233// returns zero when the input is not exactly a power of two.
1234static unsigned ExactLog2(unsigned Val) {
1235 if (Val == 0) return 0;
1236 unsigned Count = 0;
1237 while (Val != 1) {
1238 if (Val & 1) return 0;
1239 Val >>= 1;
1240 ++Count;
1241 }
1242 return Count+1;
1243}
1244
1245void ISel::doMultiplyConst(MachineBasicBlock *MBB,
1246 MachineBasicBlock::iterator &IP,
1247 unsigned DestReg, const Type *DestTy,
1248 unsigned op0Reg, unsigned ConstRHS) {
1249 unsigned Class = getClass(DestTy);
1250
1251 // If the element size is exactly a power of 2, use a shift to get it.
1252 if (unsigned Shift = ExactLog2(ConstRHS)) {
1253 switch (Class) {
1254 default: assert(0 && "Unknown class for this function!");
1255 case cByte:
1256 BMI(MBB, IP, X86::SHLir32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
1257 return;
1258 case cShort:
1259 BMI(MBB, IP, X86::SHLir32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
1260 return;
1261 case cInt:
1262 BMI(MBB, IP, X86::SHLir32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
1263 return;
1264 }
1265 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00001266
1267 if (Class == cShort) {
1268 BMI(MBB, IP, X86::IMULri16, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
1269 return;
1270 } else if (Class == cInt) {
1271 BMI(MBB, IP, X86::IMULri32, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
1272 return;
1273 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001274
1275 // Most general case, emit a normal multiply...
1276 static const unsigned MOVirTab[] = {
1277 X86::MOVir8, X86::MOVir16, X86::MOVir32
1278 };
1279
1280 unsigned TmpReg = makeAnotherReg(DestTy);
1281 BMI(MBB, IP, MOVirTab[Class], 1, TmpReg).addZImm(ConstRHS);
1282
1283 // Emit a MUL to multiply the register holding the index by
1284 // elementSize, putting the result in OffsetReg.
1285 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
1286}
1287
Chris Lattnerca9671d2002-11-02 20:28:58 +00001288/// visitMul - Multiplies are not simple binary operators because they must deal
1289/// with the EAX register explicitly.
1290///
1291void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +00001292 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00001293 unsigned DestReg = getReg(I);
1294
1295 // Simple scalar multiply?
1296 if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001297 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
1298 unsigned Val = (unsigned)CI->getRawValue(); // Cannot be 64-bit constant
1299 MachineBasicBlock::iterator MBBI = BB->end();
1300 doMultiplyConst(BB, MBBI, DestReg, I.getType(), Op0Reg, Val);
1301 } else {
1302 unsigned Op1Reg = getReg(I.getOperand(1));
1303 MachineBasicBlock::iterator MBBI = BB->end();
1304 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
1305 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001306 } else {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001307 unsigned Op1Reg = getReg(I.getOperand(1));
1308
Chris Lattner3e130a22003-01-13 00:32:26 +00001309 // Long value. We have to do things the hard way...
1310 // Multiply the two low parts... capturing carry into EDX
1311 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(Op0Reg);
1312 BuildMI(BB, X86::MULr32, 1).addReg(Op1Reg); // AL*BL
1313
1314 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
1315 BuildMI(BB, X86::MOVrr32, 1, DestReg).addReg(X86::EAX); // AL*BL
1316 BuildMI(BB, X86::MOVrr32, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32
1317
1318 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001319 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
Chris Lattnerc01d1232003-10-20 03:42:58 +00001320 BMI(BB, MBBI, X86::IMULrr32, 2, AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001321
1322 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
1323 BuildMI(BB, X86::ADDrr32, 2, // AH*BL+(AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001324 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001325
1326 MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001327 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattnerc01d1232003-10-20 03:42:58 +00001328 BMI(BB, MBBI, X86::IMULrr32, 2, ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001329
1330 BuildMI(BB, X86::ADDrr32, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001331 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001332 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001333}
Chris Lattnerca9671d2002-11-02 20:28:58 +00001334
Chris Lattner06925362002-11-17 21:56:38 +00001335
Chris Lattnerf01729e2002-11-02 20:54:46 +00001336/// visitDivRem - Handle division and remainder instructions... these
1337/// instruction both require the same instructions to be generated, they just
1338/// select the result from a different register. Note that both of these
1339/// instructions work differently for signed and unsigned operands.
1340///
1341void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001342 unsigned Class = getClass(I.getType());
1343 unsigned Op0Reg, Op1Reg, ResultReg = getReg(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001344
1345 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001346 case cFP: // Floating point divide
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001347 if (I.getOpcode() == Instruction::Div) {
1348 Op0Reg = getReg(I.getOperand(0));
1349 Op1Reg = getReg(I.getOperand(1));
Chris Lattner94af4142002-12-25 05:13:53 +00001350 BuildMI(BB, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001351 } else { // Floating point remainder...
Chris Lattner3e130a22003-01-13 00:32:26 +00001352 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001353 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00001354 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001355 Args.push_back(ValueRecord(I.getOperand(0)));
1356 Args.push_back(ValueRecord(I.getOperand(1)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001357 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
1358 }
Chris Lattner94af4142002-12-25 05:13:53 +00001359 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001360 case cLong: {
1361 static const char *FnName[] =
1362 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
1363
1364 unsigned NameIdx = I.getType()->isUnsigned()*2;
1365 NameIdx += I.getOpcode() == Instruction::Div;
1366 MachineInstr *TheCall =
1367 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
1368
1369 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001370 Args.push_back(ValueRecord(I.getOperand(0)));
1371 Args.push_back(ValueRecord(I.getOperand(1)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001372 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
1373 return;
1374 }
1375 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00001376 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00001377 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00001378 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001379
1380 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
1381 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Chris Lattner7b52c032003-06-22 03:31:18 +00001382 static const unsigned SarOpcode[]={ X86::SARir8, X86::SARir16, X86::SARir32 };
Chris Lattnerf01729e2002-11-02 20:54:46 +00001383 static const unsigned ClrOpcode[]={ X86::XORrr8, X86::XORrr16, X86::XORrr32 };
1384 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
1385
1386 static const unsigned DivOpcode[][4] = {
Chris Lattner3e130a22003-01-13 00:32:26 +00001387 { X86::DIVr8 , X86::DIVr16 , X86::DIVr32 , 0 }, // Unsigned division
1388 { X86::IDIVr8, X86::IDIVr16, X86::IDIVr32, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00001389 };
1390
1391 bool isSigned = I.getType()->isSigned();
1392 unsigned Reg = Regs[Class];
1393 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00001394
1395 // Put the first operand into one of the A registers...
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001396 Op0Reg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +00001397 BuildMI(BB, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
1398
1399 if (isSigned) {
1400 // Emit a sign extension instruction...
Chris Lattner7b52c032003-06-22 03:31:18 +00001401 unsigned ShiftResult = makeAnotherReg(I.getType());
1402 BuildMI(BB, SarOpcode[Class], 2, ShiftResult).addReg(Op0Reg).addZImm(31);
1403 BuildMI(BB, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001404 } else {
1405 // If unsigned, emit a zeroing instruction... (reg = xor reg, reg)
1406 BuildMI(BB, ClrOpcode[Class], 2, ExtReg).addReg(ExtReg).addReg(ExtReg);
1407 }
1408
Chris Lattner06925362002-11-17 21:56:38 +00001409 // Emit the appropriate divide or remainder instruction...
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001410 Op1Reg = getReg(I.getOperand(1));
Chris Lattner92845e32002-11-21 18:54:29 +00001411 BuildMI(BB, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00001412
Chris Lattnerf01729e2002-11-02 20:54:46 +00001413 // Figure out which register we want to pick the result out of...
1414 unsigned DestReg = (I.getOpcode() == Instruction::Div) ? Reg : ExtReg;
1415
Chris Lattnerf01729e2002-11-02 20:54:46 +00001416 // Put the result into the destination register...
Chris Lattner94af4142002-12-25 05:13:53 +00001417 BuildMI(BB, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00001418}
Chris Lattnere2954c82002-11-02 20:04:26 +00001419
Chris Lattner06925362002-11-17 21:56:38 +00001420
Brian Gaekea1719c92002-10-31 23:03:59 +00001421/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1422/// for constant immediate shift values, and for constant immediate
1423/// shift values equal to 1. Even the general case is sort of special,
1424/// because the shift amount has to be in CL, not just any old register.
1425///
Chris Lattner3e130a22003-01-13 00:32:26 +00001426void ISel::visitShiftInst(ShiftInst &I) {
1427 unsigned SrcReg = getReg(I.getOperand(0));
Chris Lattnerf01729e2002-11-02 20:54:46 +00001428 unsigned DestReg = getReg(I);
Chris Lattnere9913f22002-11-02 01:41:55 +00001429 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Chris Lattner3e130a22003-01-13 00:32:26 +00001430 bool isSigned = I.getType()->isSigned();
1431 unsigned Class = getClass(I.getType());
1432
1433 static const unsigned ConstantOperand[][4] = {
1434 { X86::SHRir8, X86::SHRir16, X86::SHRir32, X86::SHRDir32 }, // SHR
1435 { X86::SARir8, X86::SARir16, X86::SARir32, X86::SHRDir32 }, // SAR
1436 { X86::SHLir8, X86::SHLir16, X86::SHLir32, X86::SHLDir32 }, // SHL
1437 { X86::SHLir8, X86::SHLir16, X86::SHLir32, X86::SHLDir32 }, // SAL = SHL
1438 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001439
Chris Lattner3e130a22003-01-13 00:32:26 +00001440 static const unsigned NonConstantOperand[][4] = {
1441 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32 }, // SHR
1442 { X86::SARrr8, X86::SARrr16, X86::SARrr32 }, // SAR
1443 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SHL
1444 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SAL = SHL
1445 };
Chris Lattner796df732002-11-02 00:44:25 +00001446
Chris Lattner3e130a22003-01-13 00:32:26 +00001447 // Longs, as usual, are handled specially...
1448 if (Class == cLong) {
1449 // If we have a constant shift, we can generate much more efficient code
1450 // than otherwise...
1451 //
1452 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getOperand(1))) {
1453 unsigned Amount = CUI->getValue();
1454 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001455 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
1456 if (isLeftShift) {
1457 BuildMI(BB, Opc[3], 3,
1458 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addZImm(Amount);
1459 BuildMI(BB, Opc[2], 2, DestReg).addReg(SrcReg).addZImm(Amount);
1460 } else {
1461 BuildMI(BB, Opc[3], 3,
1462 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addZImm(Amount);
1463 BuildMI(BB, Opc[2], 2, DestReg+1).addReg(SrcReg+1).addZImm(Amount);
1464 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001465 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001466 Amount -= 32;
1467 if (isLeftShift) {
1468 BuildMI(BB, X86::SHLir32, 2,DestReg+1).addReg(SrcReg).addZImm(Amount);
1469 BuildMI(BB, X86::MOVir32, 1,DestReg ).addZImm(0);
1470 } else {
1471 unsigned Opcode = isSigned ? X86::SARir32 : X86::SHRir32;
1472 BuildMI(BB, Opcode, 2, DestReg).addReg(SrcReg+1).addZImm(Amount);
1473 BuildMI(BB, X86::MOVir32, 1, DestReg+1).addZImm(0);
1474 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001475 }
1476 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00001477 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1478
1479 if (!isLeftShift && isSigned) {
1480 // If this is a SHR of a Long, then we need to do funny sign extension
1481 // stuff. TmpReg gets the value to use as the high-part if we are
1482 // shifting more than 32 bits.
1483 BuildMI(BB, X86::SARir32, 2, TmpReg).addReg(SrcReg).addZImm(31);
1484 } else {
1485 // Other shifts use a fixed zero value if the shift is more than 32
1486 // bits.
1487 BuildMI(BB, X86::MOVir32, 1, TmpReg).addZImm(0);
1488 }
1489
1490 // Initialize CL with the shift amount...
1491 unsigned ShiftAmount = getReg(I.getOperand(1));
1492 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmount);
1493
1494 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
1495 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
1496 if (isLeftShift) {
1497 // TmpReg2 = shld inHi, inLo
1498 BuildMI(BB, X86::SHLDrr32, 2, TmpReg2).addReg(SrcReg+1).addReg(SrcReg);
1499 // TmpReg3 = shl inLo, CL
1500 BuildMI(BB, X86::SHLrr32, 1, TmpReg3).addReg(SrcReg);
1501
1502 // Set the flags to indicate whether the shift was by more than 32 bits.
1503 BuildMI(BB, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
1504
1505 // DestHi = (>32) ? TmpReg3 : TmpReg2;
1506 BuildMI(BB, X86::CMOVNErr32, 2,
1507 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
1508 // DestLo = (>32) ? TmpReg : TmpReg3;
1509 BuildMI(BB, X86::CMOVNErr32, 2, DestReg).addReg(TmpReg3).addReg(TmpReg);
1510 } else {
1511 // TmpReg2 = shrd inLo, inHi
1512 BuildMI(BB, X86::SHRDrr32, 2, TmpReg2).addReg(SrcReg).addReg(SrcReg+1);
1513 // TmpReg3 = s[ah]r inHi, CL
1514 BuildMI(BB, isSigned ? X86::SARrr32 : X86::SHRrr32, 1, TmpReg3)
1515 .addReg(SrcReg+1);
1516
1517 // Set the flags to indicate whether the shift was by more than 32 bits.
1518 BuildMI(BB, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
1519
1520 // DestLo = (>32) ? TmpReg3 : TmpReg2;
1521 BuildMI(BB, X86::CMOVNErr32, 2,
1522 DestReg).addReg(TmpReg2).addReg(TmpReg3);
1523
1524 // DestHi = (>32) ? TmpReg : TmpReg3;
1525 BuildMI(BB, X86::CMOVNErr32, 2,
1526 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
1527 }
Brian Gaekea1719c92002-10-31 23:03:59 +00001528 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001529 return;
1530 }
Chris Lattnere9913f22002-11-02 01:41:55 +00001531
Chris Lattner3e130a22003-01-13 00:32:26 +00001532 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getOperand(1))) {
1533 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
1534 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001535
Chris Lattner3e130a22003-01-13 00:32:26 +00001536 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
1537 BuildMI(BB, Opc[Class], 2, DestReg).addReg(SrcReg).addZImm(CUI->getValue());
1538 } else { // The shift amount is non-constant.
1539 BuildMI(BB, X86::MOVrr8, 1, X86::CL).addReg(getReg(I.getOperand(1)));
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001540
Chris Lattner3e130a22003-01-13 00:32:26 +00001541 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
1542 BuildMI(BB, Opc[Class], 1, DestReg).addReg(SrcReg);
1543 }
1544}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001545
Chris Lattner3e130a22003-01-13 00:32:26 +00001546
Chris Lattner3e130a22003-01-13 00:32:26 +00001547/// EmitByteSwap - Byteswap SrcReg into DestReg.
1548///
1549void ISel::EmitByteSwap(unsigned DestReg, unsigned SrcReg, unsigned Class) {
1550 // Emit the byte swap instruction...
1551 switch (Class) {
1552 case cByte:
Misha Brukmanbaf06072003-04-22 17:54:23 +00001553 // No byteswap necessary for 8 bit value...
Chris Lattner3e130a22003-01-13 00:32:26 +00001554 BuildMI(BB, X86::MOVrr8, 1, DestReg).addReg(SrcReg);
1555 break;
1556 case cInt:
1557 // Use the 32 bit bswap instruction to do a 32 bit swap...
1558 BuildMI(BB, X86::BSWAPr32, 1, DestReg).addReg(SrcReg);
1559 break;
1560
1561 case cShort:
1562 // For 16 bit we have to use an xchg instruction, because there is no
Misha Brukmanbaf06072003-04-22 17:54:23 +00001563 // 16-bit bswap. XCHG is necessarily not in SSA form, so we force things
Chris Lattner3e130a22003-01-13 00:32:26 +00001564 // into AX to do the xchg.
1565 //
1566 BuildMI(BB, X86::MOVrr16, 1, X86::AX).addReg(SrcReg);
1567 BuildMI(BB, X86::XCHGrr8, 2).addReg(X86::AL, MOTy::UseAndDef)
1568 .addReg(X86::AH, MOTy::UseAndDef);
1569 BuildMI(BB, X86::MOVrr16, 1, DestReg).addReg(X86::AX);
1570 break;
1571 default: assert(0 && "Cannot byteswap this class!");
1572 }
Brian Gaekea1719c92002-10-31 23:03:59 +00001573}
1574
Chris Lattner06925362002-11-17 21:56:38 +00001575
Chris Lattner6fc3c522002-11-17 21:11:55 +00001576/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00001577/// instruction. The load and store instructions are the only place where we
1578/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00001579///
1580void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001581 unsigned SrcAddrReg = getReg(I.getOperand(0));
1582 unsigned DestReg = getReg(I);
Chris Lattnere8f0d922002-12-24 00:03:11 +00001583
Brian Gaekebfedb912003-07-17 21:30:06 +00001584 unsigned Class = getClassB(I.getType());
Chris Lattner6ac1d712003-10-20 04:48:06 +00001585
1586 if (Class == cLong) {
1587 addDirectMem(BuildMI(BB, X86::MOVmr32, 4, DestReg), SrcAddrReg);
1588 addRegOffset(BuildMI(BB, X86::MOVmr32, 4, DestReg+1), SrcAddrReg, 4);
Chris Lattner94af4142002-12-25 05:13:53 +00001589 return;
1590 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00001591
Chris Lattner6ac1d712003-10-20 04:48:06 +00001592 static const unsigned Opcodes[] = {
1593 X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FLDr32
Chris Lattner3e130a22003-01-13 00:32:26 +00001594 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00001595 unsigned Opcode = Opcodes[Class];
1596 if (I.getType() == Type::DoubleTy) Opcode = X86::FLDr64;
1597 addDirectMem(BuildMI(BB, Opcode, 4, DestReg), SrcAddrReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001598}
1599
Chris Lattner6fc3c522002-11-17 21:11:55 +00001600/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
1601/// instruction.
1602///
1603void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001604 unsigned ValReg = getReg(I.getOperand(0));
1605 unsigned AddressReg = getReg(I.getOperand(1));
Chris Lattner6c09db22003-10-20 04:11:23 +00001606
1607 const Type *ValTy = I.getOperand(0)->getType();
1608 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00001609
1610 if (Class == cLong) {
Chris Lattner6c09db22003-10-20 04:11:23 +00001611 addDirectMem(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg).addReg(ValReg);
1612 addRegOffset(BuildMI(BB, X86::MOVrm32, 1+4), AddressReg,4).addReg(ValReg+1);
Chris Lattner94af4142002-12-25 05:13:53 +00001613 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001614 }
1615
Chris Lattner6ac1d712003-10-20 04:48:06 +00001616 static const unsigned Opcodes[] = {
1617 X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FSTr32
1618 };
1619 unsigned Opcode = Opcodes[Class];
1620 if (ValTy == Type::DoubleTy) Opcode = X86::FSTr64;
1621 addDirectMem(BuildMI(BB, Opcode, 1+4), AddressReg).addReg(ValReg);
Chris Lattner6fc3c522002-11-17 21:11:55 +00001622}
1623
1624
Brian Gaekec11232a2002-11-26 10:43:30 +00001625/// visitCastInst - Here we have various kinds of copying with or without
1626/// sign extension going on.
Chris Lattner3e130a22003-01-13 00:32:26 +00001627void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00001628 Value *Op = CI.getOperand(0);
1629 // If this is a cast from a 32-bit integer to a Long type, and the only uses
1630 // of the case are GEP instructions, then the cast does not need to be
1631 // generated explicitly, it will be folded into the GEP.
1632 if (CI.getType() == Type::LongTy &&
1633 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
1634 bool AllUsesAreGEPs = true;
1635 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
1636 if (!isa<GetElementPtrInst>(*I)) {
1637 AllUsesAreGEPs = false;
1638 break;
1639 }
1640
1641 // No need to codegen this cast if all users are getelementptr instrs...
1642 if (AllUsesAreGEPs) return;
1643 }
1644
Chris Lattner548f61d2003-04-23 17:22:12 +00001645 unsigned DestReg = getReg(CI);
1646 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00001647 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00001648}
1649
1650/// emitCastOperation - Common code shared between visitCastInst and
1651/// constant expression cast support.
1652void ISel::emitCastOperation(MachineBasicBlock *BB,
1653 MachineBasicBlock::iterator &IP,
1654 Value *Src, const Type *DestTy,
1655 unsigned DestReg) {
Chris Lattner3907d112003-04-23 17:57:48 +00001656 unsigned SrcReg = getReg(Src, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001657 const Type *SrcTy = Src->getType();
1658 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00001659 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00001660
Chris Lattner3e130a22003-01-13 00:32:26 +00001661 // Implement casts to bool by using compare on the operand followed by set if
1662 // not zero on the result.
1663 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00001664 switch (SrcClass) {
1665 case cByte:
1666 BMI(BB, IP, X86::TESTrr8, 2).addReg(SrcReg).addReg(SrcReg);
1667 break;
1668 case cShort:
1669 BMI(BB, IP, X86::TESTrr16, 2).addReg(SrcReg).addReg(SrcReg);
1670 break;
1671 case cInt:
1672 BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg).addReg(SrcReg);
1673 break;
1674 case cLong: {
1675 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1676 BMI(BB, IP, X86::ORrr32, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
1677 break;
1678 }
1679 case cFP:
1680 assert(0 && "FIXME: implement cast FP to bool");
1681 abort();
1682 }
1683
1684 // If the zero flag is not set, then the value is true, set the byte to
1685 // true.
Chris Lattner548f61d2003-04-23 17:22:12 +00001686 BMI(BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00001687 return;
1688 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001689
1690 static const unsigned RegRegMove[] = {
1691 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV, X86::MOVrr32
1692 };
1693
1694 // Implement casts between values of the same type class (as determined by
1695 // getClass) by using a register-to-register move.
1696 if (SrcClass == DestClass) {
1697 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001698 BMI(BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001699 } else if (SrcClass == cFP) {
1700 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001701 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
1702 BMI(BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001703 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001704 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
1705 "Unknown cFP member!");
1706 // Truncate from double to float by storing to memory as short, then
1707 // reading it back.
1708 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00001709 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001710 addFrameReference(BMI(BB, IP, X86::FSTr32, 5), FrameIdx).addReg(SrcReg);
1711 addFrameReference(BMI(BB, IP, X86::FLDr32, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001712 }
1713 } else if (SrcClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001714 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
1715 BMI(BB, IP, X86::MOVrr32, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001716 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00001717 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00001718 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00001719 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001720 return;
1721 }
1722
1723 // Handle cast of SMALLER int to LARGER int using a move with sign extension
1724 // or zero extension, depending on whether the source type was signed.
1725 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
1726 SrcClass < DestClass) {
1727 bool isLong = DestClass == cLong;
1728 if (isLong) DestClass = cInt;
1729
1730 static const unsigned Opc[][4] = {
1731 { X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, X86::MOVrr32 }, // s
1732 { X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16, X86::MOVrr32 } // u
1733 };
1734
1735 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattner548f61d2003-04-23 17:22:12 +00001736 BMI(BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
1737 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001738
1739 if (isLong) { // Handle upper 32 bits as appropriate...
1740 if (isUnsigned) // Zero out top bits...
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001741 BMI(BB, IP, X86::MOVir32, 1, DestReg+1).addZImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001742 else // Sign extend bottom half...
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001743 BMI(BB, IP, X86::SARir32, 2, DestReg+1).addReg(DestReg).addZImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00001744 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001745 return;
1746 }
1747
1748 // Special case long -> int ...
1749 if (SrcClass == cLong && DestClass == cInt) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001750 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001751 return;
1752 }
1753
1754 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
1755 // move out of AX or AL.
1756 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
1757 && SrcClass > DestClass) {
1758 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattner548f61d2003-04-23 17:22:12 +00001759 BMI(BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
1760 BMI(BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00001761 return;
1762 }
1763
1764 // Handle casts from integer to floating point now...
1765 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00001766 // Promote the integer to a type supported by FLD. We do this because there
1767 // are no unsigned FLD instructions, so we must promote an unsigned value to
1768 // a larger signed value, then use FLD on the larger value.
1769 //
1770 const Type *PromoteType = 0;
1771 unsigned PromoteOpcode;
1772 switch (SrcTy->getPrimitiveID()) {
1773 case Type::BoolTyID:
1774 case Type::SByteTyID:
1775 // We don't have the facilities for directly loading byte sized data from
1776 // memory (even signed). Promote it to 16 bits.
1777 PromoteType = Type::ShortTy;
1778 PromoteOpcode = X86::MOVSXr16r8;
1779 break;
1780 case Type::UByteTyID:
1781 PromoteType = Type::ShortTy;
1782 PromoteOpcode = X86::MOVZXr16r8;
1783 break;
1784 case Type::UShortTyID:
1785 PromoteType = Type::IntTy;
1786 PromoteOpcode = X86::MOVZXr32r16;
1787 break;
1788 case Type::UIntTyID: {
1789 // Make a 64 bit temporary... and zero out the top of it...
1790 unsigned TmpReg = makeAnotherReg(Type::LongTy);
1791 BMI(BB, IP, X86::MOVrr32, 1, TmpReg).addReg(SrcReg);
1792 BMI(BB, IP, X86::MOVir32, 1, TmpReg+1).addZImm(0);
1793 SrcTy = Type::LongTy;
1794 SrcClass = cLong;
1795 SrcReg = TmpReg;
1796 break;
1797 }
1798 case Type::ULongTyID:
1799 assert("FIXME: not implemented: cast ulong X to fp type!");
1800 default: // No promotion needed...
1801 break;
1802 }
1803
1804 if (PromoteType) {
1805 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner548f61d2003-04-23 17:22:12 +00001806 BMI(BB, IP, SrcTy->isSigned() ? X86::MOVSXr16r8 : X86::MOVZXr16r8,
1807 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00001808 SrcTy = PromoteType;
1809 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00001810 SrcReg = TmpReg;
1811 }
1812
1813 // Spill the integer to memory and reload it from there...
1814 int FrameIdx =
1815 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
1816
1817 if (SrcClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001818 addFrameReference(BMI(BB, IP, X86::MOVrm32, 5), FrameIdx).addReg(SrcReg);
1819 addFrameReference(BMI(BB, IP, X86::MOVrm32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001820 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001821 } else {
1822 static const unsigned Op1[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001823 addFrameReference(BMI(BB, IP, Op1[SrcClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001824 }
1825
1826 static const unsigned Op2[] =
Chris Lattner4d5a50a2003-05-12 20:36:13 +00001827 { 0/*byte*/, X86::FILDr16, X86::FILDr32, 0/*FP*/, X86::FILDr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001828 addFrameReference(BMI(BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001829 return;
1830 }
1831
1832 // Handle casts from floating point to integer now...
1833 if (SrcClass == cFP) {
1834 // Change the floating point control register to use "round towards zero"
1835 // mode when truncating to an integer value.
1836 //
1837 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Chris Lattner548f61d2003-04-23 17:22:12 +00001838 addFrameReference(BMI(BB, IP, X86::FNSTCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001839
1840 // Load the old value of the high byte of the control word...
1841 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Chris Lattner548f61d2003-04-23 17:22:12 +00001842 addFrameReference(BMI(BB, IP, X86::MOVmr8, 4, HighPartOfCW), CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001843
1844 // Set the high part to be round to zero...
Chris Lattner548f61d2003-04-23 17:22:12 +00001845 addFrameReference(BMI(BB, IP, X86::MOVim8, 5), CWFrameIdx, 1).addZImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00001846
1847 // Reload the modified control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00001848 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001849
1850 // Restore the memory image of control word to original value
Chris Lattner548f61d2003-04-23 17:22:12 +00001851 addFrameReference(BMI(BB, IP, X86::MOVrm8, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001852 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00001853
1854 // We don't have the facilities for directly storing byte sized data to
1855 // memory. Promote it to 16 bits. We also must promote unsigned values to
1856 // larger classes because we only have signed FP stores.
1857 unsigned StoreClass = DestClass;
1858 const Type *StoreTy = DestTy;
1859 if (StoreClass == cByte || DestTy->isUnsigned())
1860 switch (StoreClass) {
1861 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
1862 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
1863 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00001864 // The following treatment of cLong may not be perfectly right,
1865 // but it survives chains of casts of the form
1866 // double->ulong->double.
1867 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001868 default: assert(0 && "Unknown store class!");
1869 }
1870
1871 // Spill the integer to memory and reload it from there...
1872 int FrameIdx =
1873 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
1874
1875 static const unsigned Op1[] =
1876 { 0, X86::FISTr16, X86::FISTr32, 0, X86::FISTPr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001877 addFrameReference(BMI(BB, IP, Op1[StoreClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001878
1879 if (DestClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00001880 addFrameReference(BMI(BB, IP, X86::MOVmr32, 4, DestReg), FrameIdx);
1881 addFrameReference(BMI(BB, IP, X86::MOVmr32, 4, DestReg+1), FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00001882 } else {
1883 static const unsigned Op2[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00001884 addFrameReference(BMI(BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001885 }
1886
1887 // Reload the original control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00001888 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00001889 return;
1890 }
1891
Brian Gaeked474e9c2002-12-06 10:49:33 +00001892 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00001893 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00001894 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00001895}
Brian Gaekea1719c92002-10-31 23:03:59 +00001896
Chris Lattner73815062003-10-18 05:56:40 +00001897/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00001898///
Chris Lattner73815062003-10-18 05:56:40 +00001899void ISel::visitVANextInst(VANextInst &I) {
1900 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00001901 unsigned DestReg = getReg(I);
1902
Chris Lattnereca195e2003-05-08 19:44:13 +00001903 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00001904 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001905 default:
1906 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00001907 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001908 return;
1909 case Type::PointerTyID:
1910 case Type::UIntTyID:
1911 case Type::IntTyID:
1912 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00001913 break;
1914 case Type::ULongTyID:
1915 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00001916 case Type::DoubleTyID:
1917 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00001918 break;
1919 }
1920
1921 // Increment the VAList pointer...
Chris Lattner73815062003-10-18 05:56:40 +00001922 BuildMI(BB, X86::ADDri32, 2, DestReg).addReg(VAList).addZImm(Size);
1923}
Chris Lattnereca195e2003-05-08 19:44:13 +00001924
Chris Lattner73815062003-10-18 05:56:40 +00001925void ISel::visitVAArgInst(VAArgInst &I) {
1926 unsigned VAList = getReg(I.getOperand(0));
1927 unsigned DestReg = getReg(I);
1928
1929 switch (I.getType()->getPrimitiveID()) {
1930 default:
1931 std::cerr << I;
1932 assert(0 && "Error: bad type for va_next instruction!");
1933 return;
1934 case Type::PointerTyID:
1935 case Type::UIntTyID:
1936 case Type::IntTyID:
1937 addDirectMem(BuildMI(BB, X86::MOVmr32, 4, DestReg), VAList);
1938 break;
1939 case Type::ULongTyID:
1940 case Type::LongTyID:
1941 addDirectMem(BuildMI(BB, X86::MOVmr32, 4, DestReg), VAList);
1942 addRegOffset(BuildMI(BB, X86::MOVmr32, 4, DestReg+1), VAList, 4);
1943 break;
1944 case Type::DoubleTyID:
1945 addDirectMem(BuildMI(BB, X86::FLDr64, 4, DestReg), VAList);
1946 break;
1947 }
Chris Lattnereca195e2003-05-08 19:44:13 +00001948}
1949
1950
Chris Lattner3e130a22003-01-13 00:32:26 +00001951void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
1952 unsigned outputReg = getReg(I);
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001953 MachineBasicBlock::iterator MI = BB->end();
1954 emitGEPOperation(BB, MI, I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00001955 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001956}
1957
Brian Gaeke71794c02002-12-13 11:22:48 +00001958void ISel::emitGEPOperation(MachineBasicBlock *MBB,
Chris Lattnerf08ad9f2002-12-13 10:50:40 +00001959 MachineBasicBlock::iterator &IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +00001960 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +00001961 User::op_iterator IdxEnd, unsigned TargetReg) {
1962 const TargetData &TD = TM.getTargetData();
1963 const Type *Ty = Src->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +00001964 unsigned BaseReg = getReg(Src, MBB, IP);
Chris Lattnerc0812d82002-12-13 06:56:29 +00001965
Brian Gaeke20244b72002-12-12 15:33:40 +00001966 // GEPs have zero or more indices; we must perform a struct access
1967 // or array access for each one.
Chris Lattnerc0812d82002-12-13 06:56:29 +00001968 for (GetElementPtrInst::op_iterator oi = IdxBegin,
1969 oe = IdxEnd; oi != oe; ++oi) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001970 Value *idx = *oi;
Chris Lattner3e130a22003-01-13 00:32:26 +00001971 unsigned NextReg = BaseReg;
Chris Lattner065faeb2002-12-28 20:24:02 +00001972 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001973 // It's a struct access. idx is the index into the structure,
1974 // which names the field. This index must have ubyte type.
Chris Lattner065faeb2002-12-28 20:24:02 +00001975 const ConstantUInt *CUI = cast<ConstantUInt>(idx);
1976 assert(CUI->getType() == Type::UByteTy
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001977 && "Funny-looking structure index in GEP");
Brian Gaeke20244b72002-12-12 15:33:40 +00001978 // Use the TargetData structure to pick out what the layout of
1979 // the structure is in memory. Since the structure index must
1980 // be constant, we can get its value and use it to find the
1981 // right byte offset from the StructLayout class's list of
1982 // structure member offsets.
Chris Lattnere8f0d922002-12-24 00:03:11 +00001983 unsigned idxValue = CUI->getValue();
Chris Lattner3e130a22003-01-13 00:32:26 +00001984 unsigned FieldOff = TD.getStructLayout(StTy)->MemberOffsets[idxValue];
1985 if (FieldOff) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001986 NextReg = makeAnotherReg(Type::UIntTy);
1987 // Emit an ADD to add FieldOff to the basePtr.
1988 BMI(MBB, IP, X86::ADDri32, 2,NextReg).addReg(BaseReg).addZImm(FieldOff);
Chris Lattner3e130a22003-01-13 00:32:26 +00001989 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001990 // The next type is the member of the structure selected by the
1991 // index.
Chris Lattner065faeb2002-12-28 20:24:02 +00001992 Ty = StTy->getElementTypes()[idxValue];
1993 } else if (const SequentialType *SqTy = cast<SequentialType>(Ty)) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001994 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner8a307e82002-12-16 19:32:50 +00001995
Brian Gaeke20244b72002-12-12 15:33:40 +00001996 // idx is the index into the array. Unlike with structure
1997 // indices, we may not know its actual value at code-generation
1998 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00001999 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
2000
Chris Lattnerf5854472003-06-21 16:01:24 +00002001 // Most GEP instructions use a [cast (int/uint) to LongTy] as their
2002 // operand on X86. Handle this case directly now...
2003 if (CastInst *CI = dyn_cast<CastInst>(idx))
2004 if (CI->getOperand(0)->getType() == Type::IntTy ||
2005 CI->getOperand(0)->getType() == Type::UIntTy)
2006 idx = CI->getOperand(0);
2007
Chris Lattner3e130a22003-01-13 00:32:26 +00002008 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00002009 // must find the size of the pointed-to type (Not coincidentally, the next
2010 // type is the type of the elements in the array).
2011 Ty = SqTy->getElementType();
2012 unsigned elementSize = TD.getTypeSize(Ty);
2013
2014 // If idxReg is a constant, we don't need to perform the multiply!
2015 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002016 if (!CSI->isNullValue()) {
Chris Lattner8a307e82002-12-16 19:32:50 +00002017 unsigned Offset = elementSize*CSI->getValue();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002018 NextReg = makeAnotherReg(Type::UIntTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002019 BMI(MBB, IP, X86::ADDri32, 2,NextReg).addReg(BaseReg).addZImm(Offset);
Chris Lattner8a307e82002-12-16 19:32:50 +00002020 }
2021 } else if (elementSize == 1) {
2022 // If the element size is 1, we don't have to multiply, just add
2023 unsigned idxReg = getReg(idx, MBB, IP);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002024 NextReg = makeAnotherReg(Type::UIntTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002025 BMI(MBB, IP, X86::ADDrr32, 2, NextReg).addReg(BaseReg).addReg(idxReg);
Chris Lattner8a307e82002-12-16 19:32:50 +00002026 } else {
2027 unsigned idxReg = getReg(idx, MBB, IP);
2028 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002029
2030 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
2031
Chris Lattner8a307e82002-12-16 19:32:50 +00002032 // Emit an ADD to add OffsetReg to the basePtr.
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002033 NextReg = makeAnotherReg(Type::UIntTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002034 BMI(MBB, IP, X86::ADDrr32, 2,NextReg).addReg(BaseReg).addReg(OffsetReg);
Chris Lattner8a307e82002-12-16 19:32:50 +00002035 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002036 }
2037 // Now that we are here, further indices refer to subtypes of this
Chris Lattner3e130a22003-01-13 00:32:26 +00002038 // one, so we don't need to worry about BaseReg itself, anymore.
2039 BaseReg = NextReg;
Brian Gaeke20244b72002-12-12 15:33:40 +00002040 }
2041 // After we have processed all the indices, the result is left in
Chris Lattner3e130a22003-01-13 00:32:26 +00002042 // BaseReg. Move it to the register where we were expected to
Brian Gaeke20244b72002-12-12 15:33:40 +00002043 // put the answer. A 32-bit move should do it, because we are in
2044 // ILP32 land.
Chris Lattner3e130a22003-01-13 00:32:26 +00002045 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg);
Brian Gaeke20244b72002-12-12 15:33:40 +00002046}
2047
2048
Chris Lattner065faeb2002-12-28 20:24:02 +00002049/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2050/// frame manager, otherwise do it the hard way.
2051///
2052void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00002053 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00002054 const Type *Ty = I.getAllocatedType();
2055 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2056
2057 // If this is a fixed size alloca in the entry block for the function,
2058 // statically stack allocate the space.
2059 //
2060 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
2061 if (I.getParent() == I.getParent()->getParent()->begin()) {
2062 TySize *= CUI->getValue(); // Get total allocated size...
2063 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
2064
2065 // Create a new stack object using the frame manager...
2066 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
2067 addFrameReference(BuildMI(BB, X86::LEAr32, 5, getReg(I)), FrameIdx);
2068 return;
2069 }
2070 }
2071
2072 // Create a register to hold the temporary result of multiplying the type size
2073 // constant by the variable amount.
2074 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2075 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00002076
2077 // TotalSizeReg = mul <numelements>, <TypeSize>
2078 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002079 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002080
2081 // AddedSize = add <TotalSizeReg>, 15
2082 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2083 BuildMI(BB, X86::ADDri32, 2, AddedSizeReg).addReg(TotalSizeReg).addZImm(15);
2084
2085 // AlignedSize = and <AddedSize>, ~15
2086 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2087 BuildMI(BB, X86::ANDri32, 2, AlignedSize).addReg(AddedSizeReg).addZImm(~15);
2088
Brian Gaekee48ec012002-12-13 06:46:31 +00002089 // Subtract size from stack pointer, thereby allocating some space.
Chris Lattner3e130a22003-01-13 00:32:26 +00002090 BuildMI(BB, X86::SUBrr32, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002091
Brian Gaekee48ec012002-12-13 06:46:31 +00002092 // Put a pointer to the space into the result register, by copying
2093 // the stack pointer.
Chris Lattner065faeb2002-12-28 20:24:02 +00002094 BuildMI(BB, X86::MOVrr32, 1, getReg(I)).addReg(X86::ESP);
2095
Misha Brukman48196b32003-05-03 02:18:17 +00002096 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00002097 // object.
2098 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00002099}
Chris Lattner3e130a22003-01-13 00:32:26 +00002100
2101/// visitMallocInst - Malloc instructions are code generated into direct calls
2102/// to the library malloc.
2103///
2104void ISel::visitMallocInst(MallocInst &I) {
2105 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2106 unsigned Arg;
2107
2108 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2109 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2110 } else {
2111 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002112 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00002113 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002114 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00002115 }
2116
2117 std::vector<ValueRecord> Args;
2118 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2119 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002120 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002121 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
2122}
2123
2124
2125/// visitFreeInst - Free instructions are code gen'd to call the free libc
2126/// function.
2127///
2128void ISel::visitFreeInst(FreeInst &I) {
2129 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002130 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00002131 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002132 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002133 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
2134}
2135
Brian Gaeke20244b72002-12-12 15:33:40 +00002136
Chris Lattnerd281de22003-07-26 23:49:58 +00002137/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002138/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00002139/// generated code sucks but the implementation is nice and simple.
2140///
Brian Gaeke19df3872003-08-13 18:18:15 +00002141FunctionPass *createX86SimpleInstructionSelector(TargetMachine &TM) {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002142 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00002143}