blob: 2477da00200e6e91df7bf221d357007820af63bd [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 Lattner44827152003-12-28 09:47:19 +000021#include "llvm/IntrinsicLowering.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"
Chris Lattner94af4142002-12-25 05:13:53 +000026#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000027#include "llvm/Target/MRegisterInfo.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner3f1e8e72004-02-22 07:04:00 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000030#include "llvm/Support/InstVisitor.h"
Chris Lattnercf93cdd2004-01-30 22:13:44 +000031#include "llvm/Support/CFG.h"
Chris Lattner986618e2004-02-22 19:47:26 +000032#include "Support/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner986618e2004-02-22 19:47:26 +000035namespace {
36 Statistic<>
37 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
Chris Lattner427aeb42004-04-11 19:21:59 +000038
39 /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
40 /// Representation.
41 ///
42 enum TypeClass {
43 cByte, cShort, cInt, cFP, cLong
44 };
45}
46
47/// getClass - Turn a primitive type into a "class" number which is based on the
48/// size of the type, and whether or not it is floating point.
49///
50static inline TypeClass getClass(const Type *Ty) {
51 switch (Ty->getPrimitiveID()) {
52 case Type::SByteTyID:
53 case Type::UByteTyID: return cByte; // Byte operands are class #0
54 case Type::ShortTyID:
55 case Type::UShortTyID: return cShort; // Short operands are class #1
56 case Type::IntTyID:
57 case Type::UIntTyID:
58 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
59
60 case Type::FloatTyID:
61 case Type::DoubleTyID: return cFP; // Floating Point is #3
62
63 case Type::LongTyID:
64 case Type::ULongTyID: return cLong; // Longs are class #4
65 default:
66 assert(0 && "Invalid type to getClass!");
67 return cByte; // not reached
68 }
69}
70
71// getClassB - Just like getClass, but treat boolean values as bytes.
72static inline TypeClass getClassB(const Type *Ty) {
73 if (Ty == Type::BoolTy) return cByte;
74 return getClass(Ty);
Chris Lattner986618e2004-02-22 19:47:26 +000075}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000076
Chris Lattner72614082002-10-25 22:55:53 +000077namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 struct ISel : public FunctionPass, InstVisitor<ISel> {
79 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000080 MachineFunction *F; // The function we are compiling into
81 MachineBasicBlock *BB; // The current MBB we are compiling
82 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000083 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000084
Chris Lattner72614082002-10-25 22:55:53 +000085 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
86
Chris Lattner333b2fa2002-12-13 10:09:43 +000087 // MBBMap - Mapping between LLVM BB -> Machine BB
88 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
89
Chris Lattnerf70e0c22003-12-28 21:23:38 +000090 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000091
92 /// runOnFunction - Top level implementation of instruction selection for
93 /// the entire function.
94 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000095 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000096 // First pass over the function, lower any unknown intrinsic functions
97 // with the IntrinsicLowering class.
98 LowerUnknownIntrinsicFunctionCalls(Fn);
99
Chris Lattner36b36032002-10-29 23:40:58 +0000100 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000101
Chris Lattner065faeb2002-12-28 20:24:02 +0000102 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +0000103 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
104 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
105
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000106 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +0000107
Chris Lattner0e5b79c2004-02-15 01:04:03 +0000108 // Set up a frame object for the return address. This is used by the
109 // llvm.returnaddress & llvm.frameaddress intrinisics.
110 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
111
Chris Lattnerdbd73722003-05-06 21:32:22 +0000112 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000113 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000114
Chris Lattner333b2fa2002-12-13 10:09:43 +0000115 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000116 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000117
118 // Select the PHI nodes
119 SelectPHINodes();
120
Chris Lattner986618e2004-02-22 19:47:26 +0000121 // Insert the FP_REG_KILL instructions into blocks that need them.
122 InsertFPRegKills();
123
Chris Lattner72614082002-10-25 22:55:53 +0000124 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000125 MBBMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000126 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000127 // We always build a machine code representation for the function
128 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000129 }
130
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000131 virtual const char *getPassName() const {
132 return "X86 Simple Instruction Selection";
133 }
134
Chris Lattner72614082002-10-25 22:55:53 +0000135 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000136 /// block. This simply creates a new MachineBasicBlock to emit code into
137 /// and adds it to the current MachineFunction. Subsequent visit* for
138 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000139 ///
140 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000141 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000142 }
143
Chris Lattner44827152003-12-28 09:47:19 +0000144 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
145 /// function, lowering any calls to unknown intrinsic functions into the
146 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000147 ///
Chris Lattner44827152003-12-28 09:47:19 +0000148 void LowerUnknownIntrinsicFunctionCalls(Function &F);
149
Chris Lattner065faeb2002-12-28 20:24:02 +0000150 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
151 /// from the stack into virtual registers.
152 ///
153 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000154
155 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
156 /// because we have to generate our sources into the source basic blocks,
157 /// not the current one.
158 ///
159 void SelectPHINodes();
160
Chris Lattner986618e2004-02-22 19:47:26 +0000161 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
162 /// that need them. This only occurs due to the floating point stackifier
163 /// not being aggressive enough to handle arbitrary global stackification.
164 ///
165 void InsertFPRegKills();
166
Chris Lattner72614082002-10-25 22:55:53 +0000167 // Visitation methods for various instructions. These methods simply emit
168 // fixed X86 code for each instruction.
169 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000170
171 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000172 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000173 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000174
175 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000176 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000177 unsigned Reg;
178 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000179 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
180 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000181 };
182 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000183 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000184 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000185 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000186
187 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000188 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000189 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
190 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000191 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000192
Chris Lattnerf01729e2002-11-02 20:54:46 +0000193 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
194 void visitRem(BinaryOperator &B) { visitDivRem(B); }
195 void visitDivRem(BinaryOperator &B);
196
Chris Lattnere2954c82002-11-02 20:04:26 +0000197 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000198 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
199 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
200 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000201
Chris Lattner6d40c192003-01-16 16:43:00 +0000202 // Comparison operators...
203 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000204 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
205 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000206 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000207 void visitSelectInst(SelectInst &SI);
208
Chris Lattnerb2acc512003-10-19 21:09:10 +0000209
Chris Lattner6fc3c522002-11-17 21:11:55 +0000210 // Memory Instructions
211 void visitLoadInst(LoadInst &I);
212 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000213 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000214 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000215 void visitMallocInst(MallocInst &I);
216 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000217
Chris Lattnere2954c82002-11-02 20:04:26 +0000218 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000219 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000220 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000221 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000222 void visitVANextInst(VANextInst &I);
223 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000224
225 void visitInstruction(Instruction &I) {
226 std::cerr << "Cannot instruction select: " << I;
227 abort();
228 }
229
Brian Gaeke95780cc2002-12-13 07:56:18 +0000230 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000231 ///
232 void promote32(unsigned targetReg, const ValueRecord &VR);
233
Chris Lattner721d2d42004-03-08 01:18:36 +0000234 /// getAddressingMode - Get the addressing mode to use to address the
235 /// specified value. The returned value should be used with addFullAddress.
236 void getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
237 unsigned &IndexReg, unsigned &Disp);
238
239
240 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
241 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000242 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
243 std::vector<Value*> &GEPOps,
244 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
245 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
246
247 /// isGEPFoldable - Return true if the specified GEP can be completely
248 /// folded into the addressing mode of a load/store or lea instruction.
249 bool isGEPFoldable(MachineBasicBlock *MBB,
250 Value *Src, User::op_iterator IdxBegin,
251 User::op_iterator IdxEnd, unsigned &BaseReg,
252 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
253
Chris Lattner3e130a22003-01-13 00:32:26 +0000254 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
255 /// constant expression GEP support.
256 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000257 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000258 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000259 User::op_iterator IdxEnd, unsigned TargetReg);
260
Chris Lattner548f61d2003-04-23 17:22:12 +0000261 /// emitCastOperation - Common code shared between visitCastInst and
262 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000263 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000264 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000265 Value *Src, const Type *DestTy, unsigned TargetReg);
266
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000267 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
268 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000269 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000270 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000271 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000272 Value *Op0, Value *Op1,
273 unsigned OperatorClass, unsigned TargetReg);
274
Chris Lattner6621ed92004-04-11 21:23:56 +0000275 /// emitBinaryFPOperation - This method handles emission of floating point
276 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
277 void emitBinaryFPOperation(MachineBasicBlock *BB,
278 MachineBasicBlock::iterator IP,
279 Value *Op0, Value *Op1,
280 unsigned OperatorClass, unsigned TargetReg);
281
Chris Lattner462fa822004-04-11 20:56:28 +0000282 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
283 Value *Op0, Value *Op1, unsigned TargetReg);
284
285 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
286 unsigned DestReg, const Type *DestTy,
287 unsigned Op0Reg, unsigned Op1Reg);
288 void doMultiplyConst(MachineBasicBlock *MBB,
289 MachineBasicBlock::iterator MBBI,
290 unsigned DestReg, const Type *DestTy,
291 unsigned Op0Reg, unsigned Op1Val);
292
Chris Lattnercadff442003-10-23 17:21:43 +0000293 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000294 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000295 Value *Op0, Value *Op1, bool isDiv,
296 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000297
Chris Lattner58c41fe2003-08-24 19:19:47 +0000298 /// emitSetCCOperation - Common code shared between visitSetCondInst and
299 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000300 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000301 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000302 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000303 Value *Op0, Value *Op1, unsigned Opcode,
304 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000305
306 /// emitShiftOperation - Common code shared between visitShiftInst and
307 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000308 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000309 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000310 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000311 Value *Op, Value *ShiftAmount, bool isLeftShift,
312 const Type *ResultTy, unsigned DestReg);
313
Chris Lattner12d96a02004-03-30 21:22:00 +0000314 /// emitSelectOperation - Common code shared between visitSelectInst and the
315 /// constant expression support.
316 void emitSelectOperation(MachineBasicBlock *MBB,
317 MachineBasicBlock::iterator IP,
318 Value *Cond, Value *TrueVal, Value *FalseVal,
319 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000320
Chris Lattnerc5291f52002-10-27 21:16:59 +0000321 /// copyConstantToRegister - Output the instructions required to put the
322 /// specified constant into the specified register.
323 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000324 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000325 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000326 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000327
Chris Lattner3e130a22003-01-13 00:32:26 +0000328 /// makeAnotherReg - This method returns the next register number we haven't
329 /// yet used.
330 ///
331 /// Long values are handled somewhat specially. They are always allocated
332 /// as pairs of 32 bit integer values. The register number returned is the
333 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
334 /// of the long value.
335 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000336 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000337 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
338 "Current target doesn't have X86 reg info??");
339 const X86RegisterInfo *MRI =
340 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000341 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000342 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
343 // Create the lower part
344 F->getSSARegMap()->createVirtualRegister(RC);
345 // Create the upper part.
346 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000347 }
348
Chris Lattnerc0812d82002-12-13 06:56:29 +0000349 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000350 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000351 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000352 }
353
Chris Lattner72614082002-10-25 22:55:53 +0000354 /// getReg - This method turns an LLVM value into a register number. This
355 /// is guaranteed to produce the same register number for a particular value
356 /// every time it is queried.
357 ///
358 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000359 unsigned getReg(Value *V) {
360 // Just append to the end of the current bb.
361 MachineBasicBlock::iterator It = BB->end();
362 return getReg(V, BB, It);
363 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000364 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000365 MachineBasicBlock::iterator IPt) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000366 // If this operand is a constant, emit the code to copy the constant into
367 // the register here...
368 //
369 if (Constant *C = dyn_cast<Constant>(V)) {
370 unsigned Reg = makeAnotherReg(V->getType());
371 copyConstantToRegister(MBB, IPt, C, Reg);
372 return Reg;
373 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
374 unsigned Reg = makeAnotherReg(V->getType());
375 // Move the address of the global into the register
376 BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV);
377 return Reg;
378 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
379 // Do not emit noop casts at all.
380 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()))
381 return getReg(CI->getOperand(0), MBB, IPt);
382 }
383
Chris Lattner72614082002-10-25 22:55:53 +0000384 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000385 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000386 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000387 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000388 }
Chris Lattner72614082002-10-25 22:55:53 +0000389
Chris Lattner72614082002-10-25 22:55:53 +0000390 return Reg;
391 }
Chris Lattner72614082002-10-25 22:55:53 +0000392 };
393}
394
Chris Lattnerc5291f52002-10-27 21:16:59 +0000395/// copyConstantToRegister - Output the instructions required to put the
396/// specified constant into the specified register.
397///
Chris Lattner8a307e82002-12-16 19:32:50 +0000398void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000399 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000400 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000401 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000402 unsigned Class = 0;
403 switch (CE->getOpcode()) {
404 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000405 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000406 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000407 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000408 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000409 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000410 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000411
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000412 case Instruction::Xor: ++Class; // FALL THROUGH
413 case Instruction::Or: ++Class; // FALL THROUGH
414 case Instruction::And: ++Class; // FALL THROUGH
415 case Instruction::Sub: ++Class; // FALL THROUGH
416 case Instruction::Add:
417 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
418 Class, R);
419 return;
420
Chris Lattner462fa822004-04-11 20:56:28 +0000421 case Instruction::Mul:
422 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000423 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000424
Chris Lattnercadff442003-10-23 17:21:43 +0000425 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000426 case Instruction::Rem:
427 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
428 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000429 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000430
Chris Lattner58c41fe2003-08-24 19:19:47 +0000431 case Instruction::SetNE:
432 case Instruction::SetEQ:
433 case Instruction::SetLT:
434 case Instruction::SetGT:
435 case Instruction::SetLE:
436 case Instruction::SetGE:
437 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
438 CE->getOpcode(), R);
439 return;
440
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000441 case Instruction::Shl:
442 case Instruction::Shr:
443 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000444 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
445 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000446
Chris Lattner12d96a02004-03-30 21:22:00 +0000447 case Instruction::Select:
448 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
449 CE->getOperand(2), R);
450 return;
451
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000452 default:
453 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000454 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000455 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000456 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000457
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000458 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000459 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000460
461 if (Class == cLong) {
462 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000463 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000464 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
465 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000466 return;
467 }
468
Chris Lattner94af4142002-12-25 05:13:53 +0000469 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000470
471 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000472 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000473 };
474
Chris Lattner6b993cc2002-12-15 08:02:15 +0000475 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000476 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000477 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000478 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000479 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000480 }
Chris Lattner94af4142002-12-25 05:13:53 +0000481 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000482 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000483 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000484 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000485 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000486 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000487 // Otherwise we need to spill the constant to memory...
488 MachineConstantPool *CP = F->getConstantPool();
489 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000490 const Type *Ty = CFP->getType();
491
492 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000493 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000494 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000495 }
496
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000497 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000498 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000499 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000500 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000501 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000502 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000503 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000504 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000505 }
506}
507
Chris Lattner065faeb2002-12-28 20:24:02 +0000508/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
509/// the stack into virtual registers.
510///
511void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
512 // Emit instructions to load the arguments... On entry to a function on the
513 // X86, the stack frame looks like this:
514 //
515 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000516 // [ESP + 4] -- first argument (leftmost lexically)
517 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000518 // ...
519 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000520 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000521 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000522
523 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000524 bool ArgLive = !I->use_empty();
525 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000526 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000527
Chris Lattner065faeb2002-12-28 20:24:02 +0000528 switch (getClassB(I->getType())) {
529 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000530 if (ArgLive) {
531 FI = MFI->CreateFixedObject(1, ArgOffset);
532 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
533 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000534 break;
535 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000536 if (ArgLive) {
537 FI = MFI->CreateFixedObject(2, ArgOffset);
538 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
539 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000540 break;
541 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000542 if (ArgLive) {
543 FI = MFI->CreateFixedObject(4, ArgOffset);
544 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
545 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000546 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000547 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000548 if (ArgLive) {
549 FI = MFI->CreateFixedObject(8, ArgOffset);
550 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
551 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
552 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000553 ArgOffset += 4; // longs require 4 additional bytes
554 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000555 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000556 if (ArgLive) {
557 unsigned Opcode;
558 if (I->getType() == Type::FloatTy) {
559 Opcode = X86::FLD32m;
560 FI = MFI->CreateFixedObject(4, ArgOffset);
561 } else {
562 Opcode = X86::FLD64m;
563 FI = MFI->CreateFixedObject(8, ArgOffset);
564 }
565 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000566 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000567 if (I->getType() == Type::DoubleTy)
568 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000569 break;
570 default:
571 assert(0 && "Unhandled argument type!");
572 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000573 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000574 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000575
576 // If the function takes variable number of arguments, add a frame offset for
577 // the start of the first vararg value... this is used to expand
578 // llvm.va_start.
579 if (Fn.getFunctionType()->isVarArg())
580 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000581}
582
583
Chris Lattner333b2fa2002-12-13 10:09:43 +0000584/// SelectPHINodes - Insert machine code to generate phis. This is tricky
585/// because we have to generate our sources into the source basic blocks, not
586/// the current one.
587///
588void ISel::SelectPHINodes() {
Chris Lattner3501fea2003-01-14 22:00:31 +0000589 const TargetInstrInfo &TII = TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000590 const Function &LF = *F->getFunction(); // The LLVM function...
591 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
592 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000593 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000594
595 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000596 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000597 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000598 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000599
Chris Lattner333b2fa2002-12-13 10:09:43 +0000600 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000601 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000602 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
603 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000604
605 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000606 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
607 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
608 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000609
Chris Lattnera6e73f12003-05-12 14:22:21 +0000610 // PHIValues - Map of blocks to incoming virtual registers. We use this
611 // so that we only initialize one incoming value for a particular block,
612 // even if the block has multiple entries in the PHI node.
613 //
614 std::map<MachineBasicBlock*, unsigned> PHIValues;
615
Chris Lattner333b2fa2002-12-13 10:09:43 +0000616 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
617 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000618 unsigned ValReg;
619 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
620 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000621
Chris Lattnera6e73f12003-05-12 14:22:21 +0000622 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
623 // We already inserted an initialization of the register for this
624 // predecessor. Recycle it.
625 ValReg = EntryIt->second;
626
627 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000628 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000629 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000630 Value *Val = PN->getIncomingValue(i);
631
632 // If this is a constant or GlobalValue, we may have to insert code
633 // into the basic block to compute it into a virtual register.
634 if (isa<Constant>(Val) || isa<GlobalValue>(Val)) {
Chris Lattner6f2ab042004-03-30 19:10:12 +0000635 if (isa<ConstantExpr>(Val)) {
636 // Because we don't want to clobber any values which might be in
637 // physical registers with the computation of this constant (which
638 // might be arbitrarily complex if it is a constant expression),
639 // just insert the computation at the top of the basic block.
640 MachineBasicBlock::iterator PI = PredMBB->begin();
641
642 // Skip over any PHI nodes though!
643 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
644 ++PI;
645
646 ValReg = getReg(Val, PredMBB, PI);
647 } else {
648 // Simple constants get emitted at the end of the basic block,
649 // before any terminator instructions. We "know" that the code to
650 // move a constant into a register will never clobber any flags.
651 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
652 }
Chris Lattnera81fc682003-10-19 00:26:11 +0000653 } else {
654 ValReg = getReg(Val);
655 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000656
657 // Remember that we inserted a value for this PHI for this predecessor
658 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
659 }
660
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000661 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000662 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000663 if (LongPhiMI) {
664 LongPhiMI->addRegOperand(ValReg+1);
665 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
666 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000667 }
Chris Lattner168aa902004-02-29 07:10:16 +0000668
669 // Now that we emitted all of the incoming values for the PHI node, make
670 // sure to reposition the InsertPoint after the PHI that we just added.
671 // This is needed because we might have inserted a constant into this
672 // block, right after the PHI's which is before the old insert point!
673 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
674 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000675 }
676 }
677}
678
Chris Lattner986618e2004-02-22 19:47:26 +0000679/// RequiresFPRegKill - The floating point stackifier pass cannot insert
680/// compensation code on critical edges. As such, it requires that we kill all
681/// FP registers on the exit from any blocks that either ARE critical edges, or
682/// branch to a block that has incoming critical edges.
683///
684/// Note that this kill instruction will eventually be eliminated when
685/// restrictions in the stackifier are relaxed.
686///
687static bool RequiresFPRegKill(const BasicBlock *BB) {
688#if 0
689 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
690 const BasicBlock *Succ = *SI;
691 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
692 ++PI; // Block have at least one predecessory
693 if (PI != PE) { // If it has exactly one, this isn't crit edge
694 // If this block has more than one predecessor, check all of the
695 // predecessors to see if they have multiple successors. If so, then the
696 // block we are analyzing needs an FPRegKill.
697 for (PI = pred_begin(Succ); PI != PE; ++PI) {
698 const BasicBlock *Pred = *PI;
699 succ_const_iterator SI2 = succ_begin(Pred);
700 ++SI2; // There must be at least one successor of this block.
701 if (SI2 != succ_end(Pred))
702 return true; // Yes, we must insert the kill on this edge.
703 }
704 }
705 }
706 // If we got this far, there is no need to insert the kill instruction.
707 return false;
708#else
709 return true;
710#endif
711}
712
713// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
714// need them. This only occurs due to the floating point stackifier not being
715// aggressive enough to handle arbitrary global stackification.
716//
717// Currently we insert an FP_REG_KILL instruction into each block that uses or
718// defines a floating point virtual register.
719//
720// When the global register allocators (like linear scan) finally update live
721// variable analysis, we can keep floating point values in registers across
722// portions of the CFG that do not involve critical edges. This will be a big
723// win, but we are waiting on the global allocators before we can do this.
724//
725// With a bit of work, the floating point stackifier pass can be enhanced to
726// break critical edges as needed (to make a place to put compensation code),
727// but this will require some infrastructure improvements as well.
728//
729void ISel::InsertFPRegKills() {
730 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000731
732 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000733 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000734 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
735 MachineOperand& MO = I->getOperand(i);
736 if (MO.isRegister() && MO.getReg()) {
737 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000738 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000739 if (RegMap.getRegClass(Reg)->getSize() == 10)
740 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000741 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000742 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000743 // If we haven't found an FP register use or def in this basic block, check
744 // to see if any of our successors has an FP PHI node, which will cause a
745 // copy to be inserted into this block.
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000746 for (succ_const_iterator SI = succ_begin(BB->getBasicBlock()),
747 E = succ_end(BB->getBasicBlock()); SI != E; ++SI) {
748 MachineBasicBlock *SBB = MBBMap[*SI];
749 for (MachineBasicBlock::iterator I = SBB->begin();
750 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
751 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
752 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000753 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000754 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000755 continue;
756 UsesFPReg:
757 // Okay, this block uses an FP register. If the block has successors (ie,
758 // it's not an unwind/return), insert the FP_REG_KILL instruction.
759 if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
760 RequiresFPRegKill(BB->getBasicBlock())) {
Chris Lattneree352852004-02-29 07:22:16 +0000761 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000762 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000763 }
764 }
765}
766
767
Chris Lattner307ecba2004-03-30 22:39:09 +0000768// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
769// it into the conditional branch or select instruction which is the only user
770// of the cc instruction. This is the case if the conditional branch is the
771// only user of the setcc, and if the setcc is in the same basic block as the
772// conditional branch. We also don't handle long arguments below, so we reject
773// them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000774//
Chris Lattner307ecba2004-03-30 22:39:09 +0000775static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000776 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000777 if (SCI->hasOneUse()) {
778 Instruction *User = cast<Instruction>(SCI->use_back());
779 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
780 SCI->getParent() == User->getParent() &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000781 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
782 SCI->getOpcode() == Instruction::SetEQ ||
783 SCI->getOpcode() == Instruction::SetNE))
Chris Lattner6d40c192003-01-16 16:43:00 +0000784 return SCI;
785 }
786 return 0;
787}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000788
Chris Lattner6d40c192003-01-16 16:43:00 +0000789// Return a fixed numbering for setcc instructions which does not depend on the
790// order of the opcodes.
791//
792static unsigned getSetCCNumber(unsigned Opcode) {
793 switch(Opcode) {
794 default: assert(0 && "Unknown setcc instruction!");
795 case Instruction::SetEQ: return 0;
796 case Instruction::SetNE: return 1;
797 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000798 case Instruction::SetGE: return 3;
799 case Instruction::SetGT: return 4;
800 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000801 }
802}
Chris Lattner06925362002-11-17 21:56:38 +0000803
Chris Lattner6d40c192003-01-16 16:43:00 +0000804// LLVM -> X86 signed X86 unsigned
805// ----- ---------- ------------
806// seteq -> sete sete
807// setne -> setne setne
808// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000809// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000810// setgt -> setg seta
811// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000812// ----
813// sets // Used by comparison with 0 optimization
814// setns
815static const unsigned SetCCOpcodeTab[2][8] = {
816 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
817 0, 0 },
818 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
819 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000820};
821
Chris Lattnerb2acc512003-10-19 21:09:10 +0000822// EmitComparison - This function emits a comparison of the two operands,
823// returning the extended setcc code to use.
824unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
825 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000826 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000827 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000828 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000829 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000830 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000831
832 // Special case handling of: cmp R, i
Chris Lattnere80e6372004-04-06 16:02:27 +0000833 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
834 if (Class == cByte || Class == cShort || Class == cInt) {
835 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000836
Chris Lattner333864d2003-06-05 19:30:30 +0000837 // Mask off any upper bits of the constant, if there are any...
838 Op1v &= (1ULL << (8 << Class)) - 1;
839
Chris Lattnerb2acc512003-10-19 21:09:10 +0000840 // If this is a comparison against zero, emit more efficient code. We
841 // can't handle unsigned comparisons against zero unless they are == or
842 // !=. These should have been strength reduced already anyway.
843 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
844 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000845 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000846 };
Chris Lattneree352852004-02-29 07:22:16 +0000847 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000848
849 if (OpNum == 2) return 6; // Map jl -> js
850 if (OpNum == 3) return 7; // Map jg -> jns
851 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000852 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000853
854 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000855 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000856 };
857
Chris Lattneree352852004-02-29 07:22:16 +0000858 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000859 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000860 } else {
861 assert(Class == cLong && "Unknown integer class!");
862 unsigned LowCst = CI->getRawValue();
863 unsigned HiCst = CI->getRawValue() >> 32;
864 if (OpNum < 2) { // seteq, setne
865 unsigned LoTmp = Op0r;
866 if (LowCst != 0) {
867 LoTmp = makeAnotherReg(Type::IntTy);
868 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
869 }
870 unsigned HiTmp = Op0r+1;
871 if (HiCst != 0) {
872 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000873 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000874 }
875 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
876 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
877 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000878 } else {
879 // Emit a sequence of code which compares the high and low parts once
880 // each, then uses a conditional move to handle the overflow case. For
881 // example, a setlt for long would generate code like this:
882 //
883 // AL = lo(op1) < lo(op2) // Signedness depends on operands
884 // BL = hi(op1) < hi(op2) // Always unsigned comparison
885 // dest = hi(op1) == hi(op2) ? AL : BL;
886 //
887
888 // FIXME: This would be much better if we had hierarchical register
889 // classes! Until then, hardcode registers so that we can deal with
890 // their aliases (because we don't have conditional byte moves).
891 //
892 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
893 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
894 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
895 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
896 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
897 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
898 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
899 .addReg(X86::AX);
900 // NOTE: visitSetCondInst knows that the value is dumped into the BL
901 // register at this point for long values...
902 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000903 }
Chris Lattner333864d2003-06-05 19:30:30 +0000904 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000905 }
Chris Lattner333864d2003-06-05 19:30:30 +0000906
Chris Lattner9f08a922004-02-03 18:54:04 +0000907 // Special case handling of comparison against +/- 0.0
908 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
909 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000910 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000911 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000912 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +0000913 return OpNum;
914 }
915
Chris Lattner58c41fe2003-08-24 19:19:47 +0000916 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000917 switch (Class) {
918 default: assert(0 && "Unknown type class!");
919 // Emit: cmp <var1>, <var2> (do the comparison). We can
920 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
921 // 32-bit.
922 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000923 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000924 break;
925 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000926 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000927 break;
928 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000929 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000930 break;
931 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +0000932 BuildMI(*MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000933 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000934 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +0000935 break;
936
937 case cLong:
938 if (OpNum < 2) { // seteq, setne
939 unsigned LoTmp = makeAnotherReg(Type::IntTy);
940 unsigned HiTmp = makeAnotherReg(Type::IntTy);
941 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000942 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
943 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
944 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000945 break; // Allow the sete or setne to be generated from flags set by OR
946 } else {
947 // Emit a sequence of code which compares the high and low parts once
948 // each, then uses a conditional move to handle the overflow case. For
949 // example, a setlt for long would generate code like this:
950 //
951 // AL = lo(op1) < lo(op2) // Signedness depends on operands
952 // BL = hi(op1) < hi(op2) // Always unsigned comparison
953 // dest = hi(op1) == hi(op2) ? AL : BL;
954 //
955
Chris Lattner6d40c192003-01-16 16:43:00 +0000956 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000957 // classes! Until then, hardcode registers so that we can deal with their
958 // aliases (because we don't have conditional byte moves).
959 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000960 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +0000961 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000962 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +0000963 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
964 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
965 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000966 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +0000967 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000968 // NOTE: visitSetCondInst knows that the value is dumped into the BL
969 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000970 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000971 }
972 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000973 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000974}
Chris Lattner3e130a22003-01-13 00:32:26 +0000975
Chris Lattner6d40c192003-01-16 16:43:00 +0000976/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
977/// register, then move it to wherever the result should be.
978///
979void ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +0000980 if (canFoldSetCCIntoBranchOrSelect(&I))
981 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +0000982
Chris Lattner6d40c192003-01-16 16:43:00 +0000983 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000984 MachineBasicBlock::iterator MII = BB->end();
985 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
986 DestReg);
987}
Chris Lattner6d40c192003-01-16 16:43:00 +0000988
Chris Lattner58c41fe2003-08-24 19:19:47 +0000989/// emitSetCCOperation - Common code shared between visitSetCondInst and
990/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000991///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000992void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000993 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000994 Value *Op0, Value *Op1, unsigned Opcode,
995 unsigned TargetReg) {
996 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000997 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000998
Chris Lattnerb2acc512003-10-19 21:09:10 +0000999 const Type *CompTy = Op0->getType();
1000 unsigned CompClass = getClassB(CompTy);
1001 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1002
1003 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001004 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001005 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001006 } else {
1007 // Handle long comparisons by copying the value which is already in BL into
1008 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001009 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001010 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001011}
Chris Lattner51b49a92002-11-02 19:45:49 +00001012
Chris Lattner12d96a02004-03-30 21:22:00 +00001013void ISel::visitSelectInst(SelectInst &SI) {
1014 unsigned DestReg = getReg(SI);
1015 MachineBasicBlock::iterator MII = BB->end();
1016 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1017 SI.getFalseValue(), DestReg);
1018}
1019
1020/// emitSelect - Common code shared between visitSelectInst and the constant
1021/// expression support.
1022void ISel::emitSelectOperation(MachineBasicBlock *MBB,
1023 MachineBasicBlock::iterator IP,
1024 Value *Cond, Value *TrueVal, Value *FalseVal,
1025 unsigned DestReg) {
1026 unsigned SelectClass = getClassB(TrueVal->getType());
1027
1028 // We don't support 8-bit conditional moves. If we have incoming constants,
1029 // transform them into 16-bit constants to avoid having a run-time conversion.
1030 if (SelectClass == cByte) {
1031 if (Constant *T = dyn_cast<Constant>(TrueVal))
1032 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1033 if (Constant *F = dyn_cast<Constant>(FalseVal))
1034 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1035 }
1036
Chris Lattner307ecba2004-03-30 22:39:09 +00001037
1038 unsigned Opcode;
1039 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1040 // We successfully folded the setcc into the select instruction.
1041
1042 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1043 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1044 IP);
1045
1046 const Type *CompTy = SCI->getOperand(0)->getType();
1047 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1048
1049 // LLVM -> X86 signed X86 unsigned
1050 // ----- ---------- ------------
1051 // seteq -> cmovNE cmovNE
1052 // setne -> cmovE cmovE
1053 // setlt -> cmovGE cmovAE
1054 // setge -> cmovL cmovB
1055 // setgt -> cmovLE cmovBE
1056 // setle -> cmovG cmovA
1057 // ----
1058 // cmovNS // Used by comparison with 0 optimization
1059 // cmovS
1060
1061 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001062 default: assert(0 && "Unknown value class!");
1063 case cFP: {
1064 // Annoyingly, we don't have a full set of floating point conditional
1065 // moves. :(
1066 static const unsigned OpcodeTab[2][8] = {
1067 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1068 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1069 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1070 };
1071 Opcode = OpcodeTab[isSigned][OpNum];
1072
1073 // If opcode == 0, we hit a case that we don't support. Output a setcc
1074 // and compare the result against zero.
1075 if (Opcode == 0) {
1076 unsigned CompClass = getClassB(CompTy);
1077 unsigned CondReg;
1078 if (CompClass != cLong || OpNum < 2) {
1079 CondReg = makeAnotherReg(Type::BoolTy);
1080 // Handle normal comparisons with a setcc instruction...
1081 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1082 } else {
1083 // Long comparisons end up in the BL register.
1084 CondReg = X86::BL;
1085 }
1086
Chris Lattner68626c22004-03-31 22:22:36 +00001087 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001088 Opcode = X86::FCMOVE;
1089 }
1090 break;
1091 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001092 case cByte:
1093 case cShort: {
1094 static const unsigned OpcodeTab[2][8] = {
1095 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1096 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1097 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1098 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1099 };
1100 Opcode = OpcodeTab[isSigned][OpNum];
1101 break;
1102 }
1103 case cInt:
1104 case cLong: {
1105 static const unsigned OpcodeTab[2][8] = {
1106 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1107 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1108 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1109 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1110 };
1111 Opcode = OpcodeTab[isSigned][OpNum];
1112 break;
1113 }
1114 }
1115 } else {
1116 // Get the value being branched on, and use it to set the condition codes.
1117 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001118 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001119 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001120 default: assert(0 && "Unknown value class!");
1121 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001122 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001123 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001124 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001125 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001126 }
1127 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001128
1129 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1130 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1131 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001132
Chris Lattner12d96a02004-03-30 21:22:00 +00001133
1134 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1135 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1136 // cmove, then truncate the result.
1137 if (SelectClass == cByte) {
1138 DestReg = makeAnotherReg(Type::ShortTy);
1139 if (getClassB(TrueVal->getType()) == cByte) {
1140 // Promote the true value, by storing it into AL, and reading from AX.
1141 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1142 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1143 TrueReg = makeAnotherReg(Type::ShortTy);
1144 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1145 }
1146 if (getClassB(FalseVal->getType()) == cByte) {
1147 // Promote the true value, by storing it into CL, and reading from CX.
1148 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1149 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1150 FalseReg = makeAnotherReg(Type::ShortTy);
1151 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1152 }
1153 }
1154
1155 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1156
1157 switch (SelectClass) {
1158 case cByte:
1159 // We did the computation with 16-bit registers. Truncate back to our
1160 // result by copying into AX then copying out AL.
1161 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1162 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1163 break;
1164 case cLong:
1165 // Move the upper half of the value as well.
1166 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1167 break;
1168 }
1169}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001170
1171
1172
Brian Gaekec2505982002-11-30 11:57:28 +00001173/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1174/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001175///
Chris Lattner3e130a22003-01-13 00:32:26 +00001176void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
1177 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001178
Chris Lattner29bf0622004-04-06 01:21:00 +00001179 Value *Val = VR.Val;
1180 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001181 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001182 if (Constant *C = dyn_cast<Constant>(Val)) {
1183 Val = ConstantExpr::getCast(C, Type::IntTy);
1184 Ty = Type::IntTy;
1185 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001186
Chris Lattner502e36c2004-04-06 01:25:33 +00001187 // If this is a simple constant, just emit a MOVri directly to avoid the
1188 // copy.
1189 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1190 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
1191 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
1192 return;
1193 }
1194 }
1195
Chris Lattner29bf0622004-04-06 01:21:00 +00001196 // Make sure we have the register number for this value...
1197 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1198
1199 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001200 case cByte:
1201 // Extend value into target register (8->32)
1202 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001203 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001204 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001205 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001206 break;
1207 case cShort:
1208 // Extend value into target register (16->32)
1209 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001210 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001211 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001212 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001213 break;
1214 case cInt:
1215 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001216 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001217 break;
1218 default:
1219 assert(0 && "Unpromotable operand class in promote32");
1220 }
Brian Gaekec2505982002-11-30 11:57:28 +00001221}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001222
Chris Lattner72614082002-10-25 22:55:53 +00001223/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1224/// we have the following possibilities:
1225///
1226/// ret void: No return value, simply emit a 'ret' instruction
1227/// ret sbyte, ubyte : Extend value into EAX and return
1228/// ret short, ushort: Extend value into EAX and return
1229/// ret int, uint : Move value into EAX and return
1230/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001231/// ret long, ulong : Move value into EAX/EDX and return
1232/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001233///
Chris Lattner3e130a22003-01-13 00:32:26 +00001234void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001235 if (I.getNumOperands() == 0) {
1236 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1237 return;
1238 }
1239
1240 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001241 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001242 case cByte: // integral return values: extend or move into EAX and return
1243 case cShort:
1244 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001245 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001246 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001247 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001248 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001249 case cFP: { // Floats & Doubles: Return in ST(0)
1250 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001251 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001252 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001253 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001254 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001255 }
1256 case cLong: {
1257 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001258 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1259 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001260 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001261 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1262 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001263 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001264 }
Chris Lattner94af4142002-12-25 05:13:53 +00001265 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001266 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001267 }
Chris Lattner43189d12002-11-17 20:07:45 +00001268 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001269 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001270}
1271
Chris Lattner55f6fab2003-01-16 18:07:23 +00001272// getBlockAfter - Return the basic block which occurs lexically after the
1273// specified one.
1274static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1275 Function::iterator I = BB; ++I; // Get iterator to next block
1276 return I != BB->getParent()->end() ? &*I : 0;
1277}
1278
Chris Lattner51b49a92002-11-02 19:45:49 +00001279/// visitBranchInst - Handle conditional and unconditional branches here. Note
1280/// that since code layout is frozen at this point, that if we are trying to
1281/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001282/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001283///
Chris Lattner94af4142002-12-25 05:13:53 +00001284void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +00001285 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1286
1287 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001288 if (BI.getSuccessor(0) != NextBB)
Chris Lattner55f6fab2003-01-16 18:07:23 +00001289 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +00001290 return;
1291 }
1292
1293 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001294 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001295 if (SCI == 0) {
1296 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1297 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001298 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001299 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001300 if (BI.getSuccessor(1) == NextBB) {
1301 if (BI.getSuccessor(0) != NextBB)
1302 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
1303 } else {
1304 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
1305
1306 if (BI.getSuccessor(0) != NextBB)
1307 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
1308 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001309 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001310 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001311
1312 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001313 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001314 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001315
1316 const Type *CompTy = SCI->getOperand(0)->getType();
1317 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001318
Chris Lattnerb2acc512003-10-19 21:09:10 +00001319
Chris Lattner6d40c192003-01-16 16:43:00 +00001320 // LLVM -> X86 signed X86 unsigned
1321 // ----- ---------- ------------
1322 // seteq -> je je
1323 // setne -> jne jne
1324 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001325 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001326 // setgt -> jg ja
1327 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001328 // ----
1329 // js // Used by comparison with 0 optimization
1330 // jns
1331
1332 static const unsigned OpcodeTab[2][8] = {
1333 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1334 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1335 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001336 };
1337
Chris Lattner55f6fab2003-01-16 18:07:23 +00001338 if (BI.getSuccessor(0) != NextBB) {
1339 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
1340 if (BI.getSuccessor(1) != NextBB)
1341 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
1342 } else {
1343 // Change to the inverse condition...
1344 if (BI.getSuccessor(1) != NextBB) {
1345 OpNum ^= 1;
1346 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
1347 }
1348 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001349}
1350
Chris Lattner3e130a22003-01-13 00:32:26 +00001351
1352/// doCall - This emits an abstract call instruction, setting up the arguments
1353/// and the return value as appropriate. For the actual function call itself,
1354/// it inserts the specified CallMI instruction into the stream.
1355///
1356void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001357 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001358
Chris Lattner065faeb2002-12-28 20:24:02 +00001359 // Count how many bytes are to be pushed on the stack...
1360 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001361
Chris Lattner3e130a22003-01-13 00:32:26 +00001362 if (!Args.empty()) {
1363 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1364 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001365 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001366 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001367 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001368 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001369 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001370 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1371 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001372 default: assert(0 && "Unknown class!");
1373 }
1374
1375 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001376 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001377
1378 // Arguments go on the stack in reverse order, as specified by the ABI.
1379 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001380 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001381 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001382 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001383 case cByte:
Chris Lattner21585222004-03-01 02:42:43 +00001384 case cShort:
1385 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1386 // Zero/Sign extend constant, then stuff into memory.
1387 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1388 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1389 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1390 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1391 } else {
1392 // Promote arg to 32 bits wide into a temporary register...
1393 ArgReg = makeAnotherReg(Type::UIntTy);
1394 promote32(ArgReg, Args[i]);
1395 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1396 X86::ESP, ArgOffset).addReg(ArgReg);
1397 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001398 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001399 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001400 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1401 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1402 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1403 X86::ESP, ArgOffset).addImm(Val);
1404 } else {
1405 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1406 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1407 X86::ESP, ArgOffset).addReg(ArgReg);
1408 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001409 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001410 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001411 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1412 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1413 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1414 X86::ESP, ArgOffset).addImm(Val & ~0U);
1415 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1416 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1417 } else {
1418 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1419 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1420 X86::ESP, ArgOffset).addReg(ArgReg);
1421 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1422 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1423 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001424 ArgOffset += 4; // 8 byte entry, not 4.
1425 break;
1426
Chris Lattner065faeb2002-12-28 20:24:02 +00001427 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001428 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001429 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001430 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001431 X86::ESP, ArgOffset).addReg(ArgReg);
1432 } else {
1433 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001434 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001435 X86::ESP, ArgOffset).addReg(ArgReg);
1436 ArgOffset += 4; // 8 byte entry, not 4.
1437 }
1438 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001439
Chris Lattner3e130a22003-01-13 00:32:26 +00001440 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001441 }
1442 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001443 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001444 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001445 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001446 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001447
Chris Lattner3e130a22003-01-13 00:32:26 +00001448 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001449
Chris Lattneree352852004-02-29 07:22:16 +00001450 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001451
1452 // If there is a return value, scavenge the result from the location the call
1453 // leaves it in...
1454 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001455 if (Ret.Ty != Type::VoidTy) {
1456 unsigned DestClass = getClassB(Ret.Ty);
1457 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001458 case cByte:
1459 case cShort:
1460 case cInt: {
1461 // Integral results are in %eax, or the appropriate portion
1462 // thereof.
1463 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001464 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001465 };
1466 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001467 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001468 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001469 }
Chris Lattner94af4142002-12-25 05:13:53 +00001470 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001471 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001472 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001473 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001474 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1475 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001476 break;
1477 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001478 }
Chris Lattnera3243642002-12-04 23:45:28 +00001479 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001480}
Chris Lattner2df035b2002-11-02 19:27:56 +00001481
Chris Lattner3e130a22003-01-13 00:32:26 +00001482
1483/// visitCallInst - Push args on stack and do a procedure call instruction.
1484void ISel::visitCallInst(CallInst &CI) {
1485 MachineInstr *TheCall;
1486 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001487 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001488 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001489 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1490 return;
1491 }
1492
Chris Lattner3e130a22003-01-13 00:32:26 +00001493 // Emit a CALL instruction with PC-relative displacement.
1494 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1495 } else { // Emit an indirect call...
1496 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001497 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001498 }
1499
1500 std::vector<ValueRecord> Args;
1501 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001502 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001503
1504 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1505 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001506}
Chris Lattner3e130a22003-01-13 00:32:26 +00001507
Chris Lattneraeb54b82003-08-28 21:23:43 +00001508
Chris Lattner44827152003-12-28 09:47:19 +00001509/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1510/// function, lowering any calls to unknown intrinsic functions into the
1511/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001512///
Chris Lattner44827152003-12-28 09:47:19 +00001513void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1514 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1515 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1516 if (CallInst *CI = dyn_cast<CallInst>(I++))
1517 if (Function *F = CI->getCalledFunction())
1518 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001519 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001520 case Intrinsic::vastart:
1521 case Intrinsic::vacopy:
1522 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001523 case Intrinsic::returnaddress:
1524 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001525 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001526 case Intrinsic::memset:
John Criswell4ffff9e2004-04-08 20:31:47 +00001527 case Intrinsic::readport:
1528 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001529 // We directly implement these intrinsics
1530 break;
1531 default:
1532 // All other intrinsic calls we must lower.
1533 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001534 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001535 if (Before) { // Move iterator to instruction after call
1536 I = Before; ++I;
1537 } else {
1538 I = BB->begin();
1539 }
1540 }
1541
1542}
1543
Brian Gaeked0fde302003-11-11 22:41:34 +00001544void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001545 unsigned TmpReg1, TmpReg2;
1546 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001547 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001548 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001549 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001550 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001551 return;
1552
Chris Lattner5634b9f2004-03-13 00:24:52 +00001553 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001554 TmpReg1 = getReg(CI);
1555 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001556 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001557 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001558 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001559
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001560 case Intrinsic::returnaddress:
1561 case Intrinsic::frameaddress:
1562 TmpReg1 = getReg(CI);
1563 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1564 if (ID == Intrinsic::returnaddress) {
1565 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001566 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001567 ReturnAddressIndex);
1568 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001569 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001570 ReturnAddressIndex, -4);
1571 }
1572 } else {
1573 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001574 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001575 }
1576 return;
1577
Chris Lattner915e5e52004-02-12 17:53:22 +00001578 case Intrinsic::memcpy: {
1579 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1580 unsigned Align = 1;
1581 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1582 Align = AlignC->getRawValue();
1583 if (Align == 0) Align = 1;
1584 }
1585
1586 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001587 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001588 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001589 switch (Align & 3) {
1590 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001591 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1592 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1593 } else {
1594 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001595 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001596 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001597 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001598 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001599 break;
1600 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001601 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1602 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1603 } else {
1604 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001605 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001606 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001607 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001608 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001609 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001610 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001611 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001612 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001613 break;
1614 }
1615
1616 // No matter what the alignment is, we put the source in ESI, the
1617 // destination in EDI, and the count in ECX.
1618 TmpReg1 = getReg(CI.getOperand(1));
1619 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001620 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1621 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1622 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001623 BuildMI(BB, Opcode, 0);
1624 return;
1625 }
1626 case Intrinsic::memset: {
1627 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1628 unsigned Align = 1;
1629 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1630 Align = AlignC->getRawValue();
1631 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001632 }
1633
Chris Lattner2a0f2242004-02-14 04:46:05 +00001634 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001635 unsigned CountReg;
1636 unsigned Opcode;
1637 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1638 unsigned Val = ValC->getRawValue() & 255;
1639
1640 // If the value is a constant, then we can potentially use larger copies.
1641 switch (Align & 3) {
1642 case 2: // WORD aligned
1643 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001644 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001645 } else {
1646 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001647 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001648 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001649 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001650 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001651 Opcode = X86::REP_STOSW;
1652 break;
1653 case 0: // DWORD aligned
1654 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001655 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001656 } else {
1657 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001658 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001659 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001660 }
1661 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001662 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001663 Opcode = X86::REP_STOSD;
1664 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001665 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001666 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001667 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001668 Opcode = X86::REP_STOSB;
1669 break;
1670 }
1671 } else {
1672 // If it's not a constant value we are storing, just fall back. We could
1673 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1674 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001675 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001676 CountReg = getReg(CI.getOperand(3));
1677 Opcode = X86::REP_STOSB;
1678 }
1679
1680 // No matter what the alignment is, we put the source in ESI, the
1681 // destination in EDI, and the count in ECX.
1682 TmpReg1 = getReg(CI.getOperand(1));
1683 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001684 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1685 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001686 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001687 return;
1688 }
1689
John Criswell4ffff9e2004-04-08 20:31:47 +00001690 case Intrinsic::readport:
1691 //
1692 // First, determine that the size of the operand falls within the
1693 // acceptable range for this architecture.
1694 //
John Criswellca6ea0f2004-04-08 22:39:13 +00001695 if ((CI.getOperand(1)->getType()->getPrimitiveSize()) != 2) {
1696 std::cerr << "llvm.readport: Address size is not 16 bits\n";
1697 exit (1);
1698 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001699
1700 //
1701 // Now, move the I/O port address into the DX register and use the IN
1702 // instruction to get the input data.
1703 //
1704 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(getReg(CI.getOperand(1)));
1705 switch (CI.getCalledFunction()->getReturnType()->getPrimitiveSize()) {
1706 case 1:
John Criswellca6ea0f2004-04-08 22:39:13 +00001707 BuildMI(BB, X86::IN8, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001708 break;
1709 case 2:
John Criswellca6ea0f2004-04-08 22:39:13 +00001710 BuildMI(BB, X86::IN16, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001711 break;
1712 case 4:
John Criswellca6ea0f2004-04-08 22:39:13 +00001713 BuildMI(BB, X86::IN32, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001714 break;
1715 default:
John Criswellaee0cf32004-04-09 15:10:15 +00001716 std::cerr << "Cannot do input on this data type";
1717 exit (1);
John Criswell4ffff9e2004-04-08 20:31:47 +00001718 }
1719 return;
1720
1721 case Intrinsic::writeport:
1722 //
1723 // First, determine that the size of the operand falls within the
1724 // acceptable range for this architecture.
1725 //
John Criswellca6ea0f2004-04-08 22:39:13 +00001726 //
John Criswell6d804f42004-04-09 19:09:14 +00001727 if ((CI.getOperand(2)->getType()->getPrimitiveSize()) != 2) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001728 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1729 exit (1);
1730 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001731
1732 //
1733 // Now, move the I/O port address into the DX register and the value to
1734 // write into the AL/AX/EAX register.
1735 //
John Criswell6d804f42004-04-09 19:09:14 +00001736 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(getReg(CI.getOperand(2)));
1737 switch (CI.getOperand(1)->getType()->getPrimitiveSize()) {
John Criswell4ffff9e2004-04-08 20:31:47 +00001738 case 1:
John Criswell6d804f42004-04-09 19:09:14 +00001739 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(getReg(CI.getOperand(1)));
John Criswellca6ea0f2004-04-08 22:39:13 +00001740 BuildMI(BB, X86::OUT8, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001741 break;
1742 case 2:
John Criswell6d804f42004-04-09 19:09:14 +00001743 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(getReg(CI.getOperand(1)));
John Criswellca6ea0f2004-04-08 22:39:13 +00001744 BuildMI(BB, X86::OUT16, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001745 break;
1746 case 4:
John Criswell6d804f42004-04-09 19:09:14 +00001747 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(getReg(CI.getOperand(1)));
John Criswellca6ea0f2004-04-08 22:39:13 +00001748 BuildMI(BB, X86::OUT32, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001749 break;
1750 default:
John Criswellaee0cf32004-04-09 15:10:15 +00001751 std::cerr << "Cannot do output on this data type";
1752 exit (1);
John Criswell4ffff9e2004-04-08 20:31:47 +00001753 }
1754 return;
1755
Chris Lattner44827152003-12-28 09:47:19 +00001756 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001757 }
1758}
1759
Chris Lattner7dee5da2004-03-08 01:58:35 +00001760static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1761 if (LI.getParent() != User.getParent())
1762 return false;
1763 BasicBlock::iterator It = &LI;
1764 // Check all of the instructions between the load and the user. We should
1765 // really use alias analysis here, but for now we just do something simple.
1766 for (++It; It != BasicBlock::iterator(&User); ++It) {
1767 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001768 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001769 case Instruction::Store:
1770 case Instruction::Call:
1771 case Instruction::Invoke:
1772 return false;
1773 }
1774 }
1775 return true;
1776}
1777
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001778/// visitSimpleBinary - Implement simple binary operators for integral types...
1779/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1780/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001781///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001782void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1783 unsigned DestReg = getReg(B);
1784 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001785 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1786
Chris Lattner7dee5da2004-03-08 01:58:35 +00001787 // Special case: op Reg, load [mem]
1788 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
1789 if (!B.swapOperands())
1790 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1791
1792 unsigned Class = getClassB(B.getType());
Chris Lattner95157f72004-04-11 22:05:45 +00001793 if (isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001794 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1795
Chris Lattner95157f72004-04-11 22:05:45 +00001796 unsigned Opcode;
1797 if (Class != cFP) {
1798 static const unsigned OpcodeTab[][3] = {
1799 // Arithmetic operators
1800 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1801 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1802
1803 // Bitwise operators
1804 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1805 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1806 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1807 };
1808 Opcode = OpcodeTab[OperatorClass][Class];
1809 } else {
1810 static const unsigned OpcodeTab[][2] = {
1811 { X86::FADD32m, X86::FADD64m }, // ADD
1812 { X86::FSUB32m, X86::FSUB64m }, // SUB
1813 };
1814 const Type *Ty = Op0->getType();
1815 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1816 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
1817 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00001818
1819 unsigned BaseReg, Scale, IndexReg, Disp;
1820 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), BaseReg,
1821 Scale, IndexReg, Disp);
1822
1823 unsigned Op0r = getReg(Op0);
1824 addFullAddress(BuildMI(BB, Opcode, 2, DestReg).addReg(Op0r),
1825 BaseReg, Scale, IndexReg, Disp);
1826 return;
1827 }
1828
Chris Lattner95157f72004-04-11 22:05:45 +00001829 // If this is a floating point subtract, check to see if we can fold the first
1830 // operand in.
1831 if (Class == cFP && OperatorClass == 1 &&
1832 isa<LoadInst>(Op0) &&
1833 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
1834 const Type *Ty = Op0->getType();
1835 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1836 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
1837
1838 unsigned BaseReg, Scale, IndexReg, Disp;
1839 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), BaseReg,
1840 Scale, IndexReg, Disp);
1841
1842 unsigned Op1r = getReg(Op1);
1843 addFullAddress(BuildMI(BB, Opcode, 2, DestReg).addReg(Op1r),
1844 BaseReg, Scale, IndexReg, Disp);
1845 return;
1846 }
1847
Chris Lattner721d2d42004-03-08 01:18:36 +00001848 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001849}
Chris Lattner3e130a22003-01-13 00:32:26 +00001850
Chris Lattner6621ed92004-04-11 21:23:56 +00001851
1852/// emitBinaryFPOperation - This method handles emission of floating point
1853/// Add (0), Sub (1), Mul (2), and Div (3) operations.
1854void ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
1855 MachineBasicBlock::iterator IP,
1856 Value *Op0, Value *Op1,
1857 unsigned OperatorClass, unsigned DestReg) {
1858
1859 // Special case: op Reg, <const fp>
1860 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
1861 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
1862 // Create a constant pool entry for this constant.
1863 MachineConstantPool *CP = F->getConstantPool();
1864 unsigned CPI = CP->getConstantPoolIndex(Op1C);
1865 const Type *Ty = Op1->getType();
1866
1867 static const unsigned OpcodeTab[][4] = {
1868 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
1869 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
1870 };
1871
1872 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1873 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1874 unsigned Op0r = getReg(Op0, BB, IP);
1875 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
1876 DestReg).addReg(Op0r), CPI);
1877 return;
1878 }
1879
Chris Lattner13c07fe2004-04-12 00:12:04 +00001880 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00001881 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1882 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
1883 // -0.0 - X === -X
1884 unsigned op1Reg = getReg(Op1, BB, IP);
1885 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
1886 return;
1887 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00001888 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00001889
1890 // Create a constant pool entry for this constant.
1891 MachineConstantPool *CP = F->getConstantPool();
1892 unsigned CPI = CP->getConstantPoolIndex(CFP);
1893 const Type *Ty = CFP->getType();
1894
1895 static const unsigned OpcodeTab[][4] = {
1896 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
1897 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
1898 };
1899
1900 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
1901 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1902 unsigned Op1r = getReg(Op1, BB, IP);
1903 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
1904 DestReg).addReg(Op1r), CPI);
1905 return;
1906 }
1907
1908 // General case.
1909 static const unsigned OpcodeTab[4] = {
1910 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
1911 };
1912
1913 unsigned Opcode = OpcodeTab[OperatorClass];
1914 unsigned Op0r = getReg(Op0, BB, IP);
1915 unsigned Op1r = getReg(Op1, BB, IP);
1916 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1917}
1918
Chris Lattnerb2acc512003-10-19 21:09:10 +00001919/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1920/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1921/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001922///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001923/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1924/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001925///
1926void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001927 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001928 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001929 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001930 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001931
Chris Lattner6621ed92004-04-11 21:23:56 +00001932 if (Class == cFP) {
1933 assert(OperatorClass < 2 && "No logical ops for FP!");
1934 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
1935 return;
1936 }
1937
Chris Lattnerb2acc512003-10-19 21:09:10 +00001938 // sub 0, X -> neg X
Chris Lattner48b0c972004-04-11 20:26:20 +00001939 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
1940 if (OperatorClass == 1 && CI->isNullValue()) {
1941 unsigned op1Reg = getReg(Op1, MBB, IP);
1942 static unsigned const NEGTab[] = {
1943 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
1944 };
1945 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
1946
1947 if (Class == cLong) {
1948 // We just emitted: Dl = neg Sl
1949 // Now emit : T = addc Sh, 0
1950 // : Dh = neg T
1951 unsigned T = makeAnotherReg(Type::IntTy);
1952 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
1953 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001954 }
Chris Lattner48b0c972004-04-11 20:26:20 +00001955 return;
1956 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001957
Chris Lattner48b0c972004-04-11 20:26:20 +00001958 // Special case: op Reg, <const int>
1959 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00001960 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001961
Chris Lattner721d2d42004-03-08 01:18:36 +00001962 // xor X, -1 -> not X
1963 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001964 static unsigned const NOTTab[] = {
1965 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
1966 };
Chris Lattner721d2d42004-03-08 01:18:36 +00001967 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001968 if (Class == cLong) // Invert the top part too
1969 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00001970 return;
1971 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001972
Chris Lattner721d2d42004-03-08 01:18:36 +00001973 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00001974 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
1975 // Note that we can't use dec for 64-bit decrements, because it does not
1976 // set the carry flag!
1977 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00001978 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1979 return;
1980 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001981
Chris Lattner721d2d42004-03-08 01:18:36 +00001982 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00001983 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
1984 // Note that we can't use inc for 64-bit increments, because it does not
1985 // set the carry flag!
1986 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00001987 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00001988 return;
1989 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001990
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001991 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00001992 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001993 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
1994 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00001995
Chris Lattner721d2d42004-03-08 01:18:36 +00001996 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001997 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
1998 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
1999 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002000 };
2001
Chris Lattner721d2d42004-03-08 01:18:36 +00002002 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002003 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002004
Chris Lattner33f7fa32004-04-06 03:15:53 +00002005 if (Class != cLong) {
2006 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2007 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002008 }
2009
2010 // If this is a long value and the high or low bits have a special
2011 // property, emit some special cases.
2012 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2013
2014 // If the constant is zero in the low 32-bits, just copy the low part
2015 // across and apply the normal 32-bit operation to the high parts. There
2016 // will be no carry or borrow into the top.
2017 if (Op1l == 0) {
2018 if (OperatorClass != 2) // All but and...
2019 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2020 else
2021 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2022 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2023 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002024 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002025 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002026
2027 // If this is a logical operation and the top 32-bits are zero, just
2028 // operate on the lower 32.
2029 if (Op1h == 0 && OperatorClass > 1) {
2030 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2031 .addReg(Op0r).addImm(Op1l);
2032 if (OperatorClass != 2) // All but and
2033 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2034 else
2035 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2036 return;
2037 }
2038
2039 // TODO: We could handle lots of other special cases here, such as AND'ing
2040 // with 0xFFFFFFFF00000000 -> noop, etc.
2041
2042 // Otherwise, code generate the full operation with a constant.
2043 static const unsigned TopTab[] = {
2044 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2045 };
2046
2047 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2048 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2049 .addReg(Op0r+1).addImm(Op1h);
2050 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002051 }
2052
2053 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002054 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002055 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002056 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2057 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002058
Chris Lattnerb2acc512003-10-19 21:09:10 +00002059 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002060 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2061 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2062 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002063 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002064
Chris Lattnerb2acc512003-10-19 21:09:10 +00002065 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002066 unsigned Op0r = getReg(Op0, MBB, IP);
2067 unsigned Op1r = getReg(Op1, MBB, IP);
2068 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2069
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002070 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002071 static const unsigned TopTab[] = {
2072 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2073 };
2074 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2075 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2076 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002077}
2078
Chris Lattner3e130a22003-01-13 00:32:26 +00002079/// doMultiply - Emit appropriate instructions to multiply together the
2080/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2081/// result should be given as DestTy.
2082///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002083void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00002084 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00002085 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002086 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002087 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002088 case cInt:
2089 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002090 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002091 .addReg(op0Reg).addReg(op1Reg);
2092 return;
2093 case cByte:
2094 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002095 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2096 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2097 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002098 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002099 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002100 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002101 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002102}
2103
Chris Lattnerb2acc512003-10-19 21:09:10 +00002104// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2105// returns zero when the input is not exactly a power of two.
2106static unsigned ExactLog2(unsigned Val) {
2107 if (Val == 0) return 0;
2108 unsigned Count = 0;
2109 while (Val != 1) {
2110 if (Val & 1) return 0;
2111 Val >>= 1;
2112 ++Count;
2113 }
2114 return Count+1;
2115}
2116
Chris Lattner462fa822004-04-11 20:56:28 +00002117
2118/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2119/// 16, or 32-bit integer multiply by a constant.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002120void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002121 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002122 unsigned DestReg, const Type *DestTy,
2123 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002124 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2125 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
2126
Chris Lattnerb2acc512003-10-19 21:09:10 +00002127 unsigned Class = getClass(DestTy);
2128
Chris Lattner6ab06d52004-04-06 04:55:43 +00002129 if (ConstRHS == 0) {
2130 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2131 return;
2132 } else if (ConstRHS == 1) {
2133 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2134 return;
2135 }
2136
Chris Lattnerb2acc512003-10-19 21:09:10 +00002137 // If the element size is exactly a power of 2, use a shift to get it.
2138 if (unsigned Shift = ExactLog2(ConstRHS)) {
2139 switch (Class) {
2140 default: assert(0 && "Unknown class for this function!");
2141 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002142 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002143 return;
2144 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002145 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002146 return;
2147 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002148 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002149 return;
2150 }
2151 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002152
2153 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002154 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002155 return;
2156 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002157 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002158 return;
2159 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002160
2161 // Most general case, emit a normal multiply...
Chris Lattnerb2acc512003-10-19 21:09:10 +00002162 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002163 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002164
2165 // Emit a MUL to multiply the register holding the index by
2166 // elementSize, putting the result in OffsetReg.
2167 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2168}
2169
Chris Lattnerca9671d2002-11-02 20:28:58 +00002170/// visitMul - Multiplies are not simple binary operators because they must deal
2171/// with the EAX register explicitly.
2172///
2173void ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002174 unsigned ResultReg = getReg(I);
2175
Chris Lattner95157f72004-04-11 22:05:45 +00002176 Value *Op0 = I.getOperand(0);
2177 Value *Op1 = I.getOperand(1);
2178
2179 // Fold loads into floating point multiplies.
2180 if (getClass(Op0->getType()) == cFP) {
2181 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2182 if (!I.swapOperands())
2183 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2184 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2185 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2186 const Type *Ty = Op0->getType();
2187 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2188 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2189
2190 unsigned BaseReg, Scale, IndexReg, Disp;
2191 getAddressingMode(LI->getOperand(0), BaseReg,
2192 Scale, IndexReg, Disp);
2193
2194 unsigned Op0r = getReg(Op0);
2195 addFullAddress(BuildMI(BB, Opcode, 2, ResultReg).addReg(Op0r),
2196 BaseReg, Scale, IndexReg, Disp);
2197 return;
2198 }
2199 }
2200
Chris Lattner462fa822004-04-11 20:56:28 +00002201 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002202 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002203}
2204
2205void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2206 Value *Op0, Value *Op1, unsigned DestReg) {
2207 MachineBasicBlock &BB = *MBB;
2208 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002209
2210 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002211 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002212 switch (Class) {
2213 case cByte:
2214 case cShort:
2215 case cInt:
2216 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002217 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2218 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002219 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002220 unsigned Op1Reg = getReg(Op1, &BB, IP);
2221 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002222 }
Chris Lattner462fa822004-04-11 20:56:28 +00002223 return;
2224 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002225 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2226 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002227 case cLong:
2228 break;
2229 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002230
Chris Lattner462fa822004-04-11 20:56:28 +00002231 // Long value. We have to do things the hard way...
2232 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2233 unsigned CLow = CI->getRawValue();
2234 unsigned CHi = CI->getRawValue() >> 32;
2235
2236 if (CLow == 0) {
2237 // If the low part of the constant is all zeros, things are simple.
2238 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2239 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2240 return;
2241 }
2242
2243 // Multiply the two low parts... capturing carry into EDX
2244 unsigned OverflowReg = 0;
2245 if (CLow == 1) {
2246 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002247 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002248 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2249 OverflowReg = makeAnotherReg(Type::UIntTy);
2250 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2251 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2252 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002253
Chris Lattner462fa822004-04-11 20:56:28 +00002254 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2255 BuildMI(BB, IP, X86::MOV32rr, 1,
2256 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2257 }
2258
2259 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2260 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2261
2262 unsigned AHBLplusOverflowReg;
2263 if (OverflowReg) {
2264 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2265 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002266 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002267 } else {
2268 AHBLplusOverflowReg = AHBLReg;
2269 }
2270
2271 if (CHi == 0) {
2272 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2273 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002274 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002275 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002276
Chris Lattner462fa822004-04-11 20:56:28 +00002277 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002278 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2279 }
Chris Lattner462fa822004-04-11 20:56:28 +00002280 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002281 }
Chris Lattner462fa822004-04-11 20:56:28 +00002282
2283 // General 64x64 multiply
2284
2285 unsigned Op1Reg = getReg(Op1, &BB, IP);
2286 // Multiply the two low parts... capturing carry into EDX
2287 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2288 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2289
2290 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2291 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2292 BuildMI(BB, IP, X86::MOV32rr, 1,
2293 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2294
2295 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2296 BuildMI(BB, IP, X86::IMUL32rr, 2,
2297 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2298
2299 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2300 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2301 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2302
2303 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2304 BuildMI(BB, IP, X86::IMUL32rr, 2,
2305 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2306
2307 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2308 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002309}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002310
Chris Lattner06925362002-11-17 21:56:38 +00002311
Chris Lattnerf01729e2002-11-02 20:54:46 +00002312/// visitDivRem - Handle division and remainder instructions... these
2313/// instruction both require the same instructions to be generated, they just
2314/// select the result from a different register. Note that both of these
2315/// instructions work differently for signed and unsigned operands.
2316///
2317void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002318 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002319 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2320
2321 // Fold loads into floating point divides.
2322 if (getClass(Op0->getType()) == cFP) {
2323 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2324 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2325 const Type *Ty = Op0->getType();
2326 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2327 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2328
2329 unsigned BaseReg, Scale, IndexReg, Disp;
2330 getAddressingMode(LI->getOperand(0), BaseReg,
2331 Scale, IndexReg, Disp);
2332
2333 unsigned Op0r = getReg(Op0);
2334 addFullAddress(BuildMI(BB, Opcode, 2, ResultReg).addReg(Op0r),
2335 BaseReg, Scale, IndexReg, Disp);
2336 return;
2337 }
2338
2339 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2340 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2341 const Type *Ty = Op0->getType();
2342 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2343 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2344
2345 unsigned BaseReg, Scale, IndexReg, Disp;
2346 getAddressingMode(LI->getOperand(0), BaseReg,
2347 Scale, IndexReg, Disp);
2348
2349 unsigned Op1r = getReg(Op1);
2350 addFullAddress(BuildMI(BB, Opcode, 2, ResultReg).addReg(Op1r),
2351 BaseReg, Scale, IndexReg, Disp);
2352 return;
2353 }
2354 }
2355
Chris Lattner94af4142002-12-25 05:13:53 +00002356
Chris Lattnercadff442003-10-23 17:21:43 +00002357 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002358 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002359 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002360}
2361
2362void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002363 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +00002364 Value *Op0, Value *Op1, bool isDiv,
2365 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002366 const Type *Ty = Op0->getType();
2367 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002368 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002369 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002370 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002371 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2372 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002373 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002374 unsigned Op0Reg = getReg(Op0, BB, IP);
2375 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002376 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002377 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002378 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002379 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2380 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002381 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2382 }
Chris Lattner94af4142002-12-25 05:13:53 +00002383 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002384 case cLong: {
2385 static const char *FnName[] =
2386 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002387 unsigned Op0Reg = getReg(Op0, BB, IP);
2388 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002389 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002390 MachineInstr *TheCall =
2391 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2392
2393 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002394 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2395 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002396 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2397 return;
2398 }
2399 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002400 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002401 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002402 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002403
2404 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002405 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
2406 static const unsigned SarOpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2407 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002408 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2409
2410 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002411 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2412 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002413 };
2414
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002415 bool isSigned = Ty->isSigned();
Chris Lattnerf01729e2002-11-02 20:54:46 +00002416 unsigned Reg = Regs[Class];
2417 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002418
2419 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002420 unsigned Op0Reg = getReg(Op0, BB, IP);
2421 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002422 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002423
2424 if (isSigned) {
2425 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002426 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattneree352852004-02-29 07:22:16 +00002427 BuildMI(*BB, IP, SarOpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
2428 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002429 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002430 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002431 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002432 }
2433
Chris Lattner06925362002-11-17 21:56:38 +00002434 // Emit the appropriate divide or remainder instruction...
Chris Lattneree352852004-02-29 07:22:16 +00002435 BuildMI(*BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00002436
Chris Lattnerf01729e2002-11-02 20:54:46 +00002437 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002438 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002439
Chris Lattnerf01729e2002-11-02 20:54:46 +00002440 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002441 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002442}
Chris Lattnere2954c82002-11-02 20:04:26 +00002443
Chris Lattner06925362002-11-17 21:56:38 +00002444
Brian Gaekea1719c92002-10-31 23:03:59 +00002445/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2446/// for constant immediate shift values, and for constant immediate
2447/// shift values equal to 1. Even the general case is sort of special,
2448/// because the shift amount has to be in CL, not just any old register.
2449///
Chris Lattner3e130a22003-01-13 00:32:26 +00002450void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002451 MachineBasicBlock::iterator IP = BB->end ();
2452 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2453 I.getOpcode () == Instruction::Shl, I.getType (),
2454 getReg (I));
2455}
2456
2457/// emitShiftOperation - Common code shared between visitShiftInst and
2458/// constant expression support.
2459void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002460 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002461 Value *Op, Value *ShiftAmount, bool isLeftShift,
2462 const Type *ResultTy, unsigned DestReg) {
2463 unsigned SrcReg = getReg (Op, MBB, IP);
2464 bool isSigned = ResultTy->isSigned ();
2465 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002466
2467 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002468 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2469 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2470 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2471 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002472 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002473
Chris Lattner3e130a22003-01-13 00:32:26 +00002474 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002475 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2476 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2477 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2478 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002479 };
Chris Lattner796df732002-11-02 00:44:25 +00002480
Chris Lattner3e130a22003-01-13 00:32:26 +00002481 // Longs, as usual, are handled specially...
2482 if (Class == cLong) {
2483 // If we have a constant shift, we can generate much more efficient code
2484 // than otherwise...
2485 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002486 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002487 unsigned Amount = CUI->getValue();
2488 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002489 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2490 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002491 BuildMI(*MBB, IP, Opc[3], 3,
2492 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2493 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002494 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002495 BuildMI(*MBB, IP, Opc[3], 3,
2496 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2497 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002498 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002499 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002500 Amount -= 32;
2501 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002502 if (Amount != 0) {
2503 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2504 DestReg + 1).addReg(SrcReg).addImm(Amount);
2505 } else {
2506 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2507 }
2508 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002509 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002510 if (Amount != 0) {
2511 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2512 DestReg).addReg(SrcReg+1).addImm(Amount);
2513 } else {
2514 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2515 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002516 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002517 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002518 }
2519 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002520 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2521
2522 if (!isLeftShift && isSigned) {
2523 // If this is a SHR of a Long, then we need to do funny sign extension
2524 // stuff. TmpReg gets the value to use as the high-part if we are
2525 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002526 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002527 } else {
2528 // Other shifts use a fixed zero value if the shift is more than 32
2529 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002530 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002531 }
2532
2533 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002534 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002535 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002536
2537 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2538 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2539 if (isLeftShift) {
2540 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002541 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002542 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002543 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002544 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002545
2546 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002547 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002548
2549 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002550 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002551 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2552 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002553 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002554 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002555 } else {
2556 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002557 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002558 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002559 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002560 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002561 .addReg(SrcReg+1);
2562
2563 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002564 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002565
2566 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002567 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002568 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2569
2570 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002571 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002572 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2573 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002574 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002575 return;
2576 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002577
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002578 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002579 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2580 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002581
Chris Lattner3e130a22003-01-13 00:32:26 +00002582 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002583 BuildMI(*MBB, IP, Opc[Class], 2,
2584 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002585 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002586 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002587 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002588
Chris Lattner3e130a22003-01-13 00:32:26 +00002589 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002590 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002591 }
2592}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002593
Chris Lattner3e130a22003-01-13 00:32:26 +00002594
Chris Lattner721d2d42004-03-08 01:18:36 +00002595void ISel::getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
2596 unsigned &IndexReg, unsigned &Disp) {
2597 BaseReg = 0; Scale = 1; IndexReg = 0; Disp = 0;
2598 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
2599 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
2600 BaseReg, Scale, IndexReg, Disp))
2601 return;
2602 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
2603 if (CE->getOpcode() == Instruction::GetElementPtr)
2604 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
2605 BaseReg, Scale, IndexReg, Disp))
2606 return;
2607 }
2608
2609 // If it's not foldable, reset addr mode.
2610 BaseReg = getReg(Addr);
2611 Scale = 1; IndexReg = 0; Disp = 0;
2612}
2613
2614
Chris Lattner6fc3c522002-11-17 21:11:55 +00002615/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002616/// instruction. The load and store instructions are the only place where we
2617/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002618///
2619void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002620 // Check to see if this load instruction is going to be folded into a binary
2621 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2622 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00002623 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002624 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002625 Instruction *User = cast<Instruction>(I.use_back());
2626 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002627 case Instruction::Cast:
2628 // If this is a cast from a signed-integer type to a floating point type,
2629 // fold the cast here.
2630 if (getClass(User->getType()) == cFP &&
2631 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
2632 I.getType() == Type::LongTy)) {
2633 unsigned DestReg = getReg(User);
2634 static const unsigned Opcode[] = {
2635 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
2636 };
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002637 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
2638 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
2639 addFullAddress(BuildMI(BB, Opcode[Class], 5, DestReg),
2640 BaseReg, Scale, IndexReg, Disp);
2641 return;
2642 } else {
2643 User = 0;
2644 }
2645 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00002646
Chris Lattner7dee5da2004-03-08 01:58:35 +00002647 case Instruction::Add:
2648 case Instruction::Sub:
2649 case Instruction::And:
2650 case Instruction::Or:
2651 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002652 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002653 break;
Chris Lattner95157f72004-04-11 22:05:45 +00002654 case Instruction::Mul:
2655 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00002656 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002657 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00002658 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002659 }
2660
2661 if (User) {
2662 // Okay, we found a user. If the load is the first operand and there is
2663 // no second operand load, reverse the operand ordering. Note that this
2664 // can fail for a subtract (ie, no change will be made).
2665 if (!isa<LoadInst>(User->getOperand(1)))
2666 cast<BinaryOperator>(User)->swapOperands();
2667
2668 // Okay, now that everything is set up, if this load is used by the second
2669 // operand, and if there are no instructions that invalidate the load
2670 // before the binary operator, eliminate the load.
2671 if (User->getOperand(1) == &I &&
2672 isSafeToFoldLoadIntoInstruction(I, *User))
2673 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00002674
2675 // If this is a floating point sub or div, we won't be able to swap the
2676 // operands, but we will still be able to eliminate the load.
2677 if (Class == cFP && User->getOperand(0) == &I &&
2678 !isa<LoadInst>(User->getOperand(1)) &&
2679 (User->getOpcode() == Instruction::Sub ||
2680 User->getOpcode() == Instruction::Div) &&
2681 isSafeToFoldLoadIntoInstruction(I, *User))
2682 return; // Eliminate the load!
Chris Lattner7dee5da2004-03-08 01:58:35 +00002683 }
2684 }
2685
Chris Lattner94af4142002-12-25 05:13:53 +00002686 unsigned DestReg = getReg(I);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002687 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
Chris Lattner721d2d42004-03-08 01:18:36 +00002688 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
Chris Lattnere8f0d922002-12-24 00:03:11 +00002689
Chris Lattner6ac1d712003-10-20 04:48:06 +00002690 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002691 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002692 BaseReg, Scale, IndexReg, Disp);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002693 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002694 BaseReg, Scale, IndexReg, Disp+4);
Chris Lattner94af4142002-12-25 05:13:53 +00002695 return;
2696 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002697
Chris Lattner6ac1d712003-10-20 04:48:06 +00002698 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002699 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m
Chris Lattner3e130a22003-01-13 00:32:26 +00002700 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00002701 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002702 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002703 addFullAddress(BuildMI(BB, Opcode, 4, DestReg),
2704 BaseReg, Scale, IndexReg, Disp);
Chris Lattner3e130a22003-01-13 00:32:26 +00002705}
2706
Chris Lattner6fc3c522002-11-17 21:11:55 +00002707/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
2708/// instruction.
2709///
2710void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002711 unsigned BaseReg, Scale, IndexReg, Disp;
2712 getAddressingMode(I.getOperand(1), BaseReg, Scale, IndexReg, Disp);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002713
Chris Lattner6c09db22003-10-20 04:11:23 +00002714 const Type *ValTy = I.getOperand(0)->getType();
2715 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00002716
Chris Lattner5a830962004-02-25 02:56:58 +00002717 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
2718 uint64_t Val = CI->getRawValue();
2719 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002720 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002721 BaseReg, Scale, IndexReg, Disp).addImm(Val & ~0U);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002722 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002723 BaseReg, Scale, IndexReg, Disp+4).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00002724 } else {
2725 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002726 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00002727 };
2728 unsigned Opcode = Opcodes[Class];
Chris Lattnerb6bac512004-02-25 06:13:04 +00002729 addFullAddress(BuildMI(BB, Opcode, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002730 BaseReg, Scale, IndexReg, Disp).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00002731 }
2732 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002733 addFullAddress(BuildMI(BB, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002734 BaseReg, Scale, IndexReg, Disp).addImm(CB->getValue());
Chris Lattner5a830962004-02-25 02:56:58 +00002735 } else {
2736 if (Class == cLong) {
2737 unsigned ValReg = getReg(I.getOperand(0));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002738 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002739 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002740 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002741 BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1);
Chris Lattner5a830962004-02-25 02:56:58 +00002742 } else {
2743 unsigned ValReg = getReg(I.getOperand(0));
2744 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002745 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
Chris Lattner5a830962004-02-25 02:56:58 +00002746 };
2747 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002748 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002749 addFullAddress(BuildMI(BB, Opcode, 1+4),
2750 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Chris Lattner5a830962004-02-25 02:56:58 +00002751 }
Chris Lattner94af4142002-12-25 05:13:53 +00002752 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002753}
2754
2755
Misha Brukman538607f2004-03-01 23:53:11 +00002756/// visitCastInst - Here we have various kinds of copying with or without sign
2757/// extension going on.
2758///
Chris Lattner3e130a22003-01-13 00:32:26 +00002759void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00002760 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00002761
2762 // Noop casts are not even emitted.
2763 if (getClassB(CI.getType()) == getClassB(Op->getType()))
2764 return;
2765
Chris Lattnerf5854472003-06-21 16:01:24 +00002766 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2767 // of the case are GEP instructions, then the cast does not need to be
2768 // generated explicitly, it will be folded into the GEP.
2769 if (CI.getType() == Type::LongTy &&
2770 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
2771 bool AllUsesAreGEPs = true;
2772 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2773 if (!isa<GetElementPtrInst>(*I)) {
2774 AllUsesAreGEPs = false;
2775 break;
2776 }
2777
2778 // No need to codegen this cast if all users are getelementptr instrs...
2779 if (AllUsesAreGEPs) return;
2780 }
2781
Chris Lattner548f61d2003-04-23 17:22:12 +00002782 unsigned DestReg = getReg(CI);
2783 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00002784 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00002785}
2786
Misha Brukman538607f2004-03-01 23:53:11 +00002787/// emitCastOperation - Common code shared between visitCastInst and constant
2788/// expression cast support.
2789///
Chris Lattner548f61d2003-04-23 17:22:12 +00002790void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002791 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00002792 Value *Src, const Type *DestTy,
2793 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002794 const Type *SrcTy = Src->getType();
2795 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002796 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00002797
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002798 // If this cast converts a load from a short,int, or long integer to a FP
2799 // value, we will have folded this cast away.
2800 if (DestClass == cFP && isa<LoadInst>(Src) &&
2801 (Src->getType() == Type::ShortTy || Src->getType() == Type::IntTy ||
2802 Src->getType() == Type::LongTy))
2803 return;
2804
2805 unsigned SrcReg = getReg(Src, BB, IP);
2806
Chris Lattner3e130a22003-01-13 00:32:26 +00002807 // Implement casts to bool by using compare on the operand followed by set if
2808 // not zero on the result.
2809 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00002810 switch (SrcClass) {
2811 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002812 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002813 break;
2814 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002815 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002816 break;
2817 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002818 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002819 break;
2820 case cLong: {
2821 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002822 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00002823 break;
2824 }
2825 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00002826 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002827 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00002828 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00002829 break;
Chris Lattner20772542003-06-01 03:38:24 +00002830 }
2831
2832 // If the zero flag is not set, then the value is true, set the byte to
2833 // true.
Chris Lattneree352852004-02-29 07:22:16 +00002834 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00002835 return;
2836 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002837
2838 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002839 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00002840 };
2841
2842 // Implement casts between values of the same type class (as determined by
2843 // getClass) by using a register-to-register move.
2844 if (SrcClass == DestClass) {
2845 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00002846 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002847 } else if (SrcClass == cFP) {
2848 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002849 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00002850 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002851 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002852 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2853 "Unknown cFP member!");
2854 // Truncate from double to float by storing to memory as short, then
2855 // reading it back.
2856 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00002857 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002858 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
2859 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002860 }
2861 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002862 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
2863 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002864 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00002865 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002866 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00002867 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002868 return;
2869 }
2870
2871 // Handle cast of SMALLER int to LARGER int using a move with sign extension
2872 // or zero extension, depending on whether the source type was signed.
2873 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2874 SrcClass < DestClass) {
2875 bool isLong = DestClass == cLong;
2876 if (isLong) DestClass = cInt;
2877
2878 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002879 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
2880 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00002881 };
2882
2883 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattneree352852004-02-29 07:22:16 +00002884 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00002885 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002886
2887 if (isLong) { // Handle upper 32 bits as appropriate...
2888 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002889 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00002890 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002891 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00002892 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002893 return;
2894 }
2895
2896 // Special case long -> int ...
2897 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002898 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002899 return;
2900 }
2901
2902 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
2903 // move out of AX or AL.
2904 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2905 && SrcClass > DestClass) {
2906 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00002907 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
2908 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00002909 return;
2910 }
2911
2912 // Handle casts from integer to floating point now...
2913 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002914 // Promote the integer to a type supported by FLD. We do this because there
2915 // are no unsigned FLD instructions, so we must promote an unsigned value to
2916 // a larger signed value, then use FLD on the larger value.
2917 //
2918 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00002919 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002920 unsigned RealDestReg = DestReg;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002921 switch (SrcTy->getPrimitiveID()) {
2922 case Type::BoolTyID:
2923 case Type::SByteTyID:
2924 // We don't have the facilities for directly loading byte sized data from
2925 // memory (even signed). Promote it to 16 bits.
2926 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002927 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002928 break;
2929 case Type::UByteTyID:
2930 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002931 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002932 break;
2933 case Type::UShortTyID:
2934 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002935 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002936 break;
2937 case Type::UIntTyID: {
2938 // Make a 64 bit temporary... and zero out the top of it...
2939 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002940 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
2941 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002942 SrcTy = Type::LongTy;
2943 SrcClass = cLong;
2944 SrcReg = TmpReg;
2945 break;
2946 }
2947 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002948 // Don't fild into the read destination.
2949 DestReg = makeAnotherReg(Type::DoubleTy);
2950 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002951 default: // No promotion needed...
2952 break;
2953 }
2954
2955 if (PromoteType) {
2956 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00002957 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002958 SrcTy = PromoteType;
2959 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00002960 SrcReg = TmpReg;
2961 }
2962
2963 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00002964 int FrameIdx =
2965 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00002966
2967 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002968 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002969 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002970 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002971 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002972 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002973 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00002974 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
2975 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002976 }
2977
2978 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002979 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00002980 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002981
2982 // We need special handling for unsigned 64-bit integer sources. If the
2983 // input number has the "sign bit" set, then we loaded it incorrectly as a
2984 // negative 64-bit number. In this case, add an offset value.
2985 if (SrcTy == Type::ULongTy) {
2986 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002987 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002988
Chris Lattnerb6bac512004-02-25 06:13:04 +00002989 // If the sign bit is set, get a pointer to an offset, otherwise get a
2990 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002991 MachineConstantPool *CP = F->getConstantPool();
2992 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002993 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002994 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002995 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002996 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002997 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
2998
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002999 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003000 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003001 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003002 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003003
3004 // Load the constant for an add. FIXME: this could make an 'fadd' that
3005 // reads directly from memory, but we don't support these yet.
3006 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003007 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003008
Chris Lattneree352852004-02-29 07:22:16 +00003009 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3010 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003011 }
3012
Chris Lattner3e130a22003-01-13 00:32:26 +00003013 return;
3014 }
3015
3016 // Handle casts from floating point to integer now...
3017 if (SrcClass == cFP) {
3018 // Change the floating point control register to use "round towards zero"
3019 // mode when truncating to an integer value.
3020 //
3021 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003022 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003023
3024 // Load the old value of the high byte of the control word...
3025 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003026 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003027 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003028
3029 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003030 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003031 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003032
3033 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003034 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003035
3036 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003037 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003038 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003039
3040 // We don't have the facilities for directly storing byte sized data to
3041 // memory. Promote it to 16 bits. We also must promote unsigned values to
3042 // larger classes because we only have signed FP stores.
3043 unsigned StoreClass = DestClass;
3044 const Type *StoreTy = DestTy;
3045 if (StoreClass == cByte || DestTy->isUnsigned())
3046 switch (StoreClass) {
3047 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3048 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3049 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003050 // The following treatment of cLong may not be perfectly right,
3051 // but it survives chains of casts of the form
3052 // double->ulong->double.
3053 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003054 default: assert(0 && "Unknown store class!");
3055 }
3056
3057 // Spill the integer to memory and reload it from there...
3058 int FrameIdx =
3059 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3060
3061 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003062 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003063 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3064 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003065
3066 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003067 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3068 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003069 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003070 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003071 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003072 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003073 }
3074
3075 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003076 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003077 return;
3078 }
3079
Brian Gaeked474e9c2002-12-06 10:49:33 +00003080 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003081 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003082 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003083}
Brian Gaekea1719c92002-10-31 23:03:59 +00003084
Chris Lattner73815062003-10-18 05:56:40 +00003085/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003086///
Chris Lattner73815062003-10-18 05:56:40 +00003087void ISel::visitVANextInst(VANextInst &I) {
3088 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003089 unsigned DestReg = getReg(I);
3090
Chris Lattnereca195e2003-05-08 19:44:13 +00003091 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00003092 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003093 default:
3094 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003095 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003096 return;
3097 case Type::PointerTyID:
3098 case Type::UIntTyID:
3099 case Type::IntTyID:
3100 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003101 break;
3102 case Type::ULongTyID:
3103 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003104 case Type::DoubleTyID:
3105 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003106 break;
3107 }
3108
3109 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003110 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003111}
Chris Lattnereca195e2003-05-08 19:44:13 +00003112
Chris Lattner73815062003-10-18 05:56:40 +00003113void ISel::visitVAArgInst(VAArgInst &I) {
3114 unsigned VAList = getReg(I.getOperand(0));
3115 unsigned DestReg = getReg(I);
3116
3117 switch (I.getType()->getPrimitiveID()) {
3118 default:
3119 std::cerr << I;
3120 assert(0 && "Error: bad type for va_next instruction!");
3121 return;
3122 case Type::PointerTyID:
3123 case Type::UIntTyID:
3124 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003125 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003126 break;
3127 case Type::ULongTyID:
3128 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003129 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3130 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003131 break;
3132 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003133 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003134 break;
3135 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003136}
3137
Misha Brukman538607f2004-03-01 23:53:11 +00003138/// visitGetElementPtrInst - instruction-select GEP instructions
3139///
Chris Lattner3e130a22003-01-13 00:32:26 +00003140void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003141 // If this GEP instruction will be folded into all of its users, we don't need
3142 // to explicitly calculate it!
3143 unsigned A, B, C, D;
3144 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) {
3145 // Check all of the users of the instruction to see if they are loads and
3146 // stores.
3147 bool AllWillFold = true;
3148 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3149 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3150 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3151 cast<Instruction>(*UI)->getOperand(0) == &I) {
3152 AllWillFold = false;
3153 break;
3154 }
3155
3156 // If the instruction is foldable, and will be folded into all users, don't
3157 // emit it!
3158 if (AllWillFold) return;
3159 }
3160
Chris Lattner3e130a22003-01-13 00:32:26 +00003161 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003162 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003163 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003164}
3165
Chris Lattner985fe3d2004-02-25 03:45:50 +00003166/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3167/// GEPTypes (the derived types being stepped through at each level). On return
3168/// from this function, if some indexes of the instruction are representable as
3169/// an X86 lea instruction, the machine operands are put into the Ops
3170/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3171/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3172/// addressing mode that only partially consumes the input, the BaseReg input of
3173/// the addressing mode must be left free.
3174///
3175/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3176///
Chris Lattnerb6bac512004-02-25 06:13:04 +00003177void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
3178 std::vector<Value*> &GEPOps,
3179 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
3180 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
3181 const TargetData &TD = TM.getTargetData();
3182
Chris Lattner985fe3d2004-02-25 03:45:50 +00003183 // Clear out the state we are working with...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003184 BaseReg = 0; // No base register
3185 Scale = 1; // Unit scale
3186 IndexReg = 0; // No index register
3187 Disp = 0; // No displacement
3188
Chris Lattner985fe3d2004-02-25 03:45:50 +00003189 // While there are GEP indexes that can be folded into the current address,
3190 // keep processing them.
3191 while (!GEPTypes.empty()) {
3192 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3193 // It's a struct access. CUI is the index into the structure,
3194 // which names the field. This index must have unsigned type.
3195 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3196
3197 // Use the TargetData structure to pick out what the layout of the
3198 // structure is in memory. Since the structure index must be constant, we
3199 // can get its value and use it to find the right byte offset from the
3200 // StructLayout class's list of structure member offsets.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003201 Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003202 GEPOps.pop_back(); // Consume a GEP operand
3203 GEPTypes.pop_back();
3204 } else {
3205 // It's an array or pointer access: [ArraySize x ElementType].
3206 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3207 Value *idx = GEPOps.back();
3208
3209 // idx is the index into the array. Unlike with structure
3210 // indices, we may not know its actual value at code-generation
3211 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003212
3213 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003214 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003215 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003216 Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003217 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
3218 Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003219 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003220 // If the index reg is already taken, we can't handle this index.
3221 if (IndexReg) return;
3222
3223 // If this is a size that we can handle, then add the index as
3224 switch (TypeSize) {
3225 case 1: case 2: case 4: case 8:
3226 // These are all acceptable scales on X86.
3227 Scale = TypeSize;
3228 break;
3229 default:
3230 // Otherwise, we can't handle this scale
3231 return;
3232 }
3233
3234 if (CastInst *CI = dyn_cast<CastInst>(idx))
3235 if (CI->getOperand(0)->getType() == Type::IntTy ||
3236 CI->getOperand(0)->getType() == Type::UIntTy)
3237 idx = CI->getOperand(0);
3238
3239 IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003240 }
3241
3242 GEPOps.pop_back(); // Consume a GEP operand
3243 GEPTypes.pop_back();
3244 }
3245 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003246
3247 // GEPTypes is empty, which means we have a single operand left. See if we
3248 // can set it as the base register.
3249 //
3250 // FIXME: When addressing modes are more powerful/correct, we could load
3251 // global addresses directly as 32-bit immediates.
3252 assert(BaseReg == 0);
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003253 BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003254 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003255}
3256
3257
Chris Lattnerb6bac512004-02-25 06:13:04 +00003258/// isGEPFoldable - Return true if the specified GEP can be completely
3259/// folded into the addressing mode of a load/store or lea instruction.
3260bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
3261 Value *Src, User::op_iterator IdxBegin,
3262 User::op_iterator IdxEnd, unsigned &BaseReg,
3263 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003264 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3265 Src = CPR->getValue();
3266
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003267 std::vector<Value*> GEPOps;
3268 GEPOps.resize(IdxEnd-IdxBegin+1);
3269 GEPOps[0] = Src;
3270 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3271
3272 std::vector<const Type*> GEPTypes;
3273 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3274 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
3275
Chris Lattnerb6bac512004-02-25 06:13:04 +00003276 MachineBasicBlock::iterator IP;
3277 if (MBB) IP = MBB->end();
3278 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
3279
3280 // We can fold it away iff the getGEPIndex call eliminated all operands.
3281 return GEPOps.empty();
3282}
3283
3284void ISel::emitGEPOperation(MachineBasicBlock *MBB,
3285 MachineBasicBlock::iterator IP,
3286 Value *Src, User::op_iterator IdxBegin,
3287 User::op_iterator IdxEnd, unsigned TargetReg) {
3288 const TargetData &TD = TM.getTargetData();
3289 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3290 Src = CPR->getValue();
3291
3292 std::vector<Value*> GEPOps;
3293 GEPOps.resize(IdxEnd-IdxBegin+1);
3294 GEPOps[0] = Src;
3295 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3296
3297 std::vector<const Type*> GEPTypes;
3298 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3299 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003300
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003301 // Keep emitting instructions until we consume the entire GEP instruction.
3302 while (!GEPOps.empty()) {
3303 unsigned OldSize = GEPOps.size();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003304 unsigned BaseReg, Scale, IndexReg, Disp;
3305 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003306
Chris Lattner985fe3d2004-02-25 03:45:50 +00003307 if (GEPOps.size() != OldSize) {
3308 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003309 unsigned NextTarget = 0;
3310 if (!GEPOps.empty()) {
3311 assert(BaseReg == 0 &&
3312 "getGEPIndex should have left the base register open for chaining!");
3313 NextTarget = BaseReg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003314 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003315
3316 if (IndexReg == 0 && Disp == 0)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003317 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003318 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003319 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003320 BaseReg, Scale, IndexReg, Disp);
3321 --IP;
3322 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003323 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003324 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3325 // all operands are consumed but the base pointer. If so, just load it
3326 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003327 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003328 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003329 } else {
3330 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003331 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003332 }
3333 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003334
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003335 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003336 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003337 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3338 Value *idx = GEPOps.back();
3339 GEPOps.pop_back(); // Consume a GEP operand
3340 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003341
Chris Lattner28977af2004-04-05 01:30:19 +00003342 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003343 // operand on X86. Handle this case directly now...
3344 if (CastInst *CI = dyn_cast<CastInst>(idx))
3345 if (CI->getOperand(0)->getType() == Type::IntTy ||
3346 CI->getOperand(0)->getType() == Type::UIntTy)
3347 idx = CI->getOperand(0);
3348
Chris Lattner3e130a22003-01-13 00:32:26 +00003349 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003350 // must find the size of the pointed-to type (Not coincidentally, the next
3351 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003352 const Type *ElTy = SqTy->getElementType();
3353 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003354
3355 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003356 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003357 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003358 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003359 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003360 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003361 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003362 --IP; // Insert the next instruction before this one.
3363 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003364 }
3365 } else if (elementSize == 1) {
3366 // If the element size is 1, we don't have to multiply, just add
3367 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003368 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003369 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003370 --IP; // Insert the next instruction before this one.
3371 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003372 } else {
3373 unsigned idxReg = getReg(idx, MBB, IP);
3374 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003375
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003376 // Make sure we can back the iterator up to point to the first
3377 // instruction emitted.
3378 MachineBasicBlock::iterator BeforeIt = IP;
3379 if (IP == MBB->begin())
3380 BeforeIt = MBB->end();
3381 else
3382 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003383 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3384
Chris Lattner8a307e82002-12-16 19:32:50 +00003385 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003386 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003387 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003388 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003389
3390 // Step to the first instruction of the multiply.
3391 if (BeforeIt == MBB->end())
3392 IP = MBB->begin();
3393 else
3394 IP = ++BeforeIt;
3395
3396 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003397 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003398 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003399 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003400}
3401
3402
Chris Lattner065faeb2002-12-28 20:24:02 +00003403/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3404/// frame manager, otherwise do it the hard way.
3405///
3406void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00003407 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003408 const Type *Ty = I.getAllocatedType();
3409 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3410
3411 // If this is a fixed size alloca in the entry block for the function,
3412 // statically stack allocate the space.
3413 //
3414 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
3415 if (I.getParent() == I.getParent()->getParent()->begin()) {
3416 TySize *= CUI->getValue(); // Get total allocated size...
3417 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
3418
3419 // Create a new stack object using the frame manager...
3420 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003421 addFrameReference(BuildMI(BB, X86::LEA32r, 5, getReg(I)), FrameIdx);
Chris Lattner065faeb2002-12-28 20:24:02 +00003422 return;
3423 }
3424 }
3425
3426 // Create a register to hold the temporary result of multiplying the type size
3427 // constant by the variable amount.
3428 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3429 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003430
3431 // TotalSizeReg = mul <numelements>, <TypeSize>
3432 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003433 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003434
3435 // AddedSize = add <TotalSizeReg>, 15
3436 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003437 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003438
3439 // AlignedSize = and <AddedSize>, ~15
3440 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003441 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003442
Brian Gaekee48ec012002-12-13 06:46:31 +00003443 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003444 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003445
Brian Gaekee48ec012002-12-13 06:46:31 +00003446 // Put a pointer to the space into the result register, by copying
3447 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003448 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003449
Misha Brukman48196b32003-05-03 02:18:17 +00003450 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003451 // object.
3452 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003453}
Chris Lattner3e130a22003-01-13 00:32:26 +00003454
3455/// visitMallocInst - Malloc instructions are code generated into direct calls
3456/// to the library malloc.
3457///
3458void ISel::visitMallocInst(MallocInst &I) {
3459 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3460 unsigned Arg;
3461
3462 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3463 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3464 } else {
3465 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003466 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003467 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003468 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003469 }
3470
3471 std::vector<ValueRecord> Args;
3472 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3473 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003474 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003475 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3476}
3477
3478
3479/// visitFreeInst - Free instructions are code gen'd to call the free libc
3480/// function.
3481///
3482void ISel::visitFreeInst(FreeInst &I) {
3483 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003484 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003485 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003486 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003487 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3488}
3489
Chris Lattnerd281de22003-07-26 23:49:58 +00003490/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003491/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003492/// generated code sucks but the implementation is nice and simple.
3493///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003494FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
3495 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003496}