blob: 35dc177e1d5a9899f628b7fa69302bff54e2a643 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3e130a22003-01-13 00:32:26 +000010// This file defines a simple peephole instruction selector for the x86 target
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
Chris Lattner6fc3c522002-11-17 21:11:55 +000015#include "X86InstrBuilder.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000016#include "X86InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner72614082002-10-25 22:55:53 +000019#include "llvm/Function.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000020#include "llvm/Instructions.h"
Chris Lattner44827152003-12-28 09:47:19 +000021#include "llvm/IntrinsicLowering.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000022#include "llvm/Pass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner341a9372002-10-29 17:43:55 +000025#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner94af4142002-12-25 05:13:53 +000026#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000027#include "llvm/Target/MRegisterInfo.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner3f1e8e72004-02-22 07:04:00 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000030#include "llvm/Support/InstVisitor.h"
Chris Lattnercf93cdd2004-01-30 22:13:44 +000031#include "llvm/Support/CFG.h"
Chris Lattner986618e2004-02-22 19:47:26 +000032#include "Support/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner986618e2004-02-22 19:47:26 +000035namespace {
36 Statistic<>
37 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
38}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000039
Chris Lattner72614082002-10-25 22:55:53 +000040namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000041 struct ISel : public FunctionPass, InstVisitor<ISel> {
42 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000043 MachineFunction *F; // The function we are compiling into
44 MachineBasicBlock *BB; // The current MBB we are compiling
45 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000046 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000047
Chris Lattner72614082002-10-25 22:55:53 +000048 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
49
Chris Lattner333b2fa2002-12-13 10:09:43 +000050 // MBBMap - Mapping between LLVM BB -> Machine BB
51 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
52
Chris Lattnerf70e0c22003-12-28 21:23:38 +000053 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000054
55 /// runOnFunction - Top level implementation of instruction selection for
56 /// the entire function.
57 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000058 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000059 // First pass over the function, lower any unknown intrinsic functions
60 // with the IntrinsicLowering class.
61 LowerUnknownIntrinsicFunctionCalls(Fn);
62
Chris Lattner36b36032002-10-29 23:40:58 +000063 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +000064
Chris Lattner065faeb2002-12-28 20:24:02 +000065 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +000066 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
67 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
68
Chris Lattner14aa7fe2002-12-16 22:54:46 +000069 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +000070
Chris Lattner0e5b79c2004-02-15 01:04:03 +000071 // Set up a frame object for the return address. This is used by the
72 // llvm.returnaddress & llvm.frameaddress intrinisics.
73 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
74
Chris Lattnerdbd73722003-05-06 21:32:22 +000075 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +000076 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +000077
Chris Lattner333b2fa2002-12-13 10:09:43 +000078 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000079 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +000080
81 // Select the PHI nodes
82 SelectPHINodes();
83
Chris Lattner986618e2004-02-22 19:47:26 +000084 // Insert the FP_REG_KILL instructions into blocks that need them.
85 InsertFPRegKills();
86
Chris Lattner72614082002-10-25 22:55:53 +000087 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +000088 MBBMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000089 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +000090 // We always build a machine code representation for the function
91 return true;
Chris Lattner72614082002-10-25 22:55:53 +000092 }
93
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000094 virtual const char *getPassName() const {
95 return "X86 Simple Instruction Selection";
96 }
97
Chris Lattner72614082002-10-25 22:55:53 +000098 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000099 /// block. This simply creates a new MachineBasicBlock to emit code into
100 /// and adds it to the current MachineFunction. Subsequent visit* for
101 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000102 ///
103 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000105 }
106
Chris Lattner44827152003-12-28 09:47:19 +0000107 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
108 /// function, lowering any calls to unknown intrinsic functions into the
109 /// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +0000110 ///
Chris Lattner44827152003-12-28 09:47:19 +0000111 void LowerUnknownIntrinsicFunctionCalls(Function &F);
112
Chris Lattner065faeb2002-12-28 20:24:02 +0000113 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
114 /// from the stack into virtual registers.
115 ///
116 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000117
118 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
119 /// because we have to generate our sources into the source basic blocks,
120 /// not the current one.
121 ///
122 void SelectPHINodes();
123
Chris Lattner986618e2004-02-22 19:47:26 +0000124 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
125 /// that need them. This only occurs due to the floating point stackifier
126 /// not being aggressive enough to handle arbitrary global stackification.
127 ///
128 void InsertFPRegKills();
129
Chris Lattner72614082002-10-25 22:55:53 +0000130 // Visitation methods for various instructions. These methods simply emit
131 // fixed X86 code for each instruction.
132 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000133
134 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000135 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000136 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000137
138 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000139 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000140 unsigned Reg;
141 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000142 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
143 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000144 };
145 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000146 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000147 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000148 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000149
150 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000151 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000152 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
153 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000154 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +0000155 unsigned DestReg, const Type *DestTy,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000156 unsigned Op0Reg, unsigned Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000157 void doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000158 MachineBasicBlock::iterator MBBI,
Chris Lattnerb2acc512003-10-19 21:09:10 +0000159 unsigned DestReg, const Type *DestTy,
160 unsigned Op0Reg, unsigned Op1Val);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000161 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000162
Chris Lattnerf01729e2002-11-02 20:54:46 +0000163 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
164 void visitRem(BinaryOperator &B) { visitDivRem(B); }
165 void visitDivRem(BinaryOperator &B);
166
Chris Lattnere2954c82002-11-02 20:04:26 +0000167 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000168 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
169 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
170 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000171
Chris Lattner6d40c192003-01-16 16:43:00 +0000172 // Comparison operators...
173 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000174 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
175 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000176 MachineBasicBlock::iterator MBBI);
Chris Lattner12d96a02004-03-30 21:22:00 +0000177 void visitSelectInst(SelectInst &SI);
178
Chris Lattnerb2acc512003-10-19 21:09:10 +0000179
Chris Lattner6fc3c522002-11-17 21:11:55 +0000180 // Memory Instructions
181 void visitLoadInst(LoadInst &I);
182 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000183 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000184 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000185 void visitMallocInst(MallocInst &I);
186 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000187
Chris Lattnere2954c82002-11-02 20:04:26 +0000188 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000189 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000190 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000191 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000192 void visitVANextInst(VANextInst &I);
193 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000194
195 void visitInstruction(Instruction &I) {
196 std::cerr << "Cannot instruction select: " << I;
197 abort();
198 }
199
Brian Gaeke95780cc2002-12-13 07:56:18 +0000200 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000201 ///
202 void promote32(unsigned targetReg, const ValueRecord &VR);
203
Chris Lattner721d2d42004-03-08 01:18:36 +0000204 /// getAddressingMode - Get the addressing mode to use to address the
205 /// specified value. The returned value should be used with addFullAddress.
206 void getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
207 unsigned &IndexReg, unsigned &Disp);
208
209
210 /// getGEPIndex - This is used to fold GEP instructions into X86 addressing
211 /// expressions.
Chris Lattnerb6bac512004-02-25 06:13:04 +0000212 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
213 std::vector<Value*> &GEPOps,
214 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
215 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
216
217 /// isGEPFoldable - Return true if the specified GEP can be completely
218 /// folded into the addressing mode of a load/store or lea instruction.
219 bool isGEPFoldable(MachineBasicBlock *MBB,
220 Value *Src, User::op_iterator IdxBegin,
221 User::op_iterator IdxEnd, unsigned &BaseReg,
222 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
223
Chris Lattner3e130a22003-01-13 00:32:26 +0000224 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
225 /// constant expression GEP support.
226 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000227 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000228 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000229 User::op_iterator IdxEnd, unsigned TargetReg);
230
Chris Lattner548f61d2003-04-23 17:22:12 +0000231 /// emitCastOperation - Common code shared between visitCastInst and
232 /// constant expression cast support.
Misha Brukman538607f2004-03-01 23:53:11 +0000233 ///
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000234 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000235 Value *Src, const Type *DestTy, unsigned TargetReg);
236
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000237 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
238 /// and constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000239 ///
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000240 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000241 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000242 Value *Op0, Value *Op1,
243 unsigned OperatorClass, unsigned TargetReg);
244
Chris Lattnercadff442003-10-23 17:21:43 +0000245 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000246 MachineBasicBlock::iterator IP,
Chris Lattnercadff442003-10-23 17:21:43 +0000247 unsigned Op0Reg, unsigned Op1Reg, bool isDiv,
248 const Type *Ty, unsigned TargetReg);
249
Chris Lattner58c41fe2003-08-24 19:19:47 +0000250 /// emitSetCCOperation - Common code shared between visitSetCondInst and
251 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000252 ///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000253 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000254 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000255 Value *Op0, Value *Op1, unsigned Opcode,
256 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000257
258 /// emitShiftOperation - Common code shared between visitShiftInst and
259 /// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000260 ///
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000261 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000262 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000263 Value *Op, Value *ShiftAmount, bool isLeftShift,
264 const Type *ResultTy, unsigned DestReg);
265
Chris Lattner12d96a02004-03-30 21:22:00 +0000266 /// emitSelectOperation - Common code shared between visitSelectInst and the
267 /// constant expression support.
268 void emitSelectOperation(MachineBasicBlock *MBB,
269 MachineBasicBlock::iterator IP,
270 Value *Cond, Value *TrueVal, Value *FalseVal,
271 unsigned DestReg);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000272
Chris Lattnerc5291f52002-10-27 21:16:59 +0000273 /// copyConstantToRegister - Output the instructions required to put the
274 /// specified constant into the specified register.
275 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000276 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000277 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000278 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000279
Chris Lattner3e130a22003-01-13 00:32:26 +0000280 /// makeAnotherReg - This method returns the next register number we haven't
281 /// yet used.
282 ///
283 /// Long values are handled somewhat specially. They are always allocated
284 /// as pairs of 32 bit integer values. The register number returned is the
285 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
286 /// of the long value.
287 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000288 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000289 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
290 "Current target doesn't have X86 reg info??");
291 const X86RegisterInfo *MRI =
292 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000293 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000294 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
295 // Create the lower part
296 F->getSSARegMap()->createVirtualRegister(RC);
297 // Create the upper part.
298 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000299 }
300
Chris Lattnerc0812d82002-12-13 06:56:29 +0000301 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000302 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000303 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000304 }
305
Chris Lattner72614082002-10-25 22:55:53 +0000306 /// getReg - This method turns an LLVM value into a register number. This
307 /// is guaranteed to produce the same register number for a particular value
308 /// every time it is queried.
309 ///
310 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000311 unsigned getReg(Value *V) {
312 // Just append to the end of the current bb.
313 MachineBasicBlock::iterator It = BB->end();
314 return getReg(V, BB, It);
315 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000316 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000317 MachineBasicBlock::iterator IPt) {
Chris Lattner72614082002-10-25 22:55:53 +0000318 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000319 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000320 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000321 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000322 }
Chris Lattner72614082002-10-25 22:55:53 +0000323
Chris Lattner6f8fd252002-10-27 21:23:43 +0000324 // If this operand is a constant, emit the code to copy the constant into
325 // the register here...
326 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000327 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner8a307e82002-12-16 19:32:50 +0000328 copyConstantToRegister(MBB, IPt, C, Reg);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000329 RegMap.erase(V); // Assign a new name to this constant if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000330 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
331 // Move the address of the global into the register
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000332 BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000333 RegMap.erase(V); // Assign a new name to this address if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000334 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000335
Chris Lattner72614082002-10-25 22:55:53 +0000336 return Reg;
337 }
Chris Lattner72614082002-10-25 22:55:53 +0000338 };
339}
340
Chris Lattner43189d12002-11-17 20:07:45 +0000341/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
342/// Representation.
343///
344enum TypeClass {
Chris Lattner94af4142002-12-25 05:13:53 +0000345 cByte, cShort, cInt, cFP, cLong
Chris Lattner43189d12002-11-17 20:07:45 +0000346};
347
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000348/// getClass - Turn a primitive type into a "class" number which is based on the
349/// size of the type, and whether or not it is floating point.
350///
Chris Lattner43189d12002-11-17 20:07:45 +0000351static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000352 switch (Ty->getPrimitiveID()) {
353 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000354 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000355 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000356 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000357 case Type::IntTyID:
358 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000359 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000360
Chris Lattner94af4142002-12-25 05:13:53 +0000361 case Type::FloatTyID:
362 case Type::DoubleTyID: return cFP; // Floating Point is #3
Chris Lattner3e130a22003-01-13 00:32:26 +0000363
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000364 case Type::LongTyID:
Chris Lattner3e130a22003-01-13 00:32:26 +0000365 case Type::ULongTyID: return cLong; // Longs are class #4
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000366 default:
367 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000368 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000369 }
370}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000371
Chris Lattner6b993cc2002-12-15 08:02:15 +0000372// getClassB - Just like getClass, but treat boolean values as bytes.
373static inline TypeClass getClassB(const Type *Ty) {
374 if (Ty == Type::BoolTy) return cByte;
375 return getClass(Ty);
376}
377
Chris Lattner06925362002-11-17 21:56:38 +0000378
Chris Lattnerc5291f52002-10-27 21:16:59 +0000379/// copyConstantToRegister - Output the instructions required to put the
380/// specified constant into the specified register.
381///
Chris Lattner8a307e82002-12-16 19:32:50 +0000382void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000383 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000384 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000385 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000386 unsigned Class = 0;
387 switch (CE->getOpcode()) {
388 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000389 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000390 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000391 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000392 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000393 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000394 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000395
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000396 case Instruction::Xor: ++Class; // FALL THROUGH
397 case Instruction::Or: ++Class; // FALL THROUGH
398 case Instruction::And: ++Class; // FALL THROUGH
399 case Instruction::Sub: ++Class; // FALL THROUGH
400 case Instruction::Add:
401 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
402 Class, R);
403 return;
404
Chris Lattnercadff442003-10-23 17:21:43 +0000405 case Instruction::Mul: {
406 unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP);
407 unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP);
408 doMultiply(MBB, IP, R, CE->getType(), Op0Reg, Op1Reg);
409 return;
410 }
411 case Instruction::Div:
412 case Instruction::Rem: {
413 unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP);
414 unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP);
415 emitDivRemOperation(MBB, IP, Op0Reg, Op1Reg,
416 CE->getOpcode() == Instruction::Div,
417 CE->getType(), R);
418 return;
419 }
420
Chris Lattner58c41fe2003-08-24 19:19:47 +0000421 case Instruction::SetNE:
422 case Instruction::SetEQ:
423 case Instruction::SetLT:
424 case Instruction::SetGT:
425 case Instruction::SetLE:
426 case Instruction::SetGE:
427 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
428 CE->getOpcode(), R);
429 return;
430
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000431 case Instruction::Shl:
432 case Instruction::Shr:
433 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000434 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
435 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000436
Chris Lattner12d96a02004-03-30 21:22:00 +0000437 case Instruction::Select:
438 emitSelectOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
439 CE->getOperand(2), R);
440 return;
441
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000442 default:
443 std::cerr << "Offending expr: " << C << "\n";
Chris Lattnerb2acc512003-10-19 21:09:10 +0000444 assert(0 && "Constant expression not yet handled!\n");
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000445 }
Brian Gaeke20244b72002-12-12 15:33:40 +0000446 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000447
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000448 if (C->getType()->isIntegral()) {
Chris Lattner6b993cc2002-12-15 08:02:15 +0000449 unsigned Class = getClassB(C->getType());
Chris Lattner3e130a22003-01-13 00:32:26 +0000450
451 if (Class == cLong) {
452 // Copy the value into the register pair.
Chris Lattnerc07736a2003-07-23 15:22:26 +0000453 uint64_t Val = cast<ConstantInt>(C)->getRawValue();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000454 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(Val & 0xFFFFFFFF);
455 BuildMI(*MBB, IP, X86::MOV32ri, 1, R+1).addImm(Val >> 32);
Chris Lattner3e130a22003-01-13 00:32:26 +0000456 return;
457 }
458
Chris Lattner94af4142002-12-25 05:13:53 +0000459 assert(Class <= cInt && "Type not handled yet!");
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000460
461 static const unsigned IntegralOpcodeTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000462 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000463 };
464
Chris Lattner6b993cc2002-12-15 08:02:15 +0000465 if (C->getType() == Type::BoolTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000466 BuildMI(*MBB, IP, X86::MOV8ri, 1, R).addImm(C == ConstantBool::True);
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000467 } else {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000468 ConstantInt *CI = cast<ConstantInt>(C);
Chris Lattneree352852004-02-29 07:22:16 +0000469 BuildMI(*MBB, IP, IntegralOpcodeTab[Class],1,R).addImm(CI->getRawValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000470 }
Chris Lattner94af4142002-12-25 05:13:53 +0000471 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
Chris Lattneraf703622004-02-02 18:56:30 +0000472 if (CFP->isExactlyValue(+0.0))
Chris Lattneree352852004-02-29 07:22:16 +0000473 BuildMI(*MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000474 else if (CFP->isExactlyValue(+1.0))
Chris Lattneree352852004-02-29 07:22:16 +0000475 BuildMI(*MBB, IP, X86::FLD1, 0, R);
Chris Lattner94af4142002-12-25 05:13:53 +0000476 else {
Chris Lattner3e130a22003-01-13 00:32:26 +0000477 // Otherwise we need to spill the constant to memory...
478 MachineConstantPool *CP = F->getConstantPool();
479 unsigned CPI = CP->getConstantPoolIndex(CFP);
Chris Lattner6c09db22003-10-20 04:11:23 +0000480 const Type *Ty = CFP->getType();
481
482 assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000483 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLD32m : X86::FLD64m;
Chris Lattneree352852004-02-29 07:22:16 +0000484 addConstantPoolReference(BuildMI(*MBB, IP, LoadOpcode, 4, R), CPI);
Chris Lattner94af4142002-12-25 05:13:53 +0000485 }
486
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000487 } else if (isa<ConstantPointerNull>(C)) {
Brian Gaeke20244b72002-12-12 15:33:40 +0000488 // Copy zero (null pointer) to the register.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000489 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000490 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000491 BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue());
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000492 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +0000493 std::cerr << "Offending constant: " << C << "\n";
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000494 assert(0 && "Type not handled yet!");
Chris Lattnerc5291f52002-10-27 21:16:59 +0000495 }
496}
497
Chris Lattner065faeb2002-12-28 20:24:02 +0000498/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
499/// the stack into virtual registers.
500///
501void ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
502 // Emit instructions to load the arguments... On entry to a function on the
503 // X86, the stack frame looks like this:
504 //
505 // [ESP] -- return address
Chris Lattner3e130a22003-01-13 00:32:26 +0000506 // [ESP + 4] -- first argument (leftmost lexically)
507 // [ESP + 8] -- second argument, if first argument is four bytes in size
Chris Lattner065faeb2002-12-28 20:24:02 +0000508 // ...
509 //
Chris Lattnerf158da22003-01-16 02:20:12 +0000510 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattneraa09b752002-12-28 21:08:28 +0000511 MachineFrameInfo *MFI = F->getFrameInfo();
Chris Lattner065faeb2002-12-28 20:24:02 +0000512
513 for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) {
514 unsigned Reg = getReg(*I);
515
Chris Lattner065faeb2002-12-28 20:24:02 +0000516 int FI; // Frame object index
Chris Lattner065faeb2002-12-28 20:24:02 +0000517 switch (getClassB(I->getType())) {
518 case cByte:
Chris Lattneraa09b752002-12-28 21:08:28 +0000519 FI = MFI->CreateFixedObject(1, ArgOffset);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000520 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000521 break;
522 case cShort:
Chris Lattneraa09b752002-12-28 21:08:28 +0000523 FI = MFI->CreateFixedObject(2, ArgOffset);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000524 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000525 break;
526 case cInt:
Chris Lattneraa09b752002-12-28 21:08:28 +0000527 FI = MFI->CreateFixedObject(4, ArgOffset);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000528 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
Chris Lattner065faeb2002-12-28 20:24:02 +0000529 break;
Chris Lattner3e130a22003-01-13 00:32:26 +0000530 case cLong:
531 FI = MFI->CreateFixedObject(8, ArgOffset);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000532 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg), FI);
533 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Reg+1), FI, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +0000534 ArgOffset += 4; // longs require 4 additional bytes
535 break;
Chris Lattner065faeb2002-12-28 20:24:02 +0000536 case cFP:
537 unsigned Opcode;
538 if (I->getType() == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000539 Opcode = X86::FLD32m;
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000540 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000541 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000542 Opcode = X86::FLD64m;
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000543 FI = MFI->CreateFixedObject(8, ArgOffset);
544 ArgOffset += 4; // doubles require 4 additional bytes
Chris Lattner065faeb2002-12-28 20:24:02 +0000545 }
546 addFrameReference(BuildMI(BB, Opcode, 4, Reg), FI);
547 break;
548 default:
549 assert(0 && "Unhandled argument type!");
550 }
Chris Lattner3e130a22003-01-13 00:32:26 +0000551 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000552 }
Chris Lattnereca195e2003-05-08 19:44:13 +0000553
554 // If the function takes variable number of arguments, add a frame offset for
555 // the start of the first vararg value... this is used to expand
556 // llvm.va_start.
557 if (Fn.getFunctionType()->isVarArg())
558 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000559}
560
561
Chris Lattner333b2fa2002-12-13 10:09:43 +0000562/// SelectPHINodes - Insert machine code to generate phis. This is tricky
563/// because we have to generate our sources into the source basic blocks, not
564/// the current one.
565///
566void ISel::SelectPHINodes() {
Chris Lattner3501fea2003-01-14 22:00:31 +0000567 const TargetInstrInfo &TII = TM.getInstrInfo();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000568 const Function &LF = *F->getFunction(); // The LLVM function...
569 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
570 const BasicBlock *BB = I;
Chris Lattner168aa902004-02-29 07:10:16 +0000571 MachineBasicBlock &MBB = *MBBMap[I];
Chris Lattner333b2fa2002-12-13 10:09:43 +0000572
573 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner168aa902004-02-29 07:10:16 +0000574 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000575 for (BasicBlock::const_iterator I = BB->begin();
Chris Lattnera81fc682003-10-19 00:26:11 +0000576 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
Chris Lattner3e130a22003-01-13 00:32:26 +0000577
Chris Lattner333b2fa2002-12-13 10:09:43 +0000578 // Create a new machine instr PHI node, and insert it.
Chris Lattner3e130a22003-01-13 00:32:26 +0000579 unsigned PHIReg = getReg(*PN);
Chris Lattner168aa902004-02-29 07:10:16 +0000580 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
581 X86::PHI, PN->getNumOperands(), PHIReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000582
583 MachineInstr *LongPhiMI = 0;
Chris Lattner168aa902004-02-29 07:10:16 +0000584 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy)
585 LongPhiMI = BuildMI(MBB, PHIInsertPoint,
586 X86::PHI, PN->getNumOperands(), PHIReg+1);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000587
Chris Lattnera6e73f12003-05-12 14:22:21 +0000588 // PHIValues - Map of blocks to incoming virtual registers. We use this
589 // so that we only initialize one incoming value for a particular block,
590 // even if the block has multiple entries in the PHI node.
591 //
592 std::map<MachineBasicBlock*, unsigned> PHIValues;
593
Chris Lattner333b2fa2002-12-13 10:09:43 +0000594 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
595 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000596 unsigned ValReg;
597 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
598 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000599
Chris Lattnera6e73f12003-05-12 14:22:21 +0000600 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
601 // We already inserted an initialization of the register for this
602 // predecessor. Recycle it.
603 ValReg = EntryIt->second;
604
605 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000606 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000607 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000608 Value *Val = PN->getIncomingValue(i);
609
610 // If this is a constant or GlobalValue, we may have to insert code
611 // into the basic block to compute it into a virtual register.
612 if (isa<Constant>(Val) || isa<GlobalValue>(Val)) {
Chris Lattner6f2ab042004-03-30 19:10:12 +0000613 if (isa<ConstantExpr>(Val)) {
614 // Because we don't want to clobber any values which might be in
615 // physical registers with the computation of this constant (which
616 // might be arbitrarily complex if it is a constant expression),
617 // just insert the computation at the top of the basic block.
618 MachineBasicBlock::iterator PI = PredMBB->begin();
619
620 // Skip over any PHI nodes though!
621 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
622 ++PI;
623
624 ValReg = getReg(Val, PredMBB, PI);
625 } else {
626 // Simple constants get emitted at the end of the basic block,
627 // before any terminator instructions. We "know" that the code to
628 // move a constant into a register will never clobber any flags.
629 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
630 }
Chris Lattnera81fc682003-10-19 00:26:11 +0000631 } else {
632 ValReg = getReg(Val);
633 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000634
635 // Remember that we inserted a value for this PHI for this predecessor
636 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
637 }
638
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000639 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000640 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000641 if (LongPhiMI) {
642 LongPhiMI->addRegOperand(ValReg+1);
643 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
644 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000645 }
Chris Lattner168aa902004-02-29 07:10:16 +0000646
647 // Now that we emitted all of the incoming values for the PHI node, make
648 // sure to reposition the InsertPoint after the PHI that we just added.
649 // This is needed because we might have inserted a constant into this
650 // block, right after the PHI's which is before the old insert point!
651 PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI;
652 ++PHIInsertPoint;
Chris Lattner333b2fa2002-12-13 10:09:43 +0000653 }
654 }
655}
656
Chris Lattner986618e2004-02-22 19:47:26 +0000657/// RequiresFPRegKill - The floating point stackifier pass cannot insert
658/// compensation code on critical edges. As such, it requires that we kill all
659/// FP registers on the exit from any blocks that either ARE critical edges, or
660/// branch to a block that has incoming critical edges.
661///
662/// Note that this kill instruction will eventually be eliminated when
663/// restrictions in the stackifier are relaxed.
664///
665static bool RequiresFPRegKill(const BasicBlock *BB) {
666#if 0
667 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
668 const BasicBlock *Succ = *SI;
669 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
670 ++PI; // Block have at least one predecessory
671 if (PI != PE) { // If it has exactly one, this isn't crit edge
672 // If this block has more than one predecessor, check all of the
673 // predecessors to see if they have multiple successors. If so, then the
674 // block we are analyzing needs an FPRegKill.
675 for (PI = pred_begin(Succ); PI != PE; ++PI) {
676 const BasicBlock *Pred = *PI;
677 succ_const_iterator SI2 = succ_begin(Pred);
678 ++SI2; // There must be at least one successor of this block.
679 if (SI2 != succ_end(Pred))
680 return true; // Yes, we must insert the kill on this edge.
681 }
682 }
683 }
684 // If we got this far, there is no need to insert the kill instruction.
685 return false;
686#else
687 return true;
688#endif
689}
690
691// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
692// need them. This only occurs due to the floating point stackifier not being
693// aggressive enough to handle arbitrary global stackification.
694//
695// Currently we insert an FP_REG_KILL instruction into each block that uses or
696// defines a floating point virtual register.
697//
698// When the global register allocators (like linear scan) finally update live
699// variable analysis, we can keep floating point values in registers across
700// portions of the CFG that do not involve critical edges. This will be a big
701// win, but we are waiting on the global allocators before we can do this.
702//
703// With a bit of work, the floating point stackifier pass can be enhanced to
704// break critical edges as needed (to make a place to put compensation code),
705// but this will require some infrastructure improvements as well.
706//
707void ISel::InsertFPRegKills() {
708 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000709
710 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000711 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000712 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
713 MachineOperand& MO = I->getOperand(i);
714 if (MO.isRegister() && MO.getReg()) {
715 unsigned Reg = MO.getReg();
Chris Lattner986618e2004-02-22 19:47:26 +0000716 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000717 if (RegMap.getRegClass(Reg)->getSize() == 10)
718 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000719 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000720 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000721 // If we haven't found an FP register use or def in this basic block, check
722 // to see if any of our successors has an FP PHI node, which will cause a
723 // copy to be inserted into this block.
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000724 for (succ_const_iterator SI = succ_begin(BB->getBasicBlock()),
725 E = succ_end(BB->getBasicBlock()); SI != E; ++SI) {
726 MachineBasicBlock *SBB = MBBMap[*SI];
727 for (MachineBasicBlock::iterator I = SBB->begin();
728 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
729 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
730 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000731 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000732 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000733 continue;
734 UsesFPReg:
735 // Okay, this block uses an FP register. If the block has successors (ie,
736 // it's not an unwind/return), insert the FP_REG_KILL instruction.
737 if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
738 RequiresFPRegKill(BB->getBasicBlock())) {
Chris Lattneree352852004-02-29 07:22:16 +0000739 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000740 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000741 }
742 }
743}
744
745
Chris Lattner307ecba2004-03-30 22:39:09 +0000746// canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
747// it into the conditional branch or select instruction which is the only user
748// of the cc instruction. This is the case if the conditional branch is the
749// only user of the setcc, and if the setcc is in the same basic block as the
750// conditional branch. We also don't handle long arguments below, so we reject
751// them here as well.
Chris Lattner6d40c192003-01-16 16:43:00 +0000752//
Chris Lattner307ecba2004-03-30 22:39:09 +0000753static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000754 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattner307ecba2004-03-30 22:39:09 +0000755 if (SCI->hasOneUse()) {
756 Instruction *User = cast<Instruction>(SCI->use_back());
757 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
758 SCI->getParent() == User->getParent() &&
759 getClassB(SCI->getOperand(0)->getType()) != cLong)
Chris Lattner6d40c192003-01-16 16:43:00 +0000760 return SCI;
761 }
762 return 0;
763}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000764
Chris Lattner6d40c192003-01-16 16:43:00 +0000765// Return a fixed numbering for setcc instructions which does not depend on the
766// order of the opcodes.
767//
768static unsigned getSetCCNumber(unsigned Opcode) {
769 switch(Opcode) {
770 default: assert(0 && "Unknown setcc instruction!");
771 case Instruction::SetEQ: return 0;
772 case Instruction::SetNE: return 1;
773 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000774 case Instruction::SetGE: return 3;
775 case Instruction::SetGT: return 4;
776 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000777 }
778}
Chris Lattner06925362002-11-17 21:56:38 +0000779
Chris Lattner6d40c192003-01-16 16:43:00 +0000780// LLVM -> X86 signed X86 unsigned
781// ----- ---------- ------------
782// seteq -> sete sete
783// setne -> setne setne
784// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000785// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000786// setgt -> setg seta
787// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000788// ----
789// sets // Used by comparison with 0 optimization
790// setns
791static const unsigned SetCCOpcodeTab[2][8] = {
792 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
793 0, 0 },
794 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
795 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000796};
797
Chris Lattnerb2acc512003-10-19 21:09:10 +0000798// EmitComparison - This function emits a comparison of the two operands,
799// returning the extended setcc code to use.
800unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
801 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000802 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000803 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000804 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000805 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000806 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000807
808 // Special case handling of: cmp R, i
809 if (Class == cByte || Class == cShort || Class == cInt)
810 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000811 uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue();
812
Chris Lattner333864d2003-06-05 19:30:30 +0000813 // Mask off any upper bits of the constant, if there are any...
814 Op1v &= (1ULL << (8 << Class)) - 1;
815
Chris Lattnerb2acc512003-10-19 21:09:10 +0000816 // If this is a comparison against zero, emit more efficient code. We
817 // can't handle unsigned comparisons against zero unless they are == or
818 // !=. These should have been strength reduced already anyway.
819 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
820 static const unsigned TESTTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000821 X86::TEST8rr, X86::TEST16rr, X86::TEST32rr
Chris Lattnerb2acc512003-10-19 21:09:10 +0000822 };
Chris Lattneree352852004-02-29 07:22:16 +0000823 BuildMI(*MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000824
825 if (OpNum == 2) return 6; // Map jl -> js
826 if (OpNum == 3) return 7; // Map jg -> jns
827 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000828 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000829
830 static const unsigned CMPTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000831 X86::CMP8ri, X86::CMP16ri, X86::CMP32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +0000832 };
833
Chris Lattneree352852004-02-29 07:22:16 +0000834 BuildMI(*MBB, IP, CMPTab[Class], 2).addReg(Op0r).addImm(Op1v);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000835 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000836 }
837
Chris Lattner9f08a922004-02-03 18:54:04 +0000838 // Special case handling of comparison against +/- 0.0
839 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
840 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
Chris Lattneree352852004-02-29 07:22:16 +0000841 BuildMI(*MBB, IP, X86::FTST, 1).addReg(Op0r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000842 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000843 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner9f08a922004-02-03 18:54:04 +0000844 return OpNum;
845 }
846
Chris Lattner58c41fe2003-08-24 19:19:47 +0000847 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000848 switch (Class) {
849 default: assert(0 && "Unknown type class!");
850 // Emit: cmp <var1>, <var2> (do the comparison). We can
851 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
852 // 32-bit.
853 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000854 BuildMI(*MBB, IP, X86::CMP8rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000855 break;
856 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000857 BuildMI(*MBB, IP, X86::CMP16rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000858 break;
859 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000860 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000861 break;
862 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +0000863 BuildMI(*MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000864 BuildMI(*MBB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +0000865 BuildMI(*MBB, IP, X86::SAHF, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +0000866 break;
867
868 case cLong:
869 if (OpNum < 2) { // seteq, setne
870 unsigned LoTmp = makeAnotherReg(Type::IntTy);
871 unsigned HiTmp = makeAnotherReg(Type::IntTy);
872 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000873 BuildMI(*MBB, IP, X86::XOR32rr, 2, LoTmp).addReg(Op0r).addReg(Op1r);
874 BuildMI(*MBB, IP, X86::XOR32rr, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
875 BuildMI(*MBB, IP, X86::OR32rr, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000876 break; // Allow the sete or setne to be generated from flags set by OR
877 } else {
878 // Emit a sequence of code which compares the high and low parts once
879 // each, then uses a conditional move to handle the overflow case. For
880 // example, a setlt for long would generate code like this:
881 //
882 // AL = lo(op1) < lo(op2) // Signedness depends on operands
883 // BL = hi(op1) < hi(op2) // Always unsigned comparison
884 // dest = hi(op1) == hi(op2) ? AL : BL;
885 //
886
Chris Lattner6d40c192003-01-16 16:43:00 +0000887 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000888 // classes! Until then, hardcode registers so that we can deal with their
889 // aliases (because we don't have conditional byte moves).
890 //
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000891 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r).addReg(Op1r);
Chris Lattneree352852004-02-29 07:22:16 +0000892 BuildMI(*MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000893 BuildMI(*MBB, IP, X86::CMP32rr, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattneree352852004-02-29 07:22:16 +0000894 BuildMI(*MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
895 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
896 BuildMI(*MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000897 BuildMI(*MBB, IP, X86::CMOVE16rr, 2, X86::BX).addReg(X86::BX)
Chris Lattneree352852004-02-29 07:22:16 +0000898 .addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000899 // NOTE: visitSetCondInst knows that the value is dumped into the BL
900 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000901 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000902 }
903 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000904 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000905}
Chris Lattner3e130a22003-01-13 00:32:26 +0000906
Chris Lattner6d40c192003-01-16 16:43:00 +0000907/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
908/// register, then move it to wherever the result should be.
909///
910void ISel::visitSetCondInst(SetCondInst &I) {
Chris Lattner307ecba2004-03-30 22:39:09 +0000911 if (canFoldSetCCIntoBranchOrSelect(&I))
912 return; // Fold this into a branch or select.
Chris Lattner6d40c192003-01-16 16:43:00 +0000913
Chris Lattner6d40c192003-01-16 16:43:00 +0000914 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000915 MachineBasicBlock::iterator MII = BB->end();
916 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
917 DestReg);
918}
Chris Lattner6d40c192003-01-16 16:43:00 +0000919
Chris Lattner58c41fe2003-08-24 19:19:47 +0000920/// emitSetCCOperation - Common code shared between visitSetCondInst and
921/// constant expression support.
Misha Brukman538607f2004-03-01 23:53:11 +0000922///
Chris Lattner58c41fe2003-08-24 19:19:47 +0000923void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000924 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000925 Value *Op0, Value *Op1, unsigned Opcode,
926 unsigned TargetReg) {
927 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000928 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000929
Chris Lattnerb2acc512003-10-19 21:09:10 +0000930 const Type *CompTy = Op0->getType();
931 unsigned CompClass = getClassB(CompTy);
932 bool isSigned = CompTy->isSigned() && CompClass != cFP;
933
934 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000935 // Handle normal comparisons with a setcc instruction...
Chris Lattneree352852004-02-29 07:22:16 +0000936 BuildMI(*MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +0000937 } else {
938 // Handle long comparisons by copying the value which is already in BL into
939 // the register we want...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +0000940 BuildMI(*MBB, IP, X86::MOV8rr, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +0000941 }
Brian Gaeke1749d632002-11-07 17:59:21 +0000942}
Chris Lattner51b49a92002-11-02 19:45:49 +0000943
Chris Lattner12d96a02004-03-30 21:22:00 +0000944void ISel::visitSelectInst(SelectInst &SI) {
945 unsigned DestReg = getReg(SI);
946 MachineBasicBlock::iterator MII = BB->end();
947 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
948 SI.getFalseValue(), DestReg);
949}
950
951/// emitSelect - Common code shared between visitSelectInst and the constant
952/// expression support.
953void ISel::emitSelectOperation(MachineBasicBlock *MBB,
954 MachineBasicBlock::iterator IP,
955 Value *Cond, Value *TrueVal, Value *FalseVal,
956 unsigned DestReg) {
957 unsigned SelectClass = getClassB(TrueVal->getType());
958
959 // We don't support 8-bit conditional moves. If we have incoming constants,
960 // transform them into 16-bit constants to avoid having a run-time conversion.
961 if (SelectClass == cByte) {
962 if (Constant *T = dyn_cast<Constant>(TrueVal))
963 TrueVal = ConstantExpr::getCast(T, Type::ShortTy);
964 if (Constant *F = dyn_cast<Constant>(FalseVal))
965 FalseVal = ConstantExpr::getCast(F, Type::ShortTy);
966 }
967
Chris Lattner307ecba2004-03-30 22:39:09 +0000968
969 unsigned Opcode;
970 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
971 // We successfully folded the setcc into the select instruction.
972
973 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
974 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), MBB,
975 IP);
976
977 const Type *CompTy = SCI->getOperand(0)->getType();
978 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
979
980 // LLVM -> X86 signed X86 unsigned
981 // ----- ---------- ------------
982 // seteq -> cmovNE cmovNE
983 // setne -> cmovE cmovE
984 // setlt -> cmovGE cmovAE
985 // setge -> cmovL cmovB
986 // setgt -> cmovLE cmovBE
987 // setle -> cmovG cmovA
988 // ----
989 // cmovNS // Used by comparison with 0 optimization
990 // cmovS
991
992 switch (SelectClass) {
993 default:
994 case cFP:
995 assert(0 && "We don't support floating point selects yet, they should "
996 "have been lowered!");
997 case cByte:
998 case cShort: {
999 static const unsigned OpcodeTab[2][8] = {
1000 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVAE16rr, X86::CMOVB16rr,
1001 X86::CMOVBE16rr, X86::CMOVA16rr, 0, 0 },
1002 { X86::CMOVNE16rr, X86::CMOVE16rr, X86::CMOVGE16rr, X86::CMOVL16rr,
1003 X86::CMOVLE16rr, X86::CMOVG16rr, X86::CMOVNS16rr, X86::CMOVS16rr },
1004 };
1005 Opcode = OpcodeTab[isSigned][OpNum];
1006 break;
1007 }
1008 case cInt:
1009 case cLong: {
1010 static const unsigned OpcodeTab[2][8] = {
1011 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVAE32rr, X86::CMOVB32rr,
1012 X86::CMOVBE32rr, X86::CMOVA32rr, 0, 0 },
1013 { X86::CMOVNE32rr, X86::CMOVE32rr, X86::CMOVGE32rr, X86::CMOVL32rr,
1014 X86::CMOVLE32rr, X86::CMOVG32rr, X86::CMOVNS32rr, X86::CMOVS32rr },
1015 };
1016 Opcode = OpcodeTab[isSigned][OpNum];
1017 break;
1018 }
1019 }
1020 } else {
1021 // Get the value being branched on, and use it to set the condition codes.
1022 unsigned CondReg = getReg(Cond, MBB, IP);
1023 BuildMI(*MBB, IP, X86::CMP8ri, 2).addReg(CondReg).addImm(0);
1024 switch (SelectClass) {
1025 default:
1026 case cFP:
1027 assert(0 && "We don't support floating point selects yet, they should "
1028 "have been lowered!");
1029 case cByte:
1030 case cShort:
1031 Opcode = X86::CMOVE16rr;
1032 break;
1033 case cInt:
1034 case cLong:
1035 Opcode = X86::CMOVE32rr;
1036 break;
1037 }
1038 }
Chris Lattner12d96a02004-03-30 21:22:00 +00001039
1040 unsigned TrueReg = getReg(TrueVal, MBB, IP);
1041 unsigned FalseReg = getReg(FalseVal, MBB, IP);
1042 unsigned RealDestReg = DestReg;
Chris Lattner12d96a02004-03-30 21:22:00 +00001043
Chris Lattner12d96a02004-03-30 21:22:00 +00001044
1045 // Annoyingly enough, X86 doesn't HAVE 8-bit conditional moves. Because of
1046 // this, we have to promote the incoming values to 16 bits, perform a 16-bit
1047 // cmove, then truncate the result.
1048 if (SelectClass == cByte) {
1049 DestReg = makeAnotherReg(Type::ShortTy);
1050 if (getClassB(TrueVal->getType()) == cByte) {
1051 // Promote the true value, by storing it into AL, and reading from AX.
1052 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::AL).addReg(TrueReg);
1053 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::AH).addImm(0);
1054 TrueReg = makeAnotherReg(Type::ShortTy);
1055 BuildMI(*MBB, IP, X86::MOV16rr, 1, TrueReg).addReg(X86::AX);
1056 }
1057 if (getClassB(FalseVal->getType()) == cByte) {
1058 // Promote the true value, by storing it into CL, and reading from CX.
1059 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(FalseReg);
1060 BuildMI(*MBB, IP, X86::MOV8ri, 1, X86::CH).addImm(0);
1061 FalseReg = makeAnotherReg(Type::ShortTy);
1062 BuildMI(*MBB, IP, X86::MOV16rr, 1, FalseReg).addReg(X86::CX);
1063 }
1064 }
1065
1066 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(TrueReg).addReg(FalseReg);
1067
1068 switch (SelectClass) {
1069 case cByte:
1070 // We did the computation with 16-bit registers. Truncate back to our
1071 // result by copying into AX then copying out AL.
1072 BuildMI(*MBB, IP, X86::MOV16rr, 1, X86::AX).addReg(DestReg);
1073 BuildMI(*MBB, IP, X86::MOV8rr, 1, RealDestReg).addReg(X86::AL);
1074 break;
1075 case cLong:
1076 // Move the upper half of the value as well.
1077 BuildMI(*MBB, IP, Opcode, 2,DestReg+1).addReg(TrueReg+1).addReg(FalseReg+1);
1078 break;
1079 }
1080}
Chris Lattner58c41fe2003-08-24 19:19:47 +00001081
1082
1083
Brian Gaekec2505982002-11-30 11:57:28 +00001084/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1085/// operand, in the specified target register.
Misha Brukman538607f2004-03-01 23:53:11 +00001086///
Chris Lattner3e130a22003-01-13 00:32:26 +00001087void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
1088 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001089
1090 // Make sure we have the register number for this value...
1091 unsigned Reg = VR.Val ? getReg(VR.Val) : VR.Reg;
1092
Chris Lattner3e130a22003-01-13 00:32:26 +00001093 switch (getClassB(VR.Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +00001094 case cByte:
1095 // Extend value into target register (8->32)
1096 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001097 BuildMI(BB, X86::MOVZX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001098 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001099 BuildMI(BB, X86::MOVSX32rr8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001100 break;
1101 case cShort:
1102 // Extend value into target register (16->32)
1103 if (isUnsigned)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001104 BuildMI(BB, X86::MOVZX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001105 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001106 BuildMI(BB, X86::MOVSX32rr16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001107 break;
1108 case cInt:
1109 // Move value into target register (32->32)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001110 BuildMI(BB, X86::MOV32rr, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001111 break;
1112 default:
1113 assert(0 && "Unpromotable operand class in promote32");
1114 }
Brian Gaekec2505982002-11-30 11:57:28 +00001115}
Chris Lattnerc5291f52002-10-27 21:16:59 +00001116
Chris Lattner72614082002-10-25 22:55:53 +00001117/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
1118/// we have the following possibilities:
1119///
1120/// ret void: No return value, simply emit a 'ret' instruction
1121/// ret sbyte, ubyte : Extend value into EAX and return
1122/// ret short, ushort: Extend value into EAX and return
1123/// ret int, uint : Move value into EAX and return
1124/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +00001125/// ret long, ulong : Move value into EAX/EDX and return
1126/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +00001127///
Chris Lattner3e130a22003-01-13 00:32:26 +00001128void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001129 if (I.getNumOperands() == 0) {
1130 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1131 return;
1132 }
1133
1134 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00001135 unsigned RetReg = getReg(RetVal);
1136 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +00001137 case cByte: // integral return values: extend or move into EAX and return
1138 case cShort:
1139 case cInt:
Chris Lattner3e130a22003-01-13 00:32:26 +00001140 promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType()));
Chris Lattnerdbd73722003-05-06 21:32:22 +00001141 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001142 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001143 break;
1144 case cFP: // Floats & Doubles: Return in ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001145 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001146 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +00001147 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +00001148 break;
1149 case cLong:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001150 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(RetReg);
1151 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +00001152 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001153 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1154 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001155 break;
Chris Lattner94af4142002-12-25 05:13:53 +00001156 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001157 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001158 }
Chris Lattner43189d12002-11-17 20:07:45 +00001159 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001160 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001161}
1162
Chris Lattner55f6fab2003-01-16 18:07:23 +00001163// getBlockAfter - Return the basic block which occurs lexically after the
1164// specified one.
1165static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1166 Function::iterator I = BB; ++I; // Get iterator to next block
1167 return I != BB->getParent()->end() ? &*I : 0;
1168}
1169
Chris Lattner51b49a92002-11-02 19:45:49 +00001170/// visitBranchInst - Handle conditional and unconditional branches here. Note
1171/// that since code layout is frozen at this point, that if we are trying to
1172/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001173/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001174///
Chris Lattner94af4142002-12-25 05:13:53 +00001175void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +00001176 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1177
1178 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001179 if (BI.getSuccessor(0) != NextBB)
Chris Lattner55f6fab2003-01-16 18:07:23 +00001180 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +00001181 return;
1182 }
1183
1184 // See if we can fold the setcc into the branch itself...
Chris Lattner307ecba2004-03-30 22:39:09 +00001185 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
Chris Lattner6d40c192003-01-16 16:43:00 +00001186 if (SCI == 0) {
1187 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1188 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001189 unsigned condReg = getReg(BI.getCondition());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001190 BuildMI(BB, X86::CMP8ri, 2).addReg(condReg).addImm(0);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001191 if (BI.getSuccessor(1) == NextBB) {
1192 if (BI.getSuccessor(0) != NextBB)
1193 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
1194 } else {
1195 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
1196
1197 if (BI.getSuccessor(0) != NextBB)
1198 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
1199 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001200 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001201 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001202
1203 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001204 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001205 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001206
1207 const Type *CompTy = SCI->getOperand(0)->getType();
1208 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001209
Chris Lattnerb2acc512003-10-19 21:09:10 +00001210
Chris Lattner6d40c192003-01-16 16:43:00 +00001211 // LLVM -> X86 signed X86 unsigned
1212 // ----- ---------- ------------
1213 // seteq -> je je
1214 // setne -> jne jne
1215 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001216 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001217 // setgt -> jg ja
1218 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001219 // ----
1220 // js // Used by comparison with 0 optimization
1221 // jns
1222
1223 static const unsigned OpcodeTab[2][8] = {
1224 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1225 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1226 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001227 };
1228
Chris Lattner55f6fab2003-01-16 18:07:23 +00001229 if (BI.getSuccessor(0) != NextBB) {
1230 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
1231 if (BI.getSuccessor(1) != NextBB)
1232 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
1233 } else {
1234 // Change to the inverse condition...
1235 if (BI.getSuccessor(1) != NextBB) {
1236 OpNum ^= 1;
1237 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
1238 }
1239 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001240}
1241
Chris Lattner3e130a22003-01-13 00:32:26 +00001242
1243/// doCall - This emits an abstract call instruction, setting up the arguments
1244/// and the return value as appropriate. For the actual function call itself,
1245/// it inserts the specified CallMI instruction into the stream.
1246///
1247void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001248 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001249
Chris Lattner065faeb2002-12-28 20:24:02 +00001250 // Count how many bytes are to be pushed on the stack...
1251 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001252
Chris Lattner3e130a22003-01-13 00:32:26 +00001253 if (!Args.empty()) {
1254 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1255 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001256 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001257 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001258 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001259 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001260 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001261 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1262 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001263 default: assert(0 && "Unknown class!");
1264 }
1265
1266 // Adjust the stack pointer for the new arguments...
Chris Lattneree352852004-02-29 07:22:16 +00001267 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
Chris Lattner065faeb2002-12-28 20:24:02 +00001268
1269 // Arguments go on the stack in reverse order, as specified by the ABI.
1270 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001271 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattnerce6096f2004-03-01 02:34:08 +00001272 unsigned ArgReg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001273 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001274 case cByte:
Chris Lattner21585222004-03-01 02:42:43 +00001275 case cShort:
1276 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1277 // Zero/Sign extend constant, then stuff into memory.
1278 ConstantInt *Val = cast<ConstantInt>(Args[i].Val);
1279 Val = cast<ConstantInt>(ConstantExpr::getCast(Val, Type::IntTy));
1280 addRegOffset(BuildMI(BB, X86::MOV32mi, 5), X86::ESP, ArgOffset)
1281 .addImm(Val->getRawValue() & 0xFFFFFFFF);
1282 } else {
1283 // Promote arg to 32 bits wide into a temporary register...
1284 ArgReg = makeAnotherReg(Type::UIntTy);
1285 promote32(ArgReg, Args[i]);
1286 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1287 X86::ESP, ArgOffset).addReg(ArgReg);
1288 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001289 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001290 case cInt:
Chris Lattner21585222004-03-01 02:42:43 +00001291 if (Args[i].Val && isa<ConstantInt>(Args[i].Val)) {
1292 unsigned Val = cast<ConstantInt>(Args[i].Val)->getRawValue();
1293 addRegOffset(BuildMI(BB, X86::MOV32mi, 5),
1294 X86::ESP, ArgOffset).addImm(Val);
1295 } else {
1296 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1297 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
1298 X86::ESP, ArgOffset).addReg(ArgReg);
1299 }
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001300 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001301 case cLong:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001302 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001303 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001304 X86::ESP, ArgOffset).addReg(ArgReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001305 addRegOffset(BuildMI(BB, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001306 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1307 ArgOffset += 4; // 8 byte entry, not 4.
1308 break;
1309
Chris Lattner065faeb2002-12-28 20:24:02 +00001310 case cFP:
Chris Lattnerce6096f2004-03-01 02:34:08 +00001311 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001312 if (Args[i].Ty == Type::FloatTy) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001313 addRegOffset(BuildMI(BB, X86::FST32m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001314 X86::ESP, ArgOffset).addReg(ArgReg);
1315 } else {
1316 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001317 addRegOffset(BuildMI(BB, X86::FST64m, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001318 X86::ESP, ArgOffset).addReg(ArgReg);
1319 ArgOffset += 4; // 8 byte entry, not 4.
1320 }
1321 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001322
Chris Lattner3e130a22003-01-13 00:32:26 +00001323 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001324 }
1325 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001326 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001327 } else {
Chris Lattneree352852004-02-29 07:22:16 +00001328 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001329 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001330
Chris Lattner3e130a22003-01-13 00:32:26 +00001331 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001332
Chris Lattneree352852004-02-29 07:22:16 +00001333 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001334
1335 // If there is a return value, scavenge the result from the location the call
1336 // leaves it in...
1337 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001338 if (Ret.Ty != Type::VoidTy) {
1339 unsigned DestClass = getClassB(Ret.Ty);
1340 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001341 case cByte:
1342 case cShort:
1343 case cInt: {
1344 // Integral results are in %eax, or the appropriate portion
1345 // thereof.
1346 static const unsigned regRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001347 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr
Brian Gaeke20244b72002-12-12 15:33:40 +00001348 };
1349 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001350 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001351 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001352 }
Chris Lattner94af4142002-12-25 05:13:53 +00001353 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001354 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001355 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001356 case cLong: // Long values are left in EDX:EAX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001357 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg).addReg(X86::EAX);
1358 BuildMI(BB, X86::MOV32rr, 1, Ret.Reg+1).addReg(X86::EDX);
Chris Lattner3e130a22003-01-13 00:32:26 +00001359 break;
1360 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001361 }
Chris Lattnera3243642002-12-04 23:45:28 +00001362 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001363}
Chris Lattner2df035b2002-11-02 19:27:56 +00001364
Chris Lattner3e130a22003-01-13 00:32:26 +00001365
1366/// visitCallInst - Push args on stack and do a procedure call instruction.
1367void ISel::visitCallInst(CallInst &CI) {
1368 MachineInstr *TheCall;
1369 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001370 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001371 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001372 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1373 return;
1374 }
1375
Chris Lattner3e130a22003-01-13 00:32:26 +00001376 // Emit a CALL instruction with PC-relative displacement.
1377 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1378 } else { // Emit an indirect call...
1379 unsigned Reg = getReg(CI.getCalledValue());
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001380 TheCall = BuildMI(X86::CALL32r, 1).addReg(Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001381 }
1382
1383 std::vector<ValueRecord> Args;
1384 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001385 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001386
1387 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1388 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001389}
Chris Lattner3e130a22003-01-13 00:32:26 +00001390
Chris Lattneraeb54b82003-08-28 21:23:43 +00001391
Chris Lattner44827152003-12-28 09:47:19 +00001392/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1393/// function, lowering any calls to unknown intrinsic functions into the
1394/// equivalent LLVM code.
Misha Brukman538607f2004-03-01 23:53:11 +00001395///
Chris Lattner44827152003-12-28 09:47:19 +00001396void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1397 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1398 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1399 if (CallInst *CI = dyn_cast<CallInst>(I++))
1400 if (Function *F = CI->getCalledFunction())
1401 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001402 case Intrinsic::not_intrinsic:
Chris Lattner317201d2004-03-13 00:24:00 +00001403 case Intrinsic::vastart:
1404 case Intrinsic::vacopy:
1405 case Intrinsic::vaend:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001406 case Intrinsic::returnaddress:
1407 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001408 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001409 case Intrinsic::memset:
Chris Lattner44827152003-12-28 09:47:19 +00001410 // We directly implement these intrinsics
1411 break;
1412 default:
1413 // All other intrinsic calls we must lower.
1414 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001415 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001416 if (Before) { // Move iterator to instruction after call
1417 I = Before; ++I;
1418 } else {
1419 I = BB->begin();
1420 }
1421 }
1422
1423}
1424
Brian Gaeked0fde302003-11-11 22:41:34 +00001425void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001426 unsigned TmpReg1, TmpReg2;
1427 switch (ID) {
Chris Lattner5634b9f2004-03-13 00:24:52 +00001428 case Intrinsic::vastart:
Chris Lattnereca195e2003-05-08 19:44:13 +00001429 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001430 TmpReg1 = getReg(CI);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001431 addFrameReference(BuildMI(BB, X86::LEA32r, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001432 return;
1433
Chris Lattner5634b9f2004-03-13 00:24:52 +00001434 case Intrinsic::vacopy:
Chris Lattner73815062003-10-18 05:56:40 +00001435 TmpReg1 = getReg(CI);
1436 TmpReg2 = getReg(CI.getOperand(1));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001437 BuildMI(BB, X86::MOV32rr, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001438 return;
Chris Lattner5634b9f2004-03-13 00:24:52 +00001439 case Intrinsic::vaend: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001440
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001441 case Intrinsic::returnaddress:
1442 case Intrinsic::frameaddress:
1443 TmpReg1 = getReg(CI);
1444 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1445 if (ID == Intrinsic::returnaddress) {
1446 // Just load the return address
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001447 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001448 ReturnAddressIndex);
1449 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001450 addFrameReference(BuildMI(BB, X86::LEA32r, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001451 ReturnAddressIndex, -4);
1452 }
1453 } else {
1454 // Values other than zero are not implemented yet.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001455 BuildMI(BB, X86::MOV32ri, 1, TmpReg1).addImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001456 }
1457 return;
1458
Chris Lattner915e5e52004-02-12 17:53:22 +00001459 case Intrinsic::memcpy: {
1460 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1461 unsigned Align = 1;
1462 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1463 Align = AlignC->getRawValue();
1464 if (Align == 0) Align = 1;
1465 }
1466
1467 // Turn the byte code into # iterations
Chris Lattner915e5e52004-02-12 17:53:22 +00001468 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001469 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001470 switch (Align & 3) {
1471 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001472 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1473 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1474 } else {
1475 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001476 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001477 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001478 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001479 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001480 break;
1481 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001482 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1483 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1484 } else {
1485 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001486 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001487 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001488 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001489 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001490 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001491 default: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001492 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001493 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001494 break;
1495 }
1496
1497 // No matter what the alignment is, we put the source in ESI, the
1498 // destination in EDI, and the count in ECX.
1499 TmpReg1 = getReg(CI.getOperand(1));
1500 TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001501 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1502 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
1503 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001504 BuildMI(BB, Opcode, 0);
1505 return;
1506 }
1507 case Intrinsic::memset: {
1508 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1509 unsigned Align = 1;
1510 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1511 Align = AlignC->getRawValue();
1512 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001513 }
1514
Chris Lattner2a0f2242004-02-14 04:46:05 +00001515 // Turn the byte code into # iterations
Chris Lattner2a0f2242004-02-14 04:46:05 +00001516 unsigned CountReg;
1517 unsigned Opcode;
1518 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1519 unsigned Val = ValC->getRawValue() & 255;
1520
1521 // If the value is a constant, then we can potentially use larger copies.
1522 switch (Align & 3) {
1523 case 2: // WORD aligned
1524 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001525 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001526 } else {
1527 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001528 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001529 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001530 }
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001531 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001532 Opcode = X86::REP_STOSW;
1533 break;
1534 case 0: // DWORD aligned
1535 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001536 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001537 } else {
1538 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner8dd8d262004-02-26 01:20:02 +00001539 unsigned ByteReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001540 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001541 }
1542 Val = (Val << 8) | Val;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001543 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001544 Opcode = X86::REP_STOSD;
1545 break;
Chris Lattner8dd8d262004-02-26 01:20:02 +00001546 default: // BYTE aligned
Chris Lattner2a0f2242004-02-14 04:46:05 +00001547 CountReg = getReg(CI.getOperand(3));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001548 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001549 Opcode = X86::REP_STOSB;
1550 break;
1551 }
1552 } else {
1553 // If it's not a constant value we are storing, just fall back. We could
1554 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1555 unsigned ValReg = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001556 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001557 CountReg = getReg(CI.getOperand(3));
1558 Opcode = X86::REP_STOSB;
1559 }
1560
1561 // No matter what the alignment is, we put the source in ESI, the
1562 // destination in EDI, and the count in ECX.
1563 TmpReg1 = getReg(CI.getOperand(1));
1564 //TmpReg2 = getReg(CI.getOperand(2));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001565 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
1566 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001567 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001568 return;
1569 }
1570
Chris Lattner44827152003-12-28 09:47:19 +00001571 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001572 }
1573}
1574
Chris Lattner7dee5da2004-03-08 01:58:35 +00001575static bool isSafeToFoldLoadIntoInstruction(LoadInst &LI, Instruction &User) {
1576 if (LI.getParent() != User.getParent())
1577 return false;
1578 BasicBlock::iterator It = &LI;
1579 // Check all of the instructions between the load and the user. We should
1580 // really use alias analysis here, but for now we just do something simple.
1581 for (++It; It != BasicBlock::iterator(&User); ++It) {
1582 switch (It->getOpcode()) {
Chris Lattner85c84e72004-03-18 06:29:54 +00001583 case Instruction::Free:
Chris Lattner7dee5da2004-03-08 01:58:35 +00001584 case Instruction::Store:
1585 case Instruction::Call:
1586 case Instruction::Invoke:
1587 return false;
1588 }
1589 }
1590 return true;
1591}
1592
Chris Lattnereca195e2003-05-08 19:44:13 +00001593
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001594/// visitSimpleBinary - Implement simple binary operators for integral types...
1595/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1596/// Xor.
Misha Brukman538607f2004-03-01 23:53:11 +00001597///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001598void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1599 unsigned DestReg = getReg(B);
1600 MachineBasicBlock::iterator MI = BB->end();
Chris Lattner721d2d42004-03-08 01:18:36 +00001601 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1602
Chris Lattner7dee5da2004-03-08 01:58:35 +00001603 // Special case: op Reg, load [mem]
1604 if (isa<LoadInst>(Op0) && !isa<LoadInst>(Op1))
1605 if (!B.swapOperands())
1606 std::swap(Op0, Op1); // Make sure any loads are in the RHS.
1607
1608 unsigned Class = getClassB(B.getType());
1609 if (isa<LoadInst>(Op1) && Class < cFP &&
1610 isSafeToFoldLoadIntoInstruction(*cast<LoadInst>(Op1), B)) {
1611
1612 static const unsigned OpcodeTab[][3] = {
1613 // Arithmetic operators
1614 { X86::ADD8rm, X86::ADD16rm, X86::ADD32rm }, // ADD
1615 { X86::SUB8rm, X86::SUB16rm, X86::SUB32rm }, // SUB
1616
1617 // Bitwise operators
1618 { X86::AND8rm, X86::AND16rm, X86::AND32rm }, // AND
1619 { X86:: OR8rm, X86:: OR16rm, X86:: OR32rm }, // OR
1620 { X86::XOR8rm, X86::XOR16rm, X86::XOR32rm }, // XOR
1621 };
1622
1623 assert(Class < cFP && "General code handles 64-bit integer types!");
1624 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1625
1626 unsigned BaseReg, Scale, IndexReg, Disp;
1627 getAddressingMode(cast<LoadInst>(Op1)->getOperand(0), BaseReg,
1628 Scale, IndexReg, Disp);
1629
1630 unsigned Op0r = getReg(Op0);
1631 addFullAddress(BuildMI(BB, Opcode, 2, DestReg).addReg(Op0r),
1632 BaseReg, Scale, IndexReg, Disp);
1633 return;
1634 }
1635
Chris Lattner721d2d42004-03-08 01:18:36 +00001636 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001637}
Chris Lattner3e130a22003-01-13 00:32:26 +00001638
Chris Lattnerb2acc512003-10-19 21:09:10 +00001639/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1640/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1641/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001642///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001643/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1644/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001645///
1646void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001647 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001648 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001649 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001650 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001651
1652 // sub 0, X -> neg X
1653 if (OperatorClass == 1 && Class != cLong)
Chris Lattneraf703622004-02-02 18:56:30 +00001654 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001655 if (CI->isNullValue()) {
1656 unsigned op1Reg = getReg(Op1, MBB, IP);
1657 switch (Class) {
1658 default: assert(0 && "Unknown class for this function!");
1659 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001660 BuildMI(*MBB, IP, X86::NEG8r, 1, DestReg).addReg(op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001661 return;
1662 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001663 BuildMI(*MBB, IP, X86::NEG16r, 1, DestReg).addReg(op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001664 return;
1665 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001666 BuildMI(*MBB, IP, X86::NEG32r, 1, DestReg).addReg(op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001667 return;
1668 }
1669 }
Chris Lattner9f8fd6d2004-02-02 19:31:38 +00001670 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1671 if (CFP->isExactlyValue(-0.0)) {
1672 // -0.0 - X === -X
1673 unsigned op1Reg = getReg(Op1, MBB, IP);
Chris Lattneree352852004-02-29 07:22:16 +00001674 BuildMI(*MBB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
Chris Lattner9f8fd6d2004-02-02 19:31:38 +00001675 return;
1676 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001677
Chris Lattnerb2acc512003-10-19 21:09:10 +00001678 // Special case: op Reg, <const>
Chris Lattner721d2d42004-03-08 01:18:36 +00001679 if (Class != cLong && isa<ConstantInt>(Op1)) {
1680 ConstantInt *Op1C = cast<ConstantInt>(Op1);
1681 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001682
Chris Lattner721d2d42004-03-08 01:18:36 +00001683 // xor X, -1 -> not X
1684 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1685 static unsigned const NOTTab[] = { X86::NOT8r, X86::NOT16r, X86::NOT32r };
1686 BuildMI(*MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
1687 return;
1688 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001689
Chris Lattner721d2d42004-03-08 01:18:36 +00001690 // add X, -1 -> dec X
1691 if (OperatorClass == 0 && Op1C->isAllOnesValue()) {
1692 static unsigned const DECTab[] = { X86::DEC8r, X86::DEC16r, X86::DEC32r };
1693 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1694 return;
1695 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001696
Chris Lattner721d2d42004-03-08 01:18:36 +00001697 // add X, 1 -> inc X
1698 if (OperatorClass == 0 && Op1C->equalsInt(1)) {
1699 static unsigned const DECTab[] = { X86::INC8r, X86::INC16r, X86::INC32r };
1700 BuildMI(*MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1701 return;
1702 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001703
Chris Lattner721d2d42004-03-08 01:18:36 +00001704 static const unsigned OpcodeTab[][3] = {
1705 // Arithmetic operators
1706 { X86::ADD8ri, X86::ADD16ri, X86::ADD32ri }, // ADD
1707 { X86::SUB8ri, X86::SUB16ri, X86::SUB32ri }, // SUB
Chris Lattnerb2acc512003-10-19 21:09:10 +00001708
Chris Lattner721d2d42004-03-08 01:18:36 +00001709 // Bitwise operators
1710 { X86::AND8ri, X86::AND16ri, X86::AND32ri }, // AND
1711 { X86:: OR8ri, X86:: OR16ri, X86:: OR32ri }, // OR
1712 { X86::XOR8ri, X86::XOR16ri, X86::XOR32ri }, // XOR
1713 };
1714
1715 assert(Class < cFP && "General code handles 64-bit integer types!");
1716 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1717
1718
1719 uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
1720 BuildMI(*MBB, IP, Opcode, 5, DestReg).addReg(Op0r).addImm(Op1v);
1721 return;
1722 }
1723
1724 // Finally, handle the general case now.
1725 static const unsigned OpcodeTab[][4] = {
1726 // Arithmetic operators
1727 { X86::ADD8rr, X86::ADD16rr, X86::ADD32rr, X86::FpADD }, // ADD
1728 { X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB }, // SUB
1729
Chris Lattnerb2acc512003-10-19 21:09:10 +00001730 // Bitwise operators
Chris Lattner721d2d42004-03-08 01:18:36 +00001731 { X86::AND8rr, X86::AND16rr, X86::AND32rr, 0 }, // AND
1732 { X86:: OR8rr, X86:: OR16rr, X86:: OR32rr, 0 }, // OR
1733 { X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0 }, // XOR
Chris Lattnerb2acc512003-10-19 21:09:10 +00001734 };
Chris Lattner721d2d42004-03-08 01:18:36 +00001735
1736 bool isLong = false;
1737 if (Class == cLong) {
1738 isLong = true;
1739 Class = cInt; // Bottom 32 bits are handled just like ints
1740 }
1741
Chris Lattnerb2acc512003-10-19 21:09:10 +00001742 unsigned Opcode = OpcodeTab[OperatorClass][Class];
Chris Lattner721d2d42004-03-08 01:18:36 +00001743 assert(Opcode && "Floating point arguments to logical inst?");
1744 unsigned Op0r = getReg(Op0, MBB, IP);
1745 unsigned Op1r = getReg(Op1, MBB, IP);
1746 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1747
1748 if (isLong) { // Handle the upper 32 bits of long values...
1749 static const unsigned TopTab[] = {
1750 X86::ADC32rr, X86::SBB32rr, X86::AND32rr, X86::OR32rr, X86::XOR32rr
1751 };
1752 BuildMI(*MBB, IP, TopTab[OperatorClass], 2,
1753 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
1754 }
Chris Lattnere2954c82002-11-02 20:04:26 +00001755}
1756
Chris Lattner3e130a22003-01-13 00:32:26 +00001757/// doMultiply - Emit appropriate instructions to multiply together the
1758/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
1759/// result should be given as DestTy.
1760///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001761void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00001762 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00001763 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001764 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00001765 switch (Class) {
1766 case cFP: // Floating point multiply
Chris Lattneree352852004-02-29 07:22:16 +00001767 BuildMI(*MBB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001768 return;
Chris Lattner0f1c4612003-06-21 17:16:58 +00001769 case cInt:
1770 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001771 BuildMI(*MBB, MBBI, Class == cInt ? X86::IMUL32rr:X86::IMUL16rr, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00001772 .addReg(op0Reg).addReg(op1Reg);
1773 return;
1774 case cByte:
1775 // Must use the MUL instruction, which forces use of AL...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001776 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, X86::AL).addReg(op0Reg);
1777 BuildMI(*MBB, MBBI, X86::MUL8r, 1).addReg(op1Reg);
1778 BuildMI(*MBB, MBBI, X86::MOV8rr, 1, DestReg).addReg(X86::AL);
Chris Lattner0f1c4612003-06-21 17:16:58 +00001779 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001780 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001781 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00001782 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001783}
1784
Chris Lattnerb2acc512003-10-19 21:09:10 +00001785// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1786// returns zero when the input is not exactly a power of two.
1787static unsigned ExactLog2(unsigned Val) {
1788 if (Val == 0) return 0;
1789 unsigned Count = 0;
1790 while (Val != 1) {
1791 if (Val & 1) return 0;
1792 Val >>= 1;
1793 ++Count;
1794 }
1795 return Count+1;
1796}
1797
1798void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001799 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001800 unsigned DestReg, const Type *DestTy,
1801 unsigned op0Reg, unsigned ConstRHS) {
1802 unsigned Class = getClass(DestTy);
1803
1804 // If the element size is exactly a power of 2, use a shift to get it.
1805 if (unsigned Shift = ExactLog2(ConstRHS)) {
1806 switch (Class) {
1807 default: assert(0 && "Unknown class for this function!");
1808 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001809 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001810 return;
1811 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001812 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001813 return;
1814 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001815 BuildMI(*MBB, IP, X86::SHL32ri,2, DestReg).addReg(op0Reg).addImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001816 return;
1817 }
1818 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00001819
1820 if (Class == cShort) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001821 BuildMI(*MBB, IP, X86::IMUL16rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00001822 return;
1823 } else if (Class == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001824 BuildMI(*MBB, IP, X86::IMUL32rri,2,DestReg).addReg(op0Reg).addImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00001825 return;
1826 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001827
1828 // Most general case, emit a normal multiply...
Chris Lattner6e173a02004-02-17 06:16:44 +00001829 static const unsigned MOVriTab[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001830 X86::MOV8ri, X86::MOV16ri, X86::MOV32ri
Chris Lattnerb2acc512003-10-19 21:09:10 +00001831 };
1832
1833 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattneree352852004-02-29 07:22:16 +00001834 BuildMI(*MBB, IP, MOVriTab[Class], 1, TmpReg).addImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001835
1836 // Emit a MUL to multiply the register holding the index by
1837 // elementSize, putting the result in OffsetReg.
1838 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
1839}
1840
Chris Lattnerca9671d2002-11-02 20:28:58 +00001841/// visitMul - Multiplies are not simple binary operators because they must deal
1842/// with the EAX register explicitly.
1843///
1844void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +00001845 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00001846 unsigned DestReg = getReg(I);
1847
1848 // Simple scalar multiply?
1849 if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001850 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
1851 unsigned Val = (unsigned)CI->getRawValue(); // Cannot be 64-bit constant
1852 MachineBasicBlock::iterator MBBI = BB->end();
1853 doMultiplyConst(BB, MBBI, DestReg, I.getType(), Op0Reg, Val);
1854 } else {
1855 unsigned Op1Reg = getReg(I.getOperand(1));
1856 MachineBasicBlock::iterator MBBI = BB->end();
1857 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
1858 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001859 } else {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001860 unsigned Op1Reg = getReg(I.getOperand(1));
1861
Chris Lattner3e130a22003-01-13 00:32:26 +00001862 // Long value. We have to do things the hard way...
1863 // Multiply the two low parts... capturing carry into EDX
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001864 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Op0Reg);
1865 BuildMI(BB, X86::MUL32r, 1).addReg(Op1Reg); // AL*BL
Chris Lattner3e130a22003-01-13 00:32:26 +00001866
1867 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001868 BuildMI(BB, X86::MOV32rr, 1, DestReg).addReg(X86::EAX); // AL*BL
1869 BuildMI(BB, X86::MOV32rr, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32
Chris Lattner3e130a22003-01-13 00:32:26 +00001870
1871 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001872 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001873 BuildMI(*BB, MBBI, X86::IMUL32rr,2,AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001874
1875 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001876 BuildMI(*BB, MBBI, X86::ADD32rr, 2, // AH*BL+(AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001877 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001878
1879 MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001880 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001881 BuildMI(*BB, MBBI, X86::IMUL32rr,2,ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001882
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001883 BuildMI(*BB, MBBI, X86::ADD32rr, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001884 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001885 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001886}
Chris Lattnerca9671d2002-11-02 20:28:58 +00001887
Chris Lattner06925362002-11-17 21:56:38 +00001888
Chris Lattnerf01729e2002-11-02 20:54:46 +00001889/// visitDivRem - Handle division and remainder instructions... these
1890/// instruction both require the same instructions to be generated, they just
1891/// select the result from a different register. Note that both of these
1892/// instructions work differently for signed and unsigned operands.
1893///
1894void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00001895 unsigned Op0Reg = getReg(I.getOperand(0));
1896 unsigned Op1Reg = getReg(I.getOperand(1));
1897 unsigned ResultReg = getReg(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001898
Chris Lattnercadff442003-10-23 17:21:43 +00001899 MachineBasicBlock::iterator IP = BB->end();
1900 emitDivRemOperation(BB, IP, Op0Reg, Op1Reg, I.getOpcode() == Instruction::Div,
1901 I.getType(), ResultReg);
1902}
1903
1904void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001905 MachineBasicBlock::iterator IP,
Chris Lattnercadff442003-10-23 17:21:43 +00001906 unsigned Op0Reg, unsigned Op1Reg, bool isDiv,
1907 const Type *Ty, unsigned ResultReg) {
1908 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00001909 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001910 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00001911 if (isDiv) {
Chris Lattneree352852004-02-29 07:22:16 +00001912 BuildMI(*BB, IP, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001913 } else { // Floating point remainder...
Chris Lattner3e130a22003-01-13 00:32:26 +00001914 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001915 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00001916 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00001917 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
1918 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00001919 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
1920 }
Chris Lattner94af4142002-12-25 05:13:53 +00001921 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001922 case cLong: {
1923 static const char *FnName[] =
1924 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
1925
Chris Lattnercadff442003-10-23 17:21:43 +00001926 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00001927 MachineInstr *TheCall =
1928 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
1929
1930 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00001931 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
1932 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00001933 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
1934 return;
1935 }
1936 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00001937 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00001938 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00001939 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001940
1941 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001942 static const unsigned MovOpcode[]={ X86::MOV8rr, X86::MOV16rr, X86::MOV32rr };
1943 static const unsigned SarOpcode[]={ X86::SAR8ri, X86::SAR16ri, X86::SAR32ri };
1944 static const unsigned ClrOpcode[]={ X86::MOV8ri, X86::MOV16ri, X86::MOV32ri };
Chris Lattnerf01729e2002-11-02 20:54:46 +00001945 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
1946
1947 static const unsigned DivOpcode[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00001948 { X86::DIV8r , X86::DIV16r , X86::DIV32r , 0 }, // Unsigned division
1949 { X86::IDIV8r, X86::IDIV16r, X86::IDIV32r, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00001950 };
1951
Chris Lattnercadff442003-10-23 17:21:43 +00001952 bool isSigned = Ty->isSigned();
Chris Lattnerf01729e2002-11-02 20:54:46 +00001953 unsigned Reg = Regs[Class];
1954 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00001955
1956 // Put the first operand into one of the A registers...
Chris Lattneree352852004-02-29 07:22:16 +00001957 BuildMI(*BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001958
1959 if (isSigned) {
1960 // Emit a sign extension instruction...
Chris Lattnercadff442003-10-23 17:21:43 +00001961 unsigned ShiftResult = makeAnotherReg(Ty);
Chris Lattneree352852004-02-29 07:22:16 +00001962 BuildMI(*BB, IP, SarOpcode[Class], 2,ShiftResult).addReg(Op0Reg).addImm(31);
1963 BuildMI(*BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001964 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00001965 // If unsigned, emit a zeroing instruction... (reg = 0)
Chris Lattneree352852004-02-29 07:22:16 +00001966 BuildMI(*BB, IP, ClrOpcode[Class], 2, ExtReg).addImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001967 }
1968
Chris Lattner06925362002-11-17 21:56:38 +00001969 // Emit the appropriate divide or remainder instruction...
Chris Lattneree352852004-02-29 07:22:16 +00001970 BuildMI(*BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00001971
Chris Lattnerf01729e2002-11-02 20:54:46 +00001972 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00001973 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00001974
Chris Lattnerf01729e2002-11-02 20:54:46 +00001975 // Put the result into the destination register...
Chris Lattneree352852004-02-29 07:22:16 +00001976 BuildMI(*BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00001977}
Chris Lattnere2954c82002-11-02 20:04:26 +00001978
Chris Lattner06925362002-11-17 21:56:38 +00001979
Brian Gaekea1719c92002-10-31 23:03:59 +00001980/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1981/// for constant immediate shift values, and for constant immediate
1982/// shift values equal to 1. Even the general case is sort of special,
1983/// because the shift amount has to be in CL, not just any old register.
1984///
Chris Lattner3e130a22003-01-13 00:32:26 +00001985void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001986 MachineBasicBlock::iterator IP = BB->end ();
1987 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
1988 I.getOpcode () == Instruction::Shl, I.getType (),
1989 getReg (I));
1990}
1991
1992/// emitShiftOperation - Common code shared between visitShiftInst and
1993/// constant expression support.
1994void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001995 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001996 Value *Op, Value *ShiftAmount, bool isLeftShift,
1997 const Type *ResultTy, unsigned DestReg) {
1998 unsigned SrcReg = getReg (Op, MBB, IP);
1999 bool isSigned = ResultTy->isSigned ();
2000 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002001
2002 static const unsigned ConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002003 { X86::SHR8ri, X86::SHR16ri, X86::SHR32ri, X86::SHRD32rri8 }, // SHR
2004 { X86::SAR8ri, X86::SAR16ri, X86::SAR32ri, X86::SHRD32rri8 }, // SAR
2005 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SHL
2006 { X86::SHL8ri, X86::SHL16ri, X86::SHL32ri, X86::SHLD32rri8 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002007 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002008
Chris Lattner3e130a22003-01-13 00:32:26 +00002009 static const unsigned NonConstantOperand[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002010 { X86::SHR8rCL, X86::SHR16rCL, X86::SHR32rCL }, // SHR
2011 { X86::SAR8rCL, X86::SAR16rCL, X86::SAR32rCL }, // SAR
2012 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SHL
2013 { X86::SHL8rCL, X86::SHL16rCL, X86::SHL32rCL }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00002014 };
Chris Lattner796df732002-11-02 00:44:25 +00002015
Chris Lattner3e130a22003-01-13 00:32:26 +00002016 // Longs, as usual, are handled specially...
2017 if (Class == cLong) {
2018 // If we have a constant shift, we can generate much more efficient code
2019 // than otherwise...
2020 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002021 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002022 unsigned Amount = CUI->getValue();
2023 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002024 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
2025 if (isLeftShift) {
Chris Lattneree352852004-02-29 07:22:16 +00002026 BuildMI(*MBB, IP, Opc[3], 3,
2027 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addImm(Amount);
2028 BuildMI(*MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002029 } else {
Chris Lattneree352852004-02-29 07:22:16 +00002030 BuildMI(*MBB, IP, Opc[3], 3,
2031 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addImm(Amount);
2032 BuildMI(*MBB, IP, Opc[2],2,DestReg+1).addReg(SrcReg+1).addImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002033 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002034 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002035 Amount -= 32;
2036 if (isLeftShift) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002037 BuildMI(*MBB, IP, X86::SHL32ri, 2,
Chris Lattneree352852004-02-29 07:22:16 +00002038 DestReg + 1).addReg(SrcReg).addImm(Amount);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002039 BuildMI(*MBB, IP, X86::MOV32ri, 1,
Chris Lattneree352852004-02-29 07:22:16 +00002040 DestReg).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002041 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002042 unsigned Opcode = isSigned ? X86::SAR32ri : X86::SHR32ri;
Chris Lattneree352852004-02-29 07:22:16 +00002043 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(SrcReg+1).addImm(Amount);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002044 BuildMI(*MBB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002045 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002046 }
2047 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00002048 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2049
2050 if (!isLeftShift && isSigned) {
2051 // If this is a SHR of a Long, then we need to do funny sign extension
2052 // stuff. TmpReg gets the value to use as the high-part if we are
2053 // shifting more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002054 BuildMI(*MBB, IP, X86::SAR32ri, 2, TmpReg).addReg(SrcReg).addImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00002055 } else {
2056 // Other shifts use a fixed zero value if the shift is more than 32
2057 // bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002058 BuildMI(*MBB, IP, X86::MOV32ri, 1, TmpReg).addImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00002059 }
2060
2061 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002062 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002063 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002064
2065 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
2066 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
2067 if (isLeftShift) {
2068 // TmpReg2 = shld inHi, inLo
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002069 BuildMI(*MBB, IP, X86::SHLD32rrCL,2,TmpReg2).addReg(SrcReg+1)
Chris Lattneree352852004-02-29 07:22:16 +00002070 .addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002071 // TmpReg3 = shl inLo, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002072 BuildMI(*MBB, IP, X86::SHL32rCL, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002073
2074 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002075 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002076
2077 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002078 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002079 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
2080 // DestLo = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002081 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002082 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00002083 } else {
2084 // TmpReg2 = shrd inLo, inHi
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002085 BuildMI(*MBB, IP, X86::SHRD32rrCL,2,TmpReg2).addReg(SrcReg)
Chris Lattneree352852004-02-29 07:22:16 +00002086 .addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00002087 // TmpReg3 = s[ah]r inHi, CL
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002088 BuildMI(*MBB, IP, isSigned ? X86::SAR32rCL : X86::SHR32rCL, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00002089 .addReg(SrcReg+1);
2090
2091 // Set the flags to indicate whether the shift was by more than 32 bits.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002092 BuildMI(*MBB, IP, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00002093
2094 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002095 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002096 DestReg).addReg(TmpReg2).addReg(TmpReg3);
2097
2098 // DestHi = (>32) ? TmpReg : TmpReg3;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002099 BuildMI(*MBB, IP, X86::CMOVNE32rr, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00002100 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
2101 }
Brian Gaekea1719c92002-10-31 23:03:59 +00002102 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002103 return;
2104 }
Chris Lattnere9913f22002-11-02 01:41:55 +00002105
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002106 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002107 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2108 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002109
Chris Lattner3e130a22003-01-13 00:32:26 +00002110 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002111 BuildMI(*MBB, IP, Opc[Class], 2,
2112 DestReg).addReg(SrcReg).addImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00002113 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00002114 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002115 BuildMI(*MBB, IP, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002116
Chris Lattner3e130a22003-01-13 00:32:26 +00002117 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Chris Lattneree352852004-02-29 07:22:16 +00002118 BuildMI(*MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002119 }
2120}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00002121
Chris Lattner3e130a22003-01-13 00:32:26 +00002122
Chris Lattner721d2d42004-03-08 01:18:36 +00002123void ISel::getAddressingMode(Value *Addr, unsigned &BaseReg, unsigned &Scale,
2124 unsigned &IndexReg, unsigned &Disp) {
2125 BaseReg = 0; Scale = 1; IndexReg = 0; Disp = 0;
2126 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
2127 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
2128 BaseReg, Scale, IndexReg, Disp))
2129 return;
2130 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
2131 if (CE->getOpcode() == Instruction::GetElementPtr)
2132 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
2133 BaseReg, Scale, IndexReg, Disp))
2134 return;
2135 }
2136
2137 // If it's not foldable, reset addr mode.
2138 BaseReg = getReg(Addr);
2139 Scale = 1; IndexReg = 0; Disp = 0;
2140}
2141
2142
Chris Lattner6fc3c522002-11-17 21:11:55 +00002143/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00002144/// instruction. The load and store instructions are the only place where we
2145/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00002146///
2147void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner7dee5da2004-03-08 01:58:35 +00002148 // Check to see if this load instruction is going to be folded into a binary
2149 // instruction, like add. If so, we don't want to emit it. Wouldn't a real
2150 // pattern matching instruction selector be nice?
2151 if (I.hasOneUse() && getClassB(I.getType()) < cFP) {
2152 Instruction *User = cast<Instruction>(I.use_back());
2153 switch (User->getOpcode()) {
2154 default: User = 0; break;
2155 case Instruction::Add:
2156 case Instruction::Sub:
2157 case Instruction::And:
2158 case Instruction::Or:
2159 case Instruction::Xor:
2160 break;
2161 }
2162
2163 if (User) {
2164 // Okay, we found a user. If the load is the first operand and there is
2165 // no second operand load, reverse the operand ordering. Note that this
2166 // can fail for a subtract (ie, no change will be made).
2167 if (!isa<LoadInst>(User->getOperand(1)))
2168 cast<BinaryOperator>(User)->swapOperands();
2169
2170 // Okay, now that everything is set up, if this load is used by the second
2171 // operand, and if there are no instructions that invalidate the load
2172 // before the binary operator, eliminate the load.
2173 if (User->getOperand(1) == &I &&
2174 isSafeToFoldLoadIntoInstruction(I, *User))
2175 return; // Eliminate the load!
2176 }
2177 }
2178
Chris Lattner94af4142002-12-25 05:13:53 +00002179 unsigned DestReg = getReg(I);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002180 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
Chris Lattner721d2d42004-03-08 01:18:36 +00002181 getAddressingMode(I.getOperand(0), BaseReg, Scale, IndexReg, Disp);
Chris Lattnere8f0d922002-12-24 00:03:11 +00002182
Brian Gaekebfedb912003-07-17 21:30:06 +00002183 unsigned Class = getClassB(I.getType());
Chris Lattner6ac1d712003-10-20 04:48:06 +00002184 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002185 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002186 BaseReg, Scale, IndexReg, Disp);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002187 addFullAddress(BuildMI(BB, X86::MOV32rm, 4, DestReg+1),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002188 BaseReg, Scale, IndexReg, Disp+4);
Chris Lattner94af4142002-12-25 05:13:53 +00002189 return;
2190 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002191
Chris Lattner6ac1d712003-10-20 04:48:06 +00002192 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002193 X86::MOV8rm, X86::MOV16rm, X86::MOV32rm, X86::FLD32m
Chris Lattner3e130a22003-01-13 00:32:26 +00002194 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00002195 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002196 if (I.getType() == Type::DoubleTy) Opcode = X86::FLD64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002197 addFullAddress(BuildMI(BB, Opcode, 4, DestReg),
2198 BaseReg, Scale, IndexReg, Disp);
Chris Lattner3e130a22003-01-13 00:32:26 +00002199}
2200
Chris Lattner6fc3c522002-11-17 21:11:55 +00002201/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
2202/// instruction.
2203///
2204void ISel::visitStoreInst(StoreInst &I) {
Chris Lattner721d2d42004-03-08 01:18:36 +00002205 unsigned BaseReg, Scale, IndexReg, Disp;
2206 getAddressingMode(I.getOperand(1), BaseReg, Scale, IndexReg, Disp);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002207
Chris Lattner6c09db22003-10-20 04:11:23 +00002208 const Type *ValTy = I.getOperand(0)->getType();
2209 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00002210
Chris Lattner5a830962004-02-25 02:56:58 +00002211 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
2212 uint64_t Val = CI->getRawValue();
2213 if (Class == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002214 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002215 BaseReg, Scale, IndexReg, Disp).addImm(Val & ~0U);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002216 addFullAddress(BuildMI(BB, X86::MOV32mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002217 BaseReg, Scale, IndexReg, Disp+4).addImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00002218 } else {
2219 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002220 X86::MOV8mi, X86::MOV16mi, X86::MOV32mi
Chris Lattner5a830962004-02-25 02:56:58 +00002221 };
2222 unsigned Opcode = Opcodes[Class];
Chris Lattnerb6bac512004-02-25 06:13:04 +00002223 addFullAddress(BuildMI(BB, Opcode, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002224 BaseReg, Scale, IndexReg, Disp).addImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00002225 }
2226 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002227 addFullAddress(BuildMI(BB, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002228 BaseReg, Scale, IndexReg, Disp).addImm(CB->getValue());
Chris Lattner5a830962004-02-25 02:56:58 +00002229 } else {
2230 if (Class == cLong) {
2231 unsigned ValReg = getReg(I.getOperand(0));
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002232 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002233 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002234 addFullAddress(BuildMI(BB, X86::MOV32mr, 5),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002235 BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1);
Chris Lattner5a830962004-02-25 02:56:58 +00002236 } else {
2237 unsigned ValReg = getReg(I.getOperand(0));
2238 static const unsigned Opcodes[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002239 X86::MOV8mr, X86::MOV16mr, X86::MOV32mr, X86::FST32m
Chris Lattner5a830962004-02-25 02:56:58 +00002240 };
2241 unsigned Opcode = Opcodes[Class];
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002242 if (ValTy == Type::DoubleTy) Opcode = X86::FST64m;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002243 addFullAddress(BuildMI(BB, Opcode, 1+4),
2244 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Chris Lattner5a830962004-02-25 02:56:58 +00002245 }
Chris Lattner94af4142002-12-25 05:13:53 +00002246 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002247}
2248
2249
Misha Brukman538607f2004-03-01 23:53:11 +00002250/// visitCastInst - Here we have various kinds of copying with or without sign
2251/// extension going on.
2252///
Chris Lattner3e130a22003-01-13 00:32:26 +00002253void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00002254 Value *Op = CI.getOperand(0);
2255 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2256 // of the case are GEP instructions, then the cast does not need to be
2257 // generated explicitly, it will be folded into the GEP.
2258 if (CI.getType() == Type::LongTy &&
2259 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
2260 bool AllUsesAreGEPs = true;
2261 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2262 if (!isa<GetElementPtrInst>(*I)) {
2263 AllUsesAreGEPs = false;
2264 break;
2265 }
2266
2267 // No need to codegen this cast if all users are getelementptr instrs...
2268 if (AllUsesAreGEPs) return;
2269 }
2270
Chris Lattner548f61d2003-04-23 17:22:12 +00002271 unsigned DestReg = getReg(CI);
2272 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00002273 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00002274}
2275
Misha Brukman538607f2004-03-01 23:53:11 +00002276/// emitCastOperation - Common code shared between visitCastInst and constant
2277/// expression cast support.
2278///
Chris Lattner548f61d2003-04-23 17:22:12 +00002279void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002280 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00002281 Value *Src, const Type *DestTy,
2282 unsigned DestReg) {
Chris Lattner3907d112003-04-23 17:57:48 +00002283 unsigned SrcReg = getReg(Src, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002284 const Type *SrcTy = Src->getType();
2285 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002286 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00002287
Chris Lattner3e130a22003-01-13 00:32:26 +00002288 // Implement casts to bool by using compare on the operand followed by set if
2289 // not zero on the result.
2290 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00002291 switch (SrcClass) {
2292 case cByte:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002293 BuildMI(*BB, IP, X86::TEST8rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002294 break;
2295 case cShort:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002296 BuildMI(*BB, IP, X86::TEST16rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002297 break;
2298 case cInt:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002299 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg).addReg(SrcReg);
Chris Lattner20772542003-06-01 03:38:24 +00002300 break;
2301 case cLong: {
2302 unsigned TmpReg = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002303 BuildMI(*BB, IP, X86::OR32rr, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner20772542003-06-01 03:38:24 +00002304 break;
2305 }
2306 case cFP:
Chris Lattneree352852004-02-29 07:22:16 +00002307 BuildMI(*BB, IP, X86::FTST, 1).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002308 BuildMI(*BB, IP, X86::FNSTSW8r, 0);
Chris Lattneree352852004-02-29 07:22:16 +00002309 BuildMI(*BB, IP, X86::SAHF, 1);
Chris Lattner311ca2e2004-02-23 03:21:41 +00002310 break;
Chris Lattner20772542003-06-01 03:38:24 +00002311 }
2312
2313 // If the zero flag is not set, then the value is true, set the byte to
2314 // true.
Chris Lattneree352852004-02-29 07:22:16 +00002315 BuildMI(*BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00002316 return;
2317 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002318
2319 static const unsigned RegRegMove[] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002320 X86::MOV8rr, X86::MOV16rr, X86::MOV32rr, X86::FpMOV, X86::MOV32rr
Chris Lattner3e130a22003-01-13 00:32:26 +00002321 };
2322
2323 // Implement casts between values of the same type class (as determined by
2324 // getClass) by using a register-to-register move.
2325 if (SrcClass == DestClass) {
2326 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattneree352852004-02-29 07:22:16 +00002327 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002328 } else if (SrcClass == cFP) {
2329 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002330 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
Chris Lattneree352852004-02-29 07:22:16 +00002331 BuildMI(*BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002332 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002333 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2334 "Unknown cFP member!");
2335 // Truncate from double to float by storing to memory as short, then
2336 // reading it back.
2337 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00002338 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002339 addFrameReference(BuildMI(*BB, IP, X86::FST32m, 5), FrameIdx).addReg(SrcReg);
2340 addFrameReference(BuildMI(*BB, IP, X86::FLD32m, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002341 }
2342 } else if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002343 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
2344 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002345 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00002346 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002347 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00002348 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002349 return;
2350 }
2351
2352 // Handle cast of SMALLER int to LARGER int using a move with sign extension
2353 // or zero extension, depending on whether the source type was signed.
2354 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2355 SrcClass < DestClass) {
2356 bool isLong = DestClass == cLong;
2357 if (isLong) DestClass = cInt;
2358
2359 static const unsigned Opc[][4] = {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002360 { X86::MOVSX16rr8, X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOV32rr }, // s
2361 { X86::MOVZX16rr8, X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOV32rr } // u
Chris Lattner3e130a22003-01-13 00:32:26 +00002362 };
2363
2364 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattneree352852004-02-29 07:22:16 +00002365 BuildMI(*BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
Chris Lattner548f61d2003-04-23 17:22:12 +00002366 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002367
2368 if (isLong) { // Handle upper 32 bits as appropriate...
2369 if (isUnsigned) // Zero out top bits...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002370 BuildMI(*BB, IP, X86::MOV32ri, 1, DestReg+1).addImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00002371 else // Sign extend bottom half...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002372 BuildMI(*BB, IP, X86::SAR32ri, 2, DestReg+1).addReg(DestReg).addImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00002373 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002374 return;
2375 }
2376
2377 // Special case long -> int ...
2378 if (SrcClass == cLong && DestClass == cInt) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002379 BuildMI(*BB, IP, X86::MOV32rr, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002380 return;
2381 }
2382
2383 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
2384 // move out of AX or AL.
2385 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2386 && SrcClass > DestClass) {
2387 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattneree352852004-02-29 07:22:16 +00002388 BuildMI(*BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
2389 BuildMI(*BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00002390 return;
2391 }
2392
2393 // Handle casts from integer to floating point now...
2394 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002395 // Promote the integer to a type supported by FLD. We do this because there
2396 // are no unsigned FLD instructions, so we must promote an unsigned value to
2397 // a larger signed value, then use FLD on the larger value.
2398 //
2399 const Type *PromoteType = 0;
2400 unsigned PromoteOpcode;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002401 unsigned RealDestReg = DestReg;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002402 switch (SrcTy->getPrimitiveID()) {
2403 case Type::BoolTyID:
2404 case Type::SByteTyID:
2405 // We don't have the facilities for directly loading byte sized data from
2406 // memory (even signed). Promote it to 16 bits.
2407 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002408 PromoteOpcode = X86::MOVSX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002409 break;
2410 case Type::UByteTyID:
2411 PromoteType = Type::ShortTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002412 PromoteOpcode = X86::MOVZX16rr8;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002413 break;
2414 case Type::UShortTyID:
2415 PromoteType = Type::IntTy;
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002416 PromoteOpcode = X86::MOVZX32rr16;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002417 break;
2418 case Type::UIntTyID: {
2419 // Make a 64 bit temporary... and zero out the top of it...
2420 unsigned TmpReg = makeAnotherReg(Type::LongTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002421 BuildMI(*BB, IP, X86::MOV32rr, 1, TmpReg).addReg(SrcReg);
2422 BuildMI(*BB, IP, X86::MOV32ri, 1, TmpReg+1).addImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002423 SrcTy = Type::LongTy;
2424 SrcClass = cLong;
2425 SrcReg = TmpReg;
2426 break;
2427 }
2428 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002429 // Don't fild into the read destination.
2430 DestReg = makeAnotherReg(Type::DoubleTy);
2431 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002432 default: // No promotion needed...
2433 break;
2434 }
2435
2436 if (PromoteType) {
2437 unsigned TmpReg = makeAnotherReg(PromoteType);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002438 unsigned Opc = SrcTy->isSigned() ? X86::MOVSX16rr8 : X86::MOVZX16rr8;
Chris Lattneree352852004-02-29 07:22:16 +00002439 BuildMI(*BB, IP, Opc, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002440 SrcTy = PromoteType;
2441 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00002442 SrcReg = TmpReg;
2443 }
2444
2445 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00002446 int FrameIdx =
2447 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00002448
2449 if (SrcClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002450 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002451 FrameIdx).addReg(SrcReg);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002452 addFrameReference(BuildMI(*BB, IP, X86::MOV32mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002453 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002454 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002455 static const unsigned Op1[] = { X86::MOV8mr, X86::MOV16mr, X86::MOV32mr };
Chris Lattneree352852004-02-29 07:22:16 +00002456 addFrameReference(BuildMI(*BB, IP, Op1[SrcClass], 5),
2457 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002458 }
2459
2460 static const unsigned Op2[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002461 { 0/*byte*/, X86::FILD16m, X86::FILD32m, 0/*FP*/, X86::FILD64m };
Chris Lattneree352852004-02-29 07:22:16 +00002462 addFrameReference(BuildMI(*BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002463
2464 // We need special handling for unsigned 64-bit integer sources. If the
2465 // input number has the "sign bit" set, then we loaded it incorrectly as a
2466 // negative 64-bit number. In this case, add an offset value.
2467 if (SrcTy == Type::ULongTy) {
2468 // Emit a test instruction to see if the dynamic input value was signed.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002469 BuildMI(*BB, IP, X86::TEST32rr, 2).addReg(SrcReg+1).addReg(SrcReg+1);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002470
Chris Lattnerb6bac512004-02-25 06:13:04 +00002471 // If the sign bit is set, get a pointer to an offset, otherwise get a
2472 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002473 MachineConstantPool *CP = F->getConstantPool();
2474 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002475 Constant *Null = Constant::getNullValue(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002476 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002477 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002478 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002479 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
2480
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002481 addConstantPoolReference(BuildMI(*BB, IP, X86::LEA32r, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002482 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002483 unsigned Addr = makeAnotherReg(Type::IntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002484 BuildMI(*BB, IP, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002485
2486 // Load the constant for an add. FIXME: this could make an 'fadd' that
2487 // reads directly from memory, but we don't support these yet.
2488 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002489 addDirectMem(BuildMI(*BB, IP, X86::FLD32m, 4, ConstReg), Addr);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002490
Chris Lattneree352852004-02-29 07:22:16 +00002491 BuildMI(*BB, IP, X86::FpADD, 2, RealDestReg)
2492 .addReg(ConstReg).addReg(DestReg);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002493 }
2494
Chris Lattner3e130a22003-01-13 00:32:26 +00002495 return;
2496 }
2497
2498 // Handle casts from floating point to integer now...
2499 if (SrcClass == cFP) {
2500 // Change the floating point control register to use "round towards zero"
2501 // mode when truncating to an integer value.
2502 //
2503 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002504 addFrameReference(BuildMI(*BB, IP, X86::FNSTCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002505
2506 // Load the old value of the high byte of the control word...
2507 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002508 addFrameReference(BuildMI(*BB, IP, X86::MOV8rm, 4, HighPartOfCW),
Chris Lattneree352852004-02-29 07:22:16 +00002509 CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002510
2511 // Set the high part to be round to zero...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002512 addFrameReference(BuildMI(*BB, IP, X86::MOV8mi, 5),
Chris Lattneree352852004-02-29 07:22:16 +00002513 CWFrameIdx, 1).addImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00002514
2515 // Reload the modified control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002516 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002517
2518 // Restore the memory image of control word to original value
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002519 addFrameReference(BuildMI(*BB, IP, X86::MOV8mr, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002520 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00002521
2522 // We don't have the facilities for directly storing byte sized data to
2523 // memory. Promote it to 16 bits. We also must promote unsigned values to
2524 // larger classes because we only have signed FP stores.
2525 unsigned StoreClass = DestClass;
2526 const Type *StoreTy = DestTy;
2527 if (StoreClass == cByte || DestTy->isUnsigned())
2528 switch (StoreClass) {
2529 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
2530 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
2531 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00002532 // The following treatment of cLong may not be perfectly right,
2533 // but it survives chains of casts of the form
2534 // double->ulong->double.
2535 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00002536 default: assert(0 && "Unknown store class!");
2537 }
2538
2539 // Spill the integer to memory and reload it from there...
2540 int FrameIdx =
2541 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
2542
2543 static const unsigned Op1[] =
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002544 { 0, X86::FIST16m, X86::FIST32m, 0, X86::FISTP64m };
Chris Lattneree352852004-02-29 07:22:16 +00002545 addFrameReference(BuildMI(*BB, IP, Op1[StoreClass], 5),
2546 FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002547
2548 if (DestClass == cLong) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002549 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg), FrameIdx);
2550 addFrameReference(BuildMI(*BB, IP, X86::MOV32rm, 4, DestReg+1),
Chris Lattneree352852004-02-29 07:22:16 +00002551 FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00002552 } else {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002553 static const unsigned Op2[] = { X86::MOV8rm, X86::MOV16rm, X86::MOV32rm };
Chris Lattneree352852004-02-29 07:22:16 +00002554 addFrameReference(BuildMI(*BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002555 }
2556
2557 // Reload the original control word now...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002558 addFrameReference(BuildMI(*BB, IP, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002559 return;
2560 }
2561
Brian Gaeked474e9c2002-12-06 10:49:33 +00002562 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00002563 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002564 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00002565}
Brian Gaekea1719c92002-10-31 23:03:59 +00002566
Chris Lattner73815062003-10-18 05:56:40 +00002567/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00002568///
Chris Lattner73815062003-10-18 05:56:40 +00002569void ISel::visitVANextInst(VANextInst &I) {
2570 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00002571 unsigned DestReg = getReg(I);
2572
Chris Lattnereca195e2003-05-08 19:44:13 +00002573 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00002574 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00002575 default:
2576 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00002577 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00002578 return;
2579 case Type::PointerTyID:
2580 case Type::UIntTyID:
2581 case Type::IntTyID:
2582 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00002583 break;
2584 case Type::ULongTyID:
2585 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00002586 case Type::DoubleTyID:
2587 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00002588 break;
2589 }
2590
2591 // Increment the VAList pointer...
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002592 BuildMI(BB, X86::ADD32ri, 2, DestReg).addReg(VAList).addImm(Size);
Chris Lattner73815062003-10-18 05:56:40 +00002593}
Chris Lattnereca195e2003-05-08 19:44:13 +00002594
Chris Lattner73815062003-10-18 05:56:40 +00002595void ISel::visitVAArgInst(VAArgInst &I) {
2596 unsigned VAList = getReg(I.getOperand(0));
2597 unsigned DestReg = getReg(I);
2598
2599 switch (I.getType()->getPrimitiveID()) {
2600 default:
2601 std::cerr << I;
2602 assert(0 && "Error: bad type for va_next instruction!");
2603 return;
2604 case Type::PointerTyID:
2605 case Type::UIntTyID:
2606 case Type::IntTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002607 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00002608 break;
2609 case Type::ULongTyID:
2610 case Type::LongTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002611 addDirectMem(BuildMI(BB, X86::MOV32rm, 4, DestReg), VAList);
2612 addRegOffset(BuildMI(BB, X86::MOV32rm, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00002613 break;
2614 case Type::DoubleTyID:
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002615 addDirectMem(BuildMI(BB, X86::FLD64m, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00002616 break;
2617 }
Chris Lattnereca195e2003-05-08 19:44:13 +00002618}
2619
Misha Brukman538607f2004-03-01 23:53:11 +00002620/// visitGetElementPtrInst - instruction-select GEP instructions
2621///
Chris Lattner3e130a22003-01-13 00:32:26 +00002622void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00002623 // If this GEP instruction will be folded into all of its users, we don't need
2624 // to explicitly calculate it!
2625 unsigned A, B, C, D;
2626 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) {
2627 // Check all of the users of the instruction to see if they are loads and
2628 // stores.
2629 bool AllWillFold = true;
2630 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
2631 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
2632 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
2633 cast<Instruction>(*UI)->getOperand(0) == &I) {
2634 AllWillFold = false;
2635 break;
2636 }
2637
2638 // If the instruction is foldable, and will be folded into all users, don't
2639 // emit it!
2640 if (AllWillFold) return;
2641 }
2642
Chris Lattner3e130a22003-01-13 00:32:26 +00002643 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00002644 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00002645 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00002646}
2647
Chris Lattner985fe3d2004-02-25 03:45:50 +00002648/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
2649/// GEPTypes (the derived types being stepped through at each level). On return
2650/// from this function, if some indexes of the instruction are representable as
2651/// an X86 lea instruction, the machine operands are put into the Ops
2652/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
2653/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
2654/// addressing mode that only partially consumes the input, the BaseReg input of
2655/// the addressing mode must be left free.
2656///
2657/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
2658///
Chris Lattnerb6bac512004-02-25 06:13:04 +00002659void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2660 std::vector<Value*> &GEPOps,
2661 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
2662 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
2663 const TargetData &TD = TM.getTargetData();
2664
Chris Lattner985fe3d2004-02-25 03:45:50 +00002665 // Clear out the state we are working with...
Chris Lattnerb6bac512004-02-25 06:13:04 +00002666 BaseReg = 0; // No base register
2667 Scale = 1; // Unit scale
2668 IndexReg = 0; // No index register
2669 Disp = 0; // No displacement
2670
Chris Lattner985fe3d2004-02-25 03:45:50 +00002671 // While there are GEP indexes that can be folded into the current address,
2672 // keep processing them.
2673 while (!GEPTypes.empty()) {
2674 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
2675 // It's a struct access. CUI is the index into the structure,
2676 // which names the field. This index must have unsigned type.
2677 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
2678
2679 // Use the TargetData structure to pick out what the layout of the
2680 // structure is in memory. Since the structure index must be constant, we
2681 // can get its value and use it to find the right byte offset from the
2682 // StructLayout class's list of structure member offsets.
Chris Lattnerb6bac512004-02-25 06:13:04 +00002683 Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00002684 GEPOps.pop_back(); // Consume a GEP operand
2685 GEPTypes.pop_back();
2686 } else {
2687 // It's an array or pointer access: [ArraySize x ElementType].
2688 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
2689 Value *idx = GEPOps.back();
2690
2691 // idx is the index into the array. Unlike with structure
2692 // indices, we may not know its actual value at code-generation
2693 // time.
2694 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
2695
2696 // If idx is a constant, fold it into the offset.
Chris Lattner5f2c7b12004-02-25 07:00:55 +00002697 unsigned TypeSize = TD.getTypeSize(SqTy->getElementType());
Chris Lattner985fe3d2004-02-25 03:45:50 +00002698 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00002699 Disp += TypeSize*CSI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00002700 } else {
Chris Lattner5f2c7b12004-02-25 07:00:55 +00002701 // If the index reg is already taken, we can't handle this index.
2702 if (IndexReg) return;
2703
2704 // If this is a size that we can handle, then add the index as
2705 switch (TypeSize) {
2706 case 1: case 2: case 4: case 8:
2707 // These are all acceptable scales on X86.
2708 Scale = TypeSize;
2709 break;
2710 default:
2711 // Otherwise, we can't handle this scale
2712 return;
2713 }
2714
2715 if (CastInst *CI = dyn_cast<CastInst>(idx))
2716 if (CI->getOperand(0)->getType() == Type::IntTy ||
2717 CI->getOperand(0)->getType() == Type::UIntTy)
2718 idx = CI->getOperand(0);
2719
2720 IndexReg = MBB ? getReg(idx, MBB, IP) : 1;
Chris Lattner985fe3d2004-02-25 03:45:50 +00002721 }
2722
2723 GEPOps.pop_back(); // Consume a GEP operand
2724 GEPTypes.pop_back();
2725 }
2726 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00002727
2728 // GEPTypes is empty, which means we have a single operand left. See if we
2729 // can set it as the base register.
2730 //
2731 // FIXME: When addressing modes are more powerful/correct, we could load
2732 // global addresses directly as 32-bit immediates.
2733 assert(BaseReg == 0);
Chris Lattner5f2c7b12004-02-25 07:00:55 +00002734 BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 1;
Chris Lattnerb6bac512004-02-25 06:13:04 +00002735 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00002736}
2737
2738
Chris Lattnerb6bac512004-02-25 06:13:04 +00002739/// isGEPFoldable - Return true if the specified GEP can be completely
2740/// folded into the addressing mode of a load/store or lea instruction.
2741bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
2742 Value *Src, User::op_iterator IdxBegin,
2743 User::op_iterator IdxEnd, unsigned &BaseReg,
2744 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
Chris Lattner7ca04092004-02-22 17:35:42 +00002745 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
2746 Src = CPR->getValue();
2747
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002748 std::vector<Value*> GEPOps;
2749 GEPOps.resize(IdxEnd-IdxBegin+1);
2750 GEPOps[0] = Src;
2751 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
2752
2753 std::vector<const Type*> GEPTypes;
2754 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
2755 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
2756
Chris Lattnerb6bac512004-02-25 06:13:04 +00002757 MachineBasicBlock::iterator IP;
2758 if (MBB) IP = MBB->end();
2759 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
2760
2761 // We can fold it away iff the getGEPIndex call eliminated all operands.
2762 return GEPOps.empty();
2763}
2764
2765void ISel::emitGEPOperation(MachineBasicBlock *MBB,
2766 MachineBasicBlock::iterator IP,
2767 Value *Src, User::op_iterator IdxBegin,
2768 User::op_iterator IdxEnd, unsigned TargetReg) {
2769 const TargetData &TD = TM.getTargetData();
2770 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
2771 Src = CPR->getValue();
2772
2773 std::vector<Value*> GEPOps;
2774 GEPOps.resize(IdxEnd-IdxBegin+1);
2775 GEPOps[0] = Src;
2776 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
2777
2778 std::vector<const Type*> GEPTypes;
2779 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
2780 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00002781
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002782 // Keep emitting instructions until we consume the entire GEP instruction.
2783 while (!GEPOps.empty()) {
2784 unsigned OldSize = GEPOps.size();
Chris Lattnerb6bac512004-02-25 06:13:04 +00002785 unsigned BaseReg, Scale, IndexReg, Disp;
2786 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002787
Chris Lattner985fe3d2004-02-25 03:45:50 +00002788 if (GEPOps.size() != OldSize) {
2789 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00002790 unsigned NextTarget = 0;
2791 if (!GEPOps.empty()) {
2792 assert(BaseReg == 0 &&
2793 "getGEPIndex should have left the base register open for chaining!");
2794 NextTarget = BaseReg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00002795 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00002796
2797 if (IndexReg == 0 && Disp == 0)
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002798 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002799 else
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002800 addFullAddress(BuildMI(*MBB, IP, X86::LEA32r, 5, TargetReg),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002801 BaseReg, Scale, IndexReg, Disp);
2802 --IP;
2803 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00002804 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002805 // The getGEPIndex operation didn't want to build an LEA. Check to see if
2806 // all operands are consumed but the base pointer. If so, just load it
2807 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00002808 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002809 BuildMI(*MBB, IP, X86::MOV32ri, 1, TargetReg).addGlobalAddress(GV);
Chris Lattner7ca04092004-02-22 17:35:42 +00002810 } else {
2811 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002812 BuildMI(*MBB, IP, X86::MOV32rr, 1, TargetReg).addReg(BaseReg);
Chris Lattner7ca04092004-02-22 17:35:42 +00002813 }
2814 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00002815
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002816 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00002817 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002818 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
2819 Value *idx = GEPOps.back();
2820 GEPOps.pop_back(); // Consume a GEP operand
2821 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00002822
Brian Gaeke20244b72002-12-12 15:33:40 +00002823 // idx is the index into the array. Unlike with structure
2824 // indices, we may not know its actual value at code-generation
2825 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00002826 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
2827
Chris Lattnerf5854472003-06-21 16:01:24 +00002828 // Most GEP instructions use a [cast (int/uint) to LongTy] as their
2829 // operand on X86. Handle this case directly now...
2830 if (CastInst *CI = dyn_cast<CastInst>(idx))
2831 if (CI->getOperand(0)->getType() == Type::IntTy ||
2832 CI->getOperand(0)->getType() == Type::UIntTy)
2833 idx = CI->getOperand(0);
2834
Chris Lattner3e130a22003-01-13 00:32:26 +00002835 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00002836 // must find the size of the pointed-to type (Not coincidentally, the next
2837 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002838 const Type *ElTy = SqTy->getElementType();
2839 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00002840
2841 // If idxReg is a constant, we don't need to perform the multiply!
2842 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002843 if (!CSI->isNullValue()) {
Chris Lattner8a307e82002-12-16 19:32:50 +00002844 unsigned Offset = elementSize*CSI->getValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002845 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002846 BuildMI(*MBB, IP, X86::ADD32ri, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00002847 .addReg(Reg).addImm(Offset);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002848 --IP; // Insert the next instruction before this one.
2849 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002850 }
2851 } else if (elementSize == 1) {
2852 // If the element size is 1, we don't have to multiply, just add
2853 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002854 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002855 BuildMI(*MBB, IP, X86::ADD32rr, 2,TargetReg).addReg(Reg).addReg(idxReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002856 --IP; // Insert the next instruction before this one.
2857 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002858 } else {
2859 unsigned idxReg = getReg(idx, MBB, IP);
2860 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002861
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002862 // Make sure we can back the iterator up to point to the first
2863 // instruction emitted.
2864 MachineBasicBlock::iterator BeforeIt = IP;
2865 if (IP == MBB->begin())
2866 BeforeIt = MBB->end();
2867 else
2868 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002869 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
2870
Chris Lattner8a307e82002-12-16 19:32:50 +00002871 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002872 unsigned Reg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002873 BuildMI(*MBB, IP, X86::ADD32rr, 2, TargetReg)
Chris Lattneree352852004-02-29 07:22:16 +00002874 .addReg(Reg).addReg(OffsetReg);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002875
2876 // Step to the first instruction of the multiply.
2877 if (BeforeIt == MBB->end())
2878 IP = MBB->begin();
2879 else
2880 IP = ++BeforeIt;
2881
2882 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002883 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002884 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002885 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002886}
2887
2888
Chris Lattner065faeb2002-12-28 20:24:02 +00002889/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2890/// frame manager, otherwise do it the hard way.
2891///
2892void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00002893 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00002894 const Type *Ty = I.getAllocatedType();
2895 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2896
2897 // If this is a fixed size alloca in the entry block for the function,
2898 // statically stack allocate the space.
2899 //
2900 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
2901 if (I.getParent() == I.getParent()->getParent()->begin()) {
2902 TySize *= CUI->getValue(); // Get total allocated size...
2903 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
2904
2905 // Create a new stack object using the frame manager...
2906 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002907 addFrameReference(BuildMI(BB, X86::LEA32r, 5, getReg(I)), FrameIdx);
Chris Lattner065faeb2002-12-28 20:24:02 +00002908 return;
2909 }
2910 }
2911
2912 // Create a register to hold the temporary result of multiplying the type size
2913 // constant by the variable amount.
2914 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2915 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00002916
2917 // TotalSizeReg = mul <numelements>, <TypeSize>
2918 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002919 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002920
2921 // AddedSize = add <TotalSizeReg>, 15
2922 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002923 BuildMI(BB, X86::ADD32ri, 2, AddedSizeReg).addReg(TotalSizeReg).addImm(15);
Chris Lattner065faeb2002-12-28 20:24:02 +00002924
2925 // AlignedSize = and <AddedSize>, ~15
2926 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002927 BuildMI(BB, X86::AND32ri, 2, AlignedSize).addReg(AddedSizeReg).addImm(~15);
Chris Lattner065faeb2002-12-28 20:24:02 +00002928
Brian Gaekee48ec012002-12-13 06:46:31 +00002929 // Subtract size from stack pointer, thereby allocating some space.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002930 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002931
Brian Gaekee48ec012002-12-13 06:46:31 +00002932 // Put a pointer to the space into the result register, by copying
2933 // the stack pointer.
Alkis Evlogimenos8295f202004-02-29 08:50:03 +00002934 BuildMI(BB, X86::MOV32rr, 1, getReg(I)).addReg(X86::ESP);
Chris Lattner065faeb2002-12-28 20:24:02 +00002935
Misha Brukman48196b32003-05-03 02:18:17 +00002936 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00002937 // object.
2938 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00002939}
Chris Lattner3e130a22003-01-13 00:32:26 +00002940
2941/// visitMallocInst - Malloc instructions are code generated into direct calls
2942/// to the library malloc.
2943///
2944void ISel::visitMallocInst(MallocInst &I) {
2945 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2946 unsigned Arg;
2947
2948 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2949 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2950 } else {
2951 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002952 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00002953 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002954 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00002955 }
2956
2957 std::vector<ValueRecord> Args;
2958 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2959 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002960 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002961 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
2962}
2963
2964
2965/// visitFreeInst - Free instructions are code gen'd to call the free libc
2966/// function.
2967///
2968void ISel::visitFreeInst(FreeInst &I) {
2969 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002970 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00002971 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002972 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002973 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
2974}
2975
Chris Lattnerd281de22003-07-26 23:49:58 +00002976/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002977/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00002978/// generated code sucks but the implementation is nice and simple.
2979///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00002980FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
2981 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00002982}