blob: 68a602b31435d9a81bb8c26accf193632d536289 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3e130a22003-01-13 00:32:26 +000010// This file defines a simple peephole instruction selector for the x86 target
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "X86InstrBuilder.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000016#include "X86InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Function.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000020#include "llvm/Instructions.h"
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"
Chris Lattner986618e2004-02-22 19:47:26 +000031#include "Support/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 {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 struct ISel : public FunctionPass, InstVisitor<ISel> {
78 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
Chris Lattnerf70e0c22003-12-28 21:23:38 +000093 ISel(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 Lattner3e130a22003-01-13 00:32:26 +0000178
179 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000180 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000181 unsigned Reg;
182 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000183 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
184 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000185 };
186 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000187 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000188 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000189 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000190
191 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000192 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000193 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
194 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerca9671d2002-11-02 20:28:58 +0000195 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000196
Chris Lattnerf01729e2002-11-02 20:54:46 +0000197 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
198 void visitRem(BinaryOperator &B) { visitDivRem(B); }
199 void visitDivRem(BinaryOperator &B);
200
Chris Lattnere2954c82002-11-02 20:04:26 +0000201 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000202 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
203 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
204 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000205
Chris Lattner6d40c192003-01-16 16:43:00 +0000206 // Comparison operators...
207 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000208 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
209 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000210 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000211 void visitSelectInst(SelectInst &SI);
212
Chris Lattnerb2acc512003-10-19 21:09:10 +0000213
Chris Lattner6fc3c522002-11-17 21:11:55 +0000214 // Memory Instructions
215 void visitLoadInst(LoadInst &I);
216 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000217 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000218 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000219 void visitMallocInst(MallocInst &I);
220 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000221
Chris Lattnere2954c82002-11-02 20:04:26 +0000222 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000223 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000224 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000225 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000226 void visitVANextInst(VANextInst &I);
227 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000228
229 void visitInstruction(Instruction &I) {
230 std::cerr << "Cannot instruction select: " << I;
231 abort();
232 }
233
Brian Gaeke95780cc2002-12-13 07:56:18 +0000234 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000235 ///
236 void promote32(unsigned targetReg, const ValueRecord &VR);
237
Chris Lattner721d2d42004-03-08 01:18:36 +0000238 /// getAddressingMode - Get the addressing mode to use to address the
239 /// specified value. The returned value should be used with addFullAddress.
240 void getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
241 unsigned &IndexReg, unsigned &Disp);
242
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,
248 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
249 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
250
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,
255 User::op_iterator IdxEnd, unsigned &BaseReg,
256 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
257
Chris Lattner3e130a22003-01-13 00:32:26 +0000258 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
259 /// constant expression GEP support.
260 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000261 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000262 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000263 User::op_iterator IdxEnd, unsigned TargetReg);
264
Chris Lattner548f61d2003-04-23 17:22:12 +0000265 /// emitCastOperation - Common code shared between visitCastInst and
266 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000267 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000268 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000269 Value *Src, const Type *DestTy, unsigned TargetReg);
270
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000271 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
272 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000273 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000274 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000275 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000276 Value *Op0, Value *Op1,
277 unsigned OperatorClass, unsigned TargetReg);
278
Chris Lattner6621ed92004-04-11 21:23:56 +0000279 /// emitBinaryFPOperation - This method handles emission of floating point
280 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
281 void emitBinaryFPOperation(MachineBasicBlock *BB,
282 MachineBasicBlock::iterator IP,
283 Value *Op0, Value *Op1,
284 unsigned OperatorClass, unsigned TargetReg);
285
Chris Lattner462fa822004-04-11 20:56:28 +0000286 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
287 Value *Op0, Value *Op1, unsigned TargetReg);
288
289 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
290 unsigned DestReg, const Type *DestTy,
291 unsigned Op0Reg, unsigned Op1Reg);
292 void doMultiplyConst(MachineBasicBlock *MBB,
293 MachineBasicBlock::iterator MBBI,
294 unsigned DestReg, const Type *DestTy,
295 unsigned Op0Reg, unsigned Op1Val);
296
Chris Lattnercadff442003-10-23 17:21:43 +0000297 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000298 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +0000299 Value *Op0, Value *Op1, bool isDiv,
300 unsigned TargetReg);
Chris Lattnercadff442003-10-23 17:21:43 +0000301
Chris Lattner58c41fe2003-08-24 19:19:47 +0000302 /// emitSetCCOperation - Common code shared between visitSetCondInst and
303 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000304 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000305 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000306 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000307 Value *Op0, Value *Op1, unsigned Opcode,
308 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000309
310 /// emitShiftOperation - Common code shared between visitShiftInst and
311 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000312 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000313 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000314 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000315 Value *Op, Value *ShiftAmount, bool isLeftShift,
316 const Type *ResultTy, unsigned DestReg);
317
Chris Lattner12d96a02004-03-30 21:22:00 +0000318 /// emitSelectOperation - Common code shared between visitSelectInst and the
319 /// constant expression support.
320 void emitSelectOperation(MachineBasicBlock *MBB,
321 MachineBasicBlock::iterator IP,
322 Value *Cond, Value *TrueVal, Value *FalseVal,
323 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000324
Chris Lattnerc5291f52002-10-27 21:16:59 +0000325 /// copyConstantToRegister - Output the instructions required to put the
326 /// specified constant into the specified register.
327 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000328 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000329 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000330 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000331
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000332 void emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
333 unsigned LHS, unsigned RHS);
334
Chris Lattner3e130a22003-01-13 00:32:26 +0000335 /// makeAnotherReg - This method returns the next register number we haven't
336 /// yet used.
337 ///
338 /// Long values are handled somewhat specially. They are always allocated
339 /// as pairs of 32 bit integer values. The register number returned is the
340 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
341 /// of the long value.
342 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000343 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000344 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
345 "Current target doesn't have X86 reg info??");
346 const X86RegisterInfo *MRI =
347 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000348 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000349 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
350 // Create the lower part
351 F->getSSARegMap()->createVirtualRegister(RC);
352 // Create the upper part.
353 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000354 }
355
Chris Lattnerc0812d82002-12-13 06:56:29 +0000356 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000357 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000358 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000359 }
360
Chris Lattnercb2fd552004-05-13 07:40:27 +0000361 /// getReg - This method turns an LLVM value into a register number.
Chris Lattner72614082002-10-25 22:55:53 +0000362 ///
363 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000364 unsigned getReg(Value *V) {
365 // Just append to the end of the current bb.
366 MachineBasicBlock::iterator It = BB->end();
367 return getReg(V, BB, It);
368 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000369 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnercb2fd552004-05-13 07:40:27 +0000370 MachineBasicBlock::iterator IPt);
Chris Lattner427aeb42004-04-11 19:21:59 +0000371
Chris Lattnercb2fd552004-05-13 07:40:27 +0000372 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
373 /// that is to be statically allocated with the initial stack frame
374 /// adjustment.
375 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
Chris Lattner72614082002-10-25 22:55:53 +0000376 };
377}
378
Chris Lattnercb2fd552004-05-13 07:40:27 +0000379/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
380/// instruction in the entry block, return it. Otherwise, return a null
381/// pointer.
382static AllocaInst *dyn_castFixedAlloca(Value *V) {
383 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
384 BasicBlock *BB = AI->getParent();
385 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
386 return AI;
387 }
388 return 0;
389}
390
391/// getReg - This method turns an LLVM value into a register number.
392///
393unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB,
394 MachineBasicBlock::iterator IPt) {
395 // If this operand is a constant, emit the code to copy the constant into
396 // the register here...
397 //
398 if (Constant *C = dyn_cast<Constant>(V)) {
399 unsigned Reg = makeAnotherReg(V->getType());
400 copyConstantToRegister(MBB, IPt, C, Reg);
401 return Reg;
402 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
403 unsigned Reg = makeAnotherReg(V->getType());
404 // Move the address of the global into the register
405 BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV);
406 return Reg;
407 } 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.
434unsigned ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
435 // 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///
Chris Lattner8a307e82002-12-16 19:32:50 +0000455void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000456 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000457 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000458 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000459 unsigned Class = 0;
460 switch (CE->getOpcode()) {
461 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000462 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000463 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000464 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000465 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000466 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000467 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000468
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000469 case Instruction::Xor: ++Class; // FALL THROUGH
470 case Instruction::Or: ++Class; // FALL THROUGH
471 case Instruction::And: ++Class; // FALL THROUGH
472 case Instruction::Sub: ++Class; // FALL THROUGH
473 case Instruction::Add:
474 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
475 Class, R);
476 return;
477
Chris Lattner462fa822004-04-11 20:56:28 +0000478 case Instruction::Mul:
479 emitMultiply(MBB, IP, CE->getOperand(0), CE->getOperand(1), R);
Chris Lattnercadff442003-10-23 17:21:43 +0000480 return;
Chris Lattner462fa822004-04-11 20:56:28 +0000481
Chris Lattnercadff442003-10-23 17:21:43 +0000482 case Instruction::Div:
Chris Lattner462fa822004-04-11 20:56:28 +0000483 case Instruction::Rem:
484 emitDivRemOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
485 CE->getOpcode() == Instruction::Div, R);
Chris Lattnercadff442003-10-23 17:21:43 +0000486 return;
Chris Lattnercadff442003-10-23 17:21:43 +0000487
Chris Lattner58c41fe2003-08-24 19:19:47 +0000488 case Instruction::SetNE:
489 case Instruction::SetEQ:
490 case Instruction::SetLT:
491 case Instruction::SetGT:
492 case Instruction::SetLE:
493 case Instruction::SetGE:
494 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
495 CE->getOpcode(), R);
496 return;
497
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000498 case Instruction::Shl:
499 case Instruction::Shr:
500 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000501 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
502 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000503
Chris Lattner12d96a02004-03-30 21:22:00 +0000504 case Instruction::Select:
505 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
506 CE->getOperand(2), R);
507 return;
508
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000509 default:
510 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000511 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000512 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000513 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000514
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000515 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000516 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000517
518 if (Class == cLong) {
519 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000520 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000521 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
522 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000523 return;
524 }
525
Chris Lattner94af4142002-12-25 05:13:53 +0000526 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000527
528 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000529 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000530 };
531
Chris Lattner6b993cc2002-12-15 08:02:15 +0000532 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000533 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000534 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000535 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000536 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000537 }
Chris Lattner94af4142002-12-25 05:13:53 +0000538 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000539 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000540 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000541 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000542 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000543 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000544 // Otherwise we need to spill the constant to memory...
545 MachineConstantPool *CP = F->getConstantPool();
546 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000547 const Type *Ty = CFP->getType();
548
549 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000550 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000551 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000552 }
553
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000554 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000555 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000556 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000557 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000558 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000559 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000560 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000561 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000562 }
563}
564
Chris Lattner065faeb2002-12-28 20:24:02 +0000565/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
566/// the stack into virtual registers.
567///
568void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
569 // Emit instructions to load the arguments... On entry to a function on the
570 // X86, the stack frame looks like this:
571 //
572 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000573 // [ESP + 4] -- first argument (leftmost lexically)
574 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000575 // ...
576 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000577 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000578 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000579
580 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
Chris Lattner427aeb42004-04-11 19:21:59 +0000581 bool ArgLive = !I->use_empty();
582 unsigned Reg = ArgLive ? getReg(*I) : 0;
Chris Lattner065faeb2002-12-28 20:24:02 +0000583 int FI; // Frame object index
Chris Lattner427aeb42004-04-11 19:21:59 +0000584
Chris Lattner065faeb2002-12-28 20:24:02 +0000585 switch (getClassB(I->getType())) {
586 case cByte:
Chris Lattner427aeb42004-04-11 19:21:59 +0000587 if (ArgLive) {
588 FI = MFI->CreateFixedObject(1, ArgOffset);
589 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
590 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000591 break;
592 case cShort:
Chris Lattner427aeb42004-04-11 19:21:59 +0000593 if (ArgLive) {
594 FI = MFI->CreateFixedObject(2, ArgOffset);
595 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
596 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000597 break;
598 case cInt:
Chris Lattner427aeb42004-04-11 19:21:59 +0000599 if (ArgLive) {
600 FI = MFI->CreateFixedObject(4, ArgOffset);
601 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
602 }
Chris Lattner065faeb2002-12-28 20:24:02 +0000603 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000604 case cLong:
Chris Lattner427aeb42004-04-11 19:21:59 +0000605 if (ArgLive) {
606 FI = MFI->CreateFixedObject(8, ArgOffset);
607 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
608 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
609 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000610 ArgOffset += 4; // longs require 4 additional bytes
611 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000612 case cFP:
Chris Lattner427aeb42004-04-11 19:21:59 +0000613 if (ArgLive) {
614 unsigned Opcode;
615 if (I->getType() == Type::FloatTy) {
616 Opcode = X86::FLD32m;
617 FI = MFI->CreateFixedObject(4, ArgOffset);
618 } else {
619 Opcode = X86::FLD64m;
620 FI = MFI->CreateFixedObject(8, ArgOffset);
621 }
622 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000623 }
Chris Lattner427aeb42004-04-11 19:21:59 +0000624 if (I->getType() == Type::DoubleTy)
625 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000626 break;
627 default:
628 assert(0 && "Unhandled argument type!");
629 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000630 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000631 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000632
633 // If the function takes variable number of arguments, add a frame offset for
634 // the start of the first vararg value... this is used to expand
635 // llvm.va_start.
636 if (Fn.getFunctionType()->isVarArg())
637 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000638}
639
640
Chris Lattner333b2fa2002-12-13 10:09:43 +0000641/// SelectPHINodes - Insert machine code to generate phis. This is tricky
642/// because we have to generate our sources into the source basic blocks, not
643/// the current one.
644///
645void ISel::SelectPHINodes() {
Chris Lattnerd029cd22004-06-02 05:55:25 +0000646 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000647 const Function &LF = *F->getFunction(); // The LLVM function...
648 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
649 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000650 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000651
652 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000653 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000654 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000655 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000656
Chris Lattner333b2fa2002-12-13 10:09:43 +0000657 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000658 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000659 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
660 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000661
662 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000663 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
664 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
665 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000666
Chris Lattnera6e73f12003-05-12 14:22:21 +0000667 // PHIValues - Map of blocks to incoming virtual registers. We use this
668 // so that we only initialize one incoming value for a particular block,
669 // even if the block has multiple entries in the PHI node.
670 //
671 std::map<MachineBasicBlock*, unsigned> PHIValues;
672
Chris Lattner333b2fa2002-12-13 10:09:43 +0000673 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
674 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000675 unsigned ValReg;
676 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
677 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000678
Chris Lattnera6e73f12003-05-12 14:22:21 +0000679 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
680 // We already inserted an initialization of the register for this
681 // predecessor. Recycle it.
682 ValReg = EntryIt->second;
683
684 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000685 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000686 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000687 Value *Val = PN->getIncomingValue(i);
688
689 // If this is a constant or GlobalValue, we may have to insert code
690 // into the basic block to compute it into a virtual register.
Chris Lattnercb2fd552004-05-13 07:40:27 +0000691 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
692 isa<GlobalValue>(Val)) {
693 // Simple constants get emitted at the end of the basic block,
694 // before any terminator instructions. We "know" that the code to
695 // move a constant into a register will never clobber any flags.
696 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
Chris Lattnera81fc682003-10-19 00:26:11 +0000697 } else {
Chris Lattnercb2fd552004-05-13 07:40:27 +0000698 // Because we don't want to clobber any values which might be in
699 // physical registers with the computation of this constant (which
700 // might be arbitrarily complex if it is a constant expression),
701 // just insert the computation at the top of the basic block.
702 MachineBasicBlock::iterator PI = PredMBB->begin();
703
704 // Skip over any PHI nodes though!
705 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
706 ++PI;
707
708 ValReg = getReg(Val, PredMBB, PI);
Chris Lattnera81fc682003-10-19 00:26:11 +0000709 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000710
711 // Remember that we inserted a value for this PHI for this predecessor
712 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
713 }
714
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000715 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000716 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000717 if (LongPhiMI) {
718 LongPhiMI->addRegOperand(ValReg+1);
719 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
720 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000721 }
Chris Lattner168aa902004-02-29 07:10:16 +0000722
723 // Now that we emitted all of the incoming values for the PHI node, make
724 // sure to reposition the InsertPoint after the PHI that we just added.
725 // This is needed because we might have inserted a constant into this
726 // block, right after the PHI's which is before the old insert point!
727 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
728 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000729 }
730 }
731}
732
Chris Lattner986618e2004-02-22 19:47:26 +0000733/// RequiresFPRegKill - The floating point stackifier pass cannot insert
734/// compensation code on critical edges. As such, it requires that we kill all
735/// FP registers on the exit from any blocks that either ARE critical edges, or
736/// branch to a block that has incoming critical edges.
737///
738/// Note that this kill instruction will eventually be eliminated when
739/// restrictions in the stackifier are relaxed.
740///
Brian Gaeke1afe7732004-04-28 04:45:55 +0000741static bool RequiresFPRegKill(const MachineBasicBlock *MBB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000742#if 0
Brian Gaeke1afe7732004-04-28 04:45:55 +0000743 const BasicBlock *BB = MBB->getBasicBlock ();
Chris Lattner986618e2004-02-22 19:47:26 +0000744 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
745 const BasicBlock *Succ = *SI;
746 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
747 ++PI; // Block have at least one predecessory
748 if (PI != PE) { // If it has exactly one, this isn't crit edge
749 // If this block has more than one predecessor, check all of the
750 // predecessors to see if they have multiple successors. If so, then the
751 // block we are analyzing needs an FPRegKill.
752 for (PI = pred_begin(Succ); PI != PE; ++PI) {
753 const BasicBlock *Pred = *PI;
754 succ_const_iterator SI2 = succ_begin(Pred);
755 ++SI2; // There must be at least one successor of this block.
756 if (SI2 != succ_end(Pred))
757 return true; // Yes, we must insert the kill on this edge.
758 }
759 }
760 }
761 // If we got this far, there is no need to insert the kill instruction.
762 return false;
763#else
764 return true;
765#endif
766}
767
768// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
769// need them. This only occurs due to the floating point stackifier not being
770// aggressive enough to handle arbitrary global stackification.
771//
772// Currently we insert an FP_REG_KILL instruction into each block that uses or
773// defines a floating point virtual register.
774//
775// When the global register allocators (like linear scan) finally update live
776// variable analysis, we can keep floating point values in registers across
777// portions of the CFG that do not involve critical edges. This will be a big
778// win, but we are waiting on the global allocators before we can do this.
779//
780// With a bit of work, the floating point stackifier pass can be enhanced to
781// break critical edges as needed (to make a place to put compensation code),
782// but this will require some infrastructure improvements as well.
783//
784void ISel::InsertFPRegKills() {
785 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000786
787 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000788 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000789 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
790 MachineOperand& MO = I->getOperand(i);
791 if (MO.isRegister() && MO.getReg()) {
792 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000793 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000794 if (RegMap.getRegClass(Reg)->getSize() == 10)
795 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000796 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000797 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000798 // If we haven't found an FP register use or def in this basic block, check
799 // to see if any of our successors has an FP PHI node, which will cause a
800 // copy to be inserted into this block.
Brian Gaeke235aa5e2004-04-28 04:34:16 +0000801 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
802 SE = BB->succ_end(); SI != SE; ++SI) {
803 MachineBasicBlock *SBB = *SI;
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000804 for (MachineBasicBlock::iterator I = SBB->begin();
805 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
806 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
807 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000808 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000809 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000810 continue;
811 UsesFPReg:
812 // Okay, this block uses an FP register. If the block has successors (ie,
813 // it's not an unwind/return), insert the FP_REG_KILL instruction.
Brian Gaeke1afe7732004-04-28 04:45:55 +0000814 if (BB->succ_size () && RequiresFPRegKill(BB)) {
Chris Lattneree352852004-02-29 07:22:16 +0000815 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000816 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000817 }
818 }
819}
820
821
Chris Lattner9f1b5312004-05-13 15:12:43 +0000822void ISel::getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
823 unsigned &IndexReg, unsigned &Disp) {
824 BaseReg = 0; Scale = 1; IndexReg = 0; Disp = 0;
825 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
826 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
827 BaseReg, Scale, IndexReg, Disp))
828 return;
829 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
830 if (CE->getOpcode() == Instruction::GetElementPtr)
831 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
832 BaseReg, Scale, IndexReg, Disp))
833 return;
834 }
835
836 // If it's not foldable, reset addr mode.
837 BaseReg = getReg(Addr);
838 Scale = 1; IndexReg = 0; Disp = 0;
839}
840
Chris Lattner307ecba2004-03-30 22:39:09 +0000841// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
842// it into the conditional branch or select instruction which is the only user
843// of the cc instruction. This is the case if the conditional branch is the
Chris Lattnera6f9fe62004-06-18 00:29:22 +0000844// only user of the setcc. We also don't handle long arguments below, so we
845// reject them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000846//
Chris Lattner307ecba2004-03-30 22:39:09 +0000847static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000848 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000849 if (SCI->hasOneUse()) {
850 Instruction *User = cast<Instruction>(SCI->use_back());
851 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
Chris Lattner48c937e2004-04-06 17:34:50 +0000852 (getClassB(SCI->getOperand(0)->getType()) != cLong ||
853 SCI->getOpcode() == Instruction::SetEQ ||
854 SCI->getOpcode() == Instruction::SetNE))
Chris Lattner6d40c192003-01-16 16:43:00 +0000855 return SCI;
856 }
857 return 0;
858}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000859
Chris Lattner6d40c192003-01-16 16:43:00 +0000860// Return a fixed numbering for setcc instructions which does not depend on the
861// order of the opcodes.
862//
863static unsigned getSetCCNumber(unsigned Opcode) {
864 switch(Opcode) {
865 default: assert(0 && "Unknown setcc instruction!");
866 case Instruction::SetEQ: return 0;
867 case Instruction::SetNE: return 1;
868 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000869 case Instruction::SetGE: return 3;
870 case Instruction::SetGT: return 4;
871 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000872 }
873}
Chris Lattner06925362002-11-17 21:56:38 +0000874
Chris Lattner6d40c192003-01-16 16:43:00 +0000875// LLVM -> X86 signed X86 unsigned
876// ----- ---------- ------------
877// seteq -> sete sete
878// setne -> setne setne
879// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000880// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000881// setgt -> setg seta
882// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000883// ----
884// sets // Used by comparison with 0 optimization
885// setns
886static const unsigned SetCCOpcodeTab[2][8] = {
887 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
888 0, 0 },
889 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
890 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000891};
892
Chris Lattner01cdb1b2004-06-11 05:33:49 +0000893/// emitUCOMr - In the future when we support processors before the P6, this
894/// wraps the logic for emitting an FUCOMr vs FUCOMIr.
895void ISel::emitUCOMr(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
896 unsigned LHS, unsigned RHS) {
897 if (0) { // for processors prior to the P6
898 BuildMI(*MBB, IP, X86::FUCOMr, 2).addReg(LHS).addReg(RHS);
899 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
900 BuildMI(*MBB, IP, X86::SAHF, 1);
901 } else {
902 BuildMI(*MBB, IP, X86::FUCOMIr, 2).addReg(LHS).addReg(RHS);
903 }
904}
905
Chris Lattnerb2acc512003-10-19 21:09:10 +0000906// EmitComparison - This function emits a comparison of the two operands,
907// returning the extended setcc code to use.
908unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
909 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000910 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000911 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000912 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000913 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000914 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000915
916 // Special case handling of: cmp R, i
Chris Lattner260195d2004-05-07 19:55:55 +0000917 if (isa<ConstantPointerNull>(Op1)) {
918 if (OpNum < 2) // seteq/setne -> test
919 BuildMI(*MBB, IP, X86::TEST32rr, 2).addReg(Op0r).addReg(Op0r);
920 else
921 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(0);
922 return OpNum;
923
924 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere80e6372004-04-06 16:02:27 +0000925 if (Class == cByte || Class == cShort || Class == cInt) {
926 unsigned Op1v = CI->getRawValue();
Chris Lattnerc07736a2003-07-23 15:22:26 +0000927
Chris Lattner333864d2003-06-05 19:30:30 +0000928 // Mask off any upper bits of the constant, if there are any...
929 Op1v &= (1ULL << (8 << Class)) - 1;
930
Chris Lattnerb2acc512003-10-19 21:09:10 +0000931 // If this is a comparison against zero, emit more efficient code. We
932 // can't handle unsigned comparisons against zero unless they are == or
933 // !=. These should have been strength reduced already anyway.
934 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
935 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000936 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000937 };
Chris Lattneree352852004-02-29 07:22:16 +0000938 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000939
940 if (OpNum == 2) return 6; // Map jl -> js
941 if (OpNum == 3) return 7; // Map jg -> jns
942 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000943 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000944
945 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000946 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000947 };
948
Chris Lattneree352852004-02-29 07:22:16 +0000949 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000950 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000951 } else {
952 assert(Class == cLong && "Unknown integer class!");
953 unsigned LowCst = CI->getRawValue();
954 unsigned HiCst = CI->getRawValue() >> 32;
955 if (OpNum < 2) { // seteq, setne
956 unsigned LoTmp = Op0r;
957 if (LowCst != 0) {
958 LoTmp = makeAnotherReg(Type::IntTy);
959 BuildMI(*MBB, IP, X86::XOR32ri, 2, LoTmp).addReg(Op0r).addImm(LowCst);
960 }
961 unsigned HiTmp = Op0r+1;
962 if (HiCst != 0) {
963 HiTmp = makeAnotherReg(Type::IntTy);
Chris Lattner48c937e2004-04-06 17:34:50 +0000964 BuildMI(*MBB, IP, X86::XOR32ri, 2,HiTmp).addReg(Op0r+1).addImm(HiCst);
Chris Lattnere80e6372004-04-06 16:02:27 +0000965 }
966 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
967 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
968 return OpNum;
Chris Lattner48c937e2004-04-06 17:34:50 +0000969 } else {
970 // Emit a sequence of code which compares the high and low parts once
971 // each, then uses a conditional move to handle the overflow case. For
972 // example, a setlt for long would generate code like this:
973 //
Chris Lattner9984fd02004-05-09 23:16:33 +0000974 // AL = lo(op1) < lo(op2) // Always unsigned comparison
975 // BL = hi(op1) < hi(op2) // Signedness depends on operands
976 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner48c937e2004-04-06 17:34:50 +0000977 //
978
979 // FIXME: This would be much better if we had hierarchical register
980 // classes! Until then, hardcode registers so that we can deal with
981 // their aliases (because we don't have conditional byte moves).
982 //
983 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r).addImm(LowCst);
984 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
985 BuildMI(*MBB, IP, X86::CMP32ri, 2).addReg(Op0r+1).addImm(HiCst);
986 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0,X86::BL);
987 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
988 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
989 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
990 .addReg(X86::AX);
991 // NOTE: visitSetCondInst knows that the value is dumped into the BL
992 // register at this point for long values...
993 return OpNum;
Chris Lattnere80e6372004-04-06 16:02:27 +0000994 }
Chris Lattner333864d2003-06-05 19:30:30 +0000995 }
Chris Lattnere80e6372004-04-06 16:02:27 +0000996 }
Chris Lattner333864d2003-06-05 19:30:30 +0000997
Chris Lattner9f08a922004-02-03 18:54:04 +0000998 // Special case handling of comparison against +/- 0.0
999 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
1000 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +00001001 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001002 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00001003 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +00001004 return OpNum;
1005 }
1006
Chris Lattner58c41fe2003-08-24 19:19:47 +00001007 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001008 switch (Class) {
1009 default: assert(0 && "Unknown type class!");
1010 // Emit: cmp <var1>, <var2> (do the comparison). We can
1011 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
1012 // 32-bit.
1013 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001014 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001015 break;
1016 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001017 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001018 break;
1019 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001020 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001021 break;
1022 case cFP:
Chris Lattner01cdb1b2004-06-11 05:33:49 +00001023 emitUCOMr(MBB, IP, Op0r, Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +00001024 break;
1025
1026 case cLong:
1027 if (OpNum < 2) { // seteq, setne
1028 unsigned LoTmp = makeAnotherReg(Type::IntTy);
1029 unsigned HiTmp = makeAnotherReg(Type::IntTy);
1030 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001031 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
1032 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
1033 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001034 break; // Allow the sete or setne to be generated from flags set by OR
1035 } else {
1036 // Emit a sequence of code which compares the high and low parts once
1037 // each, then uses a conditional move to handle the overflow case. For
1038 // example, a setlt for long would generate code like this:
1039 //
1040 // AL = lo(op1) < lo(op2) // Signedness depends on operands
1041 // BL = hi(op1) < hi(op2) // Always unsigned comparison
Chris Lattner9984fd02004-05-09 23:16:33 +00001042 // dest = hi(op1) == hi(op2) ? BL : AL;
Chris Lattner3e130a22003-01-13 00:32:26 +00001043 //
1044
Chris Lattner6d40c192003-01-16 16:43:00 +00001045 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +00001046 // classes! Until then, hardcode registers so that we can deal with their
1047 // aliases (because we don't have conditional byte moves).
1048 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001049 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +00001050 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001051 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +00001052 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
1053 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
1054 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001055 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +00001056 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +00001057 // NOTE: visitSetCondInst knows that the value is dumped into the BL
1058 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +00001059 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +00001060 }
1061 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001062 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +00001063}
Chris Lattner3e130a22003-01-13 00:32:26 +00001064
Chris Lattner6d40c192003-01-16 16:43:00 +00001065/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
1066/// register, then move it to wherever the result should be.
1067///
1068void ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +00001069 if (canFoldSetCCIntoBranchOrSelect(&I))
1070 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +00001071
Chris Lattner6d40c192003-01-16 16:43:00 +00001072 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001073 MachineBasicBlock::iterator MII = BB->end();
1074 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
1075 DestReg);
1076}
Chris Lattner6d40c192003-01-16 16:43:00 +00001077
Chris Lattner58c41fe2003-08-24 19:19:47 +00001078/// emitSetCCOperation - Common code shared between visitSetCondInst and
1079/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +00001080///
Chris Lattner58c41fe2003-08-24 19:19:47 +00001081void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001082 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +00001083 Value *Op0, Value *Op1, unsigned Opcode,
1084 unsigned TargetReg) {
1085 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001086 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +00001087
Chris Lattnerb2acc512003-10-19 21:09:10 +00001088 const Type *CompTy = Op0->getType();
1089 unsigned CompClass = getClassB(CompTy);
1090 bool isSigned = CompTy->isSigned() && CompClass != cFP;
1091
1092 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +00001093 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001094 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +00001095 } else {
1096 // Handle long comparisons by copying the value which is already in BL into
1097 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001098 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +00001099 }
Brian Gaeke1749d632002-11-07 17:59:21 +00001100}
Chris Lattner51b49a92002-11-02 19:45:49 +00001101
Chris Lattner12d96a02004-03-30 21:22:00 +00001102void ISel::visitSelectInst(SelectInst &SI) {
1103 unsigned DestReg = getReg(SI);
1104 MachineBasicBlock::iterator MII = BB->end();
1105 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1106 SI.getFalseValue(), DestReg);
1107}
1108
1109/// emitSelect - Common code shared between visitSelectInst and the constant
1110/// expression support.
1111void ISel::emitSelectOperation(MachineBasicBlock *MBB,
1112 MachineBasicBlock::iterator IP,
1113 Value *Cond, Value *TrueVal, Value *FalseVal,
1114 unsigned DestReg) {
1115 unsigned SelectClass = getClassB(TrueVal->getType());
1116
1117 // We don't support 8-bit conditional moves. If we have incoming constants,
1118 // transform them into 16-bit constants to avoid having a run-time conversion.
1119 if (SelectClass == cByte) {
1120 if (Constant *T = dyn_cast<Constant>(TrueVal))
1121 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
1122 if (Constant *F = dyn_cast<Constant>(FalseVal))
1123 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
1124 }
1125
Chris Lattner82c5a992004-04-13 21:56:09 +00001126 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1127 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1128 if (TrueReg == FalseReg) {
1129 static const unsigned Opcode[] = {
1130 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
1131 };
1132 BuildMI(*MBB, IP, Opcode[SelectClass], 1, DestReg).addReg(TrueReg);
1133 if (SelectClass == cLong)
1134 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(TrueReg+1);
1135 return;
1136 }
1137
Chris Lattner307ecba2004-03-30 22:39:09 +00001138 unsigned Opcode;
1139 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1140 // We successfully folded the setcc into the select instruction.
1141
1142 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1143 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
1144 IP);
1145
1146 const Type *CompTy = SCI->getOperand(0)->getType();
1147 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
1148
1149 // LLVM -> X86 signed X86 unsigned
1150 // ----- ---------- ------------
1151 // seteq -> cmovNE cmovNE
1152 // setne -> cmovE cmovE
1153 // setlt -> cmovGE cmovAE
1154 // setge -> cmovL cmovB
1155 // setgt -> cmovLE cmovBE
1156 // setle -> cmovG cmovA
1157 // ----
1158 // cmovNS // Used by comparison with 0 optimization
1159 // cmovS
1160
1161 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001162 default: assert(0 && "Unknown value class!");
1163 case cFP: {
1164 // Annoyingly, we don't have a full set of floating point conditional
1165 // moves. :(
1166 static const unsigned OpcodeTab[2][8] = {
1167 { X86::FCMOVNE, X86::FCMOVE, X86::FCMOVAE, X86::FCMOVB,
1168 X86::FCMOVBE, X86::FCMOVA, 0, 0 },
1169 { X86::FCMOVNE, X86::FCMOVE, 0, 0, 0, 0, 0, 0 },
1170 };
1171 Opcode = OpcodeTab[isSigned][OpNum];
1172
1173 // If opcode == 0, we hit a case that we don't support. Output a setcc
1174 // and compare the result against zero.
1175 if (Opcode == 0) {
1176 unsigned CompClass = getClassB(CompTy);
1177 unsigned CondReg;
1178 if (CompClass != cLong || OpNum < 2) {
1179 CondReg = makeAnotherReg(Type::BoolTy);
1180 // Handle normal comparisons with a setcc instruction...
1181 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, CondReg);
1182 } else {
1183 // Long comparisons end up in the BL register.
1184 CondReg = X86::BL;
1185 }
1186
Chris Lattner68626c22004-03-31 22:22:36 +00001187 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner352eb482004-03-31 22:03:35 +00001188 Opcode = X86::FCMOVE;
1189 }
1190 break;
1191 }
Chris Lattner307ecba2004-03-30 22:39:09 +00001192 case cByte:
1193 case cShort: {
1194 static const unsigned OpcodeTab[2][8] = {
1195 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1196 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1197 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1198 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1199 };
1200 Opcode = OpcodeTab[isSigned][OpNum];
1201 break;
1202 }
1203 case cInt:
1204 case cLong: {
1205 static const unsigned OpcodeTab[2][8] = {
1206 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1207 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1208 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1209 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1210 };
1211 Opcode = OpcodeTab[isSigned][OpNum];
1212 break;
1213 }
1214 }
1215 } else {
1216 // Get the value being branched on, and use it to set the condition codes.
1217 unsigned CondReg = getReg(Cond, MBB, IP);
Chris Lattner68626c22004-03-31 22:22:36 +00001218 BuildMI(*MBB, IP, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
Chris Lattner307ecba2004-03-30 22:39:09 +00001219 switch (SelectClass) {
Chris Lattner352eb482004-03-31 22:03:35 +00001220 default: assert(0 && "Unknown value class!");
1221 case cFP: Opcode = X86::FCMOVE; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001222 case cByte:
Chris Lattner352eb482004-03-31 22:03:35 +00001223 case cShort: Opcode = X86::CMOVE16rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001224 case cInt:
Chris Lattner352eb482004-03-31 22:03:35 +00001225 case cLong: Opcode = X86::CMOVE32rr; break;
Chris Lattner307ecba2004-03-30 22:39:09 +00001226 }
1227 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001228
Chris Lattner12d96a02004-03-30 21:22:00 +00001229 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001230
Chris Lattner12d96a02004-03-30 21:22:00 +00001231
1232 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1233 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1234 // cmove, then truncate the result.
1235 if (SelectClass == cByte) {
1236 DestReg = makeAnotherReg(Type::ShortTy);
1237 if (getClassB(TrueVal->getType()) == cByte) {
1238 // Promote the true value, by storing it into AL, and reading from AX.
1239 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1240 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1241 TrueReg = makeAnotherReg(Type::ShortTy);
1242 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1243 }
1244 if (getClassB(FalseVal->getType()) == cByte) {
1245 // Promote the true value, by storing it into CL, and reading from CX.
1246 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1247 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1248 FalseReg = makeAnotherReg(Type::ShortTy);
1249 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1250 }
1251 }
1252
1253 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1254
1255 switch (SelectClass) {
1256 case cByte:
1257 // We did the computation with 16-bit registers. Truncate back to our
1258 // result by copying into AX then copying out AL.
1259 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1260 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1261 break;
1262 case cLong:
1263 // Move the upper half of the value as well.
1264 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1265 break;
1266 }
1267}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001268
1269
1270
Brian Gaekec2505982002-11-30 11:57:28 +00001271/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1272/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001273///
Chris Lattner3e130a22003-01-13 00:32:26 +00001274void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
Chris Lattner9984fd02004-05-09 23:16:33 +00001275 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001276
Chris Lattner29bf0622004-04-06 01:21:00 +00001277 Value *Val = VR.Val;
1278 const Type *Ty = VR.Ty;
Chris Lattner502e36c2004-04-06 01:25:33 +00001279 if (Val) {
Chris Lattner29bf0622004-04-06 01:21:00 +00001280 if (Constant *C = dyn_cast<Constant>(Val)) {
1281 Val = ConstantExpr::getCast(C, Type::IntTy);
1282 Ty = Type::IntTy;
1283 }
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001284
Chris Lattner502e36c2004-04-06 01:25:33 +00001285 // If this is a simple constant, just emit a MOVri directly to avoid the
1286 // copy.
1287 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1288 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
Chris Lattner2b10b082004-05-12 16:35:04 +00001289 BuildMI(BB, X86::MOV32ri, 1, targetReg).addImm(TheVal);
Chris Lattner502e36c2004-04-06 01:25:33 +00001290 return;
1291 }
1292 }
1293
Chris Lattner29bf0622004-04-06 01:21:00 +00001294 // Make sure we have the register number for this value...
1295 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1296
1297 switch (getClassB(Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001298 case cByte:
1299 // Extend value into target register (8->32)
1300 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001301 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001302 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001303 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001304 break;
1305 case cShort:
1306 // Extend value into target register (16->32)
1307 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001308 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001309 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001310 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001311 break;
1312 case cInt:
1313 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001314 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001315 break;
1316 default:
1317 assert(0 && "Unpromotable operand class in promote32");
1318 }
Brian Gaekec2505982002-11-30 11:57:28 +00001319}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001320
Chris Lattner72614082002-10-25 22:55:53 +00001321/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1322/// we have the following possibilities:
1323///
1324/// ret void: No return value, simply emit a 'ret' instruction
1325/// ret sbyte, ubyte : Extend value into EAX and return
1326/// ret short, ushort: Extend value into EAX and return
1327/// ret int, uint : Move value into EAX and return
1328/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001329/// ret long, ulong : Move value into EAX/EDX and return
1330/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001331///
Chris Lattner3e130a22003-01-13 00:32:26 +00001332void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001333 if (I.getNumOperands() == 0) {
1334 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1335 return;
1336 }
1337
1338 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001339 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001340 case cByte: // integral return values: extend or move into EAX and return
1341 case cShort:
1342 case cInt:
Chris Lattner29bf0622004-04-06 01:21:00 +00001343 promote32(X86::EAX, ValueRecord(RetVal));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001344 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001345 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001346 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001347 case cFP: { // Floats & Doubles: Return in ST(0)
1348 unsigned RetReg = getReg(RetVal);
Chris Lattner3e130a22003-01-13 00:32:26 +00001349 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001350 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001351 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001352 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001353 }
1354 case cLong: {
1355 unsigned RetReg = getReg(RetVal);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001356 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1357 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001358 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001359 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1360 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001361 break;
Chris Lattner29bf0622004-04-06 01:21:00 +00001362 }
Chris Lattner94af4142002-12-25 05:13:53 +00001363 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001364 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001365 }
Chris Lattner43189d12002-11-17 20:07:45 +00001366 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001367 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001368}
1369
Chris Lattner55f6fab2003-01-16 18:07:23 +00001370// getBlockAfter - Return the basic block which occurs lexically after the
1371// specified one.
1372static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1373 Function::iterator I = BB; ++I; // Get iterator to next block
1374 return I != BB->getParent()->end() ? &*I : 0;
1375}
1376
Chris Lattner51b49a92002-11-02 19:45:49 +00001377/// visitBranchInst - Handle conditional and unconditional branches here. Note
1378/// that since code layout is frozen at this point, that if we are trying to
1379/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001380/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001381///
Chris Lattner94af4142002-12-25 05:13:53 +00001382void ISel::visitBranchInst(BranchInst &BI) {
Brian Gaekeea9ca672004-04-28 04:19:37 +00001383 // Update machine-CFG edges
1384 BB->addSuccessor (MBBMap[BI.getSuccessor(0)]);
1385 if (BI.isConditional())
1386 BB->addSuccessor (MBBMap[BI.getSuccessor(1)]);
1387
Chris Lattner55f6fab2003-01-16 18:07:23 +00001388 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1389
1390 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001391 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001392 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner6d40c192003-01-16 16:43:00 +00001393 return;
1394 }
1395
1396 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001397 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001398 if (SCI == 0) {
1399 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1400 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001401 unsigned condReg = getReg(BI.getCondition());
Chris Lattner68626c22004-03-31 22:22:36 +00001402 BuildMI(BB, X86::TEST8rr, 2).addReg(condReg).addReg(condReg);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001403 if (BI.getSuccessor(1) == NextBB) {
1404 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001405 BuildMI(BB, X86::JNE, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001406 } else {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001407 BuildMI(BB, X86::JE, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001408
1409 if (BI.getSuccessor(0) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001410 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001411 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001412 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001413 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001414
1415 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001416 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001417 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001418
1419 const Type *CompTy = SCI->getOperand(0)->getType();
1420 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001421
Chris Lattnerb2acc512003-10-19 21:09:10 +00001422
Chris Lattner6d40c192003-01-16 16:43:00 +00001423 // LLVM -> X86 signed X86 unsigned
1424 // ----- ---------- ------------
1425 // seteq -> je je
1426 // setne -> jne jne
1427 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001428 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001429 // setgt -> jg ja
1430 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001431 // ----
1432 // js // Used by comparison with 0 optimization
1433 // jns
1434
1435 static const unsigned OpcodeTab[2][8] = {
1436 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1437 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1438 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001439 };
1440
Chris Lattner55f6fab2003-01-16 18:07:23 +00001441 if (BI.getSuccessor(0) != NextBB) {
Brian Gaeke9f088e42004-05-14 06:54:56 +00001442 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1443 .addMBB(MBBMap[BI.getSuccessor(0)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001444 if (BI.getSuccessor(1) != NextBB)
Brian Gaeke9f088e42004-05-14 06:54:56 +00001445 BuildMI(BB, X86::JMP, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001446 } else {
1447 // Change to the inverse condition...
1448 if (BI.getSuccessor(1) != NextBB) {
1449 OpNum ^= 1;
Brian Gaeke9f088e42004-05-14 06:54:56 +00001450 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1)
1451 .addMBB(MBBMap[BI.getSuccessor(1)]);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001452 }
1453 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001454}
1455
Chris Lattner3e130a22003-01-13 00:32:26 +00001456
1457/// doCall - This emits an abstract call instruction, setting up the arguments
1458/// and the return value as appropriate. For the actual function call itself,
1459/// it inserts the specified CallMI instruction into the stream.
1460///
1461void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001462 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001463
Chris Lattner065faeb2002-12-28 20:24:02 +00001464 // Count how many bytes are to be pushed on the stack...
1465 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001466
Chris Lattner3e130a22003-01-13 00:32:26 +00001467 if (!Args.empty()) {
1468 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1469 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001470 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001471 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001472 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001473 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001474 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001475 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1476 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001477 default: assert(0 && "Unknown class!");
1478 }
1479
1480 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001481 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001482
1483 // Arguments go on the stack in reverse order, as specified by the ABI.
1484 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001485 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001486 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001487 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001488 case cByte:
Chris Lattner2b10b082004-05-12 16:35:04 +00001489 if (Args[i].Val && isa<ConstantBool>(Args[i].Val)) {
1490 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1491 .addImm(Args[i].Val == ConstantBool::True);
1492 break;
1493 }
1494 // FALL THROUGH
Chris Lattner21585222004-03-01 02:42:43 +00001495 case cShort:
1496 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1497 // Zero/Sign extend constant, then stuff into memory.
1498 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1499 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1500 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1501 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1502 } else {
1503 // Promote arg to 32 bits wide into a temporary register...
1504 ArgReg = makeAnotherReg(Type::UIntTy);
1505 promote32(ArgReg, Args[i]);
1506 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1507 X86::ESP, ArgOffset).addReg(ArgReg);
1508 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001509 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001510 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001511 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1512 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1513 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1514 X86::ESP, ArgOffset).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00001515 } else if (Args[i].Val && isa<ConstantPointerNull>(Args[i].Val)) {
1516 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1517 X86::ESP, ArgOffset).addImm(0);
Chris Lattner21585222004-03-01 02:42:43 +00001518 } else {
1519 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1520 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1521 X86::ESP, ArgOffset).addReg(ArgReg);
1522 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001523 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001524 case cLong:
Chris Lattner92900a62004-04-06 03:23:00 +00001525 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1526 uint64_t Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1527 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1528 X86::ESP, ArgOffset).addImm(Val & ~0U);
1529 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1530 X86::ESP, ArgOffset+4).addImm(Val >> 32ULL);
1531 } else {
1532 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1533 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1534 X86::ESP, ArgOffset).addReg(ArgReg);
1535 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1536 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1537 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001538 ArgOffset += 4; // 8 byte entry, not 4.
1539 break;
1540
Chris Lattner065faeb2002-12-28 20:24:02 +00001541 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001542 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001543 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001544 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001545 X86::ESP, ArgOffset).addReg(ArgReg);
1546 } else {
1547 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001548 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001549 X86::ESP, ArgOffset).addReg(ArgReg);
1550 ArgOffset += 4; // 8 byte entry, not 4.
1551 }
1552 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001553
Chris Lattner3e130a22003-01-13 00:32:26 +00001554 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001555 }
1556 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001557 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001558 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001559 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001560 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001561
Chris Lattner3e130a22003-01-13 00:32:26 +00001562 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001563
Chris Lattneree352852004-02-29 07:22:16 +00001564 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001565
1566 // If there is a return value, scavenge the result from the location the call
1567 // leaves it in...
1568 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001569 if (Ret.Ty != Type::VoidTy) {
1570 unsigned DestClass = getClassB(Ret.Ty);
1571 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001572 case cByte:
1573 case cShort:
1574 case cInt: {
1575 // Integral results are in %eax, or the appropriate portion
1576 // thereof.
1577 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001578 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001579 };
1580 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001581 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001582 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001583 }
Chris Lattner94af4142002-12-25 05:13:53 +00001584 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001585 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001586 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001587 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001588 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1589 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001590 break;
1591 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001592 }
Chris Lattnera3243642002-12-04 23:45:28 +00001593 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001594}
Chris Lattner2df035b2002-11-02 19:27:56 +00001595
Chris Lattner3e130a22003-01-13 00:32:26 +00001596
1597/// visitCallInst - Push args on stack and do a procedure call instruction.
1598void ISel::visitCallInst(CallInst &CI) {
1599 MachineInstr *TheCall;
1600 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001601 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001602 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001603 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1604 return;
1605 }
1606
Chris Lattner3e130a22003-01-13 00:32:26 +00001607 // Emit a CALL instruction with PC-relative displacement.
1608 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1609 } else { // Emit an indirect call...
1610 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001611 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001612 }
1613
1614 std::vector<ValueRecord> Args;
1615 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001616 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001617
1618 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1619 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001620}
Chris Lattner3e130a22003-01-13 00:32:26 +00001621
Chris Lattner44827152003-12-28 09:47:19 +00001622/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1623/// function, lowering any calls to unknown intrinsic functions into the
1624/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001625///
Chris Lattner44827152003-12-28 09:47:19 +00001626void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1627 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1628 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1629 if (CallInst *CI = dyn_cast<CallInst>(I++))
1630 if (Function *F = CI->getCalledFunction())
1631 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001632 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001633 case Intrinsic::vastart:
1634 case Intrinsic::vacopy:
1635 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001636 case Intrinsic::returnaddress:
1637 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001638 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001639 case Intrinsic::memset:
Chris Lattnerdc572442004-06-15 21:36:44 +00001640 case Intrinsic::isunordered:
John Criswell4ffff9e2004-04-08 20:31:47 +00001641 case Intrinsic::readport:
1642 case Intrinsic::writeport:
Chris Lattner44827152003-12-28 09:47:19 +00001643 // We directly implement these intrinsics
1644 break;
John Criswelle5a4c152004-04-13 22:13:14 +00001645 case Intrinsic::readio: {
1646 // On X86, memory operations are in-order. Lower this intrinsic
1647 // into a volatile load.
1648 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001649 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1650 CI->replaceAllUsesWith(LI);
1651 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001652 break;
1653 }
1654 case Intrinsic::writeio: {
1655 // On X86, memory operations are in-order. Lower this intrinsic
1656 // into a volatile store.
1657 Instruction *Before = CI->getPrev();
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001658 StoreInst *LI = new StoreInst(CI->getOperand(1),
1659 CI->getOperand(2), true, CI);
1660 CI->replaceAllUsesWith(LI);
1661 BB->getInstList().erase(CI);
John Criswelle5a4c152004-04-13 22:13:14 +00001662 break;
1663 }
Chris Lattner44827152003-12-28 09:47:19 +00001664 default:
1665 // All other intrinsic calls we must lower.
1666 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001667 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001668 if (Before) { // Move iterator to instruction after call
Chris Lattnerb4fe76c2004-06-11 04:31:10 +00001669 I = Before; ++I;
Chris Lattner44827152003-12-28 09:47:19 +00001670 } else {
1671 I = BB->begin();
1672 }
1673 }
Chris Lattner44827152003-12-28 09:47:19 +00001674}
1675
Brian Gaeked0fde302003-11-11 22:41:34 +00001676void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001677 unsigned TmpReg1, TmpReg2;
1678 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001679 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001680 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001681 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001682 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001683 return;
1684
Chris Lattner5634b9f2004-03-13 00:24:52 +00001685 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001686 TmpReg1 = getReg(CI);
1687 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001688 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001689 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001690 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001691
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001692 case Intrinsic::returnaddress:
1693 case Intrinsic::frameaddress:
1694 TmpReg1 = getReg(CI);
1695 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1696 if (ID == Intrinsic::returnaddress) {
1697 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001698 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001699 ReturnAddressIndex);
1700 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001701 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001702 ReturnAddressIndex, -4);
1703 }
1704 } else {
1705 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001706 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001707 }
1708 return;
1709
Chris Lattnerdc572442004-06-15 21:36:44 +00001710 case Intrinsic::isunordered:
1711 TmpReg1 = getReg(CI.getOperand(1));
1712 TmpReg2 = getReg(CI.getOperand(2));
1713 emitUCOMr(BB, BB->end(), TmpReg2, TmpReg1);
1714 TmpReg2 = getReg(CI);
1715 BuildMI(BB, X86::SETPr, 0, TmpReg2);
1716 return;
1717
Chris Lattner915e5e52004-02-12 17:53:22 +00001718 case Intrinsic::memcpy: {
1719 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1720 unsigned Align = 1;
1721 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1722 Align = AlignC->getRawValue();
1723 if (Align == 0) Align = 1;
1724 }
1725
1726 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001727 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001728 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001729 switch (Align & 3) {
1730 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001731 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1732 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1733 } else {
1734 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001735 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001736 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001737 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001738 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001739 break;
1740 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001741 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1742 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1743 } else {
1744 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001745 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001746 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001747 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001748 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001749 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001750 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001751 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001752 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001753 break;
1754 }
1755
1756 // No matter what the alignment is, we put the source in ESI, the
1757 // destination in EDI, and the count in ECX.
1758 TmpReg1 = getReg(CI.getOperand(1));
1759 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001760 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1761 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1762 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001763 BuildMI(BB, Opcode, 0);
1764 return;
1765 }
1766 case Intrinsic::memset: {
1767 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1768 unsigned Align = 1;
1769 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1770 Align = AlignC->getRawValue();
1771 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001772 }
1773
Chris Lattner2a0f2242004-02-14 04:46:05 +00001774 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001775 unsigned CountReg;
1776 unsigned Opcode;
1777 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1778 unsigned Val = ValC->getRawValue() & 255;
1779
1780 // If the value is a constant, then we can potentially use larger copies.
1781 switch (Align & 3) {
1782 case 2: // WORD aligned
1783 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001784 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001785 } else {
1786 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001787 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001788 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001789 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001790 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001791 Opcode = X86::REP_STOSW;
1792 break;
1793 case 0: // DWORD aligned
1794 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001795 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001796 } else {
1797 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001798 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001799 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001800 }
1801 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001802 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001803 Opcode = X86::REP_STOSD;
1804 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001805 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001806 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001807 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001808 Opcode = X86::REP_STOSB;
1809 break;
1810 }
1811 } else {
1812 // If it's not a constant value we are storing, just fall back. We could
1813 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1814 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001815 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001816 CountReg = getReg(CI.getOperand(3));
1817 Opcode = X86::REP_STOSB;
1818 }
1819
1820 // No matter what the alignment is, we put the source in ESI, the
1821 // destination in EDI, and the count in ECX.
1822 TmpReg1 = getReg(CI.getOperand(1));
1823 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001824 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1825 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001826 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001827 return;
1828 }
1829
Chris Lattner87e18de2004-04-13 17:20:37 +00001830 case Intrinsic::readport: {
1831 // First, determine that the size of the operand falls within the acceptable
1832 // range for this architecture.
John Criswell4ffff9e2004-04-08 20:31:47 +00001833 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001834 if (getClassB(CI.getOperand(1)->getType()) != cShort) {
John Criswellca6ea0f2004-04-08 22:39:13 +00001835 std::cerr << "llvm.readport: Address size is not 16 bits\n";
Chris Lattner87e18de2004-04-13 17:20:37 +00001836 exit(1);
John Criswellca6ea0f2004-04-08 22:39:13 +00001837 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001838
John Criswell4ffff9e2004-04-08 20:31:47 +00001839 // Now, move the I/O port address into the DX register and use the IN
1840 // instruction to get the input data.
1841 //
Chris Lattner87e18de2004-04-13 17:20:37 +00001842 unsigned Class = getClass(CI.getCalledFunction()->getReturnType());
1843 unsigned DestReg = getReg(CI);
John Criswell4ffff9e2004-04-08 20:31:47 +00001844
Chris Lattner87e18de2004-04-13 17:20:37 +00001845 // If the port is a single-byte constant, use the immediate form.
1846 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(1)))
1847 if ((C->getRawValue() & 255) == C->getRawValue()) {
1848 switch (Class) {
1849 case cByte:
1850 BuildMI(BB, X86::IN8ri, 1).addImm((unsigned char)C->getRawValue());
1851 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1852 return;
1853 case cShort:
1854 BuildMI(BB, X86::IN16ri, 1).addImm((unsigned char)C->getRawValue());
1855 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1856 return;
1857 case cInt:
1858 BuildMI(BB, X86::IN32ri, 1).addImm((unsigned char)C->getRawValue());
1859 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1860 return;
1861 }
1862 }
1863
1864 unsigned Reg = getReg(CI.getOperand(1));
1865 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1866 switch (Class) {
1867 case cByte:
1868 BuildMI(BB, X86::IN8rr, 0);
1869 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
1870 break;
1871 case cShort:
1872 BuildMI(BB, X86::IN16rr, 0);
1873 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::AX);
1874 break;
1875 case cInt:
1876 BuildMI(BB, X86::IN32rr, 0);
1877 BuildMI(BB, X86::MOV8rr, 1, DestReg).addReg(X86::EAX);
1878 break;
1879 default:
1880 std::cerr << "Cannot do input on this data type";
John Criswellca6ea0f2004-04-08 22:39:13 +00001881 exit (1);
1882 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001883 return;
Chris Lattner87e18de2004-04-13 17:20:37 +00001884 }
John Criswell4ffff9e2004-04-08 20:31:47 +00001885
Chris Lattner87e18de2004-04-13 17:20:37 +00001886 case Intrinsic::writeport: {
1887 // First, determine that the size of the operand falls within the
1888 // acceptable range for this architecture.
1889 if (getClass(CI.getOperand(2)->getType()) != cShort) {
1890 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
1891 exit(1);
1892 }
1893
1894 unsigned Class = getClassB(CI.getOperand(1)->getType());
1895 unsigned ValReg = getReg(CI.getOperand(1));
1896 switch (Class) {
1897 case cByte:
1898 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
1899 break;
1900 case cShort:
1901 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(ValReg);
1902 break;
1903 case cInt:
1904 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(ValReg);
1905 break;
1906 default:
1907 std::cerr << "llvm.writeport: invalid data type for X86 target";
1908 exit(1);
1909 }
1910
1911
1912 // If the port is a single-byte constant, use the immediate form.
1913 if (ConstantInt *C = dyn_cast<ConstantInt>(CI.getOperand(2)))
1914 if ((C->getRawValue() & 255) == C->getRawValue()) {
1915 static const unsigned O[] = { X86::OUT8ir, X86::OUT16ir, X86::OUT32ir };
1916 BuildMI(BB, O[Class], 1).addImm((unsigned char)C->getRawValue());
1917 return;
1918 }
1919
1920 // Otherwise, move the I/O port address into the DX register and the value
1921 // to write into the AL/AX/EAX register.
1922 static const unsigned Opc[] = { X86::OUT8rr, X86::OUT16rr, X86::OUT32rr };
1923 unsigned Reg = getReg(CI.getOperand(2));
1924 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
1925 BuildMI(BB, Opc[Class], 0);
1926 return;
1927 }
1928
Chris Lattner44827152003-12-28 09:47:19 +00001929 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001930 }
1931}
1932
Chris Lattner7dee5da2004-03-08 01:58:35 +00001933static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1934 if (LI.getParent() != User.getParent())
1935 return false;
1936 BasicBlock::iterator It = &LI;
1937 // Check all of the instructions between the load and the user. We should
1938 // really use alias analysis here, but for now we just do something simple.
1939 for (++It; It != BasicBlock::iterator(&User); ++It) {
1940 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001941 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001942 case Instruction::Store:
1943 case Instruction::Call:
1944 case Instruction::Invoke:
1945 return false;
Chris Lattner133dbb12004-04-12 03:02:48 +00001946 case Instruction::Load:
1947 if (cast<LoadInst>(It)->isVolatile() && LI.isVolatile())
1948 return false;
1949 break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00001950 }
1951 }
1952 return true;
1953}
1954
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001955/// visitSimpleBinary - Implement simple binary operators for integral types...
1956/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1957/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001958///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001959void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1960 unsigned DestReg = getReg(B);
1961 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001962 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001963 unsigned Class = getClassB(B.getType());
Chris Lattner721d2d42004-03-08 01:18:36 +00001964
Chris Lattner7dee5da2004-03-08 01:58:35 +00001965 // Special case: op Reg, load [mem]
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001966 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1) && Class != cLong &&
Chris Lattnerccd97962004-06-17 22:15:25 +00001967 Op0->hasOneUse() &&
Chris Lattnerc81e6ba2004-05-10 15:15:55 +00001968 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B))
Chris Lattner7dee5da2004-03-08 01:58:35 +00001969 if (!B.swapOperands())
1970 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1971
Chris Lattnerccd97962004-06-17 22:15:25 +00001972 if (isa<LoadInst>(Op1) && Class != cLong && Op1->hasOneUse() &&
Chris Lattner7dee5da2004-03-08 01:58:35 +00001973 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1974
Chris Lattner95157f72004-04-11 22:05:45 +00001975 unsigned Opcode;
1976 if (Class != cFP) {
1977 static const unsigned OpcodeTab[][3] = {
1978 // Arithmetic operators
1979 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1980 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1981
1982 // Bitwise operators
1983 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1984 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1985 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1986 };
1987 Opcode = OpcodeTab[OperatorClass][Class];
1988 } else {
1989 static const unsigned OpcodeTab[][2] = {
1990 { X86::FADD32m, X86::FADD64m }, // ADD
1991 { X86::FSUB32m, X86::FSUB64m }, // SUB
1992 };
1993 const Type *Ty = Op0->getType();
1994 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
1995 Opcode = OpcodeTab[OperatorClass][Ty == Type::DoubleTy];
1996 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00001997
Chris Lattner7dee5da2004-03-08 01:58:35 +00001998 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00001999 if (AllocaInst *AI =
2000 dyn_castFixedAlloca(cast<LoadInst>(Op1)->getOperand(0))) {
2001 unsigned FI = getFixedSizedAllocaFI(AI);
2002 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r), FI);
2003
2004 } else {
2005 unsigned BaseReg, Scale, IndexReg, Disp;
2006 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), BaseReg,
2007 Scale, IndexReg, Disp);
2008
2009 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op0r),
2010 BaseReg, Scale, IndexReg, Disp);
2011 }
Chris Lattner7dee5da2004-03-08 01:58:35 +00002012 return;
2013 }
2014
Chris Lattner95157f72004-04-11 22:05:45 +00002015 // If this is a floating point subtract, check to see if we can fold the first
2016 // operand in.
2017 if (Class == cFP && OperatorClass == 1 &&
2018 isa<LoadInst>(Op0) &&
2019 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op0), B)) {
2020 const Type *Ty = Op0->getType();
2021 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2022 unsigned Opcode = Ty == Type::FloatTy ? X86::FSUBR32m : X86::FSUBR64m;
2023
Chris Lattner95157f72004-04-11 22:05:45 +00002024 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002025 if (AllocaInst *AI =
2026 dyn_castFixedAlloca(cast<LoadInst>(Op0)->getOperand(0))) {
2027 unsigned FI = getFixedSizedAllocaFI(AI);
2028 addFrameReference(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r), FI);
2029 } else {
2030 unsigned BaseReg, Scale, IndexReg, Disp;
2031 getAddressingMode(cast<LoadInst>(Op0)->getOperand(0), BaseReg,
2032 Scale, IndexReg, Disp);
2033
2034 addFullAddress(BuildMI(BB, Opcode, 5, DestReg).addReg(Op1r),
2035 BaseReg, Scale, IndexReg, Disp);
2036 }
Chris Lattner95157f72004-04-11 22:05:45 +00002037 return;
2038 }
2039
Chris Lattner721d2d42004-03-08 01:18:36 +00002040 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002041}
Chris Lattner3e130a22003-01-13 00:32:26 +00002042
Chris Lattner6621ed92004-04-11 21:23:56 +00002043
2044/// emitBinaryFPOperation - This method handles emission of floating point
2045/// Add (0), Sub (1), Mul (2), and Div (3) operations.
2046void ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
2047 MachineBasicBlock::iterator IP,
2048 Value *Op0, Value *Op1,
2049 unsigned OperatorClass, unsigned DestReg) {
2050
2051 // Special case: op Reg, <const fp>
2052 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1))
2053 if (!Op1C->isExactlyValue(+0.0) && !Op1C->isExactlyValue(+1.0)) {
2054 // Create a constant pool entry for this constant.
2055 MachineConstantPool *CP = F->getConstantPool();
2056 unsigned CPI = CP->getConstantPoolIndex(Op1C);
2057 const Type *Ty = Op1->getType();
2058
2059 static const unsigned OpcodeTab[][4] = {
2060 { X86::FADD32m, X86::FSUB32m, X86::FMUL32m, X86::FDIV32m }, // Float
2061 { X86::FADD64m, X86::FSUB64m, X86::FMUL64m, X86::FDIV64m }, // Double
2062 };
2063
2064 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
2065 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2066 unsigned Op0r = getReg(Op0, BB, IP);
2067 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2068 DestReg).addReg(Op0r), CPI);
2069 return;
2070 }
2071
Chris Lattner13c07fe2004-04-12 00:12:04 +00002072 // Special case: R1 = op <const fp>, R2
Chris Lattner6621ed92004-04-11 21:23:56 +00002073 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
2074 if (CFP->isExactlyValue(-0.0) && OperatorClass == 1) {
2075 // -0.0 - X === -X
2076 unsigned op1Reg = getReg(Op1, BB, IP);
2077 BuildMI(*BB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
2078 return;
2079 } else if (!CFP->isExactlyValue(+0.0) && !CFP->isExactlyValue(+1.0)) {
Chris Lattner13c07fe2004-04-12 00:12:04 +00002080 // R1 = op CST, R2 --> R1 = opr R2, CST
Chris Lattner6621ed92004-04-11 21:23:56 +00002081
2082 // Create a constant pool entry for this constant.
2083 MachineConstantPool *CP = F->getConstantPool();
2084 unsigned CPI = CP->getConstantPoolIndex(CFP);
2085 const Type *Ty = CFP->getType();
2086
2087 static const unsigned OpcodeTab[][4] = {
2088 { X86::FADD32m, X86::FSUBR32m, X86::FMUL32m, X86::FDIVR32m }, // Float
2089 { X86::FADD64m, X86::FSUBR64m, X86::FMUL64m, X86::FDIVR64m }, // Double
2090 };
2091
2092 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2093 unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass];
2094 unsigned Op1r = getReg(Op1, BB, IP);
2095 addConstantPoolReference(BuildMI(*BB, IP, Opcode, 5,
2096 DestReg).addReg(Op1r), CPI);
2097 return;
2098 }
2099
2100 // General case.
2101 static const unsigned OpcodeTab[4] = {
2102 X86::FpADD, X86::FpSUB, X86::FpMUL, X86::FpDIV
2103 };
2104
2105 unsigned Opcode = OpcodeTab[OperatorClass];
2106 unsigned Op0r = getReg(Op0, BB, IP);
2107 unsigned Op1r = getReg(Op1, BB, IP);
2108 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2109}
2110
Chris Lattnerb2acc512003-10-19 21:09:10 +00002111/// emitSimpleBinaryOperation - Implement simple binary operators for integral
2112/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
2113/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00002114///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002115/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
2116/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002117///
2118void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002119 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002120 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002121 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00002122 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00002123
Chris Lattner6621ed92004-04-11 21:23:56 +00002124 if (Class == cFP) {
2125 assert(OperatorClass < 2 && "No logical ops for FP!");
2126 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
2127 return;
2128 }
2129
Chris Lattner48b0c972004-04-11 20:26:20 +00002130 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0))
Chris Lattner667ea022004-06-18 00:50:37 +00002131 if (OperatorClass == 1) {
Chris Lattner48b0c972004-04-11 20:26:20 +00002132 static unsigned const NEGTab[] = {
2133 X86::NEG8r, X86::NEG16r, X86::NEG32r, 0, X86::NEG32r
2134 };
Chris Lattner667ea022004-06-18 00:50:37 +00002135
2136 // sub 0, X -> neg X
2137 if (CI->isNullValue()) {
2138 unsigned op1Reg = getReg(Op1, MBB, IP);
2139 BuildMI(*MBB, IP, NEGTab[Class], 1, DestReg).addReg(op1Reg);
Chris Lattner48b0c972004-04-11 20:26:20 +00002140
Chris Lattner667ea022004-06-18 00:50:37 +00002141 if (Class == cLong) {
2142 // We just emitted: Dl = neg Sl
2143 // Now emit : T = addc Sh, 0
2144 // : Dh = neg T
2145 unsigned T = makeAnotherReg(Type::IntTy);
2146 BuildMI(*MBB, IP, X86::ADC32ri, 2, T).addReg(op1Reg+1).addImm(0);
2147 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg+1).addReg(T);
2148 }
2149 return;
2150 } else if (Op1->hasOneUse() && Class != cLong) {
2151 // sub C, X -> tmp = neg X; DestReg = add tmp, C. This is better
2152 // than copying C into a temporary register, because of register
2153 // pressure (tmp and destreg can share a register.
2154 static unsigned const ADDRITab[] = {
2155 X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri
2156 };
2157 unsigned op1Reg = getReg(Op1, MBB, IP);
2158 unsigned Tmp = makeAnotherReg(Op0->getType());
2159 BuildMI(*MBB, IP, NEGTab[Class], 1, Tmp).addReg(op1Reg);
Chris Lattner30483732004-06-20 07:49:54 +00002160 BuildMI(*MBB, IP, ADDRITab[Class], 2,
2161 DestReg).addReg(Tmp).addImm(CI->getRawValue());
Chris Lattner667ea022004-06-18 00:50:37 +00002162 return;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002163 }
Chris Lattner48b0c972004-04-11 20:26:20 +00002164 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002165
Chris Lattner48b0c972004-04-11 20:26:20 +00002166 // Special case: op Reg, <const int>
2167 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002168 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002169
Chris Lattner721d2d42004-03-08 01:18:36 +00002170 // xor X, -1 -> not X
2171 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002172 static unsigned const NOTTab[] = {
2173 X86::NOT8r, X86::NOT16r, X86::NOT32r, 0, X86::NOT32r
2174 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002175 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002176 if (Class == cLong) // Invert the top part too
2177 BuildMI(*MBB, IP, X86::NOT32r, 1, DestReg+1).addReg(Op0r+1);
Chris Lattner721d2d42004-03-08 01:18:36 +00002178 return;
2179 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002180
Chris Lattner721d2d42004-03-08 01:18:36 +00002181 // add X, -1 -> dec X
Chris Lattner06521672004-04-06 03:36:57 +00002182 if (OperatorClass == 0 && Op1C->isAllOnesValue() && Class != cLong) {
2183 // Note that we can't use dec for 64-bit decrements, because it does not
2184 // set the carry flag!
2185 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
Chris Lattner721d2d42004-03-08 01:18:36 +00002186 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
2187 return;
2188 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002189
Chris Lattner721d2d42004-03-08 01:18:36 +00002190 // add X, 1 -> inc X
Chris Lattner06521672004-04-06 03:36:57 +00002191 if (OperatorClass == 0 && Op1C->equalsInt(1) && Class != cLong) {
2192 // Note that we can't use inc for 64-bit increments, because it does not
2193 // set the carry flag!
2194 static unsigned const INCTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
Alkis Evlogimenosbee8a092004-04-02 18:11:32 +00002195 BuildMI(*MBB, IP, INCTab[Class], 1, DestReg).addReg(Op0r);
Chris Lattner721d2d42004-03-08 01:18:36 +00002196 return;
2197 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002198
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002199 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002200 // Arithmetic operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002201 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri, 0, X86::ADD32ri }, // ADD
2202 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00002203
Chris Lattner721d2d42004-03-08 01:18:36 +00002204 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002205 { X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, X86::AND32ri }, // AND
2206 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri, 0, X86::OR32ri }, // OR
2207 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, X86::XOR32ri }, // XOR
Chris Lattner721d2d42004-03-08 01:18:36 +00002208 };
2209
Chris Lattner721d2d42004-03-08 01:18:36 +00002210 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner33f7fa32004-04-06 03:15:53 +00002211 unsigned Op1l = cast<ConstantInt>(Op1C)->getRawValue();
Chris Lattner721d2d42004-03-08 01:18:36 +00002212
Chris Lattner33f7fa32004-04-06 03:15:53 +00002213 if (Class != cLong) {
2214 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2215 return;
Chris Lattner6621ed92004-04-11 21:23:56 +00002216 }
2217
2218 // If this is a long value and the high or low bits have a special
2219 // property, emit some special cases.
2220 unsigned Op1h = cast<ConstantInt>(Op1C)->getRawValue() >> 32LL;
2221
2222 // If the constant is zero in the low 32-bits, just copy the low part
2223 // across and apply the normal 32-bit operation to the high parts. There
2224 // will be no carry or borrow into the top.
2225 if (Op1l == 0) {
2226 if (OperatorClass != 2) // All but and...
2227 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0r);
2228 else
2229 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2230 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg+1)
2231 .addReg(Op0r+1).addImm(Op1h);
Chris Lattner33f7fa32004-04-06 03:15:53 +00002232 return;
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002233 }
Chris Lattner6621ed92004-04-11 21:23:56 +00002234
2235 // If this is a logical operation and the top 32-bits are zero, just
2236 // operate on the lower 32.
2237 if (Op1h == 0 && OperatorClass > 1) {
2238 BuildMI(*MBB, IP, OpcodeTab[OperatorClass][cLong], 2, DestReg)
2239 .addReg(Op0r).addImm(Op1l);
2240 if (OperatorClass != 2) // All but and
2241 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(Op0r+1);
2242 else
2243 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
2244 return;
2245 }
2246
2247 // TODO: We could handle lots of other special cases here, such as AND'ing
2248 // with 0xFFFFFFFF00000000 -> noop, etc.
2249
2250 // Otherwise, code generate the full operation with a constant.
2251 static const unsigned TopTab[] = {
2252 X86::ADC32ri, X86::SBB32ri, X86::AND32ri, X86::OR32ri, X86::XOR32ri
2253 };
2254
2255 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addImm(Op1l);
2256 BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg+1)
2257 .addReg(Op0r+1).addImm(Op1h);
2258 return;
Chris Lattner721d2d42004-03-08 01:18:36 +00002259 }
2260
2261 // Finally, handle the general case now.
Chris Lattner7ba92302004-04-06 02:13:25 +00002262 static const unsigned OpcodeTab[][5] = {
Chris Lattner721d2d42004-03-08 01:18:36 +00002263 // Arithmetic operators
Chris Lattner6621ed92004-04-11 21:23:56 +00002264 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, 0, X86::ADD32rr }, // ADD
2265 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, 0, X86::SUB32rr }, // SUB
Chris Lattner721d2d42004-03-08 01:18:36 +00002266
Chris Lattnerb2acc512003-10-19 21:09:10 +00002267 // Bitwise operators
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002268 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, X86::AND32rr }, // AND
2269 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0, X86:: OR32rr }, // OR
2270 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, X86::XOR32rr }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00002271 };
Chris Lattner721d2d42004-03-08 01:18:36 +00002272
Chris Lattnerb2acc512003-10-19 21:09:10 +00002273 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00002274 unsigned Op0r = getReg(Op0, MBB, IP);
2275 unsigned Op1r = getReg(Op1, MBB, IP);
2276 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
2277
Chris Lattnerab1d0e02004-04-06 02:11:49 +00002278 if (Class == cLong) { // Handle the upper 32 bits of long values...
Chris Lattner721d2d42004-03-08 01:18:36 +00002279 static const unsigned TopTab[] = {
2280 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
2281 };
2282 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
2283 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
2284 }
Chris Lattnere2954c82002-11-02 20:04:26 +00002285}
2286
Chris Lattner3e130a22003-01-13 00:32:26 +00002287/// doMultiply - Emit appropriate instructions to multiply together the
2288/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
2289/// result should be given as DestTy.
2290///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002291void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00002292 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00002293 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002294 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00002295 switch (Class) {
Chris Lattner0f1c4612003-06-21 17:16:58 +00002296 case cInt:
2297 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002298 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00002299 .addReg(op0Reg).addReg(op1Reg);
2300 return;
2301 case cByte:
2302 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002303 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
2304 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
2305 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00002306 return;
Chris Lattner94af4142002-12-25 05:13:53 +00002307 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00002308 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00002309 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002310}
2311
Chris Lattnerb2acc512003-10-19 21:09:10 +00002312// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
2313// returns zero when the input is not exactly a power of two.
2314static unsigned ExactLog2(unsigned Val) {
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002315 if (Val == 0 || (Val & (Val-1))) return 0;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002316 unsigned Count = 0;
2317 while (Val != 1) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00002318 Val >>= 1;
2319 ++Count;
2320 }
2321 return Count+1;
2322}
2323
Chris Lattner462fa822004-04-11 20:56:28 +00002324
2325/// doMultiplyConst - This function is specialized to efficiently codegen an 8,
2326/// 16, or 32-bit integer multiply by a constant.
Chris Lattnerb2acc512003-10-19 21:09:10 +00002327void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002328 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00002329 unsigned DestReg, const Type *DestTy,
2330 unsigned op0Reg, unsigned ConstRHS) {
Chris Lattner6ab06d52004-04-06 04:55:43 +00002331 static const unsigned MOVrrTab[] = {X86::MOV8rr, X86::MOV16rr, X86::MOV32rr};
2332 static const unsigned MOVriTab[] = {X86::MOV8ri, X86::MOV16ri, X86::MOV32ri};
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002333 static const unsigned ADDrrTab[] = {X86::ADD8rr, X86::ADD16rr, X86::ADD32rr};
Chris Lattner6ab06d52004-04-06 04:55:43 +00002334
Chris Lattnerb2acc512003-10-19 21:09:10 +00002335 unsigned Class = getClass(DestTy);
2336
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002337 // Handle special cases here.
2338 switch (ConstRHS) {
2339 case 0:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002340 BuildMI(*MBB, IP, MOVriTab[Class], 1, DestReg).addImm(0);
2341 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002342 case 1:
Chris Lattner6ab06d52004-04-06 04:55:43 +00002343 BuildMI(*MBB, IP, MOVrrTab[Class], 1, DestReg).addReg(op0Reg);
2344 return;
Chris Lattner9eb9b8e2004-05-04 15:47:14 +00002345 case 2:
2346 BuildMI(*MBB, IP, ADDrrTab[Class], 1,DestReg).addReg(op0Reg).addReg(op0Reg);
2347 return;
2348 case 3:
2349 case 5:
2350 case 9:
2351 if (Class == cInt) {
2352 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, DestReg),
2353 op0Reg, ConstRHS-1, op0Reg, 0);
2354 return;
2355 }
Chris Lattner6ab06d52004-04-06 04:55:43 +00002356 }
2357
Chris Lattnerb2acc512003-10-19 21:09:10 +00002358 // If the element size is exactly a power of 2, use a shift to get it.
2359 if (unsigned Shift = ExactLog2(ConstRHS)) {
2360 switch (Class) {
2361 default: assert(0 && "Unknown class for this function!");
2362 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002363 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002364 return;
2365 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002366 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002367 return;
2368 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002369 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002370 return;
2371 }
2372 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00002373
2374 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002375 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002376 return;
2377 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002378 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00002379 return;
2380 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00002381
2382 // Most general case, emit a normal multiply...
Chris Lattnerb2acc512003-10-19 21:09:10 +00002383 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00002384 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002385
2386 // Emit a MUL to multiply the register holding the index by
2387 // elementSize, putting the result in OffsetReg.
2388 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
2389}
2390
Chris Lattnerca9671d2002-11-02 20:28:58 +00002391/// visitMul - Multiplies are not simple binary operators because they must deal
2392/// with the EAX register explicitly.
2393///
2394void ISel::visitMul(BinaryOperator &I) {
Chris Lattner462fa822004-04-11 20:56:28 +00002395 unsigned ResultReg = getReg(I);
2396
Chris Lattner95157f72004-04-11 22:05:45 +00002397 Value *Op0 = I.getOperand(0);
2398 Value *Op1 = I.getOperand(1);
2399
2400 // Fold loads into floating point multiplies.
2401 if (getClass(Op0->getType()) == cFP) {
2402 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
2403 if (!I.swapOperands())
2404 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
2405 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2406 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2407 const Type *Ty = Op0->getType();
2408 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2409 unsigned Opcode = Ty == Type::FloatTy ? X86::FMUL32m : X86::FMUL64m;
2410
Chris Lattner95157f72004-04-11 22:05:45 +00002411 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002412 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2413 unsigned FI = getFixedSizedAllocaFI(AI);
2414 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2415 } else {
2416 unsigned BaseReg, Scale, IndexReg, Disp;
2417 getAddressingMode(LI->getOperand(0), BaseReg,
2418 Scale, IndexReg, Disp);
2419
2420 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r),
2421 BaseReg, Scale, IndexReg, Disp);
2422 }
Chris Lattner95157f72004-04-11 22:05:45 +00002423 return;
2424 }
2425 }
2426
Chris Lattner462fa822004-04-11 20:56:28 +00002427 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002428 emitMultiply(BB, IP, Op0, Op1, ResultReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002429}
2430
2431void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2432 Value *Op0, Value *Op1, unsigned DestReg) {
2433 MachineBasicBlock &BB = *MBB;
2434 TypeClass Class = getClass(Op0->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +00002435
2436 // Simple scalar multiply?
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002437 unsigned Op0Reg = getReg(Op0, &BB, IP);
Chris Lattner462fa822004-04-11 20:56:28 +00002438 switch (Class) {
2439 case cByte:
2440 case cShort:
2441 case cInt:
2442 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner462fa822004-04-11 20:56:28 +00002443 unsigned Val = (unsigned)CI->getRawValue(); // Isn't a 64-bit constant
2444 doMultiplyConst(&BB, IP, DestReg, Op0->getType(), Op0Reg, Val);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002445 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002446 unsigned Op1Reg = getReg(Op1, &BB, IP);
2447 doMultiply(&BB, IP, DestReg, Op1->getType(), Op0Reg, Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002448 }
Chris Lattner462fa822004-04-11 20:56:28 +00002449 return;
2450 case cFP:
Chris Lattner6621ed92004-04-11 21:23:56 +00002451 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
2452 return;
Chris Lattner462fa822004-04-11 20:56:28 +00002453 case cLong:
2454 break;
2455 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002456
Chris Lattner462fa822004-04-11 20:56:28 +00002457 // Long value. We have to do things the hard way...
2458 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
2459 unsigned CLow = CI->getRawValue();
2460 unsigned CHi = CI->getRawValue() >> 32;
2461
2462 if (CLow == 0) {
2463 // If the low part of the constant is all zeros, things are simple.
2464 BuildMI(BB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
2465 doMultiplyConst(&BB, IP, DestReg+1, Type::UIntTy, Op0Reg, CHi);
2466 return;
2467 }
2468
2469 // Multiply the two low parts... capturing carry into EDX
2470 unsigned OverflowReg = 0;
2471 if (CLow == 1) {
2472 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(Op0Reg);
Chris Lattner028adc42004-04-06 04:29:36 +00002473 } else {
Chris Lattner462fa822004-04-11 20:56:28 +00002474 unsigned Op1RegL = makeAnotherReg(Type::UIntTy);
2475 OverflowReg = makeAnotherReg(Type::UIntTy);
2476 BuildMI(BB, IP, X86::MOV32ri, 1, Op1RegL).addImm(CLow);
2477 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2478 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1RegL); // AL*BL
Chris Lattner028adc42004-04-06 04:29:36 +00002479
Chris Lattner462fa822004-04-11 20:56:28 +00002480 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2481 BuildMI(BB, IP, X86::MOV32rr, 1,
2482 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2483 }
2484
2485 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2486 doMultiplyConst(&BB, IP, AHBLReg, Type::UIntTy, Op0Reg+1, CLow);
2487
2488 unsigned AHBLplusOverflowReg;
2489 if (OverflowReg) {
2490 AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2491 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002492 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner462fa822004-04-11 20:56:28 +00002493 } else {
2494 AHBLplusOverflowReg = AHBLReg;
2495 }
2496
2497 if (CHi == 0) {
2498 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(AHBLplusOverflowReg);
2499 } else {
Chris Lattner028adc42004-04-06 04:29:36 +00002500 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattner462fa822004-04-11 20:56:28 +00002501 doMultiplyConst(&BB, IP, ALBHReg, Type::UIntTy, Op0Reg, CHi);
Chris Lattner028adc42004-04-06 04:29:36 +00002502
Chris Lattner462fa822004-04-11 20:56:28 +00002503 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Chris Lattner028adc42004-04-06 04:29:36 +00002504 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
2505 }
Chris Lattner462fa822004-04-11 20:56:28 +00002506 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002507 }
Chris Lattner462fa822004-04-11 20:56:28 +00002508
2509 // General 64x64 multiply
2510
2511 unsigned Op1Reg = getReg(Op1, &BB, IP);
2512 // Multiply the two low parts... capturing carry into EDX
2513 BuildMI(BB, IP, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
2514 BuildMI(BB, IP, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
2515
2516 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
2517 BuildMI(BB, IP, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
2518 BuildMI(BB, IP, X86::MOV32rr, 1,
2519 OverflowReg).addReg(X86::EDX); // AL*BL >> 32
2520
2521 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
2522 BuildMI(BB, IP, X86::IMUL32rr, 2,
2523 AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
2524
2525 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
2526 BuildMI(BB, IP, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
2527 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
2528
2529 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
2530 BuildMI(BB, IP, X86::IMUL32rr, 2,
2531 ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
2532
2533 BuildMI(BB, IP, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
2534 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002535}
Chris Lattnerca9671d2002-11-02 20:28:58 +00002536
Chris Lattner06925362002-11-17 21:56:38 +00002537
Chris Lattnerf01729e2002-11-02 20:54:46 +00002538/// visitDivRem - Handle division and remainder instructions... these
2539/// instruction both require the same instructions to be generated, they just
2540/// select the result from a different register. Note that both of these
2541/// instructions work differently for signed and unsigned operands.
2542///
2543void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00002544 unsigned ResultReg = getReg(I);
Chris Lattner95157f72004-04-11 22:05:45 +00002545 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2546
2547 // Fold loads into floating point divides.
2548 if (getClass(Op0->getType()) == cFP) {
2549 if (LoadInst *LI = dyn_cast<LoadInst>(Op1))
2550 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2551 const Type *Ty = Op0->getType();
2552 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2553 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIV32m : X86::FDIV64m;
2554
Chris Lattner95157f72004-04-11 22:05:45 +00002555 unsigned Op0r = getReg(Op0);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002556 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2557 unsigned FI = getFixedSizedAllocaFI(AI);
2558 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r), FI);
2559 } else {
2560 unsigned BaseReg, Scale, IndexReg, Disp;
2561 getAddressingMode(LI->getOperand(0), BaseReg,
2562 Scale, IndexReg, Disp);
2563
2564 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op0r),
2565 BaseReg, Scale, IndexReg, Disp);
2566 }
Chris Lattner95157f72004-04-11 22:05:45 +00002567 return;
2568 }
2569
2570 if (LoadInst *LI = dyn_cast<LoadInst>(Op0))
2571 if (isSafeToFoldLoadIntoInstruction(*LI, I)) {
2572 const Type *Ty = Op0->getType();
2573 assert(Ty == Type::FloatTy||Ty == Type::DoubleTy && "Unknown FP type!");
2574 unsigned Opcode = Ty == Type::FloatTy ? X86::FDIVR32m : X86::FDIVR64m;
2575
Chris Lattner95157f72004-04-11 22:05:45 +00002576 unsigned Op1r = getReg(Op1);
Chris Lattner9f1b5312004-05-13 15:12:43 +00002577 if (AllocaInst *AI = dyn_castFixedAlloca(LI->getOperand(0))) {
2578 unsigned FI = getFixedSizedAllocaFI(AI);
2579 addFrameReference(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r), FI);
2580 } else {
2581 unsigned BaseReg, Scale, IndexReg, Disp;
2582 getAddressingMode(LI->getOperand(0), BaseReg, Scale, IndexReg, Disp);
2583 addFullAddress(BuildMI(BB, Opcode, 5, ResultReg).addReg(Op1r),
2584 BaseReg, Scale, IndexReg, Disp);
2585 }
Chris Lattner95157f72004-04-11 22:05:45 +00002586 return;
2587 }
2588 }
2589
Chris Lattner94af4142002-12-25 05:13:53 +00002590
Chris Lattnercadff442003-10-23 17:21:43 +00002591 MachineBasicBlock::iterator IP = BB->end();
Chris Lattner95157f72004-04-11 22:05:45 +00002592 emitDivRemOperation(BB, IP, Op0, Op1,
Chris Lattner462fa822004-04-11 20:56:28 +00002593 I.getOpcode() == Instruction::Div, ResultReg);
Chris Lattnercadff442003-10-23 17:21:43 +00002594}
2595
2596void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002597 MachineBasicBlock::iterator IP,
Chris Lattner462fa822004-04-11 20:56:28 +00002598 Value *Op0, Value *Op1, bool isDiv,
2599 unsigned ResultReg) {
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002600 const Type *Ty = Op0->getType();
2601 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00002602 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002603 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00002604 if (isDiv) {
Chris Lattner6621ed92004-04-11 21:23:56 +00002605 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
2606 return;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002607 } else { // Floating point remainder...
Chris Lattner462fa822004-04-11 20:56:28 +00002608 unsigned Op0Reg = getReg(Op0, BB, IP);
2609 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002610 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002611 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002612 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002613 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
2614 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002615 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
2616 }
Chris Lattner94af4142002-12-25 05:13:53 +00002617 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00002618 case cLong: {
2619 static const char *FnName[] =
2620 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
Chris Lattner462fa822004-04-11 20:56:28 +00002621 unsigned Op0Reg = getReg(Op0, BB, IP);
2622 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattner8ebf1c32004-04-11 21:09:14 +00002623 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00002624 MachineInstr *TheCall =
2625 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
2626
2627 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00002628 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
2629 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00002630 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
2631 return;
2632 }
2633 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00002634 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00002635 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00002636 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00002637
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002638 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002639 static const unsigned NEGOpcode[] = { X86::NEG8r, X86::NEG16r, X86::NEG32r };
2640 static const unsigned SAROpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
2641 static const unsigned SHROpcode[]={ X86::SHR8ri, X86::SHR16ri, X86::SHR32ri };
2642 static const unsigned ADDOpcode[]={ X86::ADD8rr, X86::ADD16rr, X86::ADD32rr };
2643
2644 // Special case signed division by power of 2.
2645 if (isDiv)
2646 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1)) {
2647 assert(Class != cLong && "This doesn't handle 64-bit divides!");
2648 int V = CI->getValue();
2649
2650 if (V == 1) { // X /s 1 => X
2651 unsigned Op0Reg = getReg(Op0, BB, IP);
2652 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2653 return;
2654 }
2655
2656 if (V == -1) { // X /s -1 => -X
2657 unsigned Op0Reg = getReg(Op0, BB, IP);
2658 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(Op0Reg);
2659 return;
2660 }
2661
2662 bool isNeg = false;
2663 if (V < 0) { // Not a positive power of 2?
2664 V = -V;
2665 isNeg = true; // Maybe it's a negative power of 2.
2666 }
2667 if (unsigned Log = ExactLog2(V)) {
2668 --Log;
2669 unsigned Op0Reg = getReg(Op0, BB, IP);
2670 unsigned TmpReg = makeAnotherReg(Op0->getType());
2671 if (Log != 1)
2672 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg)
2673 .addReg(Op0Reg).addImm(Log-1);
2674 else
2675 BuildMI(*BB, IP, MovOpcode[Class], 1, TmpReg).addReg(Op0Reg);
2676 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
2677 BuildMI(*BB, IP, SHROpcode[Class], 2, TmpReg2)
2678 .addReg(TmpReg).addImm(32-Log);
2679 unsigned TmpReg3 = makeAnotherReg(Op0->getType());
2680 BuildMI(*BB, IP, ADDOpcode[Class], 2, TmpReg3)
2681 .addReg(Op0Reg).addReg(TmpReg2);
2682
2683 unsigned TmpReg4 = isNeg ? makeAnotherReg(Op0->getType()) : ResultReg;
2684 BuildMI(*BB, IP, SAROpcode[Class], 2, TmpReg4)
2685 .addReg(Op0Reg).addImm(Log);
2686 if (isNeg)
2687 BuildMI(*BB, IP, NEGOpcode[Class], 1, ResultReg).addReg(TmpReg4);
2688 return;
2689 }
2690 }
2691
2692 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002693 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00002694 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
2695
2696 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002697 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
2698 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00002699 };
2700
Chris Lattnerf01729e2002-11-02 20:54:46 +00002701 unsigned Reg = Regs[Class];
2702 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00002703
2704 // Put the first operand into one of the A registers...
Chris Lattner462fa822004-04-11 20:56:28 +00002705 unsigned Op0Reg = getReg(Op0, BB, IP);
2706 unsigned Op1Reg = getReg(Op1, BB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00002707 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002708
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002709 if (Ty->isSigned()) {
Chris Lattnerf01729e2002-11-02 20:54:46 +00002710 // Emit a sign extension instruction...
Chris Lattner462fa822004-04-11 20:56:28 +00002711 unsigned ShiftResult = makeAnotherReg(Op0->getType());
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002712 BuildMI(*BB, IP, SAROpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
Chris Lattneree352852004-02-29 07:22:16 +00002713 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002714
2715 // Emit the appropriate divide or remainder instruction...
2716 BuildMI(*BB, IP, DivOpcode[1][Class], 1).addReg(Op1Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002717 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00002718 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00002719 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00002720
Chris Lattnerc8af02c2004-05-04 19:33:58 +00002721 // Emit the appropriate divide or remainder instruction...
2722 BuildMI(*BB, IP, DivOpcode[0][Class], 1).addReg(Op1Reg);
2723 }
Chris Lattner06925362002-11-17 21:56:38 +00002724
Chris Lattnerf01729e2002-11-02 20:54:46 +00002725 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00002726 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00002727
Chris Lattnerf01729e2002-11-02 20:54:46 +00002728 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00002729 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00002730}
Chris Lattnere2954c82002-11-02 20:04:26 +00002731
Chris Lattner06925362002-11-17 21:56:38 +00002732
Brian Gaekea1719c92002-10-31 23:03:59 +00002733/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
2734/// for constant immediate shift values, and for constant immediate
2735/// shift values equal to 1. Even the general case is sort of special,
2736/// because the shift amount has to be in CL, not just any old register.
2737///
Chris Lattner3e130a22003-01-13 00:32:26 +00002738void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002739 MachineBasicBlock::iterator IP = BB->end ();
2740 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
2741 I.getOpcode () == Instruction::Shl, I.getType (),
2742 getReg (I));
2743}
2744
2745/// emitShiftOperation - Common code shared between visitShiftInst and
2746/// constant expression support.
2747void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002748 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002749 Value *Op, Value *ShiftAmount, bool isLeftShift,
2750 const Type *ResultTy, unsigned DestReg) {
2751 unsigned SrcReg = getReg (Op, MBB, IP);
2752 bool isSigned = ResultTy->isSigned ();
2753 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002754
2755 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002756 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2757 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2758 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2759 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002760 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002761
Chris Lattner3e130a22003-01-13 00:32:26 +00002762 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002763 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2764 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2765 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2766 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002767 };
Chris Lattner796df732002-11-02 00:44:25 +00002768
Chris Lattner3e130a22003-01-13 00:32:26 +00002769 // Longs, as usual, are handled specially...
2770 if (Class == cLong) {
2771 // If we have a constant shift, we can generate much more efficient code
2772 // than otherwise...
2773 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002774 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002775 unsigned Amount = CUI->getValue();
2776 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002777 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2778 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002779 BuildMI(*MBB, IP, Opc[3], 3,
2780 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2781 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002782 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002783 BuildMI(*MBB, IP, Opc[3], 3,
2784 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2785 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002786 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002787 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002788 Amount -= 32;
2789 if (isLeftShift) {
Chris Lattner722070e2004-04-06 03:42:38 +00002790 if (Amount != 0) {
2791 BuildMI(*MBB, IP, X86::SHL32ri, 2,
2792 DestReg + 1).addReg(SrcReg).addImm(Amount);
2793 } else {
2794 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg);
2795 }
2796 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002797 } else {
Chris Lattner722070e2004-04-06 03:42:38 +00002798 if (Amount != 0) {
2799 BuildMI(*MBB, IP, isSigned ? X86::SAR32ri : X86::SHR32ri, 2,
2800 DestReg).addReg(SrcReg+1).addImm(Amount);
2801 } else {
2802 BuildMI(*MBB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg+1);
2803 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002804 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002805 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002806 }
2807 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002808 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2809
2810 if (!isLeftShift && isSigned) {
2811 // If this is a SHR of a Long, then we need to do funny sign extension
2812 // stuff. TmpReg gets the value to use as the high-part if we are
2813 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002814 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002815 } else {
2816 // Other shifts use a fixed zero value if the shift is more than 32
2817 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002818 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002819 }
2820
2821 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002822 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002823 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002824
2825 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2826 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2827 if (isLeftShift) {
2828 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002829 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002830 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002831 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002832 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002833
2834 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002835 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002836
2837 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002838 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002839 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2840 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002841 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002842 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002843 } else {
2844 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002845 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002846 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002847 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002848 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002849 .addReg(SrcReg+1);
2850
2851 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002852 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002853
2854 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002855 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002856 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2857
2858 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002859 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002860 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2861 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002862 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002863 return;
2864 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002865
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002866 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002867 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2868 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002869
Chris Lattner3e130a22003-01-13 00:32:26 +00002870 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002871 BuildMI(*MBB, IP, Opc[Class], 2,
2872 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002873 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002874 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002875 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002876
Chris Lattner3e130a22003-01-13 00:32:26 +00002877 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002878 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002879 }
2880}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002881
Chris Lattner3e130a22003-01-13 00:32:26 +00002882
Chris Lattner6fc3c522002-11-17 21:11:55 +00002883/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002884/// instruction. The load and store instructions are the only place where we
2885/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002886///
2887void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002888 // Check to see if this load instruction is going to be folded into a binary
2889 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2890 // pattern matching instruction selector be nice?
Chris Lattner95157f72004-04-11 22:05:45 +00002891 unsigned Class = getClassB(I.getType());
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002892 if (I.hasOneUse()) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002893 Instruction *User = cast<Instruction>(I.use_back());
2894 switch (User->getOpcode()) {
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002895 case Instruction::Cast:
2896 // If this is a cast from a signed-integer type to a floating point type,
2897 // fold the cast here.
John Criswell6b5bd582004-06-09 15:18:51 +00002898 if (getClassB(User->getType()) == cFP &&
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002899 (I.getType() == Type::ShortTy || I.getType() == Type::IntTy ||
2900 I.getType() == Type::LongTy)) {
2901 unsigned DestReg = getReg(User);
2902 static const unsigned Opcode[] = {
2903 0/*BYTE*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m
2904 };
Chris Lattner9f1b5312004-05-13 15:12:43 +00002905
2906 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
2907 unsigned FI = getFixedSizedAllocaFI(AI);
2908 addFrameReference(BuildMI(BB, Opcode[Class], 4, DestReg), FI);
2909 } else {
2910 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
2911 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
2912 addFullAddress(BuildMI(BB, Opcode[Class], 4, DestReg),
2913 BaseReg, Scale, IndexReg, Disp);
2914 }
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002915 return;
2916 } else {
2917 User = 0;
2918 }
2919 break;
Chris Lattner13c07fe2004-04-12 00:12:04 +00002920
Chris Lattner7dee5da2004-03-08 01:58:35 +00002921 case Instruction::Add:
2922 case Instruction::Sub:
2923 case Instruction::And:
2924 case Instruction::Or:
2925 case Instruction::Xor:
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002926 if (Class == cLong) User = 0;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002927 break;
Chris Lattner95157f72004-04-11 22:05:45 +00002928 case Instruction::Mul:
2929 case Instruction::Div:
Chris Lattner13c07fe2004-04-12 00:12:04 +00002930 if (Class != cFP) User = 0;
Chris Lattnerfeac3e12004-04-11 23:21:26 +00002931 break; // Folding only implemented for floating point.
Chris Lattner95157f72004-04-11 22:05:45 +00002932 default: User = 0; break;
Chris Lattner7dee5da2004-03-08 01:58:35 +00002933 }
2934
2935 if (User) {
2936 // Okay, we found a user. If the load is the first operand and there is
2937 // no second operand load, reverse the operand ordering. Note that this
2938 // can fail for a subtract (ie, no change will be made).
2939 if (!isa<LoadInst>(User->getOperand(1)))
2940 cast<BinaryOperator>(User)->swapOperands();
2941
2942 // Okay, now that everything is set up, if this load is used by the second
2943 // operand, and if there are no instructions that invalidate the load
2944 // before the binary operator, eliminate the load.
2945 if (User->getOperand(1) == &I &&
2946 isSafeToFoldLoadIntoInstruction(I, *User))
2947 return; // Eliminate the load!
Chris Lattner95157f72004-04-11 22:05:45 +00002948
2949 // If this is a floating point sub or div, we won't be able to swap the
2950 // operands, but we will still be able to eliminate the load.
2951 if (Class == cFP && User->getOperand(0) == &I &&
2952 !isa<LoadInst>(User->getOperand(1)) &&
2953 (User->getOpcode() == Instruction::Sub ||
2954 User->getOpcode() == Instruction::Div) &&
2955 isSafeToFoldLoadIntoInstruction(I, *User))
2956 return; // Eliminate the load!
Chris Lattner7dee5da2004-03-08 01:58:35 +00002957 }
2958 }
2959
Chris Lattner6ac1d712003-10-20 04:48:06 +00002960 static const unsigned Opcodes[] = {
Chris Lattner9f1b5312004-05-13 15:12:43 +00002961 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m, X86::MOV32rm
Chris Lattner3e130a22003-01-13 00:32:26 +00002962 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00002963 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002964 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00002965
2966 unsigned DestReg = getReg(I);
2967
2968 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(0))) {
2969 unsigned FI = getFixedSizedAllocaFI(AI);
2970 if (Class == cLong) {
2971 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg), FI);
2972 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), FI, 4);
2973 } else {
2974 addFrameReference(BuildMI(BB, Opcode, 4, DestReg), FI);
2975 }
2976 } else {
2977 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
2978 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
2979
2980 if (Class == cLong) {
2981 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg),
2982 BaseReg, Scale, IndexReg, Disp);
2983 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1),
2984 BaseReg, Scale, IndexReg, Disp+4);
2985 } else {
2986 addFullAddress(BuildMI(BB, Opcode, 4, DestReg),
2987 BaseReg, Scale, IndexReg, Disp);
2988 }
2989 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002990}
2991
Chris Lattner6fc3c522002-11-17 21:11:55 +00002992/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
2993/// instruction.
2994///
2995void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00002996 unsigned BaseReg = ~0U, Scale = ~0U, IndexReg = ~0U, Disp = ~0U;
2997 unsigned AllocaFrameIdx = ~0U;
2998
2999 if (AllocaInst *AI = dyn_castFixedAlloca(I.getOperand(1)))
3000 AllocaFrameIdx = getFixedSizedAllocaFI(AI);
3001 else
3002 getAddressingMode(I.getOperand(1), BaseReg, Scale, IndexReg, Disp);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003003
Chris Lattner6c09db22003-10-20 04:11:23 +00003004 const Type *ValTy = I.getOperand(0)->getType();
3005 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00003006
Chris Lattner5a830962004-02-25 02:56:58 +00003007 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
3008 uint64_t Val = CI->getRawValue();
3009 if (Class == cLong) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003010 if (AllocaFrameIdx != ~0U) {
3011 addFrameReference(BuildMI(BB, X86::MOV32mi, 5),
3012 AllocaFrameIdx).addImm(Val & ~0U);
3013 addFrameReference(BuildMI(BB, X86::MOV32mi, 5),
3014 AllocaFrameIdx, 4).addImm(Val>>32);
3015 } else {
3016 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
3017 BaseReg, Scale, IndexReg, Disp).addImm(Val & ~0U);
3018 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
3019 BaseReg, Scale, IndexReg, Disp+4).addImm(Val>>32);
3020 }
Chris Lattner5a830962004-02-25 02:56:58 +00003021 } else {
3022 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003023 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00003024 };
3025 unsigned Opcode = Opcodes[Class];
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003026 if (AllocaFrameIdx != ~0U)
Chris Lattner9f1b5312004-05-13 15:12:43 +00003027 addFrameReference(BuildMI(BB, Opcode, 5), AllocaFrameIdx).addImm(Val);
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003028 else
Chris Lattner9f1b5312004-05-13 15:12:43 +00003029 addFullAddress(BuildMI(BB, Opcode, 5),
3030 BaseReg, Scale, IndexReg, Disp).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00003031 }
Chris Lattnerb7cb9ff2004-05-13 15:26:48 +00003032 } else if (isa<ConstantPointerNull>(I.getOperand(0))) {
3033 if (AllocaFrameIdx != ~0U)
3034 addFrameReference(BuildMI(BB, X86::MOV32mi, 5), AllocaFrameIdx).addImm(0);
3035 else
3036 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
3037 BaseReg, Scale, IndexReg, Disp).addImm(0);
3038
Chris Lattner5a830962004-02-25 02:56:58 +00003039 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003040 if (AllocaFrameIdx != ~0U)
3041 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
3042 AllocaFrameIdx).addImm(CB->getValue());
3043 else
3044 addFullAddress(BuildMI(BB, X86::MOV8mi, 5),
3045 BaseReg, Scale, IndexReg, Disp).addImm(CB->getValue());
Chris Lattnere7a31c92004-05-07 21:18:15 +00003046 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) {
3047 // Store constant FP values with integer instructions to avoid having to
3048 // load the constants from the constant pool then do a store.
3049 if (CFP->getType() == Type::FloatTy) {
3050 union {
3051 unsigned I;
3052 float F;
3053 } V;
3054 V.F = CFP->getValue();
Chris Lattner9f1b5312004-05-13 15:12:43 +00003055 if (AllocaFrameIdx != ~0U)
3056 addFrameReference(BuildMI(BB, X86::MOV32mi, 5),
3057 AllocaFrameIdx).addImm(V.I);
3058 else
3059 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
3060 BaseReg, Scale, IndexReg, Disp).addImm(V.I);
Chris Lattner5a830962004-02-25 02:56:58 +00003061 } else {
Chris Lattnere7a31c92004-05-07 21:18:15 +00003062 union {
3063 uint64_t I;
3064 double F;
3065 } V;
3066 V.F = CFP->getValue();
Chris Lattner9f1b5312004-05-13 15:12:43 +00003067 if (AllocaFrameIdx != ~0U) {
3068 addFrameReference(BuildMI(BB, X86::MOV32mi, 5),
3069 AllocaFrameIdx).addImm((unsigned)V.I);
3070 addFrameReference(BuildMI(BB, X86::MOV32mi, 5),
3071 AllocaFrameIdx, 4).addImm(unsigned(V.I >> 32));
3072 } else {
3073 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
3074 BaseReg, Scale, IndexReg, Disp).addImm((unsigned)V.I);
3075 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
3076 BaseReg, Scale, IndexReg, Disp+4).addImm(
Chris Lattnere7a31c92004-05-07 21:18:15 +00003077 unsigned(V.I >> 32));
Chris Lattner9f1b5312004-05-13 15:12:43 +00003078 }
Chris Lattner5a830962004-02-25 02:56:58 +00003079 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003080
3081 } else if (Class == cLong) {
3082 unsigned ValReg = getReg(I.getOperand(0));
Chris Lattner9f1b5312004-05-13 15:12:43 +00003083 if (AllocaFrameIdx != ~0U) {
3084 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
3085 AllocaFrameIdx).addReg(ValReg);
3086 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
3087 AllocaFrameIdx, 4).addReg(ValReg+1);
3088 } else {
3089 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
3090 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
3091 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
3092 BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1);
3093 }
Chris Lattnere7a31c92004-05-07 21:18:15 +00003094 } else {
3095 unsigned ValReg = getReg(I.getOperand(0));
3096 static const unsigned Opcodes[] = {
3097 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
3098 };
3099 unsigned Opcode = Opcodes[Class];
3100 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003101
3102 if (AllocaFrameIdx != ~0U)
3103 addFrameReference(BuildMI(BB, Opcode, 5), AllocaFrameIdx).addReg(ValReg);
3104 else
3105 addFullAddress(BuildMI(BB, Opcode, 1+4),
3106 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003107 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00003108}
3109
3110
Misha Brukman538607f2004-03-01 23:53:11 +00003111/// visitCastInst - Here we have various kinds of copying with or without sign
3112/// extension going on.
3113///
Chris Lattner3e130a22003-01-13 00:32:26 +00003114void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003115 Value *Op = CI.getOperand(0);
Chris Lattner427aeb42004-04-11 19:21:59 +00003116
Chris Lattner99382862004-04-12 00:23:04 +00003117 unsigned SrcClass = getClassB(Op->getType());
3118 unsigned DestClass = getClassB(CI.getType());
3119 // Noop casts are not emitted: getReg will return the source operand as the
3120 // register to use for any uses of the noop cast.
Chris Lattner8b486a12004-06-29 00:14:38 +00003121 if (DestClass == SrcClass) {
3122 // The only detail in this plan is that casts from double -> float are
3123 // truncating operations that we have to codegen through memory (despite
3124 // the fact that the source/dest registers are the same class).
3125 if (CI.getType() != Type::FloatTy || Op->getType() != Type::DoubleTy)
3126 return;
3127 }
Chris Lattner427aeb42004-04-11 19:21:59 +00003128
Chris Lattnerf5854472003-06-21 16:01:24 +00003129 // If this is a cast from a 32-bit integer to a Long type, and the only uses
3130 // of the case are GEP instructions, then the cast does not need to be
3131 // generated explicitly, it will be folded into the GEP.
Chris Lattner99382862004-04-12 00:23:04 +00003132 if (DestClass == cLong && SrcClass == cInt) {
Chris Lattnerf5854472003-06-21 16:01:24 +00003133 bool AllUsesAreGEPs = true;
3134 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
3135 if (!isa<GetElementPtrInst>(*I)) {
3136 AllUsesAreGEPs = false;
3137 break;
3138 }
3139
3140 // No need to codegen this cast if all users are getelementptr instrs...
3141 if (AllUsesAreGEPs) return;
3142 }
3143
Chris Lattner99382862004-04-12 00:23:04 +00003144 // If this cast converts a load from a short,int, or long integer to a FP
3145 // value, we will have folded this cast away.
3146 if (DestClass == cFP && isa<LoadInst>(Op) && Op->hasOneUse() &&
3147 (Op->getType() == Type::ShortTy || Op->getType() == Type::IntTy ||
3148 Op->getType() == Type::LongTy))
3149 return;
3150
3151
Chris Lattner548f61d2003-04-23 17:22:12 +00003152 unsigned DestReg = getReg(CI);
3153 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00003154 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00003155}
3156
Misha Brukman538607f2004-03-01 23:53:11 +00003157/// emitCastOperation - Common code shared between visitCastInst and constant
3158/// expression cast support.
3159///
Chris Lattner548f61d2003-04-23 17:22:12 +00003160void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003161 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00003162 Value *Src, const Type *DestTy,
3163 unsigned DestReg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003164 const Type *SrcTy = Src->getType();
3165 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00003166 unsigned DestClass = getClassB(DestTy);
Chris Lattnerfeac3e12004-04-11 23:21:26 +00003167 unsigned SrcReg = getReg(Src, BB, IP);
3168
Chris Lattner3e130a22003-01-13 00:32:26 +00003169 // Implement casts to bool by using compare on the operand followed by set if
3170 // not zero on the result.
3171 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00003172 switch (SrcClass) {
3173 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003174 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003175 break;
3176 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003177 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003178 break;
3179 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003180 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00003181 break;
3182 case cLong: {
3183 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003184 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00003185 break;
3186 }
3187 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00003188 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003189 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00003190 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00003191 break;
Chris Lattner20772542003-06-01 03:38:24 +00003192 }
3193
3194 // If the zero flag is not set, then the value is true, set the byte to
3195 // true.
Chris Lattneree352852004-02-29 07:22:16 +00003196 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00003197 return;
3198 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003199
3200 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003201 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00003202 };
3203
3204 // Implement casts between values of the same type class (as determined by
3205 // getClass) by using a register-to-register move.
3206 if (SrcClass == DestClass) {
3207 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00003208 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003209 } else if (SrcClass == cFP) {
3210 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003211 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00003212 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003213 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003214 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
3215 "Unknown cFP member!");
3216 // Truncate from double to float by storing to memory as short, then
3217 // reading it back.
3218 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00003219 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003220 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
3221 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003222 }
3223 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003224 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
3225 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003226 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00003227 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003228 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00003229 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003230 return;
3231 }
3232
3233 // Handle cast of SMALLER int to LARGER int using a move with sign extension
3234 // or zero extension, depending on whether the source type was signed.
3235 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
3236 SrcClass < DestClass) {
3237 bool isLong = DestClass == cLong;
3238 if (isLong) DestClass = cInt;
3239
3240 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003241 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
3242 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00003243 };
3244
Chris Lattner96e3b422004-05-09 22:28:45 +00003245 bool isUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
Chris Lattneree352852004-02-29 07:22:16 +00003246 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00003247 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003248
3249 if (isLong) { // Handle upper 32 bits as appropriate...
3250 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003251 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00003252 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003253 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00003254 }
Chris Lattner3e130a22003-01-13 00:32:26 +00003255 return;
3256 }
3257
3258 // Special case long -> int ...
3259 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003260 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003261 return;
3262 }
3263
3264 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
3265 // move out of AX or AL.
3266 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
3267 && SrcClass > DestClass) {
3268 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00003269 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
3270 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00003271 return;
3272 }
3273
3274 // Handle casts from integer to floating point now...
3275 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003276 // Promote the integer to a type supported by FLD. We do this because there
3277 // are no unsigned FLD instructions, so we must promote an unsigned value to
3278 // a larger signed value, then use FLD on the larger value.
3279 //
3280 const Type *PromoteType = 0;
Chris Lattner85aa7092004-04-10 18:32:01 +00003281 unsigned PromoteOpcode = 0;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003282 unsigned RealDestReg = DestReg;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003283 switch (SrcTy->getTypeID()) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003284 case Type::BoolTyID:
3285 case Type::SByteTyID:
3286 // We don't have the facilities for directly loading byte sized data from
3287 // memory (even signed). Promote it to 16 bits.
3288 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003289 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003290 break;
3291 case Type::UByteTyID:
3292 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003293 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003294 break;
3295 case Type::UShortTyID:
3296 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003297 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003298 break;
3299 case Type::UIntTyID: {
3300 // Make a 64 bit temporary... and zero out the top of it...
3301 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003302 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
3303 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003304 SrcTy = Type::LongTy;
3305 SrcClass = cLong;
3306 SrcReg = TmpReg;
3307 break;
3308 }
3309 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003310 // Don't fild into the read destination.
3311 DestReg = makeAnotherReg(Type::DoubleTy);
3312 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003313 default: // No promotion needed...
3314 break;
3315 }
3316
3317 if (PromoteType) {
3318 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattner7b92de1e2004-04-06 19:29:36 +00003319 BuildMI(*BB, IP, PromoteOpcode, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00003320 SrcTy = PromoteType;
3321 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00003322 SrcReg = TmpReg;
3323 }
3324
3325 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003326 int FrameIdx =
3327 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00003328
3329 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003330 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003331 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003332 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003333 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003334 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003335 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00003336 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
3337 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003338 }
3339
3340 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003341 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00003342 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003343
3344 // We need special handling for unsigned 64-bit integer sources. If the
3345 // input number has the "sign bit" set, then we loaded it incorrectly as a
3346 // negative 64-bit number. In this case, add an offset value.
3347 if (SrcTy == Type::ULongTy) {
3348 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003349 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003350
Chris Lattnerb6bac512004-02-25 06:13:04 +00003351 // If the sign bit is set, get a pointer to an offset, otherwise get a
3352 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003353 MachineConstantPool *CP = F->getConstantPool();
3354 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003355 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003356 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003357 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003358 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003359 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
3360
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003361 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003362 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003363 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003364 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003365
3366 // Load the constant for an add. FIXME: this could make an 'fadd' that
3367 // reads directly from memory, but we don't support these yet.
3368 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003369 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003370
Chris Lattneree352852004-02-29 07:22:16 +00003371 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
3372 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00003373 }
3374
Chris Lattner3e130a22003-01-13 00:32:26 +00003375 return;
3376 }
3377
3378 // Handle casts from floating point to integer now...
3379 if (SrcClass == cFP) {
3380 // Change the floating point control register to use "round towards zero"
3381 // mode when truncating to an integer value.
3382 //
3383 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003384 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003385
3386 // Load the old value of the high byte of the control word...
3387 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003388 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00003389 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00003390
3391 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003392 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00003393 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00003394
3395 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003396 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003397
3398 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003399 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003400 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00003401
3402 // We don't have the facilities for directly storing byte sized data to
3403 // memory. Promote it to 16 bits. We also must promote unsigned values to
3404 // larger classes because we only have signed FP stores.
3405 unsigned StoreClass = DestClass;
3406 const Type *StoreTy = DestTy;
3407 if (StoreClass == cByte || DestTy->isUnsigned())
3408 switch (StoreClass) {
3409 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
3410 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
3411 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00003412 // The following treatment of cLong may not be perfectly right,
3413 // but it survives chains of casts of the form
3414 // double->ulong->double.
3415 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00003416 default: assert(0 && "Unknown store class!");
3417 }
3418
3419 // Spill the integer to memory and reload it from there...
3420 int FrameIdx =
3421 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
3422
3423 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003424 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00003425 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
3426 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00003427
3428 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003429 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
3430 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00003431 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00003432 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003433 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00003434 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003435 }
3436
3437 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003438 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00003439 return;
3440 }
3441
Brian Gaeked474e9c2002-12-06 10:49:33 +00003442 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00003443 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00003444 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00003445}
Brian Gaekea1719c92002-10-31 23:03:59 +00003446
Chris Lattner73815062003-10-18 05:56:40 +00003447/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00003448///
Chris Lattner73815062003-10-18 05:56:40 +00003449void ISel::visitVANextInst(VANextInst &I) {
3450 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00003451 unsigned DestReg = getReg(I);
3452
Chris Lattnereca195e2003-05-08 19:44:13 +00003453 unsigned Size;
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003454 switch (I.getArgType()->getTypeID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00003455 default:
3456 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00003457 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00003458 return;
3459 case Type::PointerTyID:
3460 case Type::UIntTyID:
3461 case Type::IntTyID:
3462 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00003463 break;
3464 case Type::ULongTyID:
3465 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00003466 case Type::DoubleTyID:
3467 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00003468 break;
3469 }
3470
3471 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003472 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00003473}
Chris Lattnereca195e2003-05-08 19:44:13 +00003474
Chris Lattner73815062003-10-18 05:56:40 +00003475void ISel::visitVAArgInst(VAArgInst &I) {
3476 unsigned VAList = getReg(I.getOperand(0));
3477 unsigned DestReg = getReg(I);
3478
Chris Lattnerf70c22b2004-06-17 18:19:28 +00003479 switch (I.getType()->getTypeID()) {
Chris Lattner73815062003-10-18 05:56:40 +00003480 default:
3481 std::cerr << I;
3482 assert(0 && "Error: bad type for va_next instruction!");
3483 return;
3484 case Type::PointerTyID:
3485 case Type::UIntTyID:
3486 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003487 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003488 break;
3489 case Type::ULongTyID:
3490 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003491 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
3492 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00003493 break;
3494 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003495 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00003496 break;
3497 }
Chris Lattnereca195e2003-05-08 19:44:13 +00003498}
3499
Misha Brukman538607f2004-03-01 23:53:11 +00003500/// visitGetElementPtrInst - instruction-select GEP instructions
3501///
Chris Lattner3e130a22003-01-13 00:32:26 +00003502void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00003503 // If this GEP instruction will be folded into all of its users, we don't need
3504 // to explicitly calculate it!
3505 unsigned A, B, C, D;
3506 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) {
3507 // Check all of the users of the instruction to see if they are loads and
3508 // stores.
3509 bool AllWillFold = true;
3510 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
3511 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
3512 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
3513 cast<Instruction>(*UI)->getOperand(0) == &I) {
3514 AllWillFold = false;
3515 break;
3516 }
3517
3518 // If the instruction is foldable, and will be folded into all users, don't
3519 // emit it!
3520 if (AllWillFold) return;
3521 }
3522
Chris Lattner3e130a22003-01-13 00:32:26 +00003523 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00003524 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00003525 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00003526}
3527
Chris Lattner985fe3d2004-02-25 03:45:50 +00003528/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
3529/// GEPTypes (the derived types being stepped through at each level). On return
3530/// from this function, if some indexes of the instruction are representable as
3531/// an X86 lea instruction, the machine operands are put into the Ops
3532/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
3533/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
3534/// addressing mode that only partially consumes the input, the BaseReg input of
3535/// the addressing mode must be left free.
3536///
3537/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
3538///
Chris Lattnerb6bac512004-02-25 06:13:04 +00003539void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
3540 std::vector<Value*> &GEPOps,
3541 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
3542 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
3543 const TargetData &TD = TM.getTargetData();
3544
Chris Lattner985fe3d2004-02-25 03:45:50 +00003545 // Clear out the state we are working with...
Chris Lattnerb6bac512004-02-25 06:13:04 +00003546 BaseReg = 0; // No base register
3547 Scale = 1; // Unit scale
3548 IndexReg = 0; // No index register
3549 Disp = 0; // No displacement
3550
Chris Lattner985fe3d2004-02-25 03:45:50 +00003551 // While there are GEP indexes that can be folded into the current address,
3552 // keep processing them.
3553 while (!GEPTypes.empty()) {
3554 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
3555 // It's a struct access. CUI is the index into the structure,
3556 // which names the field. This index must have unsigned type.
3557 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
3558
3559 // Use the TargetData structure to pick out what the layout of the
3560 // structure is in memory. Since the structure index must be constant, we
3561 // can get its value and use it to find the right byte offset from the
3562 // StructLayout class's list of structure member offsets.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003563 Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00003564 GEPOps.pop_back(); // Consume a GEP operand
3565 GEPTypes.pop_back();
3566 } else {
3567 // It's an array or pointer access: [ArraySize x ElementType].
3568 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3569 Value *idx = GEPOps.back();
3570
3571 // idx is the index into the array. Unlike with structure
3572 // indices, we may not know its actual value at code-generation
3573 // time.
Chris Lattner985fe3d2004-02-25 03:45:50 +00003574
3575 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003576 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00003577 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003578 Disp += TypeSize*CSI->getValue();
Chris Lattner28977af2004-04-05 01:30:19 +00003579 } else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(idx)) {
3580 Disp += TypeSize*CUI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00003581 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003582 // If the index reg is already taken, we can't handle this index.
3583 if (IndexReg) return;
3584
3585 // If this is a size that we can handle, then add the index as
3586 switch (TypeSize) {
3587 case 1: case 2: case 4: case 8:
3588 // These are all acceptable scales on X86.
3589 Scale = TypeSize;
3590 break;
3591 default:
3592 // Otherwise, we can't handle this scale
3593 return;
3594 }
3595
3596 if (CastInst *CI = dyn_cast<CastInst>(idx))
3597 if (CI->getOperand(0)->getType() == Type::IntTy ||
3598 CI->getOperand(0)->getType() == Type::UIntTy)
3599 idx = CI->getOperand(0);
3600
3601 IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003602 }
3603
3604 GEPOps.pop_back(); // Consume a GEP operand
3605 GEPTypes.pop_back();
3606 }
3607 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003608
Chris Lattnerdf040972004-05-23 21:23:12 +00003609 // GEPTypes is empty, which means we have a single operand left. Set it as
3610 // the base register.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003611 //
Chris Lattnerb6bac512004-02-25 06:13:04 +00003612 assert(BaseReg == 0);
Chris Lattnerdf040972004-05-23 21:23:12 +00003613
3614#if 0 // FIXME: TODO!
3615 if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
3616 // FIXME: When we can add FrameIndex values as the first operand, we can
3617 // make GEP's of allocas MUCH more efficient!
3618 unsigned FI = getFixedSizedAllocaFI(AI);
3619 GEPOps.pop_back();
3620 return;
3621 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3622 // FIXME: When addressing modes are more powerful/correct, we could load
3623 // global addresses directly as 32-bit immediates.
3624 }
3625#endif
3626
Chris Lattner5f2c7b12004-02-25 07:00:55 +00003627 BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00003628 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00003629}
3630
3631
Chris Lattnerb6bac512004-02-25 06:13:04 +00003632/// isGEPFoldable - Return true if the specified GEP can be completely
3633/// folded into the addressing mode of a load/store or lea instruction.
3634bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
3635 Value *Src, User::op_iterator IdxBegin,
3636 User::op_iterator IdxEnd, unsigned &BaseReg,
3637 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
Chris Lattner7ca04092004-02-22 17:35:42 +00003638 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3639 Src = CPR->getValue();
3640
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003641 std::vector<Value*> GEPOps;
3642 GEPOps.resize(IdxEnd-IdxBegin+1);
3643 GEPOps[0] = Src;
3644 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3645
Chris Lattnerdf040972004-05-23 21:23:12 +00003646 std::vector<const Type*>
3647 GEPTypes(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3648 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003649
Chris Lattnerb6bac512004-02-25 06:13:04 +00003650 MachineBasicBlock::iterator IP;
3651 if (MBB) IP = MBB->end();
3652 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
3653
3654 // We can fold it away iff the getGEPIndex call eliminated all operands.
3655 return GEPOps.empty();
3656}
3657
3658void ISel::emitGEPOperation(MachineBasicBlock *MBB,
3659 MachineBasicBlock::iterator IP,
3660 Value *Src, User::op_iterator IdxBegin,
3661 User::op_iterator IdxEnd, unsigned TargetReg) {
3662 const TargetData &TD = TM.getTargetData();
3663 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
3664 Src = CPR->getValue();
3665
Chris Lattnerd2995df2004-07-15 00:58:53 +00003666 // If this is a getelementptr null, with all constant integer indices, just
3667 // replace it with TargetReg = 42.
3668 if (isa<ConstantPointerNull>(Src)) {
3669 User::op_iterator I = IdxBegin;
3670 for (; I != IdxEnd; ++I)
3671 if (!isa<ConstantInt>(*I))
3672 break;
3673 if (I == IdxEnd) { // All constant indices
3674 unsigned Offset = TD.getIndexedOffset(Src->getType(),
3675 std::vector<Value*>(IdxBegin, IdxEnd));
3676 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addImm(Offset);
3677 return;
3678 }
3679 }
3680
Chris Lattnerb6bac512004-02-25 06:13:04 +00003681 std::vector<Value*> GEPOps;
3682 GEPOps.resize(IdxEnd-IdxBegin+1);
3683 GEPOps[0] = Src;
3684 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
3685
3686 std::vector<const Type*> GEPTypes;
3687 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
3688 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00003689
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003690 // Keep emitting instructions until we consume the entire GEP instruction.
3691 while (!GEPOps.empty()) {
3692 unsigned OldSize = GEPOps.size();
Chris Lattnerb6bac512004-02-25 06:13:04 +00003693 unsigned BaseReg, Scale, IndexReg, Disp;
3694 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003695
Chris Lattner985fe3d2004-02-25 03:45:50 +00003696 if (GEPOps.size() != OldSize) {
3697 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00003698 unsigned NextTarget = 0;
3699 if (!GEPOps.empty()) {
3700 assert(BaseReg == 0 &&
3701 "getGEPIndex should have left the base register open for chaining!");
3702 NextTarget = BaseReg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00003703 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00003704
3705 if (IndexReg == 0 && Disp == 0)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003706 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00003707 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003708 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00003709 BaseReg, Scale, IndexReg, Disp);
3710 --IP;
3711 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00003712 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003713 // The getGEPIndex operation didn't want to build an LEA. Check to see if
3714 // all operands are consumed but the base pointer. If so, just load it
3715 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00003716 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003717 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00003718 } else {
3719 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003720 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00003721 }
3722 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00003723
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003724 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00003725 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003726 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
3727 Value *idx = GEPOps.back();
3728 GEPOps.pop_back(); // Consume a GEP operand
3729 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00003730
Chris Lattner28977af2004-04-05 01:30:19 +00003731 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
Chris Lattnerf5854472003-06-21 16:01:24 +00003732 // operand on X86. Handle this case directly now...
3733 if (CastInst *CI = dyn_cast<CastInst>(idx))
3734 if (CI->getOperand(0)->getType() == Type::IntTy ||
3735 CI->getOperand(0)->getType() == Type::UIntTy)
3736 idx = CI->getOperand(0);
3737
Chris Lattner3e130a22003-01-13 00:32:26 +00003738 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00003739 // must find the size of the pointed-to type (Not coincidentally, the next
3740 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003741 const Type *ElTy = SqTy->getElementType();
3742 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00003743
3744 // If idxReg is a constant, we don't need to perform the multiply!
Chris Lattner28977af2004-04-05 01:30:19 +00003745 if (ConstantInt *CSI = dyn_cast<ConstantInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00003746 if (!CSI->isNullValue()) {
Chris Lattner28977af2004-04-05 01:30:19 +00003747 unsigned Offset = elementSize*CSI->getRawValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003748 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003749 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003750 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003751 --IP; // Insert the next instruction before this one.
3752 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003753 }
3754 } else if (elementSize == 1) {
3755 // If the element size is 1, we don't have to multiply, just add
3756 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003757 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003758 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003759 --IP; // Insert the next instruction before this one.
3760 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003761 } else {
3762 unsigned idxReg = getReg(idx, MBB, IP);
3763 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003764
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003765 // Make sure we can back the iterator up to point to the first
3766 // instruction emitted.
3767 MachineBasicBlock::iterator BeforeIt = IP;
3768 if (IP == MBB->begin())
3769 BeforeIt = MBB->end();
3770 else
3771 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00003772 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
3773
Chris Lattner8a307e82002-12-16 19:32:50 +00003774 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003775 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003776 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00003777 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00003778
3779 // Step to the first instruction of the multiply.
3780 if (BeforeIt == MBB->end())
3781 IP = MBB->begin();
3782 else
3783 IP = ++BeforeIt;
3784
3785 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00003786 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003787 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003788 }
Brian Gaeke20244b72002-12-12 15:33:40 +00003789}
3790
Chris Lattner065faeb2002-12-28 20:24:02 +00003791/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
3792/// frame manager, otherwise do it the hard way.
3793///
3794void ISel::visitAllocaInst(AllocaInst &I) {
Chris Lattner9f1b5312004-05-13 15:12:43 +00003795 // If this is a fixed size alloca in the entry block for the function, we
3796 // statically stack allocate the space, so we don't need to do anything here.
3797 //
Chris Lattnercb2fd552004-05-13 07:40:27 +00003798 if (dyn_castFixedAlloca(&I)) return;
Chris Lattner9f1b5312004-05-13 15:12:43 +00003799
Brian Gaekee48ec012002-12-13 06:46:31 +00003800 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00003801 const Type *Ty = I.getAllocatedType();
3802 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
3803
Chris Lattner065faeb2002-12-28 20:24:02 +00003804 // Create a register to hold the temporary result of multiplying the type size
3805 // constant by the variable amount.
3806 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
3807 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00003808
3809 // TotalSizeReg = mul <numelements>, <TypeSize>
3810 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003811 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003812
3813 // AddedSize = add <TotalSizeReg>, 15
3814 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003815 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003816
3817 // AlignedSize = and <AddedSize>, ~15
3818 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003819 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00003820
Brian Gaekee48ec012002-12-13 06:46:31 +00003821 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003822 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00003823
Brian Gaekee48ec012002-12-13 06:46:31 +00003824 // Put a pointer to the space into the result register, by copying
3825 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00003826 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00003827
Misha Brukman48196b32003-05-03 02:18:17 +00003828 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00003829 // object.
3830 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00003831}
Chris Lattner3e130a22003-01-13 00:32:26 +00003832
3833/// visitMallocInst - Malloc instructions are code generated into direct calls
3834/// to the library malloc.
3835///
3836void ISel::visitMallocInst(MallocInst &I) {
3837 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
3838 unsigned Arg;
3839
3840 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
3841 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
3842 } else {
3843 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00003844 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00003845 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00003846 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00003847 }
3848
3849 std::vector<ValueRecord> Args;
3850 Args.push_back(ValueRecord(Arg, Type::UIntTy));
3851 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003852 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003853 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
3854}
3855
3856
3857/// visitFreeInst - Free instructions are code gen'd to call the free libc
3858/// function.
3859///
3860void ISel::visitFreeInst(FreeInst &I) {
3861 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00003862 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00003863 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00003864 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00003865 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
3866}
3867
Chris Lattnerd281de22003-07-26 23:49:58 +00003868/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00003869/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00003870/// generated code sucks but the implementation is nice and simple.
3871///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00003872FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
3873 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00003874}