blob: 1d5fb91a7534e1a16620609073e0dba019ecb07a [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 Lattner462fa822004-04-11 20:56:28 +0000275 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
276 Value *Op0, Value *Op1, unsigned TargetReg);
277
278 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
279 unsigned DestReg, const Type *DestTy,
280 unsigned Op0Reg, unsigned Op1Reg);
281 void doMultiplyConst(MachineBasicBlock *MBB,
282 MachineBasicBlock::iterator MBBI,
283 unsigned DestReg, const Type *DestTy,
284 unsigned Op0Reg, unsigned Op1Val);
285
Chris Lattnercadff442003-10-23 17:21:43 +0000286 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000287 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000288 Value *Op0, Value *Op1, bool isDiv,
289 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000290
Chris Lattner58c41fe2003-08-24 19:19:47 +0000291 /// emitSetCCOperation - Common code shared between visitSetCondInst and
292 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000293 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000294 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000295 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000296 Value *Op0, Value *Op1, unsigned Opcode,
297 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000298
299 /// emitShiftOperation - Common code shared between visitShiftInst and
300 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000301 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000302 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000303 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000304 Value *Op, Value *ShiftAmount, bool isLeftShift,
305 const Type *ResultTy, unsigned DestReg);
306
Chris Lattner12d96a02004-03-30 21:22:00 +0000307 /// emitSelectOperation - Common code shared between visitSelectInst and the
308 /// constant expression support.
309 void emitSelectOperation(MachineBasicBlock *MBB,
310 MachineBasicBlock::iterator IP,
311 Value *Cond, Value *TrueVal, Value *FalseVal,
312 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000313
Chris Lattnerc5291f52002-10-27 21:16:59 +0000314 /// copyConstantToRegister - Output the instructions required to put the
315 /// specified constant into the specified register.
316 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000317 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000318 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000319 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000320
Chris Lattner3e130a22003-01-13 00:32:26 +0000321 /// makeAnotherReg - This method returns the next register number we haven't
322 /// yet used.
323 ///
324 /// Long values are handled somewhat specially. They are always allocated
325 /// as pairs of 32 bit integer values. The register number returned is the
326 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
327 /// of the long value.
328 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000329 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000330 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
331 "Current target doesn't have X86 reg info??");
332 const X86RegisterInfo *MRI =
333 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000334 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000335 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
336 // Create the lower part
337 F->getSSARegMap()->createVirtualRegister(RC);
338 // Create the upper part.
339 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000340 }
341
Chris Lattnerc0812d82002-12-13 06:56:29 +0000342 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000343 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000344 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000345 }
346
Chris Lattner72614082002-10-25 22:55:53 +0000347 /// getReg - This method turns an LLVM value into a register number. This
348 /// is guaranteed to produce the same register number for a particular value
349 /// every time it is queried.
350 ///
351 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000352 unsigned getReg(Value *V) {
353 // Just append to the end of the current bb.
354 MachineBasicBlock::iterator It = BB->end();
355 return getReg(V, BB, It);
356 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000357 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000358 MachineBasicBlock::iterator IPt) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000359 // If this operand is a constant, emit the code to copy the constant into
360 // the register here...
361 //
362 if (Constant *C = dyn_cast<Constant>(V)) {
363 unsigned Reg = makeAnotherReg(V->getType());
364 copyConstantToRegister(MBB, IPt, C, Reg);
365 return Reg;
366 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
367 unsigned Reg = makeAnotherReg(V->getType());
368 // Move the address of the global into the register
369 BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV);
370 return Reg;
371 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
372 // Do not emit noop casts at all.
373 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()))
374 return getReg(CI->getOperand(0), MBB, IPt);
375 }
376
Chris Lattner72614082002-10-25 22:55:53 +0000377 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000378 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000379 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000380 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000381 }
Chris Lattner72614082002-10-25 22:55:53 +0000382
Chris Lattner72614082002-10-25 22:55:53 +0000383 return Reg;
384 }
Chris Lattner72614082002-10-25 22:55:53 +0000385 };
386}
387
Chris Lattnerc5291f52002-10-27 21:16:59 +0000388/// copyConstantToRegister - Output the instructions required to put the
389/// specified constant into the specified register.
390///
Chris Lattner8a307e82002-12-16 19:32:50 +0000391void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000392 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000393 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000394 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000395 unsigned Class = 0;
396 switch (CE->getOpcode()) {
397 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000398 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000399 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000400 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000401 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000402 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000403 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000404
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000405 case Instruction::Xor: ++Class; // FALL THROUGH
406 case Instruction::Or: ++Class; // FALL THROUGH
407 case Instruction::And: ++Class; // FALL THROUGH
408 case Instruction::Sub: ++Class; // FALL THROUGH
409 case Instruction::Add:
410 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
411 Class, R);
412 return;
413
Chris Lattner462fa822004-04-11 20:56:28 +0000414 case Instruction::Mul:
415 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000416 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000417
Chris Lattnercadff442003-10-23 17:21:43 +0000418 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000419 case Instruction::Rem:
420 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
421 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000422 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000423
Chris Lattner58c41fe2003-08-24 19:19:47 +0000424 case Instruction::SetNE:
425 case Instruction::SetEQ:
426 case Instruction::SetLT:
427 case Instruction::SetGT:
428 case Instruction::SetLE:
429 case Instruction::SetGE:
430 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
431 CE->getOpcode(), R);
432 return;
433
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000434 case Instruction::Shl:
435 case Instruction::Shr:
436 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000437 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
438 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000439
Chris Lattner12d96a02004-03-30 21:22:00 +0000440 case Instruction::Select:
441 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
442 CE->getOperand(2), R);
443 return;
444
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000445 default:
446 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000447 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000448 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000449 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000450
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000451 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000452 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000453
454 if (Class == cLong) {
455 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000456 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000457 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
458 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000459 return;
460 }
461
Chris Lattner94af4142002-12-25 05:13:53 +0000462 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000463
464 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000465 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000466 };
467
Chris Lattner6b993cc2002-12-15 08:02:15 +0000468 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000469 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000470 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000471 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000472 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000473 }
Chris Lattner94af4142002-12-25 05:13:53 +0000474 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000475 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000476 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000477 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000478 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000479 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000480 // Otherwise we need to spill the constant to memory...
481 MachineConstantPool *CP = F->getConstantPool();
482 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000483 const Type *Ty = CFP->getType();
484
485 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000486 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000487 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000488 }
489
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000490 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000491 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000492 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000493 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000494 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000495 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000496 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000497 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000498 }
499}
500
Chris Lattner065faeb2002-12-28 20:24:02 +0000501/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
502/// the stack into virtual registers.
503///
504void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
505 // Emit instructions to load the arguments... On entry to a function on the
506 // X86, the stack frame looks like this:
507 //
508 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000509 // [ESP + 4] -- first argument (leftmost lexically)
510 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000511 // ...
512 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000513 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000514 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000515
516 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000517 bool ArgLive = !I->use_empty();
518 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000519 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000520
Chris Lattner065faeb2002-12-28 20:24:02 +0000521 switch (getClassB(I->getType())) {
522 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000523 if (ArgLive) {
524 FI = MFI->CreateFixedObject(1, ArgOffset);
525 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
526 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000527 break;
528 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000529 if (ArgLive) {
530 FI = MFI->CreateFixedObject(2, ArgOffset);
531 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
532 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000533 break;
534 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000535 if (ArgLive) {
536 FI = MFI->CreateFixedObject(4, ArgOffset);
537 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
538 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000539 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000540 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000541 if (ArgLive) {
542 FI = MFI->CreateFixedObject(8, ArgOffset);
543 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
544 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
545 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000546 ArgOffset += 4; // longs require 4 additional bytes
547 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000548 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000549 if (ArgLive) {
550 unsigned Opcode;
551 if (I->getType() == Type::FloatTy) {
552 Opcode = X86::FLD32m;
553 FI = MFI->CreateFixedObject(4, ArgOffset);
554 } else {
555 Opcode = X86::FLD64m;
556 FI = MFI->CreateFixedObject(8, ArgOffset);
557 }
558 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000559 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000560 if (I->getType() == Type::DoubleTy)
561 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000562 break;
563 default:
564 assert(0 && "Unhandled argument type!");
565 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000566 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000567 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000568
569 // If the function takes variable number of arguments, add a frame offset for
570 // the start of the first vararg value... this is used to expand
571 // llvm.va_start.
572 if (Fn.getFunctionType()->isVarArg())
573 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000574}
575
576
Chris Lattner333b2fa2002-12-13 10:09:43 +0000577/// SelectPHINodes - Insert machine code to generate phis. This is tricky
578/// because we have to generate our sources into the source basic blocks, not
579/// the current one.
580///
581void ISel::SelectPHINodes() {
Chris Lattner3501fea2003-01-14 22:00:31 +0000582 const TargetInstrInfo &TII = TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000583 const Function &LF = *F->getFunction(); // The LLVM function...
584 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
585 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000586 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000587
588 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000589 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000590 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000591 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000592
Chris Lattner333b2fa2002-12-13 10:09:43 +0000593 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000594 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000595 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
596 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000597
598 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000599 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
600 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
601 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000602
Chris Lattnera6e73f12003-05-12 14:22:21 +0000603 // PHIValues - Map of blocks to incoming virtual registers. We use this
604 // so that we only initialize one incoming value for a particular block,
605 // even if the block has multiple entries in the PHI node.
606 //
607 std::map<MachineBasicBlock*, unsigned> PHIValues;
608
Chris Lattner333b2fa2002-12-13 10:09:43 +0000609 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
610 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000611 unsigned ValReg;
612 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
613 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000614
Chris Lattnera6e73f12003-05-12 14:22:21 +0000615 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
616 // We already inserted an initialization of the register for this
617 // predecessor. Recycle it.
618 ValReg = EntryIt->second;
619
620 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000621 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000622 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000623 Value *Val = PN->getIncomingValue(i);
624
625 // If this is a constant or GlobalValue, we may have to insert code
626 // into the basic block to compute it into a virtual register.
627 if (isa<Constant>(Val) || isa<GlobalValue>(Val)) {
Chris Lattner6f2ab042004-03-30 19:10:12 +0000628 if (isa<ConstantExpr>(Val)) {
629 // Because we don't want to clobber any values which might be in
630 // physical registers with the computation of this constant (which
631 // might be arbitrarily complex if it is a constant expression),
632 // just insert the computation at the top of the basic block.
633 MachineBasicBlock::iterator PI = PredMBB->begin();
634
635 // Skip over any PHI nodes though!
636 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
637 ++PI;
638
639 ValReg = getReg(Val, PredMBB, PI);
640 } else {
641 // Simple constants get emitted at the end of the basic block,
642 // before any terminator instructions. We "know" that the code to
643 // move a constant into a register will never clobber any flags.
644 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
645 }
Chris Lattnera81fc682003-10-19 00:26:11 +0000646 } else {
647 ValReg = getReg(Val);
648 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000649
650 // Remember that we inserted a value for this PHI for this predecessor
651 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
652 }
653
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000654 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000655 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000656 if (LongPhiMI) {
657 LongPhiMI->addRegOperand(ValReg+1);
658 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
659 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000660 }
Chris Lattner168aa902004-02-29 07:10:16 +0000661
662 // Now that we emitted all of the incoming values for the PHI node, make
663 // sure to reposition the InsertPoint after the PHI that we just added.
664 // This is needed because we might have inserted a constant into this
665 // block, right after the PHI's which is before the old insert point!
666 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
667 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000668 }
669 }
670}
671
Chris Lattner986618e2004-02-22 19:47:26 +0000672/// RequiresFPRegKill - The floating point stackifier pass cannot insert
673/// compensation code on critical edges. As such, it requires that we kill all
674/// FP registers on the exit from any blocks that either ARE critical edges, or
675/// branch to a block that has incoming critical edges.
676///
677/// Note that this kill instruction will eventually be eliminated when
678/// restrictions in the stackifier are relaxed.
679///
680static bool RequiresFPRegKill(const BasicBlock *BB) {
681#if 0
682 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
683 const BasicBlock *Succ = *SI;
684 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
685 ++PI; // Block have at least one predecessory
686 if (PI != PE) { // If it has exactly one, this isn't crit edge
687 // If this block has more than one predecessor, check all of the
688 // predecessors to see if they have multiple successors. If so, then the
689 // block we are analyzing needs an FPRegKill.
690 for (PI = pred_begin(Succ); PI != PE; ++PI) {
691 const BasicBlock *Pred = *PI;
692 succ_const_iterator SI2 = succ_begin(Pred);
693 ++SI2; // There must be at least one successor of this block.
694 if (SI2 != succ_end(Pred))
695 return true; // Yes, we must insert the kill on this edge.
696 }
697 }
698 }
699 // If we got this far, there is no need to insert the kill instruction.
700 return false;
701#else
702 return true;
703#endif
704}
705
706// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
707// need them. This only occurs due to the floating point stackifier not being
708// aggressive enough to handle arbitrary global stackification.
709//
710// Currently we insert an FP_REG_KILL instruction into each block that uses or
711// defines a floating point virtual register.
712//
713// When the global register allocators (like linear scan) finally update live
714// variable analysis, we can keep floating point values in registers across
715// portions of the CFG that do not involve critical edges. This will be a big
716// win, but we are waiting on the global allocators before we can do this.
717//
718// With a bit of work, the floating point stackifier pass can be enhanced to
719// break critical edges as needed (to make a place to put compensation code),
720// but this will require some infrastructure improvements as well.
721//
722void ISel::InsertFPRegKills() {
723 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000724
725 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000726 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000727 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
728 MachineOperand& MO = I->getOperand(i);
729 if (MO.isRegister() && MO.getReg()) {
730 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000731 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000732 if (RegMap.getRegClass(Reg)->getSize() == 10)
733 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000734 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000735 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000736 // If we haven't found an FP register use or def in this basic block, check
737 // to see if any of our successors has an FP PHI node, which will cause a
738 // copy to be inserted into this block.
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000739 for (succ_const_iterator SI = succ_begin(BB->getBasicBlock()),
740 E = succ_end(BB->getBasicBlock()); SI != E; ++SI) {
741 MachineBasicBlock *SBB = MBBMap[*SI];
742 for (MachineBasicBlock::iterator I = SBB->begin();
743 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
744 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
745 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000746 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000747 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000748 continue;
749 UsesFPReg:
750 // Okay, this block uses an FP register. If the block has successors (ie,
751 // it's not an unwind/return), insert the FP_REG_KILL instruction.
752 if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
753 RequiresFPRegKill(BB->getBasicBlock())) {
Chris Lattneree352852004-02-29 07:22:16 +0000754 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000755 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000756 }
757 }
758}
759
760
Chris Lattner307ecba2004-03-30 22:39:09 +0000761// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
762// it into the conditional branch or select instruction which is the only user
763// of the cc instruction. This is the case if the conditional branch is the
764// only user of the setcc, and if the setcc is in the same basic block as the
765// conditional branch. We also don't handle long arguments below, so we reject
766// them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000767//
Chris Lattner307ecba2004-03-30 22:39:09 +0000768static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000769 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000770 if (SCI->hasOneUse()) {
771 Instruction *User = cast<Instruction>(SCI->use_back());
772 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
773 SCI->getParent() == User->getParent() &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000774 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
775 SCI->getOpcode() == Instruction::SetEQ ||
776 SCI->getOpcode() == Instruction::SetNE))
Chris Lattner6d40c192003-01-16 16:43:00 +0000777 return SCI;
778 }
779 return 0;
780}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000781
Chris Lattner6d40c192003-01-16 16:43:00 +0000782// Return a fixed numbering for setcc instructions which does not depend on the
783// order of the opcodes.
784//
785static unsigned getSetCCNumber(unsigned Opcode) {
786 switch(Opcode) {
787 default: assert(0 && "Unknown setcc instruction!");
788 case Instruction::SetEQ: return 0;
789 case Instruction::SetNE: return 1;
790 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000791 case Instruction::SetGE: return 3;
792 case Instruction::SetGT: return 4;
793 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000794 }
795}
Chris Lattner06925362002-11-17 21:56:38 +0000796
Chris Lattner6d40c192003-01-16 16:43:00 +0000797// LLVM -> X86 signed X86 unsigned
798// ----- ---------- ------------
799// seteq -> sete sete
800// setne -> setne setne
801// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000802// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000803// setgt -> setg seta
804// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000805// ----
806// sets // Used by comparison with 0 optimization
807// setns
808static const unsigned SetCCOpcodeTab[2][8] = {
809 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
810 0, 0 },
811 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
812 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000813};
814
Chris Lattnerb2acc512003-10-19 21:09:10 +0000815// EmitComparison - This function emits a comparison of the two operands,
816// returning the extended setcc code to use.
817unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
818 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000819 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000820 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000821 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000822 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000823 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000824
825 // Special case handling of: cmp R, i
Chris Lattnere80e6372004-04-06 16:02:27 +0000826 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
827 if (Class == cByte || Class == cShort || Class == cInt) {
828 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000829
Chris Lattner333864d2003-06-05 19:30:30 +0000830 // Mask off any upper bits of the constant, if there are any...
831 Op1v &= (1ULL << (8 << Class)) - 1;
832
Chris Lattnerb2acc512003-10-19 21:09:10 +0000833 // If this is a comparison against zero, emit more efficient code. We
834 // can't handle unsigned comparisons against zero unless they are == or
835 // !=. These should have been strength reduced already anyway.
836 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
837 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000838 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000839 };
Chris Lattneree352852004-02-29 07:22:16 +0000840 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000841
842 if (OpNum == 2) return 6; // Map jl -> js
843 if (OpNum == 3) return 7; // Map jg -> jns
844 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000845 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000846
847 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000848 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000849 };
850
Chris Lattneree352852004-02-29 07:22:16 +0000851 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000852 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000853 } else {
854 assert(Class == cLong && "Unknown integer class!");
855 unsigned LowCst = CI->getRawValue();
856 unsigned HiCst = CI->getRawValue() >> 32;
857 if (OpNum < 2) { // seteq, setne
858 unsigned LoTmp = Op0r;
859 if (LowCst != 0) {
860 LoTmp = makeAnotherReg(Type::IntTy);
861 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
862 }
863 unsigned HiTmp = Op0r+1;
864 if (HiCst != 0) {
865 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000866 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000867 }
868 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
869 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
870 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000871 } else {
872 // Emit a sequence of code which compares the high and low parts once
873 // each, then uses a conditional move to handle the overflow case. For
874 // example, a setlt for long would generate code like this:
875 //
876 // AL = lo(op1) < lo(op2) // Signedness depends on operands
877 // BL = hi(op1) < hi(op2) // Always unsigned comparison
878 // dest = hi(op1) == hi(op2) ? AL : BL;
879 //
880
881 // FIXME: This would be much better if we had hierarchical register
882 // classes! Until then, hardcode registers so that we can deal with
883 // their aliases (because we don't have conditional byte moves).
884 //
885 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
886 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
887 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
888 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
889 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
890 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
891 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
892 .addReg(X86::AX);
893 // NOTE: visitSetCondInst knows that the value is dumped into the BL
894 // register at this point for long values...
895 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000896 }
Chris Lattner333864d2003-06-05 19:30:30 +0000897 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000898 }
Chris Lattner333864d2003-06-05 19:30:30 +0000899
Chris Lattner9f08a922004-02-03 18:54:04 +0000900 // Special case handling of comparison against +/- 0.0
901 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
902 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000903 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000904 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000905 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +0000906 return OpNum;
907 }
908
Chris Lattner58c41fe2003-08-24 19:19:47 +0000909 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000910 switch (Class) {
911 default: assert(0 && "Unknown type class!");
912 // Emit: cmp <var1>, <var2> (do the comparison). We can
913 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
914 // 32-bit.
915 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000916 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000917 break;
918 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000919 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000920 break;
921 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000922 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000923 break;
924 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +0000925 BuildMI(*MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000926 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000927 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +0000928 break;
929
930 case cLong:
931 if (OpNum < 2) { // seteq, setne
932 unsigned LoTmp = makeAnotherReg(Type::IntTy);
933 unsigned HiTmp = makeAnotherReg(Type::IntTy);
934 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000935 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
936 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
937 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000938 break; // Allow the sete or setne to be generated from flags set by OR
939 } else {
940 // Emit a sequence of code which compares the high and low parts once
941 // each, then uses a conditional move to handle the overflow case. For
942 // example, a setlt for long would generate code like this:
943 //
944 // AL = lo(op1) < lo(op2) // Signedness depends on operands
945 // BL = hi(op1) < hi(op2) // Always unsigned comparison
946 // dest = hi(op1) == hi(op2) ? AL : BL;
947 //
948
Chris Lattner6d40c192003-01-16 16:43:00 +0000949 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000950 // classes! Until then, hardcode registers so that we can deal with their
951 // aliases (because we don't have conditional byte moves).
952 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000953 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +0000954 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000955 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +0000956 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
957 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
958 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000959 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +0000960 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000961 // NOTE: visitSetCondInst knows that the value is dumped into the BL
962 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000963 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000964 }
965 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000966 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000967}
Chris Lattner3e130a22003-01-13 00:32:26 +0000968
Chris Lattner6d40c192003-01-16 16:43:00 +0000969/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
970/// register, then move it to wherever the result should be.
971///
972void ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +0000973 if (canFoldSetCCIntoBranchOrSelect(&I))
974 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +0000975
Chris Lattner6d40c192003-01-16 16:43:00 +0000976 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000977 MachineBasicBlock::iterator MII = BB->end();
978 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
979 DestReg);
980}
Chris Lattner6d40c192003-01-16 16:43:00 +0000981
Chris Lattner58c41fe2003-08-24 19:19:47 +0000982/// emitSetCCOperation - Common code shared between visitSetCondInst and
983/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000984///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000985void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000986 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000987 Value *Op0, Value *Op1, unsigned Opcode,
988 unsigned TargetReg) {
989 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000990 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000991
Chris Lattnerb2acc512003-10-19 21:09:10 +0000992 const Type *CompTy = Op0->getType();
993 unsigned CompClass = getClassB(CompTy);
994 bool isSigned = CompTy->isSigned() && CompClass != cFP;
995
996 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000997 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +0000998 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +0000999 } else {
1000 // Handle long comparisons by copying the value which is already in BL into
1001 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001002 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001003 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001004}
Chris Lattner51b49a92002-11-02 19:45:49 +00001005
Chris Lattner12d96a02004-03-30 21:22:00 +00001006void ISel::visitSelectInst(SelectInst &SI) {
1007 unsigned DestReg = getReg(SI);
1008 MachineBasicBlock::iterator MII = BB->end();
1009 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1010 SI.getFalseValue(), DestReg);
1011}
1012
1013/// emitSelect - Common code shared between visitSelectInst and the constant
1014/// expression support.
1015void ISel::emitSelectOperation(MachineBasicBlock *MBB,
1016 MachineBasicBlock::iterator IP,
1017 Value *Cond, Value *TrueVal, Value *FalseVal,
1018 unsigned DestReg) {
1019 unsigned SelectClass = getClassB(TrueVal->getType());
1020
1021 // We don't support 8-bit conditional moves. If we have incoming constants,
1022 // transform them into 16-bit constants to avoid having a run-time conversion.
1023 if (SelectClass == cByte) {
1024 if (Constant *T = dyn_cast<Constant>(TrueVal))
1025 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1026 if (Constant *F = dyn_cast<Constant>(FalseVal))
1027 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1028 }
1029
Chris Lattner307ecba2004-03-30 22:39:09 +00001030
1031 unsigned Opcode;
1032 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1033 // We successfully folded the setcc into the select instruction.
1034
1035 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1036 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1037 IP);
1038
1039 const Type *CompTy = SCI->getOperand(0)->getType();
1040 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1041
1042 // LLVM -> X86 signed X86 unsigned
1043 // ----- ---------- ------------
1044 // seteq -> cmovNE cmovNE
1045 // setne -> cmovE cmovE
1046 // setlt -> cmovGE cmovAE
1047 // setge -> cmovL cmovB
1048 // setgt -> cmovLE cmovBE
1049 // setle -> cmovG cmovA
1050 // ----
1051 // cmovNS // Used by comparison with 0 optimization
1052 // cmovS
1053
1054 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001055 default: assert(0 && "Unknown value class!");
1056 case cFP: {
1057 // Annoyingly, we don't have a full set of floating point conditional
1058 // moves. :(
1059 static const unsigned OpcodeTab[2][8] = {
1060 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1061 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1062 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1063 };
1064 Opcode = OpcodeTab[isSigned][OpNum];
1065
1066 // If opcode == 0, we hit a case that we don't support. Output a setcc
1067 // and compare the result against zero.
1068 if (Opcode == 0) {
1069 unsigned CompClass = getClassB(CompTy);
1070 unsigned CondReg;
1071 if (CompClass != cLong || OpNum < 2) {
1072 CondReg = makeAnotherReg(Type::BoolTy);
1073 // Handle normal comparisons with a setcc instruction...
1074 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1075 } else {
1076 // Long comparisons end up in the BL register.
1077 CondReg = X86::BL;
1078 }
1079
Chris Lattner68626c22004-03-31 22:22:36 +00001080 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001081 Opcode = X86::FCMOVE;
1082 }
1083 break;
1084 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001085 case cByte:
1086 case cShort: {
1087 static const unsigned OpcodeTab[2][8] = {
1088 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1089 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1090 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1091 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1092 };
1093 Opcode = OpcodeTab[isSigned][OpNum];
1094 break;
1095 }
1096 case cInt:
1097 case cLong: {
1098 static const unsigned OpcodeTab[2][8] = {
1099 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1100 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1101 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1102 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1103 };
1104 Opcode = OpcodeTab[isSigned][OpNum];
1105 break;
1106 }
1107 }
1108 } else {
1109 // Get the value being branched on, and use it to set the condition codes.
1110 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001111 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001112 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001113 default: assert(0 && "Unknown value class!");
1114 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001115 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001116 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001117 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001118 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001119 }
1120 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001121
1122 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1123 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1124 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001125
Chris Lattner12d96a02004-03-30 21:22:00 +00001126
1127 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1128 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1129 // cmove, then truncate the result.
1130 if (SelectClass == cByte) {
1131 DestReg = makeAnotherReg(Type::ShortTy);
1132 if (getClassB(TrueVal->getType()) == cByte) {
1133 // Promote the true value, by storing it into AL, and reading from AX.
1134 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1135 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1136 TrueReg = makeAnotherReg(Type::ShortTy);
1137 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1138 }
1139 if (getClassB(FalseVal->getType()) == cByte) {
1140 // Promote the true value, by storing it into CL, and reading from CX.
1141 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1142 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1143 FalseReg = makeAnotherReg(Type::ShortTy);
1144 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1145 }
1146 }
1147
1148 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1149
1150 switch (SelectClass) {
1151 case cByte:
1152 // We did the computation with 16-bit registers. Truncate back to our
1153 // result by copying into AX then copying out AL.
1154 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1155 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1156 break;
1157 case cLong:
1158 // Move the upper half of the value as well.
1159 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1160 break;
1161 }
1162}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001163
1164
1165
Brian Gaekec2505982002-11-30 11:57:28 +00001166/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1167/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001168///
Chris Lattner3e130a22003-01-13 00:32:26 +00001169void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
1170 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001171
Chris Lattner29bf0622004-04-06 01:21:00 +00001172 Value *Val = VR.Val;
1173 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001174 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001175 if (Constant *C = dyn_cast<Constant>(Val)) {
1176 Val = ConstantExpr::getCast(C, Type::IntTy);
1177 Ty = Type::IntTy;
1178 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001179
Chris Lattner502e36c2004-04-06 01:25:33 +00001180 // If this is a simple constant, just emit a MOVri directly to avoid the
1181 // copy.
1182 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1183 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
1184 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
1185 return;
1186 }
1187 }
1188
Chris Lattner29bf0622004-04-06 01:21:00 +00001189 // Make sure we have the register number for this value...
1190 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1191
1192 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001193 case cByte:
1194 // Extend value into target register (8->32)
1195 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001196 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001197 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001198 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001199 break;
1200 case cShort:
1201 // Extend value into target register (16->32)
1202 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001203 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001204 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001205 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001206 break;
1207 case cInt:
1208 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001209 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001210 break;
1211 default:
1212 assert(0 && "Unpromotable operand class in promote32");
1213 }
Brian Gaekec2505982002-11-30 11:57:28 +00001214}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001215
Chris Lattner72614082002-10-25 22:55:53 +00001216/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1217/// we have the following possibilities:
1218///
1219/// ret void: No return value, simply emit a 'ret' instruction
1220/// ret sbyte, ubyte : Extend value into EAX and return
1221/// ret short, ushort: Extend value into EAX and return
1222/// ret int, uint : Move value into EAX and return
1223/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001224/// ret long, ulong : Move value into EAX/EDX and return
1225/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001226///
Chris Lattner3e130a22003-01-13 00:32:26 +00001227void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001228 if (I.getNumOperands() == 0) {
1229 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1230 return;
1231 }
1232
1233 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001234 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001235 case cByte: // integral return values: extend or move into EAX and return
1236 case cShort:
1237 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001238 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001239 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001240 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001241 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001242 case cFP: { // Floats & Doubles: Return in ST(0)
1243 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001244 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001245 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001246 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001247 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001248 }
1249 case cLong: {
1250 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001251 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1252 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001253 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001254 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1255 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001256 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001257 }
Chris Lattner94af4142002-12-25 05:13:53 +00001258 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001259 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001260 }
Chris Lattner43189d12002-11-17 20:07:45 +00001261 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001262 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001263}
1264
Chris Lattner55f6fab2003-01-16 18:07:23 +00001265// getBlockAfter - Return the basic block which occurs lexically after the
1266// specified one.
1267static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1268 Function::iterator I = BB; ++I; // Get iterator to next block
1269 return I != BB->getParent()->end() ? &*I : 0;
1270}
1271
Chris Lattner51b49a92002-11-02 19:45:49 +00001272/// visitBranchInst - Handle conditional and unconditional branches here. Note
1273/// that since code layout is frozen at this point, that if we are trying to
1274/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001275/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001276///
Chris Lattner94af4142002-12-25 05:13:53 +00001277void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +00001278 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1279
1280 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001281 if (BI.getSuccessor(0) != NextBB)
Chris Lattner55f6fab2003-01-16 18:07:23 +00001282 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +00001283 return;
1284 }
1285
1286 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001287 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001288 if (SCI == 0) {
1289 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1290 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001291 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001292 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001293 if (BI.getSuccessor(1) == NextBB) {
1294 if (BI.getSuccessor(0) != NextBB)
1295 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
1296 } else {
1297 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
1298
1299 if (BI.getSuccessor(0) != NextBB)
1300 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
1301 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001302 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001303 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001304
1305 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001306 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001307 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001308
1309 const Type *CompTy = SCI->getOperand(0)->getType();
1310 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001311
Chris Lattnerb2acc512003-10-19 21:09:10 +00001312
Chris Lattner6d40c192003-01-16 16:43:00 +00001313 // LLVM -> X86 signed X86 unsigned
1314 // ----- ---------- ------------
1315 // seteq -> je je
1316 // setne -> jne jne
1317 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001318 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001319 // setgt -> jg ja
1320 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001321 // ----
1322 // js // Used by comparison with 0 optimization
1323 // jns
1324
1325 static const unsigned OpcodeTab[2][8] = {
1326 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1327 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1328 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001329 };
1330
Chris Lattner55f6fab2003-01-16 18:07:23 +00001331 if (BI.getSuccessor(0) != NextBB) {
1332 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
1333 if (BI.getSuccessor(1) != NextBB)
1334 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
1335 } else {
1336 // Change to the inverse condition...
1337 if (BI.getSuccessor(1) != NextBB) {
1338 OpNum ^= 1;
1339 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
1340 }
1341 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001342}
1343
Chris Lattner3e130a22003-01-13 00:32:26 +00001344
1345/// doCall - This emits an abstract call instruction, setting up the arguments
1346/// and the return value as appropriate. For the actual function call itself,
1347/// it inserts the specified CallMI instruction into the stream.
1348///
1349void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001350 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001351
Chris Lattner065faeb2002-12-28 20:24:02 +00001352 // Count how many bytes are to be pushed on the stack...
1353 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001354
Chris Lattner3e130a22003-01-13 00:32:26 +00001355 if (!Args.empty()) {
1356 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1357 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001358 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001359 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001360 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001361 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001362 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001363 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1364 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001365 default: assert(0 && "Unknown class!");
1366 }
1367
1368 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001369 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001370
1371 // Arguments go on the stack in reverse order, as specified by the ABI.
1372 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001373 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001374 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001375 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001376 case cByte:
Chris Lattner21585222004-03-01 02:42:43 +00001377 case cShort:
1378 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1379 // Zero/Sign extend constant, then stuff into memory.
1380 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1381 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1382 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1383 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1384 } else {
1385 // Promote arg to 32 bits wide into a temporary register...
1386 ArgReg = makeAnotherReg(Type::UIntTy);
1387 promote32(ArgReg, Args[i]);
1388 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1389 X86::ESP, ArgOffset).addReg(ArgReg);
1390 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001391 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001392 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001393 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1394 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1395 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1396 X86::ESP, ArgOffset).addImm(Val);
1397 } else {
1398 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1399 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1400 X86::ESP, ArgOffset).addReg(ArgReg);
1401 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001402 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001403 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001404 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1405 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1406 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1407 X86::ESP, ArgOffset).addImm(Val & ~0U);
1408 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1409 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1410 } else {
1411 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1412 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1413 X86::ESP, ArgOffset).addReg(ArgReg);
1414 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1415 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1416 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001417 ArgOffset += 4; // 8 byte entry, not 4.
1418 break;
1419
Chris Lattner065faeb2002-12-28 20:24:02 +00001420 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001421 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001422 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001423 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001424 X86::ESP, ArgOffset).addReg(ArgReg);
1425 } else {
1426 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001427 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001428 X86::ESP, ArgOffset).addReg(ArgReg);
1429 ArgOffset += 4; // 8 byte entry, not 4.
1430 }
1431 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001432
Chris Lattner3e130a22003-01-13 00:32:26 +00001433 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001434 }
1435 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001436 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001437 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001438 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001439 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001440
Chris Lattner3e130a22003-01-13 00:32:26 +00001441 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001442
Chris Lattneree352852004-02-29 07:22:16 +00001443 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001444
1445 // If there is a return value, scavenge the result from the location the call
1446 // leaves it in...
1447 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001448 if (Ret.Ty != Type::VoidTy) {
1449 unsigned DestClass = getClassB(Ret.Ty);
1450 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001451 case cByte:
1452 case cShort:
1453 case cInt: {
1454 // Integral results are in %eax, or the appropriate portion
1455 // thereof.
1456 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001457 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001458 };
1459 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001460 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001461 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001462 }
Chris Lattner94af4142002-12-25 05:13:53 +00001463 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001464 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001465 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001466 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001467 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1468 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001469 break;
1470 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001471 }
Chris Lattnera3243642002-12-04 23:45:28 +00001472 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001473}
Chris Lattner2df035b2002-11-02 19:27:56 +00001474
Chris Lattner3e130a22003-01-13 00:32:26 +00001475
1476/// visitCallInst - Push args on stack and do a procedure call instruction.
1477void ISel::visitCallInst(CallInst &CI) {
1478 MachineInstr *TheCall;
1479 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001480 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001481 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001482 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1483 return;
1484 }
1485
Chris Lattner3e130a22003-01-13 00:32:26 +00001486 // Emit a CALL instruction with PC-relative displacement.
1487 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1488 } else { // Emit an indirect call...
1489 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001490 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001491 }
1492
1493 std::vector<ValueRecord> Args;
1494 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001495 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001496
1497 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1498 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001499}
Chris Lattner3e130a22003-01-13 00:32:26 +00001500
Chris Lattneraeb54b82003-08-28 21:23:43 +00001501
Chris Lattner44827152003-12-28 09:47:19 +00001502/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1503/// function, lowering any calls to unknown intrinsic functions into the
1504/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001505///
Chris Lattner44827152003-12-28 09:47:19 +00001506void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1507 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1508 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1509 if (CallInst *CI = dyn_cast<CallInst>(I++))
1510 if (Function *F = CI->getCalledFunction())
1511 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001512 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001513 case Intrinsic::vastart:
1514 case Intrinsic::vacopy:
1515 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001516 case Intrinsic::returnaddress:
1517 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001518 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001519 case Intrinsic::memset:
John Criswell4ffff9e2004-04-08 20:31:47 +00001520 case Intrinsic::readport:
1521 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001522 // We directly implement these intrinsics
1523 break;
1524 default:
1525 // All other intrinsic calls we must lower.
1526 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001527 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001528 if (Before) { // Move iterator to instruction after call
1529 I = Before; ++I;
1530 } else {
1531 I = BB->begin();
1532 }
1533 }
1534
1535}
1536
Brian Gaeked0fde302003-11-11 22:41:34 +00001537void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001538 unsigned TmpReg1, TmpReg2;
1539 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001540 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001541 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001542 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001543 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001544 return;
1545
Chris Lattner5634b9f2004-03-13 00:24:52 +00001546 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001547 TmpReg1 = getReg(CI);
1548 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001549 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001550 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001551 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001552
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001553 case Intrinsic::returnaddress:
1554 case Intrinsic::frameaddress:
1555 TmpReg1 = getReg(CI);
1556 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1557 if (ID == Intrinsic::returnaddress) {
1558 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001559 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001560 ReturnAddressIndex);
1561 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001562 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001563 ReturnAddressIndex, -4);
1564 }
1565 } else {
1566 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001567 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001568 }
1569 return;
1570
Chris Lattner915e5e52004-02-12 17:53:22 +00001571 case Intrinsic::memcpy: {
1572 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1573 unsigned Align = 1;
1574 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1575 Align = AlignC->getRawValue();
1576 if (Align == 0) Align = 1;
1577 }
1578
1579 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001580 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001581 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001582 switch (Align & 3) {
1583 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001584 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1585 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1586 } else {
1587 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001588 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001589 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001590 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001591 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001592 break;
1593 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001594 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1595 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1596 } else {
1597 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001598 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001599 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001600 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001601 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001602 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001603 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001604 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001605 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001606 break;
1607 }
1608
1609 // No matter what the alignment is, we put the source in ESI, the
1610 // destination in EDI, and the count in ECX.
1611 TmpReg1 = getReg(CI.getOperand(1));
1612 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001613 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1614 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1615 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001616 BuildMI(BB, Opcode, 0);
1617 return;
1618 }
1619 case Intrinsic::memset: {
1620 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1621 unsigned Align = 1;
1622 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1623 Align = AlignC->getRawValue();
1624 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001625 }
1626
Chris Lattner2a0f2242004-02-14 04:46:05 +00001627 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001628 unsigned CountReg;
1629 unsigned Opcode;
1630 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1631 unsigned Val = ValC->getRawValue() & 255;
1632
1633 // If the value is a constant, then we can potentially use larger copies.
1634 switch (Align & 3) {
1635 case 2: // WORD aligned
1636 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001637 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001638 } else {
1639 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001640 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001641 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001642 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001643 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001644 Opcode = X86::REP_STOSW;
1645 break;
1646 case 0: // DWORD aligned
1647 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001648 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001649 } else {
1650 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001651 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001652 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001653 }
1654 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001655 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001656 Opcode = X86::REP_STOSD;
1657 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001658 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001659 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001660 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001661 Opcode = X86::REP_STOSB;
1662 break;
1663 }
1664 } else {
1665 // If it's not a constant value we are storing, just fall back. We could
1666 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1667 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001668 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001669 CountReg = getReg(CI.getOperand(3));
1670 Opcode = X86::REP_STOSB;
1671 }
1672
1673 // No matter what the alignment is, we put the source in ESI, the
1674 // destination in EDI, and the count in ECX.
1675 TmpReg1 = getReg(CI.getOperand(1));
1676 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001677 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1678 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001679 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001680 return;
1681 }
1682
John Criswell4ffff9e2004-04-08 20:31:47 +00001683 case Intrinsic::readport:
1684 //
1685 // First, determine that the size of the operand falls within the
1686 // acceptable range for this architecture.
1687 //
John Criswellca6ea0f2004-04-08 22:39:13 +00001688 if ((CI.getOperand(1)->getType()->getPrimitiveSize()) != 2) {
1689 std::cerr << "llvm.readport: Address size is not 16 bits\n";
1690 exit (1);
1691 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001692
1693 //
1694 // Now, move the I/O port address into the DX register and use the IN
1695 // instruction to get the input data.
1696 //
1697 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(getReg(CI.getOperand(1)));
1698 switch (CI.getCalledFunction()->getReturnType()->getPrimitiveSize()) {
1699 case 1:
John Criswellca6ea0f2004-04-08 22:39:13 +00001700 BuildMI(BB, X86::IN8, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001701 break;
1702 case 2:
John Criswellca6ea0f2004-04-08 22:39:13 +00001703 BuildMI(BB, X86::IN16, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001704 break;
1705 case 4:
John Criswellca6ea0f2004-04-08 22:39:13 +00001706 BuildMI(BB, X86::IN32, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001707 break;
1708 default:
John Criswellaee0cf32004-04-09 15:10:15 +00001709 std::cerr << "Cannot do input on this data type";
1710 exit (1);
John Criswell4ffff9e2004-04-08 20:31:47 +00001711 }
1712 return;
1713
1714 case Intrinsic::writeport:
1715 //
1716 // First, determine that the size of the operand falls within the
1717 // acceptable range for this architecture.
1718 //
John Criswellca6ea0f2004-04-08 22:39:13 +00001719 //
John Criswell6d804f42004-04-09 19:09:14 +00001720 if ((CI.getOperand(2)->getType()->getPrimitiveSize()) != 2) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001721 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1722 exit (1);
1723 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001724
1725 //
1726 // Now, move the I/O port address into the DX register and the value to
1727 // write into the AL/AX/EAX register.
1728 //
John Criswell6d804f42004-04-09 19:09:14 +00001729 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(getReg(CI.getOperand(2)));
1730 switch (CI.getOperand(1)->getType()->getPrimitiveSize()) {
John Criswell4ffff9e2004-04-08 20:31:47 +00001731 case 1:
John Criswell6d804f42004-04-09 19:09:14 +00001732 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(getReg(CI.getOperand(1)));
John Criswellca6ea0f2004-04-08 22:39:13 +00001733 BuildMI(BB, X86::OUT8, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001734 break;
1735 case 2:
John Criswell6d804f42004-04-09 19:09:14 +00001736 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(getReg(CI.getOperand(1)));
John Criswellca6ea0f2004-04-08 22:39:13 +00001737 BuildMI(BB, X86::OUT16, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001738 break;
1739 case 4:
John Criswell6d804f42004-04-09 19:09:14 +00001740 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(getReg(CI.getOperand(1)));
John Criswellca6ea0f2004-04-08 22:39:13 +00001741 BuildMI(BB, X86::OUT32, 0);
John Criswell4ffff9e2004-04-08 20:31:47 +00001742 break;
1743 default:
John Criswellaee0cf32004-04-09 15:10:15 +00001744 std::cerr << "Cannot do output on this data type";
1745 exit (1);
John Criswell4ffff9e2004-04-08 20:31:47 +00001746 }
1747 return;
1748
Chris Lattner44827152003-12-28 09:47:19 +00001749 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001750 }
1751}
1752
Chris Lattner7dee5da2004-03-08 01:58:35 +00001753static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1754 if (LI.getParent() != User.getParent())
1755 return false;
1756 BasicBlock::iterator It = &LI;
1757 // Check all of the instructions between the load and the user. We should
1758 // really use alias analysis here, but for now we just do something simple.
1759 for (++It; It != BasicBlock::iterator(&User); ++It) {
1760 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001761 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001762 case Instruction::Store:
1763 case Instruction::Call:
1764 case Instruction::Invoke:
1765 return false;
1766 }
1767 }
1768 return true;
1769}
1770
Chris Lattnereca195e2003-05-08 19:44:13 +00001771
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001772/// visitSimpleBinary - Implement simple binary operators for integral types...
1773/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1774/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001775///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001776void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1777 unsigned DestReg = getReg(B);
1778 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001779 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1780
Chris Lattner7dee5da2004-03-08 01:58:35 +00001781 // Special case: op Reg, load [mem]
1782 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
1783 if (!B.swapOperands())
1784 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1785
1786 unsigned Class = getClassB(B.getType());
1787 if (isa<LoadInst>(Op1) && Class < cFP &&
1788 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1789
1790 static const unsigned OpcodeTab[][3] = {
1791 // Arithmetic operators
1792 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1793 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1794
1795 // Bitwise operators
1796 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1797 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1798 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1799 };
1800
1801 assert(Class < cFP && "General code handles 64-bit integer types!");
1802 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1803
1804 unsigned BaseReg, Scale, IndexReg, Disp;
1805 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), BaseReg,
1806 Scale, IndexReg, Disp);
1807
1808 unsigned Op0r = getReg(Op0);
1809 addFullAddress(BuildMI(BB, Opcode, 2, DestReg).addReg(Op0r),
1810 BaseReg, Scale, IndexReg, Disp);
1811 return;
1812 }
1813
Chris Lattner721d2d42004-03-08 01:18:36 +00001814 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001815}
Chris Lattner3e130a22003-01-13 00:32:26 +00001816
Chris Lattnerb2acc512003-10-19 21:09:10 +00001817/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1818/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1819/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001820///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001821/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1822/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001823///
1824void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001825 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001826 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001827 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001828 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001829
1830 // sub 0, X -> neg X
Chris Lattner48b0c972004-04-11 20:26:20 +00001831 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
1832 if (OperatorClass == 1 && CI->isNullValue()) {
1833 unsigned op1Reg = getReg(Op1, MBB, IP);
1834 static unsigned const NEGTab[] = {
1835 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
1836 };
1837 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
1838
1839 if (Class == cLong) {
1840 // We just emitted: Dl = neg Sl
1841 // Now emit : T = addc Sh, 0
1842 // : Dh = neg T
1843 unsigned T = makeAnotherReg(Type::IntTy);
1844 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
1845 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001846 }
Chris Lattner48b0c972004-04-11 20:26:20 +00001847 return;
1848 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001849
Chris Lattner48b0c972004-04-11 20:26:20 +00001850 // Special case: op Reg, <const int>
1851 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00001852 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001853
Chris Lattner721d2d42004-03-08 01:18:36 +00001854 // xor X, -1 -> not X
1855 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001856 static unsigned const NOTTab[] = {
1857 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
1858 };
Chris Lattner721d2d42004-03-08 01:18:36 +00001859 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001860 if (Class == cLong) // Invert the top part too
1861 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00001862 return;
1863 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001864
Chris Lattner721d2d42004-03-08 01:18:36 +00001865 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00001866 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
1867 // Note that we can't use dec for 64-bit decrements, because it does not
1868 // set the carry flag!
1869 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00001870 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1871 return;
1872 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001873
Chris Lattner721d2d42004-03-08 01:18:36 +00001874 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00001875 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
1876 // Note that we can't use inc for 64-bit increments, because it does not
1877 // set the carry flag!
1878 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00001879 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00001880 return;
1881 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001882
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001883 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00001884 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001885 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
1886 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00001887
Chris Lattner721d2d42004-03-08 01:18:36 +00001888 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001889 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
1890 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
1891 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00001892 };
1893
Chris Lattner721d2d42004-03-08 01:18:36 +00001894 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00001895 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00001896
Chris Lattner33f7fa32004-04-06 03:15:53 +00001897 if (Class != cLong) {
1898 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
1899 return;
1900 } else {
1901 // If this is a long value and the high or low bits have a special
1902 // property, emit some special cases.
1903 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001904
Chris Lattner33f7fa32004-04-06 03:15:53 +00001905 // If the constant is zero in the low 32-bits, just copy the low part
1906 // across and apply the normal 32-bit operation to the high parts. There
1907 // will be no carry or borrow into the top.
1908 if (Op1l == 0) {
1909 if (OperatorClass != 2) // All but and...
1910 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
1911 else
1912 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
1913 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
1914 .addReg(Op0r+1).addImm(Op1h);
1915 return;
1916 }
1917
1918 // If this is a logical operation and the top 32-bits are zero, just
1919 // operate on the lower 32.
1920 if (Op1h == 0 && OperatorClass > 1) {
1921 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
1922 .addReg(Op0r).addImm(Op1l);
1923 if (OperatorClass != 2) // All but and
1924 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
1925 else
1926 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
1927 return;
1928 }
1929
1930 // TODO: We could handle lots of other special cases here, such as AND'ing
1931 // with 0xFFFFFFFF00000000 -> noop, etc.
1932
1933 // Otherwise, code generate the full operation with a constant.
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001934 static const unsigned TopTab[] = {
1935 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
1936 };
Chris Lattner33f7fa32004-04-06 03:15:53 +00001937
1938 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001939 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
Chris Lattner33f7fa32004-04-06 03:15:53 +00001940 .addReg(Op0r+1).addImm(Op1h);
1941 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001942 }
Chris Lattner721d2d42004-03-08 01:18:36 +00001943 }
1944
Chris Lattner48b0c972004-04-11 20:26:20 +00001945 // Special case: op Reg, <const fp>
1946 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
1947 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
1948 assert(OperatorClass < 2 && "FP operations only support add/sub!");
1949
1950 // Create a constant pool entry for this constant.
1951 MachineConstantPool *CP = F->getConstantPool();
1952 unsigned CPI = CP->getConstantPoolIndex(Op1C);
1953 const Type *Ty = Op1->getType();
1954
1955 static const unsigned OpcodeTab[][2] = {
1956 { X86::FADD32m, X86::FSUB32m }, // Float
1957 { X86::FADD64m, X86::FSUB64m }, // Double
1958 };
1959
1960 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1961 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
1962 unsigned Op0r = getReg(Op0, MBB, IP);
1963 addConstantPoolReference(BuildMI(*MBB, IP, Opcode, 5,
1964 DestReg).addReg(Op0r), CPI);
1965 return;
1966 }
1967
1968 // Special case: R1 = sub <const fp>, R2
1969 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1970 if (OperatorClass == 1) { // sub only
1971 if (CFP->isExactlyValue(-0.0)) {
1972 // -0.0 - X === -X
1973 unsigned op1Reg = getReg(Op1, MBB, IP);
1974 BuildMI(*MBB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
1975 return;
1976 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
1977 // R1 = sub CST, R2 --> R1 = subr R2, CST
1978
1979 // Create a constant pool entry for this constant.
1980 MachineConstantPool *CP = F->getConstantPool();
1981 unsigned CPI = CP->getConstantPoolIndex(CFP);
1982 const Type *Ty = CFP->getType();
1983
1984 static const unsigned OpcodeTab[2] = { X86::FSUBR32m, X86::FSUBR64m };
1985
1986 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
1987 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy];
1988 unsigned Op1r = getReg(Op1, MBB, IP);
1989 addConstantPoolReference(BuildMI(*MBB, IP, Opcode, 5,
1990 DestReg).addReg(Op1r), CPI);
1991 return;
1992 }
1993 }
1994
Chris Lattner721d2d42004-03-08 01:18:36 +00001995 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00001996 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00001997 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00001998 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, X86::FpADD, X86::ADD32rr },// ADD
1999 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB, X86::SUB32rr },// SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002000
Chris Lattnerb2acc512003-10-19 21:09:10 +00002001 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002002 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2003 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2004 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002005 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002006
Chris Lattnerb2acc512003-10-19 21:09:10 +00002007 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002008 assert(Opcode && "Floating point arguments to logical inst?");
2009 unsigned Op0r = getReg(Op0, MBB, IP);
2010 unsigned Op1r = getReg(Op1, MBB, IP);
2011 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2012
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002013 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002014 static const unsigned TopTab[] = {
2015 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2016 };
2017 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2018 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2019 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002020}
2021
Chris Lattner3e130a22003-01-13 00:32:26 +00002022/// doMultiply - Emit appropriate instructions to multiply together the
2023/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2024/// result should be given as DestTy.
2025///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002026void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00002027 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00002028 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002029 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002030 switch (Class) {
2031 case cFP: // Floating point multiply
Chris Lattneree352852004-02-29 07:22:16 +00002032 BuildMI(*MBB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00002033 return;
Chris Lattner0f1c4612003-06-21 17:16:58 +00002034 case cInt:
2035 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002036 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002037 .addReg(op0Reg).addReg(op1Reg);
2038 return;
2039 case cByte:
2040 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002041 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2042 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2043 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002044 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002045 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002046 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002047 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002048}
2049
Chris Lattnerb2acc512003-10-19 21:09:10 +00002050// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2051// returns zero when the input is not exactly a power of two.
2052static unsigned ExactLog2(unsigned Val) {
2053 if (Val == 0) return 0;
2054 unsigned Count = 0;
2055 while (Val != 1) {
2056 if (Val & 1) return 0;
2057 Val >>= 1;
2058 ++Count;
2059 }
2060 return Count+1;
2061}
2062
Chris Lattner462fa822004-04-11 20:56:28 +00002063
2064/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2065/// 16, or 32-bit integer multiply by a constant.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002066void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002067 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002068 unsigned DestReg, const Type *DestTy,
2069 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002070 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2071 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
2072
Chris Lattnerb2acc512003-10-19 21:09:10 +00002073 unsigned Class = getClass(DestTy);
2074
Chris Lattner6ab06d52004-04-06 04:55:43 +00002075 if (ConstRHS == 0) {
2076 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2077 return;
2078 } else if (ConstRHS == 1) {
2079 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2080 return;
2081 }
2082
Chris Lattnerb2acc512003-10-19 21:09:10 +00002083 // If the element size is exactly a power of 2, use a shift to get it.
2084 if (unsigned Shift = ExactLog2(ConstRHS)) {
2085 switch (Class) {
2086 default: assert(0 && "Unknown class for this function!");
2087 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002088 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002089 return;
2090 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002091 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002092 return;
2093 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002094 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002095 return;
2096 }
2097 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002098
2099 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002100 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002101 return;
2102 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002103 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002104 return;
2105 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002106
2107 // Most general case, emit a normal multiply...
Chris Lattnerb2acc512003-10-19 21:09:10 +00002108 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002109 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002110
2111 // Emit a MUL to multiply the register holding the index by
2112 // elementSize, putting the result in OffsetReg.
2113 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2114}
2115
Chris Lattnerca9671d2002-11-02 20:28:58 +00002116/// visitMul - Multiplies are not simple binary operators because they must deal
2117/// with the EAX register explicitly.
2118///
2119void ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002120 unsigned ResultReg = getReg(I);
2121
2122 MachineBasicBlock::iterator IP = BB->end();
2123 emitMultiply(BB, IP, I.getOperand(0), I.getOperand(1), ResultReg);
2124}
2125
2126void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2127 Value *Op0, Value *Op1, unsigned DestReg) {
2128 MachineBasicBlock &BB = *MBB;
2129 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002130
2131 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002132 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002133 switch (Class) {
2134 case cByte:
2135 case cShort:
2136 case cInt:
2137 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002138 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2139 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002140 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002141 unsigned Op1Reg = getReg(Op1, &BB, IP);
2142 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002143 }
Chris Lattner462fa822004-04-11 20:56:28 +00002144 return;
2145 case cFP:
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002146 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2147 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2148 // Create a constant pool entry for this constant.
2149 MachineConstantPool *CP = F->getConstantPool();
2150 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2151 const Type *Ty = Op1C->getType();
2152
2153 static const unsigned OpcodeTab[2] = { X86::FMUL32m, X86::FMUL64m };
2154
2155 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy&&"Unknown FP type!");
2156 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy];
2157 addConstantPoolReference(BuildMI(*MBB, IP, Opcode, 5,
2158 DestReg).addReg(Op0Reg), CPI);
2159 return;
2160 }
2161
Chris Lattner462fa822004-04-11 20:56:28 +00002162 {
Chris Lattner462fa822004-04-11 20:56:28 +00002163 unsigned Op1Reg = getReg(Op1, &BB, IP);
2164 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
2165 return;
2166 }
2167 case cLong:
2168 break;
2169 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002170
Chris Lattner462fa822004-04-11 20:56:28 +00002171 // Long value. We have to do things the hard way...
2172 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2173 unsigned CLow = CI->getRawValue();
2174 unsigned CHi = CI->getRawValue() >> 32;
2175
2176 if (CLow == 0) {
2177 // If the low part of the constant is all zeros, things are simple.
2178 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2179 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2180 return;
2181 }
2182
2183 // Multiply the two low parts... capturing carry into EDX
2184 unsigned OverflowReg = 0;
2185 if (CLow == 1) {
2186 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002187 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002188 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2189 OverflowReg = makeAnotherReg(Type::UIntTy);
2190 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2191 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2192 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002193
Chris Lattner462fa822004-04-11 20:56:28 +00002194 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2195 BuildMI(BB, IP, X86::MOV32rr, 1,
2196 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2197 }
2198
2199 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2200 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2201
2202 unsigned AHBLplusOverflowReg;
2203 if (OverflowReg) {
2204 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2205 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002206 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002207 } else {
2208 AHBLplusOverflowReg = AHBLReg;
2209 }
2210
2211 if (CHi == 0) {
2212 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2213 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002214 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002215 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002216
Chris Lattner462fa822004-04-11 20:56:28 +00002217 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002218 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2219 }
Chris Lattner462fa822004-04-11 20:56:28 +00002220 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002221 }
Chris Lattner462fa822004-04-11 20:56:28 +00002222
2223 // General 64x64 multiply
2224
2225 unsigned Op1Reg = getReg(Op1, &BB, IP);
2226 // Multiply the two low parts... capturing carry into EDX
2227 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2228 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2229
2230 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2231 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2232 BuildMI(BB, IP, X86::MOV32rr, 1,
2233 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2234
2235 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2236 BuildMI(BB, IP, X86::IMUL32rr, 2,
2237 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2238
2239 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2240 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2241 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2242
2243 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2244 BuildMI(BB, IP, X86::IMUL32rr, 2,
2245 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2246
2247 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2248 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002249}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002250
Chris Lattner06925362002-11-17 21:56:38 +00002251
Chris Lattnerf01729e2002-11-02 20:54:46 +00002252/// visitDivRem - Handle division and remainder instructions... these
2253/// instruction both require the same instructions to be generated, they just
2254/// select the result from a different register. Note that both of these
2255/// instructions work differently for signed and unsigned operands.
2256///
2257void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002258 unsigned ResultReg = getReg(I);
Chris Lattner94af4142002-12-25 05:13:53 +00002259
Chris Lattnercadff442003-10-23 17:21:43 +00002260 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner462fa822004-04-11 20:56:28 +00002261 emitDivRemOperation(BB, IP, I.getOperand(0), I.getOperand(1),
2262 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002263}
2264
2265void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002266 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +00002267 Value *Op0, Value *Op1, bool isDiv,
2268 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002269 const Type *Ty = Op0->getType();
2270 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002271 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002272 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002273 if (isDiv) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002274 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2275 if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
2276 // Create a constant pool entry for this constant.
2277 MachineConstantPool *CP = F->getConstantPool();
2278 unsigned CPI = CP->getConstantPoolIndex(CFP);
2279 static const unsigned OpcodeTab[2] = { X86::FDIVR32m, X86::FDIVR64m };
2280
2281 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy&&"Unknown FP type!");
2282 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy];
2283 unsigned Op1Reg = getReg(Op1, BB, IP);
2284 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2285 ResultReg).addReg(Op1Reg), CPI);
2286 return;
2287 }
2288
2289 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
2290 if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
2291 // Create a constant pool entry for this constant.
2292 MachineConstantPool *CP = F->getConstantPool();
2293 unsigned CPI = CP->getConstantPoolIndex(CFP);
2294
2295 static const unsigned OpcodeTab[2] = { X86::FDIV32m, X86::FDIV64m };
2296
2297 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy&&"Unknown FP type!");
2298 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy];
2299 unsigned Op0Reg = getReg(Op0, BB, IP);
2300 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2301 ResultReg).addReg(Op0Reg), CPI);
2302 return;
2303 }
2304
Chris Lattner462fa822004-04-11 20:56:28 +00002305 unsigned Op0Reg = getReg(Op0, BB, IP);
2306 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002307 BuildMI(*BB, IP, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002308 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002309 unsigned Op0Reg = getReg(Op0, BB, IP);
2310 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002311 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002312 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002313 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002314 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2315 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002316 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2317 }
Chris Lattner94af4142002-12-25 05:13:53 +00002318 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002319 case cLong: {
2320 static const char *FnName[] =
2321 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002322 unsigned Op0Reg = getReg(Op0, BB, IP);
2323 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002324 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002325 MachineInstr *TheCall =
2326 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2327
2328 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002329 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2330 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002331 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2332 return;
2333 }
2334 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002335 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002336 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002337 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002338
2339 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002340 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
2341 static const unsigned SarOpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2342 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002343 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2344
2345 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002346 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2347 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002348 };
2349
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002350 bool isSigned = Ty->isSigned();
Chris Lattnerf01729e2002-11-02 20:54:46 +00002351 unsigned Reg = Regs[Class];
2352 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002353
2354 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002355 unsigned Op0Reg = getReg(Op0, BB, IP);
2356 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002357 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002358
2359 if (isSigned) {
2360 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002361 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattneree352852004-02-29 07:22:16 +00002362 BuildMI(*BB, IP, SarOpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
2363 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002364 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002365 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002366 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002367 }
2368
Chris Lattner06925362002-11-17 21:56:38 +00002369 // Emit the appropriate divide or remainder instruction...
Chris Lattneree352852004-02-29 07:22:16 +00002370 BuildMI(*BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00002371
Chris Lattnerf01729e2002-11-02 20:54:46 +00002372 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002373 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002374
Chris Lattnerf01729e2002-11-02 20:54:46 +00002375 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002376 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002377}
Chris Lattnere2954c82002-11-02 20:04:26 +00002378
Chris Lattner06925362002-11-17 21:56:38 +00002379
Brian Gaekea1719c92002-10-31 23:03:59 +00002380/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2381/// for constant immediate shift values, and for constant immediate
2382/// shift values equal to 1. Even the general case is sort of special,
2383/// because the shift amount has to be in CL, not just any old register.
2384///
Chris Lattner3e130a22003-01-13 00:32:26 +00002385void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002386 MachineBasicBlock::iterator IP = BB->end ();
2387 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2388 I.getOpcode () == Instruction::Shl, I.getType (),
2389 getReg (I));
2390}
2391
2392/// emitShiftOperation - Common code shared between visitShiftInst and
2393/// constant expression support.
2394void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002395 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002396 Value *Op, Value *ShiftAmount, bool isLeftShift,
2397 const Type *ResultTy, unsigned DestReg) {
2398 unsigned SrcReg = getReg (Op, MBB, IP);
2399 bool isSigned = ResultTy->isSigned ();
2400 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002401
2402 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002403 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2404 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2405 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2406 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002407 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002408
Chris Lattner3e130a22003-01-13 00:32:26 +00002409 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002410 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2411 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2412 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2413 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002414 };
Chris Lattner796df732002-11-02 00:44:25 +00002415
Chris Lattner3e130a22003-01-13 00:32:26 +00002416 // Longs, as usual, are handled specially...
2417 if (Class == cLong) {
2418 // If we have a constant shift, we can generate much more efficient code
2419 // than otherwise...
2420 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002421 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002422 unsigned Amount = CUI->getValue();
2423 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002424 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2425 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002426 BuildMI(*MBB, IP, Opc[3], 3,
2427 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2428 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002429 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002430 BuildMI(*MBB, IP, Opc[3], 3,
2431 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2432 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002433 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002434 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002435 Amount -= 32;
2436 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002437 if (Amount != 0) {
2438 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2439 DestReg + 1).addReg(SrcReg).addImm(Amount);
2440 } else {
2441 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2442 }
2443 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002444 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002445 if (Amount != 0) {
2446 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2447 DestReg).addReg(SrcReg+1).addImm(Amount);
2448 } else {
2449 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2450 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002451 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002452 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002453 }
2454 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002455 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2456
2457 if (!isLeftShift && isSigned) {
2458 // If this is a SHR of a Long, then we need to do funny sign extension
2459 // stuff. TmpReg gets the value to use as the high-part if we are
2460 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002461 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002462 } else {
2463 // Other shifts use a fixed zero value if the shift is more than 32
2464 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002465 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002466 }
2467
2468 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002469 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002470 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002471
2472 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2473 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2474 if (isLeftShift) {
2475 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002476 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002477 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002478 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002479 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002480
2481 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002482 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002483
2484 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002485 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002486 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2487 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002488 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002489 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002490 } else {
2491 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002492 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002493 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002494 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002495 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002496 .addReg(SrcReg+1);
2497
2498 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002499 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002500
2501 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002502 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002503 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2504
2505 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002506 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002507 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2508 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002509 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002510 return;
2511 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002512
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002513 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002514 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2515 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002516
Chris Lattner3e130a22003-01-13 00:32:26 +00002517 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002518 BuildMI(*MBB, IP, Opc[Class], 2,
2519 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002520 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002521 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002522 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002523
Chris Lattner3e130a22003-01-13 00:32:26 +00002524 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002525 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002526 }
2527}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002528
Chris Lattner3e130a22003-01-13 00:32:26 +00002529
Chris Lattner721d2d42004-03-08 01:18:36 +00002530void ISel::getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
2531 unsigned &IndexReg, unsigned &Disp) {
2532 BaseReg = 0; Scale = 1; IndexReg = 0; Disp = 0;
2533 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
2534 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
2535 BaseReg, Scale, IndexReg, Disp))
2536 return;
2537 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
2538 if (CE->getOpcode() == Instruction::GetElementPtr)
2539 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
2540 BaseReg, Scale, IndexReg, Disp))
2541 return;
2542 }
2543
2544 // If it's not foldable, reset addr mode.
2545 BaseReg = getReg(Addr);
2546 Scale = 1; IndexReg = 0; Disp = 0;
2547}
2548
2549
Chris Lattner6fc3c522002-11-17 21:11:55 +00002550/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002551/// instruction. The load and store instructions are the only place where we
2552/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002553///
2554void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002555 // Check to see if this load instruction is going to be folded into a binary
2556 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2557 // pattern matching instruction selector be nice?
2558 if (I.hasOneUse() && getClassB(I.getType()) < cFP) {
2559 Instruction *User = cast<Instruction>(I.use_back());
2560 switch (User->getOpcode()) {
2561 default: User = 0; break;
2562 case Instruction::Add:
2563 case Instruction::Sub:
2564 case Instruction::And:
2565 case Instruction::Or:
2566 case Instruction::Xor:
2567 break;
2568 }
2569
2570 if (User) {
2571 // Okay, we found a user. If the load is the first operand and there is
2572 // no second operand load, reverse the operand ordering. Note that this
2573 // can fail for a subtract (ie, no change will be made).
2574 if (!isa<LoadInst>(User->getOperand(1)))
2575 cast<BinaryOperator>(User)->swapOperands();
2576
2577 // Okay, now that everything is set up, if this load is used by the second
2578 // operand, and if there are no instructions that invalidate the load
2579 // before the binary operator, eliminate the load.
2580 if (User->getOperand(1) == &I &&
2581 isSafeToFoldLoadIntoInstruction(I, *User))
2582 return; // Eliminate the load!
2583 }
2584 }
2585
Chris Lattner94af4142002-12-25 05:13:53 +00002586 unsigned DestReg = getReg(I);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002587 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
Chris Lattner721d2d42004-03-08 01:18:36 +00002588 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
Chris Lattnere8f0d922002-12-24 00:03:11 +00002589
Brian Gaekebfedb912003-07-17 21:30:06 +00002590 unsigned Class = getClassB(I.getType());
Chris Lattner6ac1d712003-10-20 04:48:06 +00002591 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002592 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002593 BaseReg, Scale, IndexReg, Disp);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002594 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002595 BaseReg, Scale, IndexReg, Disp+4);
Chris Lattner94af4142002-12-25 05:13:53 +00002596 return;
2597 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002598
Chris Lattner6ac1d712003-10-20 04:48:06 +00002599 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002600 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m
Chris Lattner3e130a22003-01-13 00:32:26 +00002601 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00002602 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002603 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002604 addFullAddress(BuildMI(BB, Opcode, 4, DestReg),
2605 BaseReg, Scale, IndexReg, Disp);
Chris Lattner3e130a22003-01-13 00:32:26 +00002606}
2607
Chris Lattner6fc3c522002-11-17 21:11:55 +00002608/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
2609/// instruction.
2610///
2611void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002612 unsigned BaseReg, Scale, IndexReg, Disp;
2613 getAddressingMode(I.getOperand(1), BaseReg, Scale, IndexReg, Disp);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002614
Chris Lattner6c09db22003-10-20 04:11:23 +00002615 const Type *ValTy = I.getOperand(0)->getType();
2616 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00002617
Chris Lattner5a830962004-02-25 02:56:58 +00002618 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
2619 uint64_t Val = CI->getRawValue();
2620 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002621 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002622 BaseReg, Scale, IndexReg, Disp).addImm(Val & ~0U);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002623 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002624 BaseReg, Scale, IndexReg, Disp+4).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00002625 } else {
2626 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002627 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00002628 };
2629 unsigned Opcode = Opcodes[Class];
Chris Lattnerb6bac512004-02-25 06:13:04 +00002630 addFullAddress(BuildMI(BB, Opcode, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002631 BaseReg, Scale, IndexReg, Disp).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00002632 }
2633 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002634 addFullAddress(BuildMI(BB, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002635 BaseReg, Scale, IndexReg, Disp).addImm(CB->getValue());
Chris Lattner5a830962004-02-25 02:56:58 +00002636 } else {
2637 if (Class == cLong) {
2638 unsigned ValReg = getReg(I.getOperand(0));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002639 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002640 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002641 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002642 BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1);
Chris Lattner5a830962004-02-25 02:56:58 +00002643 } else {
2644 unsigned ValReg = getReg(I.getOperand(0));
2645 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002646 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
Chris Lattner5a830962004-02-25 02:56:58 +00002647 };
2648 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002649 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002650 addFullAddress(BuildMI(BB, Opcode, 1+4),
2651 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Chris Lattner5a830962004-02-25 02:56:58 +00002652 }
Chris Lattner94af4142002-12-25 05:13:53 +00002653 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002654}
2655
2656
Misha Brukman538607f2004-03-01 23:53:11 +00002657/// visitCastInst - Here we have various kinds of copying with or without sign
2658/// extension going on.
2659///
Chris Lattner3e130a22003-01-13 00:32:26 +00002660void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00002661 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00002662
2663 // Noop casts are not even emitted.
2664 if (getClassB(CI.getType()) == getClassB(Op->getType()))
2665 return;
2666
Chris Lattnerf5854472003-06-21 16:01:24 +00002667 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2668 // of the case are GEP instructions, then the cast does not need to be
2669 // generated explicitly, it will be folded into the GEP.
2670 if (CI.getType() == Type::LongTy &&
2671 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
2672 bool AllUsesAreGEPs = true;
2673 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2674 if (!isa<GetElementPtrInst>(*I)) {
2675 AllUsesAreGEPs = false;
2676 break;
2677 }
2678
2679 // No need to codegen this cast if all users are getelementptr instrs...
2680 if (AllUsesAreGEPs) return;
2681 }
2682
Chris Lattner548f61d2003-04-23 17:22:12 +00002683 unsigned DestReg = getReg(CI);
2684 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00002685 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00002686}
2687
Misha Brukman538607f2004-03-01 23:53:11 +00002688/// emitCastOperation - Common code shared between visitCastInst and constant
2689/// expression cast support.
2690///
Chris Lattner548f61d2003-04-23 17:22:12 +00002691void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002692 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00002693 Value *Src, const Type *DestTy,
2694 unsigned DestReg) {
Chris Lattner3907d112003-04-23 17:57:48 +00002695 unsigned SrcReg = getReg(Src, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002696 const Type *SrcTy = Src->getType();
2697 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002698 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00002699
Chris Lattner3e130a22003-01-13 00:32:26 +00002700 // Implement casts to bool by using compare on the operand followed by set if
2701 // not zero on the result.
2702 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00002703 switch (SrcClass) {
2704 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002705 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002706 break;
2707 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002708 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002709 break;
2710 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002711 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002712 break;
2713 case cLong: {
2714 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002715 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00002716 break;
2717 }
2718 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00002719 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002720 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00002721 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00002722 break;
Chris Lattner20772542003-06-01 03:38:24 +00002723 }
2724
2725 // If the zero flag is not set, then the value is true, set the byte to
2726 // true.
Chris Lattneree352852004-02-29 07:22:16 +00002727 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00002728 return;
2729 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002730
2731 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002732 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00002733 };
2734
2735 // Implement casts between values of the same type class (as determined by
2736 // getClass) by using a register-to-register move.
2737 if (SrcClass == DestClass) {
2738 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00002739 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002740 } else if (SrcClass == cFP) {
2741 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002742 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00002743 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002744 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002745 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2746 "Unknown cFP member!");
2747 // Truncate from double to float by storing to memory as short, then
2748 // reading it back.
2749 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00002750 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002751 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
2752 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002753 }
2754 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002755 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
2756 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002757 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00002758 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002759 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00002760 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002761 return;
2762 }
2763
2764 // Handle cast of SMALLER int to LARGER int using a move with sign extension
2765 // or zero extension, depending on whether the source type was signed.
2766 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2767 SrcClass < DestClass) {
2768 bool isLong = DestClass == cLong;
2769 if (isLong) DestClass = cInt;
2770
2771 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002772 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
2773 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00002774 };
2775
2776 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattneree352852004-02-29 07:22:16 +00002777 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00002778 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002779
2780 if (isLong) { // Handle upper 32 bits as appropriate...
2781 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002782 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00002783 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002784 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00002785 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002786 return;
2787 }
2788
2789 // Special case long -> int ...
2790 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002791 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002792 return;
2793 }
2794
2795 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
2796 // move out of AX or AL.
2797 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2798 && SrcClass > DestClass) {
2799 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00002800 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
2801 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00002802 return;
2803 }
2804
2805 // Handle casts from integer to floating point now...
2806 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002807 // Promote the integer to a type supported by FLD. We do this because there
2808 // are no unsigned FLD instructions, so we must promote an unsigned value to
2809 // a larger signed value, then use FLD on the larger value.
2810 //
2811 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00002812 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002813 unsigned RealDestReg = DestReg;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002814 switch (SrcTy->getPrimitiveID()) {
2815 case Type::BoolTyID:
2816 case Type::SByteTyID:
2817 // We don't have the facilities for directly loading byte sized data from
2818 // memory (even signed). Promote it to 16 bits.
2819 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002820 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002821 break;
2822 case Type::UByteTyID:
2823 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002824 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002825 break;
2826 case Type::UShortTyID:
2827 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002828 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002829 break;
2830 case Type::UIntTyID: {
2831 // Make a 64 bit temporary... and zero out the top of it...
2832 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002833 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
2834 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002835 SrcTy = Type::LongTy;
2836 SrcClass = cLong;
2837 SrcReg = TmpReg;
2838 break;
2839 }
2840 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002841 // Don't fild into the read destination.
2842 DestReg = makeAnotherReg(Type::DoubleTy);
2843 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002844 default: // No promotion needed...
2845 break;
2846 }
2847
2848 if (PromoteType) {
2849 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00002850 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002851 SrcTy = PromoteType;
2852 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00002853 SrcReg = TmpReg;
2854 }
2855
2856 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00002857 int FrameIdx =
2858 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00002859
2860 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002861 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002862 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002863 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002864 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002865 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002866 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00002867 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
2868 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002869 }
2870
2871 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002872 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00002873 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002874
2875 // We need special handling for unsigned 64-bit integer sources. If the
2876 // input number has the "sign bit" set, then we loaded it incorrectly as a
2877 // negative 64-bit number. In this case, add an offset value.
2878 if (SrcTy == Type::ULongTy) {
2879 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002880 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002881
Chris Lattnerb6bac512004-02-25 06:13:04 +00002882 // If the sign bit is set, get a pointer to an offset, otherwise get a
2883 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002884 MachineConstantPool *CP = F->getConstantPool();
2885 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002886 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002887 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002888 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002889 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002890 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
2891
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002892 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002893 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002894 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002895 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002896
2897 // Load the constant for an add. FIXME: this could make an 'fadd' that
2898 // reads directly from memory, but we don't support these yet.
2899 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002900 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002901
Chris Lattneree352852004-02-29 07:22:16 +00002902 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
2903 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002904 }
2905
Chris Lattner3e130a22003-01-13 00:32:26 +00002906 return;
2907 }
2908
2909 // Handle casts from floating point to integer now...
2910 if (SrcClass == cFP) {
2911 // Change the floating point control register to use "round towards zero"
2912 // mode when truncating to an integer value.
2913 //
2914 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002915 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002916
2917 // Load the old value of the high byte of the control word...
2918 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002919 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00002920 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002921
2922 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002923 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002924 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00002925
2926 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002927 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002928
2929 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002930 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002931 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00002932
2933 // We don't have the facilities for directly storing byte sized data to
2934 // memory. Promote it to 16 bits. We also must promote unsigned values to
2935 // larger classes because we only have signed FP stores.
2936 unsigned StoreClass = DestClass;
2937 const Type *StoreTy = DestTy;
2938 if (StoreClass == cByte || DestTy->isUnsigned())
2939 switch (StoreClass) {
2940 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
2941 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
2942 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00002943 // The following treatment of cLong may not be perfectly right,
2944 // but it survives chains of casts of the form
2945 // double->ulong->double.
2946 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00002947 default: assert(0 && "Unknown store class!");
2948 }
2949
2950 // Spill the integer to memory and reload it from there...
2951 int FrameIdx =
2952 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
2953
2954 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002955 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00002956 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
2957 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002958
2959 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002960 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
2961 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00002962 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00002963 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002964 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00002965 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002966 }
2967
2968 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002969 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002970 return;
2971 }
2972
Brian Gaeked474e9c2002-12-06 10:49:33 +00002973 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00002974 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002975 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00002976}
Brian Gaekea1719c92002-10-31 23:03:59 +00002977
Chris Lattner73815062003-10-18 05:56:40 +00002978/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00002979///
Chris Lattner73815062003-10-18 05:56:40 +00002980void ISel::visitVANextInst(VANextInst &I) {
2981 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00002982 unsigned DestReg = getReg(I);
2983
Chris Lattnereca195e2003-05-08 19:44:13 +00002984 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00002985 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00002986 default:
2987 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00002988 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00002989 return;
2990 case Type::PointerTyID:
2991 case Type::UIntTyID:
2992 case Type::IntTyID:
2993 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00002994 break;
2995 case Type::ULongTyID:
2996 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00002997 case Type::DoubleTyID:
2998 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00002999 break;
3000 }
3001
3002 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003003 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003004}
Chris Lattnereca195e2003-05-08 19:44:13 +00003005
Chris Lattner73815062003-10-18 05:56:40 +00003006void ISel::visitVAArgInst(VAArgInst &I) {
3007 unsigned VAList = getReg(I.getOperand(0));
3008 unsigned DestReg = getReg(I);
3009
3010 switch (I.getType()->getPrimitiveID()) {
3011 default:
3012 std::cerr << I;
3013 assert(0 && "Error: bad type for va_next instruction!");
3014 return;
3015 case Type::PointerTyID:
3016 case Type::UIntTyID:
3017 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003018 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003019 break;
3020 case Type::ULongTyID:
3021 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003022 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3023 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003024 break;
3025 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003026 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003027 break;
3028 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003029}
3030
Misha Brukman538607f2004-03-01 23:53:11 +00003031/// visitGetElementPtrInst - instruction-select GEP instructions
3032///
Chris Lattner3e130a22003-01-13 00:32:26 +00003033void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003034 // If this GEP instruction will be folded into all of its users, we don't need
3035 // to explicitly calculate it!
3036 unsigned A, B, C, D;
3037 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) {
3038 // Check all of the users of the instruction to see if they are loads and
3039 // stores.
3040 bool AllWillFold = true;
3041 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3042 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3043 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3044 cast<Instruction>(*UI)->getOperand(0) == &I) {
3045 AllWillFold = false;
3046 break;
3047 }
3048
3049 // If the instruction is foldable, and will be folded into all users, don't
3050 // emit it!
3051 if (AllWillFold) return;
3052 }
3053
Chris Lattner3e130a22003-01-13 00:32:26 +00003054 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003055 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003056 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003057}
3058
Chris Lattner985fe3d2004-02-25 03:45:50 +00003059/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3060/// GEPTypes (the derived types being stepped through at each level). On return
3061/// from this function, if some indexes of the instruction are representable as
3062/// an X86 lea instruction, the machine operands are put into the Ops
3063/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3064/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3065/// addressing mode that only partially consumes the input, the BaseReg input of
3066/// the addressing mode must be left free.
3067///
3068/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3069///
Chris Lattnerb6bac512004-02-25 06:13:04 +00003070void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
3071 std::vector<Value*> &GEPOps,
3072 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
3073 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
3074 const TargetData &TD = TM.getTargetData();
3075
Chris Lattner985fe3d2004-02-25 03:45:50 +00003076 // Clear out the state we are working with...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003077 BaseReg = 0; // No base register
3078 Scale = 1; // Unit scale
3079 IndexReg = 0; // No index register
3080 Disp = 0; // No displacement
3081
Chris Lattner985fe3d2004-02-25 03:45:50 +00003082 // While there are GEP indexes that can be folded into the current address,
3083 // keep processing them.
3084 while (!GEPTypes.empty()) {
3085 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3086 // It's a struct access. CUI is the index into the structure,
3087 // which names the field. This index must have unsigned type.
3088 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3089
3090 // Use the TargetData structure to pick out what the layout of the
3091 // structure is in memory. Since the structure index must be constant, we
3092 // can get its value and use it to find the right byte offset from the
3093 // StructLayout class's list of structure member offsets.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003094 Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003095 GEPOps.pop_back(); // Consume a GEP operand
3096 GEPTypes.pop_back();
3097 } else {
3098 // It's an array or pointer access: [ArraySize x ElementType].
3099 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3100 Value *idx = GEPOps.back();
3101
3102 // idx is the index into the array. Unlike with structure
3103 // indices, we may not know its actual value at code-generation
3104 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003105
3106 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003107 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003108 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003109 Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003110 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
3111 Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003112 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003113 // If the index reg is already taken, we can't handle this index.
3114 if (IndexReg) return;
3115
3116 // If this is a size that we can handle, then add the index as
3117 switch (TypeSize) {
3118 case 1: case 2: case 4: case 8:
3119 // These are all acceptable scales on X86.
3120 Scale = TypeSize;
3121 break;
3122 default:
3123 // Otherwise, we can't handle this scale
3124 return;
3125 }
3126
3127 if (CastInst *CI = dyn_cast<CastInst>(idx))
3128 if (CI->getOperand(0)->getType() == Type::IntTy ||
3129 CI->getOperand(0)->getType() == Type::UIntTy)
3130 idx = CI->getOperand(0);
3131
3132 IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003133 }
3134
3135 GEPOps.pop_back(); // Consume a GEP operand
3136 GEPTypes.pop_back();
3137 }
3138 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003139
3140 // GEPTypes is empty, which means we have a single operand left. See if we
3141 // can set it as the base register.
3142 //
3143 // FIXME: When addressing modes are more powerful/correct, we could load
3144 // global addresses directly as 32-bit immediates.
3145 assert(BaseReg == 0);
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003146 BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003147 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003148}
3149
3150
Chris Lattnerb6bac512004-02-25 06:13:04 +00003151/// isGEPFoldable - Return true if the specified GEP can be completely
3152/// folded into the addressing mode of a load/store or lea instruction.
3153bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
3154 Value *Src, User::op_iterator IdxBegin,
3155 User::op_iterator IdxEnd, unsigned &BaseReg,
3156 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003157 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3158 Src = CPR->getValue();
3159
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003160 std::vector<Value*> GEPOps;
3161 GEPOps.resize(IdxEnd-IdxBegin+1);
3162 GEPOps[0] = Src;
3163 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3164
3165 std::vector<const Type*> GEPTypes;
3166 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3167 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
3168
Chris Lattnerb6bac512004-02-25 06:13:04 +00003169 MachineBasicBlock::iterator IP;
3170 if (MBB) IP = MBB->end();
3171 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
3172
3173 // We can fold it away iff the getGEPIndex call eliminated all operands.
3174 return GEPOps.empty();
3175}
3176
3177void ISel::emitGEPOperation(MachineBasicBlock *MBB,
3178 MachineBasicBlock::iterator IP,
3179 Value *Src, User::op_iterator IdxBegin,
3180 User::op_iterator IdxEnd, unsigned TargetReg) {
3181 const TargetData &TD = TM.getTargetData();
3182 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3183 Src = CPR->getValue();
3184
3185 std::vector<Value*> GEPOps;
3186 GEPOps.resize(IdxEnd-IdxBegin+1);
3187 GEPOps[0] = Src;
3188 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3189
3190 std::vector<const Type*> GEPTypes;
3191 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3192 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003193
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003194 // Keep emitting instructions until we consume the entire GEP instruction.
3195 while (!GEPOps.empty()) {
3196 unsigned OldSize = GEPOps.size();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003197 unsigned BaseReg, Scale, IndexReg, Disp;
3198 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003199
Chris Lattner985fe3d2004-02-25 03:45:50 +00003200 if (GEPOps.size() != OldSize) {
3201 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003202 unsigned NextTarget = 0;
3203 if (!GEPOps.empty()) {
3204 assert(BaseReg == 0 &&
3205 "getGEPIndex should have left the base register open for chaining!");
3206 NextTarget = BaseReg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003207 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003208
3209 if (IndexReg == 0 && Disp == 0)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003210 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003211 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003212 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003213 BaseReg, Scale, IndexReg, Disp);
3214 --IP;
3215 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003216 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003217 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3218 // all operands are consumed but the base pointer. If so, just load it
3219 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003220 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003221 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003222 } else {
3223 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003224 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003225 }
3226 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003227
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003228 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003229 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003230 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3231 Value *idx = GEPOps.back();
3232 GEPOps.pop_back(); // Consume a GEP operand
3233 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003234
Chris Lattner28977af2004-04-05 01:30:19 +00003235 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003236 // operand on X86. Handle this case directly now...
3237 if (CastInst *CI = dyn_cast<CastInst>(idx))
3238 if (CI->getOperand(0)->getType() == Type::IntTy ||
3239 CI->getOperand(0)->getType() == Type::UIntTy)
3240 idx = CI->getOperand(0);
3241
Chris Lattner3e130a22003-01-13 00:32:26 +00003242 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003243 // must find the size of the pointed-to type (Not coincidentally, the next
3244 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003245 const Type *ElTy = SqTy->getElementType();
3246 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003247
3248 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003249 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003250 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003251 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003252 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003253 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003254 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003255 --IP; // Insert the next instruction before this one.
3256 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003257 }
3258 } else if (elementSize == 1) {
3259 // If the element size is 1, we don't have to multiply, just add
3260 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003261 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003262 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003263 --IP; // Insert the next instruction before this one.
3264 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003265 } else {
3266 unsigned idxReg = getReg(idx, MBB, IP);
3267 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003268
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003269 // Make sure we can back the iterator up to point to the first
3270 // instruction emitted.
3271 MachineBasicBlock::iterator BeforeIt = IP;
3272 if (IP == MBB->begin())
3273 BeforeIt = MBB->end();
3274 else
3275 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003276 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3277
Chris Lattner8a307e82002-12-16 19:32:50 +00003278 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003279 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003280 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003281 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003282
3283 // Step to the first instruction of the multiply.
3284 if (BeforeIt == MBB->end())
3285 IP = MBB->begin();
3286 else
3287 IP = ++BeforeIt;
3288
3289 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003290 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003291 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003292 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003293}
3294
3295
Chris Lattner065faeb2002-12-28 20:24:02 +00003296/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3297/// frame manager, otherwise do it the hard way.
3298///
3299void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00003300 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003301 const Type *Ty = I.getAllocatedType();
3302 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3303
3304 // If this is a fixed size alloca in the entry block for the function,
3305 // statically stack allocate the space.
3306 //
3307 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
3308 if (I.getParent() == I.getParent()->getParent()->begin()) {
3309 TySize *= CUI->getValue(); // Get total allocated size...
3310 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
3311
3312 // Create a new stack object using the frame manager...
3313 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003314 addFrameReference(BuildMI(BB, X86::LEA32r, 5, getReg(I)), FrameIdx);
Chris Lattner065faeb2002-12-28 20:24:02 +00003315 return;
3316 }
3317 }
3318
3319 // Create a register to hold the temporary result of multiplying the type size
3320 // constant by the variable amount.
3321 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3322 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003323
3324 // TotalSizeReg = mul <numelements>, <TypeSize>
3325 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003326 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003327
3328 // AddedSize = add <TotalSizeReg>, 15
3329 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003330 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003331
3332 // AlignedSize = and <AddedSize>, ~15
3333 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003334 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003335
Brian Gaekee48ec012002-12-13 06:46:31 +00003336 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003337 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003338
Brian Gaekee48ec012002-12-13 06:46:31 +00003339 // Put a pointer to the space into the result register, by copying
3340 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003341 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003342
Misha Brukman48196b32003-05-03 02:18:17 +00003343 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003344 // object.
3345 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003346}
Chris Lattner3e130a22003-01-13 00:32:26 +00003347
3348/// visitMallocInst - Malloc instructions are code generated into direct calls
3349/// to the library malloc.
3350///
3351void ISel::visitMallocInst(MallocInst &I) {
3352 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3353 unsigned Arg;
3354
3355 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3356 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3357 } else {
3358 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003359 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003360 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003361 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003362 }
3363
3364 std::vector<ValueRecord> Args;
3365 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3366 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003367 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003368 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3369}
3370
3371
3372/// visitFreeInst - Free instructions are code gen'd to call the free libc
3373/// function.
3374///
3375void ISel::visitFreeInst(FreeInst &I) {
3376 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003377 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003378 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003379 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003380 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3381}
3382
Chris Lattnerd281de22003-07-26 23:49:58 +00003383/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003384/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003385/// generated code sucks but the implementation is nice and simple.
3386///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003387FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
3388 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003389}