blob: bb0fb8262207870ced5d70e101449dd0e0d83c51 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86ISelSimple.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"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000021#include "llvm/Pass.h"
Chris Lattner30483732004-06-20 07:49:54 +000022#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000023#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"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner986618e2004-02-22 19:47:26 +000034namespace {
35 Statistic<>
36 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
Chris Lattner427aeb42004-04-11 19:21:59 +000037
38 /// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
39 /// Representation.
40 ///
41 enum TypeClass {
42 cByte, cShort, cInt, cFP, cLong
43 };
44}
45
46/// getClass - Turn a primitive type into a "class" number which is based on the
47/// size of the type, and whether or not it is floating point.
48///
49static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000050 switch (Ty->getTypeID()) {
Chris Lattner427aeb42004-04-11 19:21:59 +000051 case Type::SByteTyID:
52 case Type::UByteTyID: return cByte; // Byte operands are class #0
53 case Type::ShortTyID:
54 case Type::UShortTyID: return cShort; // Short operands are class #1
55 case Type::IntTyID:
56 case Type::UIntTyID:
57 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
58
59 case Type::FloatTyID:
60 case Type::DoubleTyID: return cFP; // Floating Point is #3
61
62 case Type::LongTyID:
63 case Type::ULongTyID: return cLong; // Longs are class #4
64 default:
65 assert(0 && "Invalid type to getClass!");
66 return cByte; // not reached
67 }
68}
69
70// getClassB - Just like getClass, but treat boolean values as bytes.
71static inline TypeClass getClassB(const Type *Ty) {
72 if (Ty == Type::BoolTy) return cByte;
73 return getClass(Ty);
Chris Lattner986618e2004-02-22 19:47:26 +000074}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000075
Chris Lattner72614082002-10-25 22:55:53 +000076namespace {
Misha Brukmaneae1bf12004-09-21 18:21:21 +000077 struct X86ISel : public FunctionPass, InstVisitor<X86ISel> {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000079 MachineFunction *F; // The function we are compiling into
80 MachineBasicBlock *BB; // The current MBB we are compiling
81 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000082 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000083
Chris Lattner72614082002-10-25 22:55:53 +000084 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
85
Chris Lattner333b2fa2002-12-13 10:09:43 +000086 // MBBMap - Mapping between LLVM BB -> Machine BB
87 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
88
Chris Lattnercb2fd552004-05-13 07:40:27 +000089 // AllocaMap - Mapping from fixed sized alloca instructions to the
90 // FrameIndex for the alloca.
91 std::map<AllocaInst*, unsigned> AllocaMap;
92
Misha Brukmaneae1bf12004-09-21 18:21:21 +000093 X86ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000094
95 /// runOnFunction - Top level implementation of instruction selection for
96 /// the entire function.
97 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000098 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000099 // First pass over the function, lower any unknown intrinsic functions
100 // with the IntrinsicLowering class.
101 LowerUnknownIntrinsicFunctionCalls(Fn);
102
Chris Lattner36b36032002-10-29 23:40:58 +0000103 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104
Chris Lattner065faeb2002-12-28 20:24:02 +0000105 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +0000106 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
107 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
108
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000109 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +0000110
Chris Lattner0e5b79c2004-02-15 01:04:03 +0000111 // Set up a frame object for the return address. This is used by the
112 // llvm.returnaddress & llvm.frameaddress intrinisics.
113 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
114
Chris Lattnerdbd73722003-05-06 21:32:22 +0000115 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000116 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000117
Chris Lattner333b2fa2002-12-13 10:09:43 +0000118 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000120
121 // Select the PHI nodes
122 SelectPHINodes();
123
Chris Lattner986618e2004-02-22 19:47:26 +0000124 // Insert the FP_REG_KILL instructions into blocks that need them.
125 InsertFPRegKills();
126
Chris Lattner72614082002-10-25 22:55:53 +0000127 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000128 MBBMap.clear();
Chris Lattnercb2fd552004-05-13 07:40:27 +0000129 AllocaMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000130 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000131 // We always build a machine code representation for the function
132 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000133 }
134
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000135 virtual const char *getPassName() const {
136 return "X86 Simple Instruction Selection";
137 }
138
Chris Lattner72614082002-10-25 22:55:53 +0000139 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000140 /// block. This simply creates a new MachineBasicBlock to emit code into
141 /// and adds it to the current MachineFunction. Subsequent visit* for
142 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000143 ///
144 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000145 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000146 }
147
Chris Lattner44827152003-12-28 09:47:19 +0000148 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
149 /// function, lowering any calls to unknown intrinsic functions into the
150 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000151 ///
Chris Lattner44827152003-12-28 09:47:19 +0000152 void LowerUnknownIntrinsicFunctionCalls(Function &F);
153
Chris Lattner065faeb2002-12-28 20:24:02 +0000154 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
155 /// from the stack into virtual registers.
156 ///
157 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000158
159 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
160 /// because we have to generate our sources into the source basic blocks,
161 /// not the current one.
162 ///
163 void SelectPHINodes();
164
Chris Lattner986618e2004-02-22 19:47:26 +0000165 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
166 /// that need them. This only occurs due to the floating point stackifier
167 /// not being aggressive enough to handle arbitrary global stackification.
168 ///
169 void InsertFPRegKills();
170
Chris Lattner72614082002-10-25 22:55:53 +0000171 // Visitation methods for various instructions. These methods simply emit
172 // fixed X86 code for each instruction.
173 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000174
175 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000176 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000177 void visitBranchInst(BranchInst &BI);
Chris Lattner30483b02004-10-16 18:13:05 +0000178 void visitUnreachableInst(UnreachableInst &UI) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000179
180 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000181 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000182 unsigned Reg;
183 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000184 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
185 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000186 };
187 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000188 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000189 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000190 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000191
192 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000193 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000194 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
195 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000196 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000197
Chris Lattnerf01729e2002-11-02 20:54:46 +0000198 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
199 void visitRem(BinaryOperator &B) { visitDivRem(B); }
200 void visitDivRem(BinaryOperator &B);
201
Chris Lattnere2954c82002-11-02 20:04:26 +0000202 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000203 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
204 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
205 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000206
Chris Lattner6d40c192003-01-16 16:43:00 +0000207 // Comparison operators...
208 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000209 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
210 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000211 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000212 void visitSelectInst(SelectInst &SI);
213
Chris Lattnerb2acc512003-10-19 21:09:10 +0000214
Chris Lattner6fc3c522002-11-17 21:11:55 +0000215 // Memory Instructions
216 void visitLoadInst(LoadInst &I);
217 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000218 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000219 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000220 void visitMallocInst(MallocInst &I);
221 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000222
Chris Lattnere2954c82002-11-02 20:04:26 +0000223 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000224 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000225 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000226 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000227 void visitVANextInst(VANextInst &I);
228 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000229
230 void visitInstruction(Instruction &I) {
231 std::cerr << "Cannot instruction select: " << I;
232 abort();
233 }
234
Brian Gaeke95780cc2002-12-13 07:56:18 +0000235 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000236 ///
237 void promote32(unsigned targetReg, const ValueRecord &VR);
238
Chris Lattner721d2d42004-03-08 01:18:36 +0000239 /// getAddressingMode - Get the addressing mode to use to address the
240 /// specified value. The returned value should be used with addFullAddress.
Reid Spencerfc989e12004-08-30 00:13:26 +0000241 void getAddressingMode(Value *Addr, X86AddressMode &AM);
Chris Lattner721d2d42004-03-08 01:18:36 +0000242
243
244 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
245 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000246 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
247 std::vector<Value*> &GEPOps,
Reid Spencerfc989e12004-08-30 00:13:26 +0000248 std::vector<const Type*> &GEPTypes,
249 X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000250
251 /// isGEPFoldable - Return true if the specified GEP can be completely
252 /// folded into the addressing mode of a load/store or lea instruction.
253 bool isGEPFoldable(MachineBasicBlock *MBB,
254 Value *Src, User::op_iterator IdxBegin,
Reid Spencerfc989e12004-08-30 00:13:26 +0000255 User::op_iterator IdxEnd, X86AddressMode &AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +0000256
Chris Lattner3e130a22003-01-13 00:32:26 +0000257 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
258 /// constant expression GEP support.
259 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000260 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000261 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000262 User::op_iterator IdxEnd, unsigned TargetReg);
263
Chris Lattner548f61d2003-04-23 17:22:12 +0000264 /// emitCastOperation - Common code shared between visitCastInst and
265 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000266 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000267 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000268 Value *Src, const Type *DestTy, unsigned TargetReg);
269
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000270 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
271 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000272 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000273 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000274 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000275 Value *Op0, Value *Op1,
276 unsigned OperatorClass, unsigned TargetReg);
277
Chris Lattner6621ed92004-04-11 21:23:56 +0000278 /// emitBinaryFPOperation - This method handles emission of floating point
279 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
280 void emitBinaryFPOperation(MachineBasicBlock *BB,
281 MachineBasicBlock::iterator IP,
282 Value *Op0, Value *Op1,
283 unsigned OperatorClass, unsigned TargetReg);
284
Chris Lattner462fa822004-04-11 20:56:28 +0000285 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
286 Value *Op0, Value *Op1, unsigned TargetReg);
287
288 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
289 unsigned DestReg, const Type *DestTy,
290 unsigned Op0Reg, unsigned Op1Reg);
291 void doMultiplyConst(MachineBasicBlock *MBB,
292 MachineBasicBlock::iterator MBBI,
293 unsigned DestReg, const Type *DestTy,
294 unsigned Op0Reg, unsigned Op1Val);
295
Chris Lattnercadff442003-10-23 17:21:43 +0000296 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000297 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000298 Value *Op0, Value *Op1, bool isDiv,
299 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000300
Chris Lattner58c41fe2003-08-24 19:19:47 +0000301 /// emitSetCCOperation - Common code shared between visitSetCondInst and
302 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000303 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000304 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000305 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000306 Value *Op0, Value *Op1, unsigned Opcode,
307 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000308
309 /// emitShiftOperation - Common code shared between visitShiftInst and
310 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000311 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000312 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000313 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000314 Value *Op, Value *ShiftAmount, bool isLeftShift,
315 const Type *ResultTy, unsigned DestReg);
Chris Lattnerce7cafa2004-11-13 20:48:57 +0000316
317 // Emit code for a 'SHLD DestReg, Op0, Op1, Amt' operation, where Amt is a
318 // constant.
319 void doSHLDConst(MachineBasicBlock *MBB,
320 MachineBasicBlock::iterator MBBI,
321 unsigned DestReg, unsigned Op0Reg, unsigned Op1Reg,
322 unsigned Op1Val);
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000323
Chris Lattner12d96a02004-03-30 21:22:00 +0000324 /// emitSelectOperation - Common code shared between visitSelectInst and the
325 /// constant expression support.
326 void emitSelectOperation(MachineBasicBlock *MBB,
327 MachineBasicBlock::iterator IP,
328 Value *Cond, Value *TrueVal, Value *FalseVal,
329 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000330
Chris Lattnerc5291f52002-10-27 21:16:59 +0000331 /// copyConstantToRegister - Output the instructions required to put the
332 /// specified constant into the specified register.
333 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000334 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000335 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000336 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000337
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000338 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
339 unsigned LHS, unsigned RHS);
340
Chris Lattner3e130a22003-01-13 00:32:26 +0000341 /// makeAnotherReg - This method returns the next register number we haven't
342 /// yet used.
343 ///
344 /// Long values are handled somewhat specially. They are always allocated
345 /// as pairs of 32 bit integer values. The register number returned is the
346 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
347 /// of the long value.
348 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000349 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000350 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
351 "Current target doesn't have X86 reg info??");
352 const X86RegisterInfo *MRI =
353 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000354 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000355 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
356 // Create the lower part
357 F->getSSARegMap()->createVirtualRegister(RC);
358 // Create the upper part.
359 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000360 }
361
Chris Lattnerc0812d82002-12-13 06:56:29 +0000362 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000363 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000364 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000365 }
366
Chris Lattnercb2fd552004-05-13 07:40:27 +0000367 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000368 ///
369 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000370 unsigned getReg(Value *V) {
371 // Just append to the end of the current bb.
372 MachineBasicBlock::iterator It = BB->end();
373 return getReg(V, BB, It);
374 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000375 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000376 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000377
Chris Lattnercb2fd552004-05-13 07:40:27 +0000378 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
379 /// that is to be statically allocated with the initial stack frame
380 /// adjustment.
381 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000382 };
383}
384
Chris Lattnercb2fd552004-05-13 07:40:27 +0000385/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
386/// instruction in the entry block, return it. Otherwise, return a null
387/// pointer.
388static AllocaInst *dyn_castFixedAlloca(Value *V) {
389 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
390 BasicBlock *BB = AI->getParent();
391 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
392 return AI;
393 }
394 return 0;
395}
396
397/// getReg - This method turns an LLVM value into a register number.
398///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000399unsigned X86ISel::getReg(Value *V, MachineBasicBlock *MBB,
400 MachineBasicBlock::iterator IPt) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000401 // If this operand is a constant, emit the code to copy the constant into
402 // the register here...
Chris Lattnercb2fd552004-05-13 07:40:27 +0000403 if (Constant *C = dyn_cast<Constant>(V)) {
404 unsigned Reg = makeAnotherReg(V->getType());
405 copyConstantToRegister(MBB, IPt, C, Reg);
406 return Reg;
Chris Lattnercb2fd552004-05-13 07:40:27 +0000407 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
Chris Lattner8b486a12004-06-29 00:14:38 +0000408 // Do not emit noop casts at all, unless it's a double -> float cast.
409 if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) &&
410 (CI->getType() != Type::FloatTy ||
411 CI->getOperand(0)->getType() != Type::DoubleTy))
Chris Lattnercb2fd552004-05-13 07:40:27 +0000412 return getReg(CI->getOperand(0), MBB, IPt);
413 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
414 // If the alloca address couldn't be folded into the instruction addressing,
415 // emit an explicit LEA as appropriate.
416 unsigned Reg = makeAnotherReg(V->getType());
417 unsigned FI = getFixedSizedAllocaFI(AI);
418 addFrameReference(BuildMI(*MBB, IPt, X86::LEA32r, 4, Reg), FI);
419 return Reg;
420 }
421
422 unsigned &Reg = RegMap[V];
423 if (Reg == 0) {
424 Reg = makeAnotherReg(V->getType());
425 RegMap[V] = Reg;
426 }
427
428 return Reg;
429}
430
431/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
432/// that is to be statically allocated with the initial stack frame
433/// adjustment.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000434unsigned X86ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000435 // Already computed this?
436 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
437 if (I != AllocaMap.end() && I->first == AI) return I->second;
438
439 const Type *Ty = AI->getAllocatedType();
440 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
441 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
442 TySize *= CUI->getValue(); // Get total allocated size...
443 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
444
445 // Create a new stack object using the frame manager...
446 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
447 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
448 return FrameIdx;
449}
450
451
Chris Lattnerc5291f52002-10-27 21:16:59 +0000452/// copyConstantToRegister - Output the instructions required to put the
453/// specified constant into the specified register.
454///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000455void X86ISel::copyConstantToRegister(MachineBasicBlock *MBB,
456 MachineBasicBlock::iterator IP,
457 Constant *C, unsigned R) {
Chris Lattner30483b02004-10-16 18:13:05 +0000458 if (isa<UndefValue>(C)) {
459 switch (getClassB(C->getType())) {
460 case cFP:
461 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
462 BuildMI(*MBB, IP, X86::FLD0, 0, R);
463 return;
464 case cLong:
465 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R+1);
466 // FALL THROUGH
467 default:
468 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, R);
469 return;
470 }
471 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000472 unsigned Class = 0;
473 switch (CE->getOpcode()) {
474 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000475 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000476 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000477 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000478 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000479 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000480 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000481
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000482 case Instruction::Xor: ++Class; // FALL THROUGH
483 case Instruction::Or: ++Class; // FALL THROUGH
484 case Instruction::And: ++Class; // FALL THROUGH
485 case Instruction::Sub: ++Class; // FALL THROUGH
486 case Instruction::Add:
487 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
488 Class, R);
489 return;
490
Chris Lattner462fa822004-04-11 20:56:28 +0000491 case Instruction::Mul:
492 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000493 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000494
Chris Lattnercadff442003-10-23 17:21:43 +0000495 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000496 case Instruction::Rem:
497 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
498 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000499 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000500
Chris Lattner58c41fe2003-08-24 19:19:47 +0000501 case Instruction::SetNE:
502 case Instruction::SetEQ:
503 case Instruction::SetLT:
504 case Instruction::SetGT:
505 case Instruction::SetLE:
506 case Instruction::SetGE:
507 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
508 CE->getOpcode(), R);
509 return;
510
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000511 case Instruction::Shl:
512 case Instruction::Shr:
513 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000514 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
515 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000516
Chris Lattner12d96a02004-03-30 21:22:00 +0000517 case Instruction::Select:
518 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
519 CE->getOperand(2), R);
520 return;
521
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000522 default:
Chris Lattner76e2df22004-07-15 02:14:30 +0000523 std::cerr << "Offending expr: " << *C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000524 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000525 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000526 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000527
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000528 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000529 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000530
531 if (Class == cLong) {
532 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000533 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000534 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
535 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000536 return;
537 }
538
Chris Lattner94af4142002-12-25 05:13:53 +0000539 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000540
541 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000542 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000543 };
544
Chris Lattner6b993cc2002-12-15 08:02:15 +0000545 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000546 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000547 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000548 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000549 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000550 }
Chris Lattner94af4142002-12-25 05:13:53 +0000551 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000552 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000553 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000554 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000555 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000556 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000557 // Otherwise we need to spill the constant to memory...
558 MachineConstantPool *CP = F->getConstantPool();
559 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000560 const Type *Ty = CFP->getType();
561
562 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000563 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000564 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000565 }
566
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000567 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000568 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000569 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Reid Spencer8863f182004-07-18 00:38:32 +0000570 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
571 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000572 } else {
Chris Lattner76e2df22004-07-15 02:14:30 +0000573 std::cerr << "Offending constant: " << *C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000574 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000575 }
576}
577
Chris Lattner065faeb2002-12-28 20:24:02 +0000578/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
579/// the stack into virtual registers.
580///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000581void X86ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
Chris Lattner065faeb2002-12-28 20:24:02 +0000582 // Emit instructions to load the arguments... On entry to a function on the
583 // X86, the stack frame looks like this:
584 //
585 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000586 // [ESP + 4] -- first argument (leftmost lexically)
587 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000588 // ...
589 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000590 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000591 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000592
593 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000594 bool ArgLive = !I->use_empty();
595 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000596 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000597
Chris Lattner065faeb2002-12-28 20:24:02 +0000598 switch (getClassB(I->getType())) {
599 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000600 if (ArgLive) {
601 FI = MFI->CreateFixedObject(1, ArgOffset);
602 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
603 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000604 break;
605 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000606 if (ArgLive) {
607 FI = MFI->CreateFixedObject(2, ArgOffset);
608 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
609 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000610 break;
611 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000612 if (ArgLive) {
613 FI = MFI->CreateFixedObject(4, ArgOffset);
614 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
615 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000616 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000617 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000618 if (ArgLive) {
619 FI = MFI->CreateFixedObject(8, ArgOffset);
620 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
621 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
622 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000623 ArgOffset += 4; // longs require 4 additional bytes
624 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000625 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000626 if (ArgLive) {
627 unsigned Opcode;
628 if (I->getType() == Type::FloatTy) {
629 Opcode = X86::FLD32m;
630 FI = MFI->CreateFixedObject(4, ArgOffset);
631 } else {
632 Opcode = X86::FLD64m;
633 FI = MFI->CreateFixedObject(8, ArgOffset);
634 }
635 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000636 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000637 if (I->getType() == Type::DoubleTy)
638 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000639 break;
640 default:
641 assert(0 && "Unhandled argument type!");
642 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000643 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000644 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000645
646 // If the function takes variable number of arguments, add a frame offset for
647 // the start of the first vararg value... this is used to expand
648 // llvm.va_start.
649 if (Fn.getFunctionType()->isVarArg())
650 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000651}
652
653
Chris Lattner333b2fa2002-12-13 10:09:43 +0000654/// SelectPHINodes - Insert machine code to generate phis. This is tricky
655/// because we have to generate our sources into the source basic blocks, not
656/// the current one.
657///
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000658void X86ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000659 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000660 const Function &LF = *F->getFunction(); // The LLVM function...
661 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
662 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000663 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000664
665 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000666 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000667 for (BasicBlock::const_iterator I = BB->begin(); isa<PHINode>(I); ++I) {
668 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I));
Chris Lattner3e130a22003-01-13 00:32:26 +0000669
Chris Lattner333b2fa2002-12-13 10:09:43 +0000670 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000671 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000672 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
673 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000674
675 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000676 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
677 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
678 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000679
Chris Lattnera6e73f12003-05-12 14:22:21 +0000680 // PHIValues - Map of blocks to incoming virtual registers. We use this
681 // so that we only initialize one incoming value for a particular block,
682 // even if the block has multiple entries in the PHI node.
683 //
684 std::map<MachineBasicBlock*, unsigned> PHIValues;
685
Chris Lattner333b2fa2002-12-13 10:09:43 +0000686 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
687 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000688 unsigned ValReg;
689 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
690 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000691
Chris Lattnera6e73f12003-05-12 14:22:21 +0000692 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
693 // We already inserted an initialization of the register for this
694 // predecessor. Recycle it.
695 ValReg = EntryIt->second;
696
697 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000698 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000699 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000700 Value *Val = PN->getIncomingValue(i);
701
702 // If this is a constant or GlobalValue, we may have to insert code
703 // into the basic block to compute it into a virtual register.
Reid Spencer8863f182004-07-18 00:38:32 +0000704 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val))) {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000705 // Simple constants get emitted at the end of the basic block,
706 // before any terminator instructions. We "know" that the code to
707 // move a constant into a register will never clobber any flags.
708 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000709 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000710 // Because we don't want to clobber any values which might be in
711 // physical registers with the computation of this constant (which
712 // might be arbitrarily complex if it is a constant expression),
713 // just insert the computation at the top of the basic block.
714 MachineBasicBlock::iterator PI = PredMBB->begin();
715
716 // Skip over any PHI nodes though!
717 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
718 ++PI;
719
720 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000721 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000722
723 // Remember that we inserted a value for this PHI for this predecessor
724 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
725 }
726
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000727 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000728 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000729 if (LongPhiMI) {
730 LongPhiMI->addRegOperand(ValReg+1);
731 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
732 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000733 }
Chris Lattner168aa902004-02-29 07:10:16 +0000734
735 // Now that we emitted all of the incoming values for the PHI node, make
736 // sure to reposition the InsertPoint after the PHI that we just added.
737 // This is needed because we might have inserted a constant into this
738 // block, right after the PHI's which is before the old insert point!
739 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
740 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000741 }
742 }
743}
744
Chris Lattner986618e2004-02-22 19:47:26 +0000745/// RequiresFPRegKill - The floating point stackifier pass cannot insert
746/// compensation code on critical edges. As such, it requires that we kill all
747/// FP registers on the exit from any blocks that either ARE critical edges, or
748/// branch to a block that has incoming critical edges.
749///
750/// Note that this kill instruction will eventually be eliminated when
751/// restrictions in the stackifier are relaxed.
752///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000753static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000754#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000755 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000756 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
757 const BasicBlock *Succ = *SI;
758 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
759 ++PI; // Block have at least one predecessory
760 if (PI != PE) { // If it has exactly one, this isn't crit edge
761 // If this block has more than one predecessor, check all of the
762 // predecessors to see if they have multiple successors. If so, then the
763 // block we are analyzing needs an FPRegKill.
764 for (PI = pred_begin(Succ); PI != PE; ++PI) {
765 const BasicBlock *Pred = *PI;
766 succ_const_iterator SI2 = succ_begin(Pred);
767 ++SI2; // There must be at least one successor of this block.
768 if (SI2 != succ_end(Pred))
769 return true; // Yes, we must insert the kill on this edge.
770 }
771 }
772 }
773 // If we got this far, there is no need to insert the kill instruction.
774 return false;
775#else
776 return true;
777#endif
778}
779
780// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
781// need them. This only occurs due to the floating point stackifier not being
782// aggressive enough to handle arbitrary global stackification.
783//
784// Currently we insert an FP_REG_KILL instruction into each block that uses or
785// defines a floating point virtual register.
786//
787// When the global register allocators (like linear scan) finally update live
788// variable analysis, we can keep floating point values in registers across
789// portions of the CFG that do not involve critical edges. This will be a big
790// win, but we are waiting on the global allocators before we can do this.
791//
792// With a bit of work, the floating point stackifier pass can be enhanced to
793// break critical edges as needed (to make a place to put compensation code),
794// but this will require some infrastructure improvements as well.
795//
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000796void X86ISel::InsertFPRegKills() {
Chris Lattner986618e2004-02-22 19:47:26 +0000797 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000798
799 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000800 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000801 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
802 MachineOperand& MO = I->getOperand(i);
803 if (MO.isRegister() && MO.getReg()) {
804 unsigned Reg = MO.getReg();
Chris Lattner223d4c42004-12-03 05:13:15 +0000805 if (MRegisterInfo::isVirtualRegister(Reg)) {
806 unsigned RegSize = RegMap.getRegClass(Reg)->getSize();
807 if (RegSize == 10 || RegSize == 8)
Chris Lattner65cf42d2004-02-23 07:29:45 +0000808 goto UsesFPReg;
Chris Lattner223d4c42004-12-03 05:13:15 +0000809 }
Chris Lattner986618e2004-02-22 19:47:26 +0000810 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000811 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000812 // If we haven't found an FP register use or def in this basic block, check
813 // to see if any of our successors has an FP PHI node, which will cause a
814 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000815 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
816 SE = BB->succ_end(); SI != SE; ++SI) {
817 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000818 for (MachineBasicBlock::iterator I = SBB->begin();
819 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
Chris Lattner39869242004-12-02 17:57:21 +0000820 const TargetRegisterClass *RC =
821 RegMap.getRegClass(I->getOperand(0).getReg());
822 if (RC->getSize() == 10 || RC->getSize() == 8)
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000823 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000824 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000825 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000826 continue;
827 UsesFPReg:
828 // Okay, this block uses an FP register. If the block has successors (ie,
829 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Brian Gaeke1afe7732004-04-28 04:45:55 +0000830 if (BB->succ_size () && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000831 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000832 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000833 }
834 }
835}
836
837
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000838void X86ISel::getAddressingMode(Value *Addr, X86AddressMode &AM) {
Reid Spencerfc989e12004-08-30 00:13:26 +0000839 AM.BaseType = X86AddressMode::RegBase;
840 AM.Base.Reg = 0; AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000841 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
842 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000843 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000844 return;
845 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
846 if (CE->getOpcode() == Instruction::GetElementPtr)
847 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
Reid Spencerfc989e12004-08-30 00:13:26 +0000848 AM))
Chris Lattner9f1b5312004-05-13 15:12:43 +0000849 return;
Reid Spencerfc989e12004-08-30 00:13:26 +0000850 } else if (AllocaInst *AI = dyn_castFixedAlloca(Addr)) {
851 AM.BaseType = X86AddressMode::FrameIndexBase;
852 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
853 return;
Chris Lattner358a9022004-10-15 05:05:29 +0000854 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
855 AM.GV = GV;
856 return;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000857 }
858
859 // If it's not foldable, reset addr mode.
Reid Spencerfc989e12004-08-30 00:13:26 +0000860 AM.BaseType = X86AddressMode::RegBase;
861 AM.Base.Reg = getReg(Addr);
862 AM.Scale = 1; AM.IndexReg = 0; AM.Disp = 0;
Chris Lattner9f1b5312004-05-13 15:12:43 +0000863}
864
Chris Lattner307ecba2004-03-30 22:39:09 +0000865// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
866// it into the conditional branch or select instruction which is the only user
867// of the cc instruction. This is the case if the conditional branch is the
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000868// only user of the setcc. We also don't handle long arguments below, so we
869// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000870//
Chris Lattner307ecba2004-03-30 22:39:09 +0000871static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000872 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000873 if (SCI->hasOneUse()) {
874 Instruction *User = cast<Instruction>(SCI->use_back());
Tanya Lattner9855b842004-12-01 18:27:03 +0000875 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
876 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
877 SCI->getOpcode() == Instruction::SetEQ ||
878 SCI->getOpcode() == Instruction::SetNE) &&
879 (isa<BranchInst>(User) || User->getOperand(0) == V))
Chris Lattner6d40c192003-01-16 16:43:00 +0000880 return SCI;
881 }
882 return 0;
883}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000884
Chris Lattner6d40c192003-01-16 16:43:00 +0000885// Return a fixed numbering for setcc instructions which does not depend on the
886// order of the opcodes.
887//
888static unsigned getSetCCNumber(unsigned Opcode) {
889 switch(Opcode) {
890 default: assert(0 && "Unknown setcc instruction!");
891 case Instruction::SetEQ: return 0;
892 case Instruction::SetNE: return 1;
893 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000894 case Instruction::SetGE: return 3;
895 case Instruction::SetGT: return 4;
896 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000897 }
898}
Chris Lattner06925362002-11-17 21:56:38 +0000899
Chris Lattner6d40c192003-01-16 16:43:00 +0000900// LLVM -> X86 signed X86 unsigned
901// ----- ---------- ------------
902// seteq -> sete sete
903// setne -> setne setne
904// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000905// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000906// setgt -> setg seta
907// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000908// ----
909// sets // Used by comparison with 0 optimization
910// setns
911static const unsigned SetCCOpcodeTab[2][8] = {
912 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
913 0, 0 },
914 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
915 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000916};
917
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000918/// emitUCOMr - In the future when we support processors before the P6, this
919/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000920void X86ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
921 unsigned LHS, unsigned RHS) {
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000922 if (0) { // for processors prior to the P6
923 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
924 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
925 BuildMI(*MBB, IP, X86::SAHF, 1);
926 } else {
927 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
928 }
929}
930
Chris Lattnerb2acc512003-10-19 21:09:10 +0000931// EmitComparison - This function emits a comparison of the two operands,
932// returning the extended setcc code to use.
Misha Brukmaneae1bf12004-09-21 18:21:21 +0000933unsigned X86ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
934 MachineBasicBlock *MBB,
935 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000936 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000937 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000938 unsigned Class = getClassB(CompTy);
Chris Lattner333864d2003-06-05 19:30:30 +0000939
940 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000941 if (isa<ConstantPointerNull>(Op1)) {
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000942 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner260195d2004-05-07 19:55:55 +0000943 if (OpNum < 2) // seteq/setne -> test
944 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
945 else
946 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
947 return OpNum;
948
949 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000950 if (Class == cByte || Class == cShort || Class == cInt) {
951 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000952
Chris Lattner333864d2003-06-05 19:30:30 +0000953 // Mask off any upper bits of the constant, if there are any...
954 Op1v &= (1ULL << (8 << Class)) - 1;
955
Chris Lattnerb2acc512003-10-19 21:09:10 +0000956 // If this is a comparison against zero, emit more efficient code. We
957 // can't handle unsigned comparisons against zero unless they are == or
958 // !=. These should have been strength reduced already anyway.
959 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000960
961 // If this is a comparison against zero and the LHS is an and of a
962 // register with a constant, use the test to do the and.
963 if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
964 if (Op0I->getOpcode() == Instruction::And && Op0->hasOneUse() &&
965 isa<ConstantInt>(Op0I->getOperand(1))) {
966 static const unsigned TESTTab[] = {
967 X86::TEST8ri, X86::TEST16ri, X86::TEST32ri
968 };
969
970 // Emit test X, i
971 unsigned LHS = getReg(Op0I->getOperand(0), MBB, IP);
972 unsigned Imm =
973 cast<ConstantInt>(Op0I->getOperand(1))->getRawValue();
974 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(LHS).addImm(Imm);
975
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000976 if (OpNum == 2) return 6; // Map jl -> js
977 if (OpNum == 3) return 7; // Map jg -> jns
978 return OpNum;
979 }
980
981 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000982 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000983 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000984 };
Chris Lattneree352852004-02-29 07:22:16 +0000985 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000986
987 if (OpNum == 2) return 6; // Map jl -> js
988 if (OpNum == 3) return 7; // Map jg -> jns
989 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000990 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000991
992 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000993 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000994 };
995
Chris Lattnerde95c9e2004-10-17 06:10:40 +0000996 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattneree352852004-02-29 07:22:16 +0000997 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000998 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000999 } else {
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001000 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnere80e6372004-04-06 16:02:27 +00001001 assert(Class == cLong && "Unknown integer class!");
1002 unsigned LowCst = CI->getRawValue();
1003 unsigned HiCst = CI->getRawValue() >> 32;
1004 if (OpNum < 2) { // seteq, setne
1005 unsigned LoTmp = Op0r;
1006 if (LowCst != 0) {
1007 LoTmp = makeAnotherReg(Type::IntTy);
1008 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
1009 }
1010 unsigned HiTmp = Op0r+1;
1011 if (HiCst != 0) {
1012 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +00001013 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +00001014 }
1015 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
1016 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
1017 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +00001018 } else {
Tanya Lattner9855b842004-12-01 18:27:03 +00001019 // Emit a sequence of code which compares the high and low parts once
1020 // each, then uses a conditional move to handle the overflow case. For
1021 // example, a setlt for long would generate code like this:
1022 //
1023 // AL = lo(op1) < lo(op2) // Always unsigned comparison
1024 // BL = hi(op1) < hi(op2) // Signedness depends on operands
1025 // dest = hi(op1) == hi(op2) ? BL : AL;
1026 //
1027
1028 // FIXME: This would be much better if we had hierarchical register
1029 // classes! Until then, hardcode registers so that we can deal with
1030 // their aliases (because we don't have conditional byte moves).
1031 //
1032 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
1033 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
1034 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
1035 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
1036 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1037 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
1038 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
1039 .addReg(X86::AX);
1040 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1041 // register at this point for long values...
Chris Lattner48c937e2004-04-06 17:34:50 +00001042 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +00001043 }
Chris Lattner333864d2003-06-05 19:30:30 +00001044 }
Chris Lattnere80e6372004-04-06 16:02:27 +00001045 }
Chris Lattner333864d2003-06-05 19:30:30 +00001046
Chris Lattnerde95c9e2004-10-17 06:10:40 +00001047 unsigned Op0r = getReg(Op0, MBB, IP);
1048
Chris Lattner9f08a922004-02-03 18:54:04 +00001049 // Special case handling of comparison against +/- 0.0
1050 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
1051 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +00001052 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001053 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00001054 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001055 return OpNum;
1056 }
1057
Chris Lattner58c41fe2003-08-24 19:19:47 +00001058 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001059 switch (Class) {
1060 default: assert(0 && "Unknown type class!");
1061 // Emit: cmp <var1>, <var2> (do the comparison). We can
1062 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1063 // 32-bit.
1064 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001065 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001066 break;
1067 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001068 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001069 break;
1070 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001071 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001072 break;
1073 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001074 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001075 break;
1076
1077 case cLong:
1078 if (OpNum < 2) { // seteq, setne
1079 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1080 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1081 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001082 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1083 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1084 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001085 break; // Allow the sete or setne to be generated from flags set by OR
1086 } else {
1087 // Emit a sequence of code which compares the high and low parts once
1088 // each, then uses a conditional move to handle the overflow case. For
1089 // example, a setlt for long would generate code like this:
1090 //
1091 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1092 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001093 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001094 //
1095
Chris Lattner6d40c192003-01-16 16:43:00 +00001096 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001097 // classes! Until then, hardcode registers so that we can deal with their
1098 // aliases (because we don't have conditional byte moves).
1099 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001100 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001101 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001102 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001103 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1104 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1105 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001106 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001107 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001108 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1109 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001110 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001111 }
1112 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001113 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001114}
Chris Lattner3e130a22003-01-13 00:32:26 +00001115
Chris Lattner6d40c192003-01-16 16:43:00 +00001116/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1117/// register, then move it to wherever the result should be.
1118///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001119void X86ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001120 if (canFoldSetCCIntoBranchOrSelect(&I))
1121 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001122
Chris Lattner6d40c192003-01-16 16:43:00 +00001123 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001124 MachineBasicBlock::iterator MII = BB->end();
1125 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1126 DestReg);
1127}
Chris Lattner6d40c192003-01-16 16:43:00 +00001128
Chris Lattner58c41fe2003-08-24 19:19:47 +00001129/// emitSetCCOperation - Common code shared between visitSetCondInst and
1130/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001131///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001132void X86ISel::emitSetCCOperation(MachineBasicBlock *MBB,
1133 MachineBasicBlock::iterator IP,
1134 Value *Op0, Value *Op1, unsigned Opcode,
1135 unsigned TargetReg) {
Chris Lattner58c41fe2003-08-24 19:19:47 +00001136 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001137 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001138
Chris Lattnerb2acc512003-10-19 21:09:10 +00001139 const Type *CompTy = Op0->getType();
1140 unsigned CompClass = getClassB(CompTy);
1141 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1142
Tanya Lattner9855b842004-12-01 18:27:03 +00001143 if (CompClass != cLong || OpNum < 2) {
1144 // Handle normal comparisons with a setcc instruction...
1145 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
1146 } else {
1147 // Handle long comparisons by copying the value which is already in BL into
1148 // the register we want...
1149 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
1150 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001151}
Chris Lattner51b49a92002-11-02 19:45:49 +00001152
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001153void X86ISel::visitSelectInst(SelectInst &SI) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001154 unsigned DestReg = getReg(SI);
1155 MachineBasicBlock::iterator MII = BB->end();
1156 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1157 SI.getFalseValue(), DestReg);
1158}
1159
1160/// emitSelect - Common code shared between visitSelectInst and the constant
1161/// expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001162void X86ISel::emitSelectOperation(MachineBasicBlock *MBB,
1163 MachineBasicBlock::iterator IP,
1164 Value *Cond, Value *TrueVal, Value *FalseVal,
1165 unsigned DestReg) {
Chris Lattner12d96a02004-03-30 21:22:00 +00001166 unsigned SelectClass = getClassB(TrueVal->getType());
1167
1168 // We don't support 8-bit conditional moves. If we have incoming constants,
1169 // transform them into 16-bit constants to avoid having a run-time conversion.
1170 if (SelectClass == cByte) {
1171 if (Constant *T = dyn_cast<Constant>(TrueVal))
1172 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1173 if (Constant *F = dyn_cast<Constant>(FalseVal))
1174 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1175 }
1176
Chris Lattner82c5a992004-04-13 21:56:09 +00001177 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1178 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1179 if (TrueReg == FalseReg) {
1180 static const unsigned Opcode[] = {
1181 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1182 };
1183 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1184 if (SelectClass == cLong)
1185 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1186 return;
1187 }
1188
Chris Lattner307ecba2004-03-30 22:39:09 +00001189 unsigned Opcode;
1190 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1191 // We successfully folded the setcc into the select instruction.
1192
1193 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1194 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1195 IP);
1196
1197 const Type *CompTy = SCI->getOperand(0)->getType();
1198 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1199
1200 // LLVM -> X86 signed X86 unsigned
1201 // ----- ---------- ------------
1202 // seteq -> cmovNE cmovNE
1203 // setne -> cmovE cmovE
1204 // setlt -> cmovGE cmovAE
1205 // setge -> cmovL cmovB
1206 // setgt -> cmovLE cmovBE
1207 // setle -> cmovG cmovA
1208 // ----
1209 // cmovNS // Used by comparison with 0 optimization
1210 // cmovS
1211
1212 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001213 default: assert(0 && "Unknown value class!");
1214 case cFP: {
1215 // Annoyingly, we don't have a full set of floating point conditional
1216 // moves. :(
1217 static const unsigned OpcodeTab[2][8] = {
1218 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1219 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1220 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1221 };
1222 Opcode = OpcodeTab[isSigned][OpNum];
1223
1224 // If opcode == 0, we hit a case that we don't support. Output a setcc
1225 // and compare the result against zero.
1226 if (Opcode == 0) {
1227 unsigned CompClass = getClassB(CompTy);
1228 unsigned CondReg;
1229 if (CompClass != cLong || OpNum < 2) {
1230 CondReg = makeAnotherReg(Type::BoolTy);
1231 // Handle normal comparisons with a setcc instruction...
1232 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1233 } else {
1234 // Long comparisons end up in the BL register.
1235 CondReg = X86::BL;
1236 }
1237
Chris Lattner68626c22004-03-31 22:22:36 +00001238 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001239 Opcode = X86::FCMOVE;
1240 }
1241 break;
1242 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001243 case cByte:
1244 case cShort: {
1245 static const unsigned OpcodeTab[2][8] = {
1246 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1247 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1248 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1249 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1250 };
1251 Opcode = OpcodeTab[isSigned][OpNum];
1252 break;
1253 }
1254 case cInt:
1255 case cLong: {
1256 static const unsigned OpcodeTab[2][8] = {
1257 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1258 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1259 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1260 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1261 };
1262 Opcode = OpcodeTab[isSigned][OpNum];
1263 break;
1264 }
1265 }
1266 } else {
1267 // Get the value being branched on, and use it to set the condition codes.
1268 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001269 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001270 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001271 default: assert(0 && "Unknown value class!");
1272 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001273 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001274 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001275 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001276 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001277 }
1278 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001279
Chris Lattner12d96a02004-03-30 21:22:00 +00001280 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001281
Chris Lattner12d96a02004-03-30 21:22:00 +00001282
1283 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1284 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1285 // cmove, then truncate the result.
1286 if (SelectClass == cByte) {
1287 DestReg = makeAnotherReg(Type::ShortTy);
1288 if (getClassB(TrueVal->getType()) == cByte) {
1289 // Promote the true value, by storing it into AL, and reading from AX.
1290 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1291 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1292 TrueReg = makeAnotherReg(Type::ShortTy);
1293 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1294 }
1295 if (getClassB(FalseVal->getType()) == cByte) {
1296 // Promote the true value, by storing it into CL, and reading from CX.
1297 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1298 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1299 FalseReg = makeAnotherReg(Type::ShortTy);
1300 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1301 }
1302 }
1303
1304 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1305
1306 switch (SelectClass) {
1307 case cByte:
1308 // We did the computation with 16-bit registers. Truncate back to our
1309 // result by copying into AX then copying out AL.
1310 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1311 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1312 break;
1313 case cLong:
1314 // Move the upper half of the value as well.
1315 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1316 break;
1317 }
1318}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001319
1320
1321
Brian Gaekec2505982002-11-30 11:57:28 +00001322/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1323/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001324///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001325void X86ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001326 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001327
Chris Lattner29bf0622004-04-06 01:21:00 +00001328 Value *Val = VR.Val;
1329 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001330 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001331 if (Constant *C = dyn_cast<Constant>(Val)) {
1332 Val = ConstantExpr::getCast(C, Type::IntTy);
1333 Ty = Type::IntTy;
1334 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001335
Chris Lattner502e36c2004-04-06 01:25:33 +00001336 // If this is a simple constant, just emit a MOVri directly to avoid the
1337 // copy.
1338 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1339 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001340 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001341 return;
1342 }
1343 }
1344
Chris Lattner29bf0622004-04-06 01:21:00 +00001345 // Make sure we have the register number for this value...
1346 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1347
1348 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001349 case cByte:
1350 // Extend value into target register (8->32)
1351 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001352 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001353 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001354 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001355 break;
1356 case cShort:
1357 // Extend value into target register (16->32)
1358 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001359 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001360 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001361 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001362 break;
1363 case cInt:
1364 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001365 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001366 break;
1367 default:
1368 assert(0 && "Unpromotable operand class in promote32");
1369 }
Brian Gaekec2505982002-11-30 11:57:28 +00001370}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001371
Chris Lattner72614082002-10-25 22:55:53 +00001372/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1373/// we have the following possibilities:
1374///
1375/// ret void: No return value, simply emit a 'ret' instruction
1376/// ret sbyte, ubyte : Extend value into EAX and return
1377/// ret short, ushort: Extend value into EAX and return
1378/// ret int, uint : Move value into EAX and return
1379/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001380/// ret long, ulong : Move value into EAX/EDX and return
1381/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001382///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001383void X86ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001384 if (I.getNumOperands() == 0) {
1385 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1386 return;
1387 }
1388
1389 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001390 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001391 case cByte: // integral return values: extend or move into EAX and return
1392 case cShort:
1393 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001394 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001395 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001396 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001397 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001398 case cFP: { // Floats & Doubles: Return in ST(0)
1399 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001400 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001401 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001402 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001403 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001404 }
1405 case cLong: {
1406 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001407 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1408 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001409 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001410 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1411 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001412 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001413 }
Chris Lattner94af4142002-12-25 05:13:53 +00001414 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001415 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001416 }
Chris Lattner43189d12002-11-17 20:07:45 +00001417 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001418 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001419}
1420
Chris Lattner55f6fab2003-01-16 18:07:23 +00001421// getBlockAfter - Return the basic block which occurs lexically after the
1422// specified one.
1423static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1424 Function::iterator I = BB; ++I; // Get iterator to next block
1425 return I != BB->getParent()->end() ? &*I : 0;
1426}
1427
Chris Lattner51b49a92002-11-02 19:45:49 +00001428/// visitBranchInst - Handle conditional and unconditional branches here. Note
1429/// that since code layout is frozen at this point, that if we are trying to
1430/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001431/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001432///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001433void X86ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001434 // Update machine-CFG edges
1435 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1436 if (BI.isConditional())
1437 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1438
Chris Lattner55f6fab2003-01-16 18:07:23 +00001439 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1440
1441 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001442 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001443 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001444 return;
1445 }
1446
1447 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001448 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001449 if (SCI == 0) {
1450 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1451 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001452 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001453 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001454 if (BI.getSuccessor(1) == NextBB) {
1455 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001456 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001457 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001458 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001459
1460 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001461 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001462 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001463 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001464 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001465
1466 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001467 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001468 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001469
1470 const Type *CompTy = SCI->getOperand(0)->getType();
1471 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001472
Chris Lattnerb2acc512003-10-19 21:09:10 +00001473
Chris Lattner6d40c192003-01-16 16:43:00 +00001474 // LLVM -> X86 signed X86 unsigned
1475 // ----- ---------- ------------
1476 // seteq -> je je
1477 // setne -> jne jne
1478 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001479 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001480 // setgt -> jg ja
1481 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001482 // ----
1483 // js // Used by comparison with 0 optimization
1484 // jns
1485
1486 static const unsigned OpcodeTab[2][8] = {
1487 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1488 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1489 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001490 };
1491
Chris Lattner55f6fab2003-01-16 18:07:23 +00001492 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001493 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1494 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001495 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001496 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001497 } else {
1498 // Change to the inverse condition...
1499 if (BI.getSuccessor(1) != NextBB) {
1500 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001501 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1502 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001503 }
1504 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001505}
1506
Chris Lattner3e130a22003-01-13 00:32:26 +00001507
1508/// doCall - This emits an abstract call instruction, setting up the arguments
1509/// and the return value as appropriate. For the actual function call itself,
1510/// it inserts the specified CallMI instruction into the stream.
1511///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001512void X86ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1513 const std::vector<ValueRecord> &Args) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001514 // Count how many bytes are to be pushed on the stack...
1515 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001516
Chris Lattner3e130a22003-01-13 00:32:26 +00001517 if (!Args.empty()) {
1518 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1519 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001520 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001521 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001522 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001523 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001524 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001525 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1526 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001527 default: assert(0 && "Unknown class!");
1528 }
1529
1530 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001531 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001532
1533 // Arguments go on the stack in reverse order, as specified by the ABI.
1534 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001535 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001536 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001537 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001538 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001539 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1540 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1541 .addImm(Args[i].Val == ConstantBool::True);
1542 break;
1543 }
1544 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001545 case cShort:
1546 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1547 // Zero/Sign extend constant, then stuff into memory.
1548 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1549 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1550 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1551 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1552 } else {
1553 // Promote arg to 32 bits wide into a temporary register...
1554 ArgReg = makeAnotherReg(Type::UIntTy);
1555 promote32(ArgReg, Args[i]);
1556 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1557 X86::ESP, ArgOffset).addReg(ArgReg);
1558 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001559 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001560 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001561 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1562 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1563 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1564 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001565 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1566 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1567 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001568 } else {
1569 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1570 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1571 X86::ESP, ArgOffset).addReg(ArgReg);
1572 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001573 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001574 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001575 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1576 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1577 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1578 X86::ESP, ArgOffset).addImm(Val & ~0U);
1579 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1580 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1581 } else {
1582 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1583 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1584 X86::ESP, ArgOffset).addReg(ArgReg);
1585 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1586 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1587 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001588 ArgOffset += 4; // 8 byte entry, not 4.
1589 break;
1590
Chris Lattner065faeb2002-12-28 20:24:02 +00001591 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001592 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001593 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001594 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001595 X86::ESP, ArgOffset).addReg(ArgReg);
1596 } else {
1597 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001598 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001599 X86::ESP, ArgOffset).addReg(ArgReg);
1600 ArgOffset += 4; // 8 byte entry, not 4.
1601 }
1602 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001603
Chris Lattner3e130a22003-01-13 00:32:26 +00001604 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001605 }
1606 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001607 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001608 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001609 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001610 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001611
Chris Lattner3e130a22003-01-13 00:32:26 +00001612 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001613
Chris Lattneree352852004-02-29 07:22:16 +00001614 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001615
1616 // If there is a return value, scavenge the result from the location the call
1617 // leaves it in...
1618 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001619 if (Ret.Ty != Type::VoidTy) {
1620 unsigned DestClass = getClassB(Ret.Ty);
1621 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001622 case cByte:
1623 case cShort:
1624 case cInt: {
1625 // Integral results are in %eax, or the appropriate portion
1626 // thereof.
1627 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001628 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001629 };
1630 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001631 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001632 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001633 }
Chris Lattner94af4142002-12-25 05:13:53 +00001634 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001635 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001636 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001637 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001638 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1639 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001640 break;
1641 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001642 }
Chris Lattnera3243642002-12-04 23:45:28 +00001643 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001644}
Chris Lattner2df035b2002-11-02 19:27:56 +00001645
Chris Lattner3e130a22003-01-13 00:32:26 +00001646
1647/// visitCallInst - Push args on stack and do a procedure call instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001648void X86ISel::visitCallInst(CallInst &CI) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001649 MachineInstr *TheCall;
1650 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001651 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001652 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001653 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1654 return;
1655 }
1656
Chris Lattner3e130a22003-01-13 00:32:26 +00001657 // Emit a CALL instruction with PC-relative displacement.
1658 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1659 } else { // Emit an indirect call...
1660 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001661 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001662 }
1663
1664 std::vector<ValueRecord> Args;
1665 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001666 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001667
1668 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1669 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001670}
Chris Lattner3e130a22003-01-13 00:32:26 +00001671
Chris Lattner44827152003-12-28 09:47:19 +00001672/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1673/// function, lowering any calls to unknown intrinsic functions into the
1674/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001675///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001676void X86ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
Chris Lattner44827152003-12-28 09:47:19 +00001677 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1678 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1679 if (CallInst *CI = dyn_cast<CallInst>(I++))
1680 if (Function *F = CI->getCalledFunction())
1681 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001682 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001683 case Intrinsic::vastart:
1684 case Intrinsic::vacopy:
1685 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001686 case Intrinsic::returnaddress:
1687 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001688 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001689 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001690 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001691 case Intrinsic::readport:
1692 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001693 // We directly implement these intrinsics
1694 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001695 case Intrinsic::readio: {
1696 // On X86, memory operations are in-order. Lower this intrinsic
1697 // into a volatile load.
1698 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001699 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1700 CI->replaceAllUsesWith(LI);
1701 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001702 break;
1703 }
1704 case Intrinsic::writeio: {
1705 // On X86, memory operations are in-order. Lower this intrinsic
1706 // into a volatile store.
1707 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001708 StoreInst *LI = new StoreInst(CI->getOperand(1),
1709 CI->getOperand(2), true, CI);
1710 CI->replaceAllUsesWith(LI);
1711 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001712 break;
1713 }
Chris Lattner44827152003-12-28 09:47:19 +00001714 default:
1715 // All other intrinsic calls we must lower.
1716 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001717 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001718 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001719 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001720 } else {
1721 I = BB->begin();
1722 }
1723 }
Chris Lattner44827152003-12-28 09:47:19 +00001724}
1725
Misha Brukmaneae1bf12004-09-21 18:21:21 +00001726void X86ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001727 unsigned TmpReg1, TmpReg2;
1728 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001729 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001730 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001731 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001732 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001733 return;
1734
Chris Lattner5634b9f2004-03-13 00:24:52 +00001735 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001736 TmpReg1 = getReg(CI);
1737 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001738 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001739 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001740 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001741
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001742 case Intrinsic::returnaddress:
1743 case Intrinsic::frameaddress:
1744 TmpReg1 = getReg(CI);
1745 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1746 if (ID == Intrinsic::returnaddress) {
1747 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001748 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001749 ReturnAddressIndex);
1750 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001751 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001752 ReturnAddressIndex, -4);
1753 }
1754 } else {
1755 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001756 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001757 }
1758 return;
1759
Chris Lattnerdc572442004-06-15 21:36:44 +00001760 case Intrinsic::isunordered:
1761 TmpReg1 = getReg(CI.getOperand(1));
1762 TmpReg2 = getReg(CI.getOperand(2));
1763 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1764 TmpReg2 = getReg(CI);
1765 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1766 return;
1767
Chris Lattner915e5e52004-02-12 17:53:22 +00001768 case Intrinsic::memcpy: {
1769 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1770 unsigned Align = 1;
1771 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1772 Align = AlignC->getRawValue();
1773 if (Align == 0) Align = 1;
1774 }
1775
1776 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001777 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001778 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001779 switch (Align & 3) {
1780 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001781 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1782 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1783 } else {
1784 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001785 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001786 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001787 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001788 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001789 break;
1790 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001791 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1792 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1793 } else {
1794 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001795 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001796 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001797 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001798 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001799 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001800 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001801 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001802 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001803 break;
1804 }
1805
1806 // No matter what the alignment is, we put the source in ESI, the
1807 // destination in EDI, and the count in ECX.
1808 TmpReg1 = getReg(CI.getOperand(1));
1809 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001810 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1811 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1812 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001813 BuildMI(BB, Opcode, 0);
1814 return;
1815 }
1816 case Intrinsic::memset: {
1817 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1818 unsigned Align = 1;
1819 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1820 Align = AlignC->getRawValue();
1821 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001822 }
1823
Chris Lattner2a0f2242004-02-14 04:46:05 +00001824 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001825 unsigned CountReg;
1826 unsigned Opcode;
1827 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1828 unsigned Val = ValC->getRawValue() & 255;
1829
1830 // If the value is a constant, then we can potentially use larger copies.
1831 switch (Align & 3) {
1832 case 2: // WORD aligned
1833 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001834 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001835 } else {
1836 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001837 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001838 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001839 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001840 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001841 Opcode = X86::REP_STOSW;
1842 break;
1843 case 0: // DWORD aligned
1844 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001845 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001846 } else {
1847 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001848 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001849 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001850 }
1851 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001852 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001853 Opcode = X86::REP_STOSD;
1854 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001855 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001856 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001857 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001858 Opcode = X86::REP_STOSB;
1859 break;
1860 }
1861 } else {
1862 // If it's not a constant value we are storing, just fall back. We could
1863 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1864 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001865 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001866 CountReg = getReg(CI.getOperand(3));
1867 Opcode = X86::REP_STOSB;
1868 }
1869
1870 // No matter what the alignment is, we put the source in ESI, the
1871 // destination in EDI, and the count in ECX.
1872 TmpReg1 = getReg(CI.getOperand(1));
1873 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001874 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1875 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001876 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001877 return;
1878 }
1879
Chris Lattner87e18de2004-04-13 17:20:37 +00001880 case Intrinsic::readport: {
1881 // First, determine that the size of the operand falls within the acceptable
1882 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001883 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001884 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001885 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001886 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001887 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001888
John Criswell4ffff9e2004-04-08 20:31:47 +00001889 // Now, move the I/O port address into the DX register and use the IN
1890 // instruction to get the input data.
1891 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001892 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1893 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001894
Chris Lattner87e18de2004-04-13 17:20:37 +00001895 // If the port is a single-byte constant, use the immediate form.
1896 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1897 if ((C->getRawValue() & 255) == C->getRawValue()) {
1898 switch (Class) {
1899 case cByte:
1900 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1901 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1902 return;
1903 case cShort:
1904 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1905 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1906 return;
1907 case cInt:
1908 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1909 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1910 return;
1911 }
1912 }
1913
1914 unsigned Reg = getReg(CI.getOperand(1));
1915 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1916 switch (Class) {
1917 case cByte:
1918 BuildMI(BB, X86::IN8rr, 0);
1919 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1920 break;
1921 case cShort:
1922 BuildMI(BB, X86::IN16rr, 0);
1923 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1924 break;
1925 case cInt:
1926 BuildMI(BB, X86::IN32rr, 0);
1927 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1928 break;
1929 default:
1930 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001931 exit (1);
1932 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001933 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001934 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001935
Chris Lattner87e18de2004-04-13 17:20:37 +00001936 case Intrinsic::writeport: {
1937 // First, determine that the size of the operand falls within the
1938 // acceptable range for this architecture.
1939 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1940 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1941 exit(1);
1942 }
1943
1944 unsigned Class = getClassB(CI.getOperand(1)->getType());
1945 unsigned ValReg = getReg(CI.getOperand(1));
1946 switch (Class) {
1947 case cByte:
1948 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1949 break;
1950 case cShort:
1951 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1952 break;
1953 case cInt:
1954 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1955 break;
1956 default:
1957 std::cerr << "llvm.writeport: invalid data type for X86 target";
1958 exit(1);
1959 }
1960
1961
1962 // If the port is a single-byte constant, use the immediate form.
1963 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1964 if ((C->getRawValue() & 255) == C->getRawValue()) {
1965 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1966 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1967 return;
1968 }
1969
1970 // Otherwise, move the I/O port address into the DX register and the value
1971 // to write into the AL/AX/EAX register.
1972 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1973 unsigned Reg = getReg(CI.getOperand(2));
1974 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1975 BuildMI(BB, Opc[Class], 0);
1976 return;
1977 }
1978
Chris Lattner44827152003-12-28 09:47:19 +00001979 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001980 }
1981}
1982
Chris Lattner7dee5da2004-03-08 01:58:35 +00001983static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1984 if (LI.getParent() != User.getParent())
1985 return false;
1986 BasicBlock::iterator It = &LI;
1987 // Check all of the instructions between the load and the user. We should
1988 // really use alias analysis here, but for now we just do something simple.
1989 for (++It; It != BasicBlock::iterator(&User); ++It) {
1990 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001991 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001992 case Instruction::Store:
1993 case Instruction::Call:
1994 case Instruction::Invoke:
1995 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001996 case Instruction::Load:
1997 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1998 return false;
1999 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002000 }
2001 }
2002 return true;
2003}
2004
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002005/// visitSimpleBinary - Implement simple binary operators for integral types...
2006/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
2007/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00002008///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002009void X86ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002010 unsigned DestReg = getReg(B);
2011 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00002012 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002013 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00002014
Chris Lattnerde95c9e2004-10-17 06:10:40 +00002015 // If this is AND X, C, and it is only used by a setcc instruction, it will
2016 // be folded. There is no need to emit this instruction.
2017 if (B.hasOneUse() && OperatorClass == 2 && isa<ConstantInt>(Op1))
2018 if (Class == cByte || Class == cShort || Class == cInt) {
2019 Instruction *Use = cast<Instruction>(B.use_back());
2020 if (isa<SetCondInst>(Use) &&
2021 Use->getOperand(1) == Constant::getNullValue(B.getType())) {
2022 switch (getSetCCNumber(Use->getOpcode())) {
2023 case 0:
2024 case 1:
2025 return;
2026 default:
2027 if (B.getType()->isSigned()) return;
2028 }
2029 }
2030 }
2031
Chris Lattner7dee5da2004-03-08 01:58:35 +00002032 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002033 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00002034 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00002035 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00002036 if (!B.swapOperands())
2037 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2038
Chris Lattnerccd97962004-06-17 22:15:25 +00002039 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00002040 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
2041
Chris Lattner95157f72004-04-11 22:05:45 +00002042 unsigned Opcode;
2043 if (Class != cFP) {
2044 static const unsigned OpcodeTab[][3] = {
2045 // Arithmetic operators
2046 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
2047 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
2048
2049 // Bitwise operators
2050 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
2051 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
2052 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
2053 };
2054 Opcode = OpcodeTab[OperatorClass][Class];
2055 } else {
2056 static const unsigned OpcodeTab[][2] = {
2057 { X86::FADD32m, X86::FADD64m }, // ADD
2058 { X86::FSUB32m, X86::FSUB64m }, // SUB
2059 };
2060 const Type *Ty = Op0->getType();
2061 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2062 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
2063 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002064
Chris Lattner7dee5da2004-03-08 01:58:35 +00002065 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002066 if (AllocaInst *AI =
2067 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
2068 unsigned FI = getFixedSizedAllocaFI(AI);
2069 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
2070
2071 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002072 X86AddressMode AM;
2073 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002074
Reid Spencerfc989e12004-08-30 00:13:26 +00002075 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002076 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002077 return;
2078 }
2079
Chris Lattner95157f72004-04-11 22:05:45 +00002080 // If this is a floating point subtract, check to see if we can fold the first
2081 // operand in.
2082 if (Class == cFP && OperatorClass == 1 &&
2083 isa<LoadInst>(Op0) &&
2084 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2085 const Type *Ty = Op0->getType();
2086 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2087 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2088
Chris Lattner95157f72004-04-11 22:05:45 +00002089 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002090 if (AllocaInst *AI =
2091 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2092 unsigned FI = getFixedSizedAllocaFI(AI);
2093 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2094 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002095 X86AddressMode AM;
2096 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002097
Reid Spencerfc989e12004-08-30 00:13:26 +00002098 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002099 }
Chris Lattner95157f72004-04-11 22:05:45 +00002100 return;
2101 }
2102
Chris Lattner721d2d42004-03-08 01:18:36 +00002103 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002104}
Chris Lattner3e130a22003-01-13 00:32:26 +00002105
Chris Lattner6621ed92004-04-11 21:23:56 +00002106
2107/// emitBinaryFPOperation - This method handles emission of floating point
2108/// Add (0), Sub (1), Mul (2), and Div (3) operations.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002109void X86ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2110 MachineBasicBlock::iterator IP,
2111 Value *Op0, Value *Op1,
2112 unsigned OperatorClass, unsigned DestReg) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002113 // Special case: op Reg, <const fp>
2114 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2115 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2116 // Create a constant pool entry for this constant.
2117 MachineConstantPool *CP = F->getConstantPool();
2118 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2119 const Type *Ty = Op1->getType();
2120
2121 static const unsigned OpcodeTab[][4] = {
2122 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2123 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2124 };
2125
2126 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2127 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2128 unsigned Op0r = getReg(Op0, BB, IP);
2129 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2130 DestReg).addReg(Op0r), CPI);
2131 return;
2132 }
2133
Chris Lattner13c07fe2004-04-12 00:12:04 +00002134 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002135 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2136 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2137 // -0.0 - X === -X
2138 unsigned op1Reg = getReg(Op1, BB, IP);
2139 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2140 return;
2141 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002142 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002143
2144 // Create a constant pool entry for this constant.
2145 MachineConstantPool *CP = F->getConstantPool();
2146 unsigned CPI = CP->getConstantPoolIndex(CFP);
2147 const Type *Ty = CFP->getType();
2148
2149 static const unsigned OpcodeTab[][4] = {
2150 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2151 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2152 };
2153
2154 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2155 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2156 unsigned Op1r = getReg(Op1, BB, IP);
2157 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2158 DestReg).addReg(Op1r), CPI);
2159 return;
2160 }
2161
2162 // General case.
2163 static const unsigned OpcodeTab[4] = {
2164 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2165 };
2166
2167 unsigned Opcode = OpcodeTab[OperatorClass];
2168 unsigned Op0r = getReg(Op0, BB, IP);
2169 unsigned Op1r = getReg(Op1, BB, IP);
2170 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2171}
2172
Chris Lattnerb2acc512003-10-19 21:09:10 +00002173/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2174/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2175/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002176///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002177/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2178/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002179///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002180void X86ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
2181 MachineBasicBlock::iterator IP,
2182 Value *Op0, Value *Op1,
2183 unsigned OperatorClass,
2184 unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002185 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002186
Chris Lattner6621ed92004-04-11 21:23:56 +00002187 if (Class == cFP) {
2188 assert(OperatorClass < 2 && "No logical ops for FP!");
2189 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2190 return;
2191 }
2192
Chris Lattner48b0c972004-04-11 20:26:20 +00002193 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002194 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002195 static unsigned const NEGTab[] = {
2196 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2197 };
Chris Lattner667ea022004-06-18 00:50:37 +00002198
2199 // sub 0, X -> neg X
2200 if (CI->isNullValue()) {
2201 unsigned op1Reg = getReg(Op1, MBB, IP);
2202 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002203
Chris Lattner667ea022004-06-18 00:50:37 +00002204 if (Class == cLong) {
2205 // We just emitted: Dl = neg Sl
2206 // Now emit : T = addc Sh, 0
2207 // : Dh = neg T
2208 unsigned T = makeAnotherReg(Type::IntTy);
2209 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2210 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2211 }
2212 return;
2213 } else if (Op1->hasOneUse() && Class != cLong) {
2214 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2215 // than copying C into a temporary register, because of register
2216 // pressure (tmp and destreg can share a register.
2217 static unsigned const ADDRITab[] = {
2218 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2219 };
2220 unsigned op1Reg = getReg(Op1, MBB, IP);
2221 unsigned Tmp = makeAnotherReg(Op0->getType());
2222 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002223 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2224 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002225 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002226 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002227 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002228
Chris Lattner48b0c972004-04-11 20:26:20 +00002229 // Special case: op Reg, <const int>
2230 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002231 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002232
Chris Lattner721d2d42004-03-08 01:18:36 +00002233 // xor X, -1 -> not X
2234 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002235 static unsigned const NOTTab[] = {
2236 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2237 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002238 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002239 if (Class == cLong) // Invert the top part too
2240 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002241 return;
2242 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002243
Chris Lattner721d2d42004-03-08 01:18:36 +00002244 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002245 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2246 // Note that we can't use dec for 64-bit decrements, because it does not
2247 // set the carry flag!
2248 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002249 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2250 return;
2251 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002252
Chris Lattner721d2d42004-03-08 01:18:36 +00002253 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002254 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2255 // Note that we can't use inc for 64-bit increments, because it does not
2256 // set the carry flag!
2257 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002258 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002259 return;
2260 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002261
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002262 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002263 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002264 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2265 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002266
Chris Lattner721d2d42004-03-08 01:18:36 +00002267 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002268 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2269 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2270 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002271 };
2272
Chris Lattner721d2d42004-03-08 01:18:36 +00002273 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002274 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002275
Chris Lattner33f7fa32004-04-06 03:15:53 +00002276 if (Class != cLong) {
2277 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2278 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002279 }
2280
2281 // If this is a long value and the high or low bits have a special
2282 // property, emit some special cases.
2283 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2284
2285 // If the constant is zero in the low 32-bits, just copy the low part
2286 // across and apply the normal 32-bit operation to the high parts. There
2287 // will be no carry or borrow into the top.
2288 if (Op1l == 0) {
2289 if (OperatorClass != 2) // All but and...
2290 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2291 else
2292 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2293 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2294 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002295 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002296 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002297
2298 // If this is a logical operation and the top 32-bits are zero, just
2299 // operate on the lower 32.
2300 if (Op1h == 0 && OperatorClass > 1) {
2301 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2302 .addReg(Op0r).addImm(Op1l);
2303 if (OperatorClass != 2) // All but and
2304 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2305 else
2306 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2307 return;
2308 }
2309
2310 // TODO: We could handle lots of other special cases here, such as AND'ing
2311 // with 0xFFFFFFFF00000000 -> noop, etc.
2312
2313 // Otherwise, code generate the full operation with a constant.
2314 static const unsigned TopTab[] = {
2315 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2316 };
2317
2318 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2319 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2320 .addReg(Op0r+1).addImm(Op1h);
2321 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002322 }
2323
2324 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002325 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002326 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002327 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2328 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002329
Chris Lattnerb2acc512003-10-19 21:09:10 +00002330 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002331 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2332 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2333 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002334 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002335
Chris Lattnerb2acc512003-10-19 21:09:10 +00002336 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002337 unsigned Op0r = getReg(Op0, MBB, IP);
2338 unsigned Op1r = getReg(Op1, MBB, IP);
2339 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2340
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002341 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002342 static const unsigned TopTab[] = {
2343 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2344 };
2345 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2346 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2347 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002348}
2349
Chris Lattner3e130a22003-01-13 00:32:26 +00002350/// doMultiply - Emit appropriate instructions to multiply together the
2351/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2352/// result should be given as DestTy.
2353///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002354void X86ISel::doMultiply(MachineBasicBlock *MBB,
2355 MachineBasicBlock::iterator MBBI,
2356 unsigned DestReg, const Type *DestTy,
2357 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002358 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002359 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002360 case cInt:
2361 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002362 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002363 .addReg(op0Reg).addReg(op1Reg);
2364 return;
2365 case cByte:
2366 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002367 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2368 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2369 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002370 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002371 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002372 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002373 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002374}
2375
Chris Lattnerb2acc512003-10-19 21:09:10 +00002376// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2377// returns zero when the input is not exactly a power of two.
2378static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002379 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002380 unsigned Count = 0;
2381 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002382 Val >>= 1;
2383 ++Count;
2384 }
2385 return Count+1;
2386}
2387
Chris Lattner462fa822004-04-11 20:56:28 +00002388
2389/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2390/// 16, or 32-bit integer multiply by a constant.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002391void X86ISel::doMultiplyConst(MachineBasicBlock *MBB,
2392 MachineBasicBlock::iterator IP,
2393 unsigned DestReg, const Type *DestTy,
2394 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002395 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2396 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002397 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner596b97f2004-07-19 23:47:21 +00002398 static const unsigned NEGrTab[] = {X86::NEG8r , X86::NEG16r , X86::NEG32r };
Chris Lattner6ab06d52004-04-06 04:55:43 +00002399
Chris Lattnerb2acc512003-10-19 21:09:10 +00002400 unsigned Class = getClass(DestTy);
Chris Lattner596b97f2004-07-19 23:47:21 +00002401 unsigned TmpReg;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002402
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002403 // Handle special cases here.
2404 switch (ConstRHS) {
Chris Lattner596b97f2004-07-19 23:47:21 +00002405 case -2:
2406 TmpReg = makeAnotherReg(DestTy);
2407 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2408 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(TmpReg).addReg(TmpReg);
2409 return;
2410 case -1:
2411 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(op0Reg);
2412 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002413 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002414 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2415 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002416 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002417 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2418 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002419 case 2:
2420 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2421 return;
2422 case 3:
2423 case 5:
2424 case 9:
2425 if (Class == cInt) {
Reid Spencerfc989e12004-08-30 00:13:26 +00002426 X86AddressMode AM;
2427 AM.BaseType = X86AddressMode::RegBase;
2428 AM.Base.Reg = op0Reg;
2429 AM.Scale = ConstRHS-1;
2430 AM.IndexReg = op0Reg;
2431 AM.Disp = 0;
2432 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg), AM);
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002433 return;
2434 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002435 case -3:
2436 case -5:
2437 case -9:
2438 if (Class == cInt) {
2439 TmpReg = makeAnotherReg(DestTy);
Reid Spencerfc989e12004-08-30 00:13:26 +00002440 X86AddressMode AM;
2441 AM.BaseType = X86AddressMode::RegBase;
2442 AM.Base.Reg = op0Reg;
2443 AM.Scale = -ConstRHS-1;
2444 AM.IndexReg = op0Reg;
2445 AM.Disp = 0;
2446 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TmpReg), AM);
Chris Lattner596b97f2004-07-19 23:47:21 +00002447 BuildMI(*MBB, IP, NEGrTab[Class], 1, DestReg).addReg(TmpReg);
2448 return;
2449 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002450 }
2451
Chris Lattnerb2acc512003-10-19 21:09:10 +00002452 // If the element size is exactly a power of 2, use a shift to get it.
2453 if (unsigned Shift = ExactLog2(ConstRHS)) {
2454 switch (Class) {
2455 default: assert(0 && "Unknown class for this function!");
2456 case cByte:
Chris Lattner596b97f2004-07-19 23:47:21 +00002457 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002458 return;
2459 case cShort:
Chris Lattner596b97f2004-07-19 23:47:21 +00002460 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002461 return;
2462 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002463 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002464 return;
2465 }
2466 }
Chris Lattner596b97f2004-07-19 23:47:21 +00002467
2468 // If the element size is a negative power of 2, use a shift/neg to get it.
2469 if (unsigned Shift = ExactLog2(-ConstRHS)) {
2470 TmpReg = makeAnotherReg(DestTy);
2471 BuildMI(*MBB, IP, NEGrTab[Class], 1, TmpReg).addReg(op0Reg);
2472 switch (Class) {
2473 default: assert(0 && "Unknown class for this function!");
2474 case cByte:
2475 BuildMI(*MBB, IP, X86::SHL8ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2476 return;
2477 case cShort:
2478 BuildMI(*MBB, IP, X86::SHL16ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2479 return;
2480 case cInt:
2481 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(TmpReg).addImm(Shift-1);
2482 return;
2483 }
2484 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002485
2486 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002487 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002488 return;
2489 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002490 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002491 return;
2492 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002493
2494 // Most general case, emit a normal multiply...
Chris Lattner596b97f2004-07-19 23:47:21 +00002495 TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002496 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002497
2498 // Emit a MUL to multiply the register holding the index by
2499 // elementSize, putting the result in OffsetReg.
2500 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2501}
2502
Chris Lattnerca9671d2002-11-02 20:28:58 +00002503/// visitMul - Multiplies are not simple binary operators because they must deal
2504/// with the EAX register explicitly.
2505///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002506void X86ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002507 unsigned ResultReg = getReg(I);
2508
Chris Lattner95157f72004-04-11 22:05:45 +00002509 Value *Op0 = I.getOperand(0);
2510 Value *Op1 = I.getOperand(1);
2511
2512 // Fold loads into floating point multiplies.
2513 if (getClass(Op0->getType()) == cFP) {
2514 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2515 if (!I.swapOperands())
2516 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2517 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2518 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2519 const Type *Ty = Op0->getType();
2520 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2521 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2522
Chris Lattner95157f72004-04-11 22:05:45 +00002523 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002524 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2525 unsigned FI = getFixedSizedAllocaFI(AI);
2526 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2527 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002528 X86AddressMode AM;
2529 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002530
Reid Spencerfc989e12004-08-30 00:13:26 +00002531 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002532 }
Chris Lattner95157f72004-04-11 22:05:45 +00002533 return;
2534 }
2535 }
2536
Chris Lattner462fa822004-04-11 20:56:28 +00002537 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002538 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002539}
2540
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002541void X86ISel::emitMultiply(MachineBasicBlock *MBB,
2542 MachineBasicBlock::iterator IP,
2543 Value *Op0, Value *Op1, unsigned DestReg) {
Chris Lattner462fa822004-04-11 20:56:28 +00002544 MachineBasicBlock &BB = *MBB;
2545 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002546
2547 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002548 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002549 switch (Class) {
2550 case cByte:
2551 case cShort:
2552 case cInt:
2553 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002554 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2555 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002556 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002557 unsigned Op1Reg = getReg(Op1, &BB, IP);
2558 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002559 }
Chris Lattner462fa822004-04-11 20:56:28 +00002560 return;
2561 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002562 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2563 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002564 case cLong:
2565 break;
2566 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002567
Chris Lattner462fa822004-04-11 20:56:28 +00002568 // Long value. We have to do things the hard way...
2569 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2570 unsigned CLow = CI->getRawValue();
2571 unsigned CHi = CI->getRawValue() >> 32;
2572
2573 if (CLow == 0) {
2574 // If the low part of the constant is all zeros, things are simple.
2575 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2576 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2577 return;
2578 }
2579
2580 // Multiply the two low parts... capturing carry into EDX
2581 unsigned OverflowReg = 0;
2582 if (CLow == 1) {
2583 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002584 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002585 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2586 OverflowReg = makeAnotherReg(Type::UIntTy);
2587 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2588 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2589 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002590
Chris Lattner462fa822004-04-11 20:56:28 +00002591 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2592 BuildMI(BB, IP, X86::MOV32rr, 1,
2593 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2594 }
2595
2596 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2597 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2598
2599 unsigned AHBLplusOverflowReg;
2600 if (OverflowReg) {
2601 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2602 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002603 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002604 } else {
2605 AHBLplusOverflowReg = AHBLReg;
2606 }
2607
2608 if (CHi == 0) {
2609 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2610 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002611 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002612 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002613
Chris Lattner462fa822004-04-11 20:56:28 +00002614 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002615 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2616 }
Chris Lattner462fa822004-04-11 20:56:28 +00002617 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002618 }
Chris Lattner462fa822004-04-11 20:56:28 +00002619
2620 // General 64x64 multiply
2621
2622 unsigned Op1Reg = getReg(Op1, &BB, IP);
2623 // Multiply the two low parts... capturing carry into EDX
2624 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2625 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2626
2627 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2628 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2629 BuildMI(BB, IP, X86::MOV32rr, 1,
2630 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2631
2632 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2633 BuildMI(BB, IP, X86::IMUL32rr, 2,
2634 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2635
2636 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2637 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2638 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2639
2640 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2641 BuildMI(BB, IP, X86::IMUL32rr, 2,
2642 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2643
2644 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2645 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002646}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002647
Chris Lattner06925362002-11-17 21:56:38 +00002648
Chris Lattnerf01729e2002-11-02 20:54:46 +00002649/// visitDivRem - Handle division and remainder instructions... these
2650/// instruction both require the same instructions to be generated, they just
2651/// select the result from a different register. Note that both of these
2652/// instructions work differently for signed and unsigned operands.
2653///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002654void X86ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002655 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002656 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2657
2658 // Fold loads into floating point divides.
2659 if (getClass(Op0->getType()) == cFP) {
2660 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2661 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2662 const Type *Ty = Op0->getType();
2663 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2664 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2665
Chris Lattner95157f72004-04-11 22:05:45 +00002666 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002667 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2668 unsigned FI = getFixedSizedAllocaFI(AI);
2669 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2670 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002671 X86AddressMode AM;
2672 getAddressingMode(LI->getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002673
Reid Spencerfc989e12004-08-30 00:13:26 +00002674 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002675 }
Chris Lattner95157f72004-04-11 22:05:45 +00002676 return;
2677 }
2678
2679 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2680 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2681 const Type *Ty = Op0->getType();
2682 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2683 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2684
Chris Lattner95157f72004-04-11 22:05:45 +00002685 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002686 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2687 unsigned FI = getFixedSizedAllocaFI(AI);
2688 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2689 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00002690 X86AddressMode AM;
2691 getAddressingMode(LI->getOperand(0), AM);
2692 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002693 }
Chris Lattner95157f72004-04-11 22:05:45 +00002694 return;
2695 }
2696 }
2697
Chris Lattner94af4142002-12-25 05:13:53 +00002698
Chris Lattnercadff442003-10-23 17:21:43 +00002699 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002700 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002701 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002702}
2703
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002704void X86ISel::emitDivRemOperation(MachineBasicBlock *BB,
2705 MachineBasicBlock::iterator IP,
2706 Value *Op0, Value *Op1, bool isDiv,
2707 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002708 const Type *Ty = Op0->getType();
2709 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002710 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002711 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002712 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002713 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2714 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002715 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002716 unsigned Op0Reg = getReg(Op0, BB, IP);
2717 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002718 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002719 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002720 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002721 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2722 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002723 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2724 }
Chris Lattner94af4142002-12-25 05:13:53 +00002725 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002726 case cLong: {
2727 static const char *FnName[] =
2728 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002729 unsigned Op0Reg = getReg(Op0, BB, IP);
2730 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002731 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002732 MachineInstr *TheCall =
2733 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2734
2735 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002736 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2737 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002738 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2739 return;
2740 }
2741 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002742 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002743 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002744 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002745
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002746 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattner2483f672004-10-06 05:01:07 +00002747 static const unsigned NEGOpcode[]={ X86::NEG8r, X86::NEG16r, X86::NEG32r };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002748 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2749 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2750 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2751
2752 // Special case signed division by power of 2.
Chris Lattner2483f672004-10-06 05:01:07 +00002753 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1))
2754 if (isDiv) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002755 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2756 int V = CI->getValue();
2757
2758 if (V == 1) { // X /s 1 => X
2759 unsigned Op0Reg = getReg(Op0, BB, IP);
2760 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2761 return;
2762 }
2763
2764 if (V == -1) { // X /s -1 => -X
2765 unsigned Op0Reg = getReg(Op0, BB, IP);
2766 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2767 return;
2768 }
2769
Chris Lattner610f1e22004-10-06 04:02:39 +00002770 if (V == 2 || V == -2) { // X /s 2
2771 static const unsigned CMPOpcode[] = {
2772 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
2773 };
2774 static const unsigned SBBOpcode[] = {
2775 X86::SBB8ri, X86::SBB16ri, X86::SBB32ri
2776 };
2777 unsigned Op0Reg = getReg(Op0, BB, IP);
2778 unsigned SignBit = 1 << (CI->getType()->getPrimitiveSize()*8-1);
2779 BuildMI(*BB, IP, CMPOpcode[Class], 2).addReg(Op0Reg).addImm(SignBit);
2780
2781 unsigned TmpReg = makeAnotherReg(Op0->getType());
2782 BuildMI(*BB, IP, SBBOpcode[Class], 2, TmpReg).addReg(Op0Reg).addImm(-1);
2783
2784 unsigned TmpReg2 = V == 2 ? ResultReg : makeAnotherReg(Op0->getType());
2785 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg2).addReg(TmpReg).addImm(1);
2786 if (V == -2) {
2787 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg2);
2788 }
2789 return;
2790 }
2791
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002792 bool isNeg = false;
2793 if (V < 0) { // Not a positive power of 2?
2794 V = -V;
2795 isNeg = true; // Maybe it's a negative power of 2.
2796 }
2797 if (unsigned Log = ExactLog2(V)) {
2798 --Log;
2799 unsigned Op0Reg = getReg(Op0, BB, IP);
2800 unsigned TmpReg = makeAnotherReg(Op0->getType());
Chris Lattner3ffdff62004-10-06 04:19:43 +00002801 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2802 .addReg(Op0Reg).addImm(Log-1);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002803 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2804 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2805 .addReg(TmpReg).addImm(32-Log);
2806 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2807 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2808 .addReg(Op0Reg).addReg(TmpReg2);
2809
2810 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2811 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
Chris Lattner3ffdff62004-10-06 04:19:43 +00002812 .addReg(TmpReg3).addImm(Log);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002813 if (isNeg)
2814 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2815 return;
2816 }
Chris Lattner2483f672004-10-06 05:01:07 +00002817 } else { // X % C
2818 assert(Class != cLong && "This doesn't handle 64-bit remainder!");
2819 int V = CI->getValue();
2820
2821 if (V == 2 || V == -2) { // X % 2, X % -2
Chris Lattner2483f672004-10-06 05:01:07 +00002822 static const unsigned SExtOpcode[] = { X86::CBW, X86::CWD, X86::CDQ };
2823 static const unsigned BaseReg[] = { X86::AL , X86::AX , X86::EAX };
2824 static const unsigned SExtReg[] = { X86::AH , X86::DX , X86::EDX };
2825 static const unsigned ANDOpcode[] = {
2826 X86::AND8ri, X86::AND16ri, X86::AND32ri
2827 };
2828 static const unsigned XOROpcode[] = {
2829 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr
2830 };
2831 static const unsigned SUBOpcode[] = {
2832 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr
2833 };
2834
2835 // Sign extend result into reg of -1 or 0.
2836 unsigned Op0Reg = getReg(Op0, BB, IP);
2837 BuildMI(*BB, IP, MovOpcode[Class], 1, BaseReg[Class]).addReg(Op0Reg);
2838 BuildMI(*BB, IP, SExtOpcode[Class], 0);
2839 unsigned TmpReg0 = makeAnotherReg(Op0->getType());
2840 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg0).addReg(SExtReg[Class]);
2841
2842 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
2843 BuildMI(*BB, IP, ANDOpcode[Class], 2, TmpReg1).addReg(Op0Reg).addImm(1);
2844
2845 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2846 BuildMI(*BB, IP, XOROpcode[Class], 2,
2847 TmpReg2).addReg(TmpReg1).addReg(TmpReg0);
2848 BuildMI(*BB, IP, SUBOpcode[Class], 2,
2849 ResultReg).addReg(TmpReg2).addReg(TmpReg0);
2850 return;
2851 }
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002852 }
2853
2854 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002855 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002856 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2857
2858 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002859 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2860 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002861 };
2862
Chris Lattnerf01729e2002-11-02 20:54:46 +00002863 unsigned Reg = Regs[Class];
2864 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002865
2866 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002867 unsigned Op0Reg = getReg(Op0, BB, IP);
2868 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002869 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002870
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002871 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002872 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002873 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002874 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002875 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002876
2877 // Emit the appropriate divide or remainder instruction...
2878 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002879 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002880 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002881 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002882
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002883 // Emit the appropriate divide or remainder instruction...
2884 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2885 }
Chris Lattner06925362002-11-17 21:56:38 +00002886
Chris Lattnerf01729e2002-11-02 20:54:46 +00002887 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002888 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002889
Chris Lattnerf01729e2002-11-02 20:54:46 +00002890 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002891 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002892}
Chris Lattnere2954c82002-11-02 20:04:26 +00002893
Chris Lattner06925362002-11-17 21:56:38 +00002894
Brian Gaekea1719c92002-10-31 23:03:59 +00002895/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2896/// for constant immediate shift values, and for constant immediate
2897/// shift values equal to 1. Even the general case is sort of special,
2898/// because the shift amount has to be in CL, not just any old register.
2899///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002900void X86ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002901 MachineBasicBlock::iterator IP = BB->end ();
2902 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2903 I.getOpcode () == Instruction::Shl, I.getType (),
2904 getReg (I));
2905}
2906
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002907/// Emit code for a 'SHLD DestReg, Op0, Op1, Amt' operation, where Amt is a
2908/// constant.
2909void X86ISel::doSHLDConst(MachineBasicBlock *MBB,
2910 MachineBasicBlock::iterator IP,
2911 unsigned DestReg, unsigned Op0Reg, unsigned Op1Reg,
2912 unsigned Amt) {
2913 // SHLD is a very inefficient operation on every processor, try to do
2914 // somethign simpler for common values of 'Amt'.
2915 if (Amt == 0) {
2916 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
2917 } else if (Amt == 1) {
2918 unsigned Tmp = makeAnotherReg(Type::UIntTy);
2919 BuildMI(*MBB, IP, X86::ADD32rr, 2, Tmp).addReg(Op1Reg).addReg(Op1Reg);
2920 BuildMI(*MBB, IP, X86::ADC32rr, 2, DestReg).addReg(Op0Reg).addReg(Op0Reg);
2921 } else if (Amt == 2 || Amt == 3) {
2922 // On the P4 and Athlon it is cheaper to replace shld ..., 2|3 with a
2923 // shift/lea pair. NOTE: This should not be done on the P6 family!
2924 unsigned Tmp = makeAnotherReg(Type::UIntTy);
2925 BuildMI(*MBB, IP, X86::SHR32ri, 2, Tmp).addReg(Op1Reg).addImm(32-Amt);
2926 X86AddressMode AM;
2927 AM.BaseType = X86AddressMode::RegBase;
2928 AM.Base.Reg = Tmp;
2929 AM.Scale = 1 << Amt;
2930 AM.IndexReg = Op0Reg;
2931 AM.Disp = 0;
2932 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 4, DestReg), AM);
2933 } else {
2934 // NOTE: It is always cheaper on the P4 to emit SHLD as two shifts and an OR
2935 // than it is to emit a real SHLD.
2936
2937 BuildMI(*MBB, IP, X86::SHLD32rri8, 3,
2938 DestReg).addReg(Op0Reg).addReg(Op1Reg).addImm(Amt);
2939 }
2940}
2941
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002942/// emitShiftOperation - Common code shared between visitShiftInst and
2943/// constant expression support.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00002944void X86ISel::emitShiftOperation(MachineBasicBlock *MBB,
2945 MachineBasicBlock::iterator IP,
2946 Value *Op, Value *ShiftAmount,
2947 bool isLeftShift, const Type *ResultTy,
2948 unsigned DestReg) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002949 unsigned SrcReg = getReg (Op, MBB, IP);
2950 bool isSigned = ResultTy->isSigned ();
2951 unsigned Class = getClass (ResultTy);
Chris Lattnerde95c9e2004-10-17 06:10:40 +00002952
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002953 static const unsigned ConstantOperand[][3] = {
2954 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri }, // SHR
2955 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri }, // SAR
2956 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri }, // SHL
2957 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002958 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002959
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002960 static const unsigned NonConstantOperand[][3] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002961 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2962 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2963 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2964 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002965 };
Chris Lattner796df732002-11-02 00:44:25 +00002966
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002967 // Longs, as usual, are handled specially.
Chris Lattner3e130a22003-01-13 00:32:26 +00002968 if (Class == cLong) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002969 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002970 unsigned Amount = CUI->getValue();
Chris Lattner62f5a942004-11-13 20:04:38 +00002971 if (Amount == 1 && isLeftShift) { // X << 1 == X+X
Chris Lattner44205ca2004-11-13 20:03:48 +00002972 BuildMI(*MBB, IP, X86::ADD32rr, 2,
2973 DestReg).addReg(SrcReg).addReg(SrcReg);
2974 BuildMI(*MBB, IP, X86::ADC32rr, 2,
2975 DestReg+1).addReg(SrcReg+1).addReg(SrcReg+1);
2976 } else if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002977 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2978 if (isLeftShift) {
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002979 doSHLDConst(MBB, IP, DestReg+1, SrcReg+1, SrcReg, Amount);
Chris Lattneree352852004-02-29 07:22:16 +00002980 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002981 } else {
Chris Lattnerce7cafa2004-11-13 20:48:57 +00002982 BuildMI(*MBB, IP, X86::SHRD32rri8, 3,
2983 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
Chris Lattneree352852004-02-29 07:22:16 +00002984 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002985 }
Chris Lattner36c625d2004-11-15 23:16:34 +00002986 } else if (Amount == 32) {
2987 if (isLeftShift) {
2988 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2989 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2990 } else {
Chris Lattner39a83dc2004-11-16 18:40:52 +00002991 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
Chris Lattner36c625d2004-11-15 23:16:34 +00002992 if (!isSigned) {
2993 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2994 } else {
2995 BuildMI(*MBB, IP, X86::SAR32ri, 2,
2996 DestReg+1).addReg(SrcReg).addImm(31);
2997 }
2998 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002999 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003000 Amount -= 32;
3001 if (isLeftShift) {
Chris Lattner36c625d2004-11-15 23:16:34 +00003002 BuildMI(*MBB, IP, X86::SHL32ri, 2,
3003 DestReg + 1).addReg(SrcReg).addImm(Amount);
Chris Lattner722070e2004-04-06 03:42:38 +00003004 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003005 } else {
Chris Lattner36c625d2004-11-15 23:16:34 +00003006 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
3007 DestReg).addReg(SrcReg+1).addImm(Amount);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003008 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003009 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003010 }
3011 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00003012 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Chris Lattner9171ef52003-06-01 01:56:54 +00003013 if (!isLeftShift && isSigned) {
3014 // If this is a SHR of a Long, then we need to do funny sign extension
3015 // stuff. TmpReg gets the value to use as the high-part if we are
3016 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003017 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00003018 } else {
3019 // Other shifts use a fixed zero value if the shift is more than 32
3020 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003021 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00003022 }
3023
3024 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003025 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003026 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003027
3028 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
3029 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
3030 if (isLeftShift) {
3031 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003032 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00003033 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003034 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003035 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003036
3037 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003038 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00003039
3040 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003041 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003042 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
3043 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003044 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003045 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00003046 } else {
3047 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003048 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00003049 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00003050 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003051 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00003052 .addReg(SrcReg+1);
3053
3054 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003055 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00003056
3057 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003058 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003059 DestReg).addReg(TmpReg2).addReg(TmpReg3);
3060
3061 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003062 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00003063 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
3064 }
Brian Gaekea1719c92002-10-31 23:03:59 +00003065 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003066 return;
3067 }
Chris Lattnere9913f22002-11-02 01:41:55 +00003068
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003069 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003070 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
3071 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003072
Chris Lattner44205ca2004-11-13 20:03:48 +00003073 if (CUI->getValue() == 1 && isLeftShift) { // X << 1 -> X+X
3074 static const int AddOpC[] = { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
3075 BuildMI(*MBB, IP, AddOpC[Class], 2,DestReg).addReg(SrcReg).addReg(SrcReg);
3076 } else {
3077 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
3078 BuildMI(*MBB, IP, Opc[Class], 2,
3079 DestReg).addReg(SrcReg).addImm(CUI->getValue());
3080 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003081 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00003082 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003083 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003084
Chris Lattner3e130a22003-01-13 00:32:26 +00003085 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00003086 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003087 }
3088}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00003089
Chris Lattner3e130a22003-01-13 00:32:26 +00003090
Chris Lattner6fc3c522002-11-17 21:11:55 +00003091/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00003092/// instruction. The load and store instructions are the only place where we
3093/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00003094///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003095void X86ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003096 // Check to see if this load instruction is going to be folded into a binary
3097 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
3098 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00003099 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003100 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00003101 Instruction *User = cast<Instruction>(I.use_back());
3102 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003103 case Instruction::Cast:
3104 // If this is a cast from a signed-integer type to a floating point type,
3105 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00003106 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003107 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
3108 I.getType() == Type::LongTy)) {
3109 unsigned DestReg = getReg(User);
3110 static const unsigned Opcode[] = {
3111 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
3112 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00003113
3114 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3115 unsigned FI = getFixedSizedAllocaFI(AI);
3116 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
3117 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003118 X86AddressMode AM;
3119 getAddressingMode(I.getOperand(0), AM);
3120 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003121 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003122 return;
3123 } else {
3124 User = 0;
3125 }
3126 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00003127
Chris Lattner7dee5da2004-03-08 01:58:35 +00003128 case Instruction::Add:
3129 case Instruction::Sub:
3130 case Instruction::And:
3131 case Instruction::Or:
3132 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003133 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003134 break;
Chris Lattner95157f72004-04-11 22:05:45 +00003135 case Instruction::Mul:
3136 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00003137 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003138 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00003139 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003140 }
3141
3142 if (User) {
3143 // Okay, we found a user. If the load is the first operand and there is
3144 // no second operand load, reverse the operand ordering. Note that this
3145 // can fail for a subtract (ie, no change will be made).
Chris Lattner3dbb5042004-07-21 21:28:26 +00003146 bool Swapped = false;
Chris Lattner7dee5da2004-03-08 01:58:35 +00003147 if (!isa<LoadInst>(User->getOperand(1)))
Chris Lattner3dbb5042004-07-21 21:28:26 +00003148 Swapped = !cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003149
3150 // Okay, now that everything is set up, if this load is used by the second
3151 // operand, and if there are no instructions that invalidate the load
3152 // before the binary operator, eliminate the load.
3153 if (User->getOperand(1) == &I &&
3154 isSafeToFoldLoadIntoInstruction(I, *User))
3155 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00003156
3157 // If this is a floating point sub or div, we won't be able to swap the
3158 // operands, but we will still be able to eliminate the load.
3159 if (Class == cFP && User->getOperand(0) == &I &&
3160 !isa<LoadInst>(User->getOperand(1)) &&
3161 (User->getOpcode() == Instruction::Sub ||
3162 User->getOpcode() == Instruction::Div) &&
3163 isSafeToFoldLoadIntoInstruction(I, *User))
3164 return; // Eliminate the load!
Chris Lattner3dbb5042004-07-21 21:28:26 +00003165
3166 // If we swapped the operands to the instruction, but couldn't fold the
3167 // load anyway, swap them back. We don't want to break add X, int
3168 // folding.
3169 if (Swapped) cast<BinaryOperator>(User)->swapOperands();
Chris Lattner7dee5da2004-03-08 01:58:35 +00003170 }
3171 }
3172
Chris Lattner6ac1d712003-10-20 04:48:06 +00003173 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003174 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00003175 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00003176 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003177 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003178
3179 unsigned DestReg = getReg(I);
3180
3181 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
3182 unsigned FI = getFixedSizedAllocaFI(AI);
3183 if (Class == cLong) {
3184 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
3185 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
3186 } else {
3187 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
3188 }
3189 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003190 X86AddressMode AM;
3191 getAddressingMode(I.getOperand(0), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003192
3193 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003194 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg), AM);
3195 AM.Disp += 4;
3196 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003197 } else {
Reid Spencerfc989e12004-08-30 00:13:26 +00003198 addFullAddress(BuildMI(BB, Opcode, 4, DestReg), AM);
Chris Lattner9f1b5312004-05-13 15:12:43 +00003199 }
3200 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003201}
3202
Chris Lattner6fc3c522002-11-17 21:11:55 +00003203/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
3204/// instruction.
3205///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003206void X86ISel::visitStoreInst(StoreInst &I) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003207 X86AddressMode AM;
3208 getAddressingMode(I.getOperand(1), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003209
Chris Lattner6c09db22003-10-20 04:11:23 +00003210 const Type *ValTy = I.getOperand(0)->getType();
3211 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003212
Chris Lattner5a830962004-02-25 02:56:58 +00003213 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3214 uint64_t Val = CI->getRawValue();
3215 if (Class == cLong) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003216 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val & ~0U);
3217 AM.Disp += 4;
3218 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00003219 } else {
3220 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003221 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003222 };
3223 unsigned Opcode = Opcodes[Class];
Reid Spencerfc989e12004-08-30 00:13:26 +00003224 addFullAddress(BuildMI(BB, Opcode, 5), AM).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003225 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003226 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
Chris Lattner358a9022004-10-15 05:05:29 +00003227 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(0);
Chris Lattner5a830962004-02-25 02:56:58 +00003228 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003229 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003230 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3231 // Store constant FP values with integer instructions to avoid having to
3232 // load the constants from the constant pool then do a store.
3233 if (CFP->getType() == Type::FloatTy) {
3234 union {
3235 unsigned I;
3236 float F;
3237 } V;
3238 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003239 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003240 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003241 union {
3242 uint64_t I;
3243 double F;
3244 } V;
3245 V.F = CFP->getValue();
Reid Spencerfc989e12004-08-30 00:13:26 +00003246 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm((unsigned)V.I);
3247 AM.Disp += 4;
3248 addFullAddress(BuildMI(BB, X86::MOV32mi, 5), AM).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003249 unsigned(V.I >> 32));
Chris Lattner5a830962004-02-25 02:56:58 +00003250 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003251
3252 } else if (Class == cLong) {
3253 unsigned ValReg = getReg(I.getOperand(0));
Reid Spencerfc989e12004-08-30 00:13:26 +00003254 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg);
3255 AM.Disp += 4;
3256 addFullAddress(BuildMI(BB, X86::MOV32mr, 5), AM).addReg(ValReg+1);
Chris Lattnere7a31c92004-05-07 21:18:15 +00003257 } else {
Chris Lattner358a9022004-10-15 05:05:29 +00003258 // FIXME: stop emitting these two instructions:
3259 // movl $global,%eax
3260 // movl %eax,(%ebx)
3261 // when one instruction will suffice. That includes when the global
3262 // has an offset applied to it.
Chris Lattnere7a31c92004-05-07 21:18:15 +00003263 unsigned ValReg = getReg(I.getOperand(0));
3264 static const unsigned Opcodes[] = {
3265 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3266 };
3267 unsigned Opcode = Opcodes[Class];
3268 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003269
Reid Spencerfc989e12004-08-30 00:13:26 +00003270 addFullAddress(BuildMI(BB, Opcode, 1+4), AM).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003271 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003272}
3273
3274
Misha Brukman538607f2004-03-01 23:53:11 +00003275/// visitCastInst - Here we have various kinds of copying with or without sign
3276/// extension going on.
3277///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003278void X86ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003279 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003280
Chris Lattner99382862004-04-12 00:23:04 +00003281 unsigned SrcClass = getClassB(Op->getType());
3282 unsigned DestClass = getClassB(CI.getType());
3283 // Noop casts are not emitted: getReg will return the source operand as the
3284 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003285 if (DestClass == SrcClass) {
3286 // The only detail in this plan is that casts from double -> float are
3287 // truncating operations that we have to codegen through memory (despite
3288 // the fact that the source/dest registers are the same class).
3289 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3290 return;
3291 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003292
Chris Lattnerf5854472003-06-21 16:01:24 +00003293 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3294 // of the case are GEP instructions, then the cast does not need to be
3295 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003296 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003297 bool AllUsesAreGEPs = true;
3298 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3299 if (!isa<GetElementPtrInst>(*I)) {
3300 AllUsesAreGEPs = false;
3301 break;
3302 }
3303
3304 // No need to codegen this cast if all users are getelementptr instrs...
3305 if (AllUsesAreGEPs) return;
3306 }
3307
Chris Lattner99382862004-04-12 00:23:04 +00003308 // If this cast converts a load from a short,int, or long integer to a FP
3309 // value, we will have folded this cast away.
3310 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3311 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3312 Op->getType() == Type::LongTy))
3313 return;
3314
3315
Chris Lattner548f61d2003-04-23 17:22:12 +00003316 unsigned DestReg = getReg(CI);
3317 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003318 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003319}
3320
Misha Brukman538607f2004-03-01 23:53:11 +00003321/// emitCastOperation - Common code shared between visitCastInst and constant
3322/// expression cast support.
3323///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003324void X86ISel::emitCastOperation(MachineBasicBlock *BB,
3325 MachineBasicBlock::iterator IP,
3326 Value *Src, const Type *DestTy,
3327 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003328 const Type *SrcTy = Src->getType();
3329 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003330 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003331 unsigned SrcReg = getReg(Src, BB, IP);
3332
Chris Lattner3e130a22003-01-13 00:32:26 +00003333 // Implement casts to bool by using compare on the operand followed by set if
3334 // not zero on the result.
3335 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003336 switch (SrcClass) {
3337 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003338 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003339 break;
3340 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003341 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003342 break;
3343 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003344 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003345 break;
3346 case cLong: {
3347 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003348 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003349 break;
3350 }
3351 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003352 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003353 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003354 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003355 break;
Chris Lattner20772542003-06-01 03:38:24 +00003356 }
3357
3358 // If the zero flag is not set, then the value is true, set the byte to
3359 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003360 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003361 return;
3362 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003363
3364 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003365 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003366 };
3367
3368 // Implement casts between values of the same type class (as determined by
3369 // getClass) by using a register-to-register move.
3370 if (SrcClass == DestClass) {
3371 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003372 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003373 } else if (SrcClass == cFP) {
3374 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003375 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003376 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003377 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003378 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3379 "Unknown cFP member!");
3380 // Truncate from double to float by storing to memory as short, then
3381 // reading it back.
3382 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003383 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003384 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3385 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003386 }
3387 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003388 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3389 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003390 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003391 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003392 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003393 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003394 return;
3395 }
3396
3397 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3398 // or zero extension, depending on whether the source type was signed.
3399 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3400 SrcClass < DestClass) {
3401 bool isLong = DestClass == cLong;
3402 if (isLong) DestClass = cInt;
3403
3404 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003405 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3406 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003407 };
3408
Chris Lattner96e3b422004-05-09 22:28:45 +00003409 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003410 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003411 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003412
3413 if (isLong) { // Handle upper 32 bits as appropriate...
3414 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003415 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003416 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003417 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003418 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003419 return;
3420 }
3421
3422 // Special case long -> int ...
3423 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003424 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003425 return;
3426 }
3427
3428 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3429 // move out of AX or AL.
3430 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3431 && SrcClass > DestClass) {
3432 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003433 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3434 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003435 return;
3436 }
3437
3438 // Handle casts from integer to floating point now...
3439 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003440 // Promote the integer to a type supported by FLD. We do this because there
3441 // are no unsigned FLD instructions, so we must promote an unsigned value to
3442 // a larger signed value, then use FLD on the larger value.
3443 //
3444 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003445 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003446 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003447 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003448 case Type::BoolTyID:
3449 case Type::SByteTyID:
3450 // We don't have the facilities for directly loading byte sized data from
3451 // memory (even signed). Promote it to 16 bits.
3452 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003453 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003454 break;
3455 case Type::UByteTyID:
3456 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003457 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003458 break;
3459 case Type::UShortTyID:
3460 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003461 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003462 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003463 case Type::ULongTyID:
Chris Lattner56a31c62004-10-17 08:01:28 +00003464 case Type::UIntTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003465 // Don't fild into the read destination.
3466 DestReg = makeAnotherReg(Type::DoubleTy);
3467 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003468 default: // No promotion needed...
3469 break;
3470 }
3471
3472 if (PromoteType) {
3473 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003474 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003475 SrcTy = PromoteType;
3476 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003477 SrcReg = TmpReg;
3478 }
3479
3480 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003481 int FrameIdx =
3482 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003483
3484 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003485 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003486 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003487 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003488 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003489 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003490 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003491 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3492 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003493 }
3494
3495 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003496 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003497 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003498
Chris Lattner56a31c62004-10-17 08:01:28 +00003499 if (SrcTy == Type::UIntTy) {
3500 // If this is a cast from uint -> double, we need to be careful about if
3501 // the "sign" bit is set. If so, we don't want to make a negative number,
3502 // we want to make a positive number. Emit code to add an offset if the
3503 // sign bit is set.
3504
3505 // Compute whether the sign bit is set by shifting the reg right 31 bits.
3506 unsigned IsNeg = makeAnotherReg(Type::IntTy);
3507 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(SrcReg).addImm(31);
3508
3509 // Create a CP value that has the offset in one word and 0 in the other.
3510 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
3511 0x4f80000000000000ULL);
3512 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
3513 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(DestReg)
3514 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
3515
3516 } else if (SrcTy == Type::ULongTy) {
3517 // We need special handling for unsigned 64-bit integer sources. If the
3518 // input number has the "sign bit" set, then we loaded it incorrectly as a
3519 // negative 64-bit number. In this case, add an offset value.
3520
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003521 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003522 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003523
Chris Lattnerb6bac512004-02-25 06:13:04 +00003524 // If the sign bit is set, get a pointer to an offset, otherwise get a
3525 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003526 MachineConstantPool *CP = F->getConstantPool();
3527 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003528 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003529 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003530 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003531 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003532 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3533
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003534 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003535 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003536 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003537 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003538
3539 // Load the constant for an add. FIXME: this could make an 'fadd' that
3540 // reads directly from memory, but we don't support these yet.
3541 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003542 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003543
Chris Lattneree352852004-02-29 07:22:16 +00003544 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3545 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003546 }
3547
Chris Lattner3e130a22003-01-13 00:32:26 +00003548 return;
3549 }
3550
3551 // Handle casts from floating point to integer now...
3552 if (SrcClass == cFP) {
3553 // Change the floating point control register to use "round towards zero"
3554 // mode when truncating to an integer value.
3555 //
3556 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003557 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003558
3559 // Load the old value of the high byte of the control word...
3560 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003561 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003562 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003563
3564 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003565 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003566 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003567
3568 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003569 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003570
3571 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003572 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003573 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003574
3575 // We don't have the facilities for directly storing byte sized data to
3576 // memory. Promote it to 16 bits. We also must promote unsigned values to
3577 // larger classes because we only have signed FP stores.
3578 unsigned StoreClass = DestClass;
3579 const Type *StoreTy = DestTy;
3580 if (StoreClass == cByte || DestTy->isUnsigned())
3581 switch (StoreClass) {
3582 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3583 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3584 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003585 // The following treatment of cLong may not be perfectly right,
3586 // but it survives chains of casts of the form
3587 // double->ulong->double.
3588 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003589 default: assert(0 && "Unknown store class!");
3590 }
3591
3592 // Spill the integer to memory and reload it from there...
3593 int FrameIdx =
3594 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3595
3596 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003597 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003598 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3599 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003600
3601 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003602 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3603 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003604 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003605 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003606 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003607 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003608 }
3609
3610 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003611 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003612 return;
3613 }
3614
Brian Gaeked474e9c2002-12-06 10:49:33 +00003615 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003616 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003617 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003618}
Brian Gaekea1719c92002-10-31 23:03:59 +00003619
Chris Lattner73815062003-10-18 05:56:40 +00003620/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003621///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003622void X86ISel::visitVANextInst(VANextInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003623 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003624 unsigned DestReg = getReg(I);
3625
Chris Lattnereca195e2003-05-08 19:44:13 +00003626 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003627 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003628 default:
3629 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003630 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003631 return;
3632 case Type::PointerTyID:
3633 case Type::UIntTyID:
3634 case Type::IntTyID:
3635 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003636 break;
3637 case Type::ULongTyID:
3638 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003639 case Type::DoubleTyID:
3640 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003641 break;
3642 }
3643
3644 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003645 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003646}
Chris Lattnereca195e2003-05-08 19:44:13 +00003647
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003648void X86ISel::visitVAArgInst(VAArgInst &I) {
Chris Lattner73815062003-10-18 05:56:40 +00003649 unsigned VAList = getReg(I.getOperand(0));
3650 unsigned DestReg = getReg(I);
3651
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003652 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003653 default:
3654 std::cerr << I;
3655 assert(0 && "Error: bad type for va_next instruction!");
3656 return;
3657 case Type::PointerTyID:
3658 case Type::UIntTyID:
3659 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003660 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003661 break;
3662 case Type::ULongTyID:
3663 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003664 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3665 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003666 break;
3667 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003668 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003669 break;
3670 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003671}
3672
Misha Brukman538607f2004-03-01 23:53:11 +00003673/// visitGetElementPtrInst - instruction-select GEP instructions
3674///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003675void X86ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003676 // If this GEP instruction will be folded into all of its users, we don't need
3677 // to explicitly calculate it!
Reid Spencerfc989e12004-08-30 00:13:26 +00003678 X86AddressMode AM;
3679 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), AM)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003680 // Check all of the users of the instruction to see if they are loads and
3681 // stores.
3682 bool AllWillFold = true;
3683 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3684 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3685 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3686 cast<Instruction>(*UI)->getOperand(0) == &I) {
3687 AllWillFold = false;
3688 break;
3689 }
3690
3691 // If the instruction is foldable, and will be folded into all users, don't
3692 // emit it!
3693 if (AllWillFold) return;
3694 }
3695
Chris Lattner3e130a22003-01-13 00:32:26 +00003696 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003697 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003698 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003699}
3700
Chris Lattner985fe3d2004-02-25 03:45:50 +00003701/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3702/// GEPTypes (the derived types being stepped through at each level). On return
3703/// from this function, if some indexes of the instruction are representable as
3704/// an X86 lea instruction, the machine operands are put into the Ops
3705/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3706/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3707/// addressing mode that only partially consumes the input, the BaseReg input of
3708/// the addressing mode must be left free.
3709///
3710/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3711///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003712void X86ISel::getGEPIndex(MachineBasicBlock *MBB,
3713 MachineBasicBlock::iterator IP,
3714 std::vector<Value*> &GEPOps,
3715 std::vector<const Type*> &GEPTypes,
3716 X86AddressMode &AM) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003717 const TargetData &TD = TM.getTargetData();
3718
Chris Lattner985fe3d2004-02-25 03:45:50 +00003719 // Clear out the state we are working with...
Reid Spencerfc989e12004-08-30 00:13:26 +00003720 AM.BaseType = X86AddressMode::RegBase;
3721 AM.Base.Reg = 0; // No base register
3722 AM.Scale = 1; // Unit scale
3723 AM.IndexReg = 0; // No index register
3724 AM.Disp = 0; // No displacement
Chris Lattnerb6bac512004-02-25 06:13:04 +00003725
Chris Lattner985fe3d2004-02-25 03:45:50 +00003726 // While there are GEP indexes that can be folded into the current address,
3727 // keep processing them.
3728 while (!GEPTypes.empty()) {
3729 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3730 // It's a struct access. CUI is the index into the structure,
3731 // which names the field. This index must have unsigned type.
3732 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3733
3734 // Use the TargetData structure to pick out what the layout of the
3735 // structure is in memory. Since the structure index must be constant, we
3736 // can get its value and use it to find the right byte offset from the
3737 // StructLayout class's list of structure member offsets.
Reid Spencerfc989e12004-08-30 00:13:26 +00003738 AM.Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003739 GEPOps.pop_back(); // Consume a GEP operand
3740 GEPTypes.pop_back();
3741 } else {
3742 // It's an array or pointer access: [ArraySize x ElementType].
3743 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3744 Value *idx = GEPOps.back();
3745
3746 // idx is the index into the array. Unlike with structure
3747 // indices, we may not know its actual value at code-generation
3748 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003749
3750 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003751 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003752 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003753 AM.Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003754 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003755 AM.Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003756 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003757 // If the index reg is already taken, we can't handle this index.
Reid Spencerfc989e12004-08-30 00:13:26 +00003758 if (AM.IndexReg) return;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003759
3760 // If this is a size that we can handle, then add the index as
3761 switch (TypeSize) {
3762 case 1: case 2: case 4: case 8:
3763 // These are all acceptable scales on X86.
Reid Spencerfc989e12004-08-30 00:13:26 +00003764 AM.Scale = TypeSize;
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003765 break;
3766 default:
3767 // Otherwise, we can't handle this scale
3768 return;
3769 }
3770
3771 if (CastInst *CI = dyn_cast<CastInst>(idx))
3772 if (CI->getOperand(0)->getType() == Type::IntTy ||
3773 CI->getOperand(0)->getType() == Type::UIntTy)
3774 idx = CI->getOperand(0);
3775
Reid Spencerfc989e12004-08-30 00:13:26 +00003776 AM.IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003777 }
3778
3779 GEPOps.pop_back(); // Consume a GEP operand
3780 GEPTypes.pop_back();
3781 }
3782 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003783
Chris Lattnerdf040972004-05-23 21:23:12 +00003784 // GEPTypes is empty, which means we have a single operand left. Set it as
3785 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003786 //
Reid Spencerfc989e12004-08-30 00:13:26 +00003787 assert(AM.Base.Reg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003788
Reid Spencerfc989e12004-08-30 00:13:26 +00003789 if (AllocaInst *AI = dyn_castFixedAlloca(GEPOps.back())) {
3790 AM.BaseType = X86AddressMode::FrameIndexBase;
3791 AM.Base.FrameIndex = getFixedSizedAllocaFI(AI);
Chris Lattnerdf040972004-05-23 21:23:12 +00003792 GEPOps.pop_back();
3793 return;
Reid Spencerfc989e12004-08-30 00:13:26 +00003794 }
3795
Chris Lattner358a9022004-10-15 05:05:29 +00003796 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps.back())) {
3797 AM.GV = GV;
3798 GEPOps.pop_back();
3799 return;
Chris Lattnerdf040972004-05-23 21:23:12 +00003800 }
Chris Lattnerdf040972004-05-23 21:23:12 +00003801
Reid Spencerfc989e12004-08-30 00:13:26 +00003802 AM.Base.Reg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003803 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003804}
3805
3806
Chris Lattnerb6bac512004-02-25 06:13:04 +00003807/// isGEPFoldable - Return true if the specified GEP can be completely
3808/// folded into the addressing mode of a load/store or lea instruction.
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003809bool X86ISel::isGEPFoldable(MachineBasicBlock *MBB,
3810 Value *Src, User::op_iterator IdxBegin,
3811 User::op_iterator IdxEnd, X86AddressMode &AM) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003812
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003813 std::vector<Value*> GEPOps;
3814 GEPOps.resize(IdxEnd-IdxBegin+1);
3815 GEPOps[0] = Src;
3816 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3817
Chris Lattnerdf040972004-05-23 21:23:12 +00003818 std::vector<const Type*>
3819 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3820 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003821
Chris Lattnerb6bac512004-02-25 06:13:04 +00003822 MachineBasicBlock::iterator IP;
3823 if (MBB) IP = MBB->end();
Reid Spencerfc989e12004-08-30 00:13:26 +00003824 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003825
3826 // We can fold it away iff the getGEPIndex call eliminated all operands.
3827 return GEPOps.empty();
3828}
3829
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003830void X86ISel::emitGEPOperation(MachineBasicBlock *MBB,
3831 MachineBasicBlock::iterator IP,
3832 Value *Src, User::op_iterator IdxBegin,
3833 User::op_iterator IdxEnd, unsigned TargetReg) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003834 const TargetData &TD = TM.getTargetData();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003835
Chris Lattnerd2995df2004-07-15 00:58:53 +00003836 // If this is a getelementptr null, with all constant integer indices, just
3837 // replace it with TargetReg = 42.
3838 if (isa<ConstantPointerNull>(Src)) {
3839 User::op_iterator I = IdxBegin;
3840 for (; I != IdxEnd; ++I)
3841 if (!isa<ConstantInt>(*I))
3842 break;
3843 if (I == IdxEnd) { // All constant indices
3844 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3845 std::vector<Value*>(IdxBegin, IdxEnd));
3846 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3847 return;
3848 }
3849 }
3850
Chris Lattnerb6bac512004-02-25 06:13:04 +00003851 std::vector<Value*> GEPOps;
3852 GEPOps.resize(IdxEnd-IdxBegin+1);
3853 GEPOps[0] = Src;
3854 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3855
3856 std::vector<const Type*> GEPTypes;
3857 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3858 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003859
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003860 // Keep emitting instructions until we consume the entire GEP instruction.
3861 while (!GEPOps.empty()) {
3862 unsigned OldSize = GEPOps.size();
Reid Spencerfc989e12004-08-30 00:13:26 +00003863 X86AddressMode AM;
3864 getGEPIndex(MBB, IP, GEPOps, GEPTypes, AM);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003865
Chris Lattner985fe3d2004-02-25 03:45:50 +00003866 if (GEPOps.size() != OldSize) {
3867 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003868 unsigned NextTarget = 0;
3869 if (!GEPOps.empty()) {
Reid Spencerfc989e12004-08-30 00:13:26 +00003870 assert(AM.Base.Reg == 0 &&
Chris Lattnerb6bac512004-02-25 06:13:04 +00003871 "getGEPIndex should have left the base register open for chaining!");
Reid Spencerfc989e12004-08-30 00:13:26 +00003872 NextTarget = AM.Base.Reg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003873 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003874
Reid Spencerfc989e12004-08-30 00:13:26 +00003875 if (AM.BaseType == X86AddressMode::RegBase &&
Chris Lattner358a9022004-10-15 05:05:29 +00003876 AM.IndexReg == 0 && AM.Disp == 0 && !AM.GV)
Reid Spencerfc989e12004-08-30 00:13:26 +00003877 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(AM.Base.Reg);
Chris Lattner358a9022004-10-15 05:05:29 +00003878 else if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0 &&
3879 AM.IndexReg == 0 && AM.Disp == 0)
3880 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(AM.GV);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003881 else
Reid Spencerfc989e12004-08-30 00:13:26 +00003882 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg), AM);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003883 --IP;
3884 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003885 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003886 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3887 // all operands are consumed but the base pointer. If so, just load it
3888 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003889 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003890 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003891 } else {
3892 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003893 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003894 }
3895 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003896
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003897 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003898 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003899 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3900 Value *idx = GEPOps.back();
3901 GEPOps.pop_back(); // Consume a GEP operand
3902 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003903
Chris Lattner28977af2004-04-05 01:30:19 +00003904 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003905 // operand on X86. Handle this case directly now...
3906 if (CastInst *CI = dyn_cast<CastInst>(idx))
3907 if (CI->getOperand(0)->getType() == Type::IntTy ||
3908 CI->getOperand(0)->getType() == Type::UIntTy)
3909 idx = CI->getOperand(0);
3910
Chris Lattner3e130a22003-01-13 00:32:26 +00003911 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003912 // must find the size of the pointed-to type (Not coincidentally, the next
3913 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003914 const Type *ElTy = SqTy->getElementType();
3915 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003916
3917 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003918 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003919 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003920 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003921 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003922 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003923 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003924 --IP; // Insert the next instruction before this one.
3925 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003926 }
3927 } else if (elementSize == 1) {
3928 // If the element size is 1, we don't have to multiply, just add
3929 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003930 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003931 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003932 --IP; // Insert the next instruction before this one.
3933 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003934 } else {
3935 unsigned idxReg = getReg(idx, MBB, IP);
3936 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003937
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003938 // Make sure we can back the iterator up to point to the first
3939 // instruction emitted.
3940 MachineBasicBlock::iterator BeforeIt = IP;
3941 if (IP == MBB->begin())
3942 BeforeIt = MBB->end();
3943 else
3944 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003945 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3946
Chris Lattner8a307e82002-12-16 19:32:50 +00003947 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003948 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003949 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003950 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003951
3952 // Step to the first instruction of the multiply.
3953 if (BeforeIt == MBB->end())
3954 IP = MBB->begin();
3955 else
3956 IP = ++BeforeIt;
3957
3958 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003959 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003960 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003961 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003962}
3963
Chris Lattner065faeb2002-12-28 20:24:02 +00003964/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3965/// frame manager, otherwise do it the hard way.
3966///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00003967void X86ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003968 // If this is a fixed size alloca in the entry block for the function, we
3969 // statically stack allocate the space, so we don't need to do anything here.
3970 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003971 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003972
Brian Gaekee48ec012002-12-13 06:46:31 +00003973 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003974 const Type *Ty = I.getAllocatedType();
3975 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3976
Chris Lattner065faeb2002-12-28 20:24:02 +00003977 // Create a register to hold the temporary result of multiplying the type size
3978 // constant by the variable amount.
3979 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3980 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003981
3982 // TotalSizeReg = mul <numelements>, <TypeSize>
3983 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003984 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003985
3986 // AddedSize = add <TotalSizeReg>, 15
3987 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003988 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003989
3990 // AlignedSize = and <AddedSize>, ~15
3991 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003992 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003993
Brian Gaekee48ec012002-12-13 06:46:31 +00003994 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003995 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003996
Brian Gaekee48ec012002-12-13 06:46:31 +00003997 // Put a pointer to the space into the result register, by copying
3998 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003999 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00004000
Misha Brukman48196b32003-05-03 02:18:17 +00004001 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00004002 // object.
4003 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00004004}
Chris Lattner3e130a22003-01-13 00:32:26 +00004005
4006/// visitMallocInst - Malloc instructions are code generated into direct calls
4007/// to the library malloc.
4008///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004009void X86ISel::visitMallocInst(MallocInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004010 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
4011 unsigned Arg;
4012
4013 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
4014 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
4015 } else {
4016 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00004017 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00004018 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00004019 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00004020 }
4021
4022 std::vector<ValueRecord> Args;
4023 Args.push_back(ValueRecord(Arg, Type::UIntTy));
4024 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00004025 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00004026 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
4027}
4028
4029
4030/// visitFreeInst - Free instructions are code gen'd to call the free libc
4031/// function.
4032///
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004033void X86ISel::visitFreeInst(FreeInst &I) {
Chris Lattner3e130a22003-01-13 00:32:26 +00004034 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00004035 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00004036 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00004037 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00004038 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
4039}
4040
Chris Lattnerd281de22003-07-26 23:49:58 +00004041/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00004042/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00004043/// generated code sucks but the implementation is nice and simple.
4044///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00004045FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
Misha Brukmaneae1bf12004-09-21 18:21:21 +00004046 return new X86ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00004047}