blob: d6e3a75186f0af9a4c72a12105222b3f704fa4c8 [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"
Misha Brukmand2cc0172002-11-20 00:58:23 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner94af4142002-12-25 05:13:53 +000027#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmand2cc0172002-11-20 00:58:23 +000028#include "llvm/Target/MRegisterInfo.h"
Misha Brukmanc8893fc2003-10-23 16:22:08 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattner3f1e8e72004-02-22 07:04:00 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner67580ed2003-05-13 20:21:19 +000031#include "llvm/Support/InstVisitor.h"
Chris Lattnercf93cdd2004-01-30 22:13:44 +000032#include "llvm/Support/CFG.h"
Chris Lattner986618e2004-02-22 19:47:26 +000033#include "Support/Statistic.h"
Chris Lattner44827152003-12-28 09:47:19 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner986618e2004-02-22 19:47:26 +000036namespace {
37 Statistic<>
38 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
39}
Chris Lattnercf93cdd2004-01-30 22:13:44 +000040
Chris Lattner333b2fa2002-12-13 10:09:43 +000041/// BMI - A special BuildMI variant that takes an iterator to insert the
Chris Lattner8bdd1292003-04-25 21:58:54 +000042/// instruction at as well as a basic block. This is the version for when you
43/// have a destination register in mind.
Brian Gaeke71794c02002-12-13 11:22:48 +000044inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000045 MachineBasicBlock::iterator I,
Chris Lattner8cc72d22003-06-03 15:41:58 +000046 int Opcode, unsigned NumOperands,
Chris Lattner333b2fa2002-12-13 10:09:43 +000047 unsigned DestReg) {
48 MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000049 MBB->insert(I, MI);
Alkis Evlogimenos890f9232004-02-22 19:23:26 +000050 return MachineInstrBuilder(MI).addReg(DestReg, MachineOperand::Def);
Chris Lattner333b2fa2002-12-13 10:09:43 +000051}
52
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000053/// BMI - A special BuildMI variant that takes an iterator to insert the
54/// instruction at as well as a basic block.
Brian Gaeke71794c02002-12-13 11:22:48 +000055inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000056 MachineBasicBlock::iterator I,
Chris Lattner8cc72d22003-06-03 15:41:58 +000057 int Opcode, unsigned NumOperands) {
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000058 MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000059 MBB->insert(I, MI);
Chris Lattnerf08ad9f2002-12-13 10:50:40 +000060 return MachineInstrBuilder(MI);
61}
62
Chris Lattner333b2fa2002-12-13 10:09:43 +000063
Chris Lattner72614082002-10-25 22:55:53 +000064namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000065 struct ISel : public FunctionPass, InstVisitor<ISel> {
66 TargetMachine &TM;
Chris Lattnereca195e2003-05-08 19:44:13 +000067 MachineFunction *F; // The function we are compiling into
68 MachineBasicBlock *BB; // The current MBB we are compiling
69 int VarArgsFrameIndex; // FrameIndex for start of varargs area
Chris Lattner0e5b79c2004-02-15 01:04:03 +000070 int ReturnAddressIndex; // FrameIndex for the return address
Chris Lattner72614082002-10-25 22:55:53 +000071
Chris Lattner72614082002-10-25 22:55:53 +000072 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
73
Chris Lattner333b2fa2002-12-13 10:09:43 +000074 // MBBMap - Mapping between LLVM BB -> Machine BB
75 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
76
Chris Lattnerf70e0c22003-12-28 21:23:38 +000077 ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
Chris Lattner72614082002-10-25 22:55:53 +000078
79 /// runOnFunction - Top level implementation of instruction selection for
80 /// the entire function.
81 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000082 bool runOnFunction(Function &Fn) {
Chris Lattner44827152003-12-28 09:47:19 +000083 // First pass over the function, lower any unknown intrinsic functions
84 // with the IntrinsicLowering class.
85 LowerUnknownIntrinsicFunctionCalls(Fn);
86
Chris Lattner36b36032002-10-29 23:40:58 +000087 F = &MachineFunction::construct(&Fn, TM);
Chris Lattner333b2fa2002-12-13 10:09:43 +000088
Chris Lattner065faeb2002-12-28 20:24:02 +000089 // Create all of the machine basic blocks for the function...
Chris Lattner333b2fa2002-12-13 10:09:43 +000090 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
91 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
92
Chris Lattner14aa7fe2002-12-16 22:54:46 +000093 BB = &F->front();
Chris Lattnerdbd73722003-05-06 21:32:22 +000094
Chris Lattner0e5b79c2004-02-15 01:04:03 +000095 // Set up a frame object for the return address. This is used by the
96 // llvm.returnaddress & llvm.frameaddress intrinisics.
97 ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
98
Chris Lattnerdbd73722003-05-06 21:32:22 +000099 // Copy incoming arguments off of the stack...
Chris Lattner065faeb2002-12-28 20:24:02 +0000100 LoadArgumentsToVirtualRegs(Fn);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000101
Chris Lattner333b2fa2002-12-13 10:09:43 +0000102 // Instruction select everything except PHI nodes
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000103 visit(Fn);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000104
105 // Select the PHI nodes
106 SelectPHINodes();
107
Chris Lattner986618e2004-02-22 19:47:26 +0000108 // Insert the FP_REG_KILL instructions into blocks that need them.
109 InsertFPRegKills();
110
Chris Lattner72614082002-10-25 22:55:53 +0000111 RegMap.clear();
Chris Lattner333b2fa2002-12-13 10:09:43 +0000112 MBBMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000113 F = 0;
Chris Lattner2a865b02003-07-26 23:05:37 +0000114 // We always build a machine code representation for the function
115 return true;
Chris Lattner72614082002-10-25 22:55:53 +0000116 }
117
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000118 virtual const char *getPassName() const {
119 return "X86 Simple Instruction Selection";
120 }
121
Chris Lattner72614082002-10-25 22:55:53 +0000122 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +0000123 /// block. This simply creates a new MachineBasicBlock to emit code into
124 /// and adds it to the current MachineFunction. Subsequent visit* for
125 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +0000126 ///
127 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner333b2fa2002-12-13 10:09:43 +0000128 BB = MBBMap[&LLVM_BB];
Chris Lattner72614082002-10-25 22:55:53 +0000129 }
130
Chris Lattner44827152003-12-28 09:47:19 +0000131 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
132 /// function, lowering any calls to unknown intrinsic functions into the
133 /// equivalent LLVM code.
134 void LowerUnknownIntrinsicFunctionCalls(Function &F);
135
Chris Lattner065faeb2002-12-28 20:24:02 +0000136 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
137 /// from the stack into virtual registers.
138 ///
139 void LoadArgumentsToVirtualRegs(Function &F);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000140
141 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
142 /// because we have to generate our sources into the source basic blocks,
143 /// not the current one.
144 ///
145 void SelectPHINodes();
146
Chris Lattner986618e2004-02-22 19:47:26 +0000147 /// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks
148 /// that need them. This only occurs due to the floating point stackifier
149 /// not being aggressive enough to handle arbitrary global stackification.
150 ///
151 void InsertFPRegKills();
152
Chris Lattner72614082002-10-25 22:55:53 +0000153 // Visitation methods for various instructions. These methods simply emit
154 // fixed X86 code for each instruction.
155 //
Brian Gaekefa8d5712002-11-22 11:07:01 +0000156
157 // Control flow operators
Chris Lattner72614082002-10-25 22:55:53 +0000158 void visitReturnInst(ReturnInst &RI);
Chris Lattner2df035b2002-11-02 19:27:56 +0000159 void visitBranchInst(BranchInst &BI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000160
161 struct ValueRecord {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000162 Value *Val;
Chris Lattner3e130a22003-01-13 00:32:26 +0000163 unsigned Reg;
164 const Type *Ty;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000165 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
166 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
Chris Lattner3e130a22003-01-13 00:32:26 +0000167 };
168 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000169 const std::vector<ValueRecord> &Args);
Brian Gaekefa8d5712002-11-22 11:07:01 +0000170 void visitCallInst(CallInst &I);
Brian Gaeked0fde302003-11-11 22:41:34 +0000171 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
Chris Lattnere2954c82002-11-02 20:04:26 +0000172
173 // Arithmetic operators
Chris Lattnerf01729e2002-11-02 20:54:46 +0000174 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
Chris Lattner68aad932002-11-02 20:13:22 +0000175 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
176 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000177 void doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +0000178 unsigned DestReg, const Type *DestTy,
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000179 unsigned Op0Reg, unsigned Op1Reg);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000180 void doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000181 MachineBasicBlock::iterator MBBI,
Chris Lattnerb2acc512003-10-19 21:09:10 +0000182 unsigned DestReg, const Type *DestTy,
183 unsigned Op0Reg, unsigned Op1Val);
Chris Lattnerca9671d2002-11-02 20:28:58 +0000184 void visitMul(BinaryOperator &B);
Chris Lattnere2954c82002-11-02 20:04:26 +0000185
Chris Lattnerf01729e2002-11-02 20:54:46 +0000186 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
187 void visitRem(BinaryOperator &B) { visitDivRem(B); }
188 void visitDivRem(BinaryOperator &B);
189
Chris Lattnere2954c82002-11-02 20:04:26 +0000190 // Bitwise operators
Chris Lattner68aad932002-11-02 20:13:22 +0000191 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
192 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
193 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
Chris Lattnere2954c82002-11-02 20:04:26 +0000194
Chris Lattner6d40c192003-01-16 16:43:00 +0000195 // Comparison operators...
196 void visitSetCondInst(SetCondInst &I);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000197 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
198 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000199 MachineBasicBlock::iterator MBBI);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000200
Chris Lattner6fc3c522002-11-17 21:11:55 +0000201 // Memory Instructions
202 void visitLoadInst(LoadInst &I);
203 void visitStoreInst(StoreInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000204 void visitGetElementPtrInst(GetElementPtrInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000205 void visitAllocaInst(AllocaInst &I);
Chris Lattner3e130a22003-01-13 00:32:26 +0000206 void visitMallocInst(MallocInst &I);
207 void visitFreeInst(FreeInst &I);
Brian Gaeke20244b72002-12-12 15:33:40 +0000208
Chris Lattnere2954c82002-11-02 20:04:26 +0000209 // Other operators
Brian Gaekea1719c92002-10-31 23:03:59 +0000210 void visitShiftInst(ShiftInst &I);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000211 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
Brian Gaekefa8d5712002-11-22 11:07:01 +0000212 void visitCastInst(CastInst &I);
Chris Lattner73815062003-10-18 05:56:40 +0000213 void visitVANextInst(VANextInst &I);
214 void visitVAArgInst(VAArgInst &I);
Chris Lattner72614082002-10-25 22:55:53 +0000215
216 void visitInstruction(Instruction &I) {
217 std::cerr << "Cannot instruction select: " << I;
218 abort();
219 }
220
Brian Gaeke95780cc2002-12-13 07:56:18 +0000221 /// promote32 - Make a value 32-bits wide, and put it somewhere.
Chris Lattner3e130a22003-01-13 00:32:26 +0000222 ///
223 void promote32(unsigned targetReg, const ValueRecord &VR);
224
Chris Lattnerb6bac512004-02-25 06:13:04 +0000225 // getGEPIndex - This is used to fold GEP instructions into X86 addressing
226 // expressions.
227 void getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
228 std::vector<Value*> &GEPOps,
229 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
230 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
231
232 /// isGEPFoldable - Return true if the specified GEP can be completely
233 /// folded into the addressing mode of a load/store or lea instruction.
234 bool isGEPFoldable(MachineBasicBlock *MBB,
235 Value *Src, User::op_iterator IdxBegin,
236 User::op_iterator IdxEnd, unsigned &BaseReg,
237 unsigned &Scale, unsigned &IndexReg, unsigned &Disp);
238
Chris Lattner3e130a22003-01-13 00:32:26 +0000239 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
240 /// constant expression GEP support.
241 ///
Chris Lattner827832c2004-02-22 17:05:38 +0000242 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
Chris Lattner333b2fa2002-12-13 10:09:43 +0000243 Value *Src, User::op_iterator IdxBegin,
Chris Lattnerc0812d82002-12-13 06:56:29 +0000244 User::op_iterator IdxEnd, unsigned TargetReg);
245
Chris Lattner548f61d2003-04-23 17:22:12 +0000246 /// emitCastOperation - Common code shared between visitCastInst and
247 /// constant expression cast support.
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000248 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +0000249 Value *Src, const Type *DestTy, unsigned TargetReg);
250
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000251 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
252 /// and constant expression support.
253 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000254 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000255 Value *Op0, Value *Op1,
256 unsigned OperatorClass, unsigned TargetReg);
257
Chris Lattnercadff442003-10-23 17:21:43 +0000258 void emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000259 MachineBasicBlock::iterator IP,
Chris Lattnercadff442003-10-23 17:21:43 +0000260 unsigned Op0Reg, unsigned Op1Reg, bool isDiv,
261 const Type *Ty, unsigned TargetReg);
262
Chris Lattner58c41fe2003-08-24 19:19:47 +0000263 /// emitSetCCOperation - Common code shared between visitSetCondInst and
264 /// constant expression support.
265 void emitSetCCOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000266 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000267 Value *Op0, Value *Op1, unsigned Opcode,
268 unsigned TargetReg);
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000269
270 /// emitShiftOperation - Common code shared between visitShiftInst and
271 /// constant expression support.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000272 void emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000273 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000274 Value *Op, Value *ShiftAmount, bool isLeftShift,
275 const Type *ResultTy, unsigned DestReg);
276
Chris Lattner58c41fe2003-08-24 19:19:47 +0000277
Chris Lattnerc5291f52002-10-27 21:16:59 +0000278 /// copyConstantToRegister - Output the instructions required to put the
279 /// specified constant into the specified register.
280 ///
Chris Lattner8a307e82002-12-16 19:32:50 +0000281 void copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000282 MachineBasicBlock::iterator MBBI,
Chris Lattner8a307e82002-12-16 19:32:50 +0000283 Constant *C, unsigned Reg);
Chris Lattnerc5291f52002-10-27 21:16:59 +0000284
Chris Lattner3e130a22003-01-13 00:32:26 +0000285 /// makeAnotherReg - This method returns the next register number we haven't
286 /// yet used.
287 ///
288 /// Long values are handled somewhat specially. They are always allocated
289 /// as pairs of 32 bit integer values. The register number returned is the
290 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
291 /// of the long value.
292 ///
Chris Lattnerc0812d82002-12-13 06:56:29 +0000293 unsigned makeAnotherReg(const Type *Ty) {
Chris Lattner7db1fa92003-07-30 05:33:48 +0000294 assert(dynamic_cast<const X86RegisterInfo*>(TM.getRegisterInfo()) &&
295 "Current target doesn't have X86 reg info??");
296 const X86RegisterInfo *MRI =
297 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
Chris Lattner3e130a22003-01-13 00:32:26 +0000298 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000299 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
300 // Create the lower part
301 F->getSSARegMap()->createVirtualRegister(RC);
302 // Create the upper part.
303 return F->getSSARegMap()->createVirtualRegister(RC)-1;
Chris Lattner3e130a22003-01-13 00:32:26 +0000304 }
305
Chris Lattnerc0812d82002-12-13 06:56:29 +0000306 // Add the mapping of regnumber => reg class to MachineFunction
Chris Lattner7db1fa92003-07-30 05:33:48 +0000307 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
Chris Lattner3e130a22003-01-13 00:32:26 +0000308 return F->getSSARegMap()->createVirtualRegister(RC);
Brian Gaeke20244b72002-12-12 15:33:40 +0000309 }
310
Chris Lattner72614082002-10-25 22:55:53 +0000311 /// getReg - This method turns an LLVM value into a register number. This
312 /// is guaranteed to produce the same register number for a particular value
313 /// every time it is queried.
314 ///
315 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
Chris Lattnerf08ad9f2002-12-13 10:50:40 +0000316 unsigned getReg(Value *V) {
317 // Just append to the end of the current bb.
318 MachineBasicBlock::iterator It = BB->end();
319 return getReg(V, BB, It);
320 }
Brian Gaeke71794c02002-12-13 11:22:48 +0000321 unsigned getReg(Value *V, MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000322 MachineBasicBlock::iterator IPt) {
Chris Lattner72614082002-10-25 22:55:53 +0000323 unsigned &Reg = RegMap[V];
Misha Brukmand2cc0172002-11-20 00:58:23 +0000324 if (Reg == 0) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000325 Reg = makeAnotherReg(V->getType());
Misha Brukmand2cc0172002-11-20 00:58:23 +0000326 RegMap[V] = Reg;
Misha Brukmand2cc0172002-11-20 00:58:23 +0000327 }
Chris Lattner72614082002-10-25 22:55:53 +0000328
Chris Lattner6f8fd252002-10-27 21:23:43 +0000329 // If this operand is a constant, emit the code to copy the constant into
330 // the register here...
331 //
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000332 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner8a307e82002-12-16 19:32:50 +0000333 copyConstantToRegister(MBB, IPt, C, Reg);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000334 RegMap.erase(V); // Assign a new name to this constant if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000335 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
336 // Move the address of the global into the register
Chris Lattner6e173a02004-02-17 06:16:44 +0000337 BMI(MBB, IPt, X86::MOVri32, 1, Reg).addGlobalAddress(GV);
Chris Lattner14aa7fe2002-12-16 22:54:46 +0000338 RegMap.erase(V); // Assign a new name to this address if ref'd again
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000339 }
Chris Lattnerc5291f52002-10-27 21:16:59 +0000340
Chris Lattner72614082002-10-25 22:55:53 +0000341 return Reg;
342 }
Chris Lattner72614082002-10-25 22:55:53 +0000343 };
344}
345
Chris Lattner43189d12002-11-17 20:07:45 +0000346/// TypeClass - Used by the X86 backend to group LLVM types by their basic X86
347/// Representation.
348///
349enum TypeClass {
Chris Lattner94af4142002-12-25 05:13:53 +0000350 cByte, cShort, cInt, cFP, cLong
Chris Lattner43189d12002-11-17 20:07:45 +0000351};
352
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000353/// getClass - Turn a primitive type into a "class" number which is based on the
354/// size of the type, and whether or not it is floating point.
355///
Chris Lattner43189d12002-11-17 20:07:45 +0000356static inline TypeClass getClass(const Type *Ty) {
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000357 switch (Ty->getPrimitiveID()) {
358 case Type::SByteTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000359 case Type::UByteTyID: return cByte; // Byte operands are class #0
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000360 case Type::ShortTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000361 case Type::UShortTyID: return cShort; // Short operands are class #1
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000362 case Type::IntTyID:
363 case Type::UIntTyID:
Chris Lattner43189d12002-11-17 20:07:45 +0000364 case Type::PointerTyID: return cInt; // Int's and pointers are class #2
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000365
Chris Lattner94af4142002-12-25 05:13:53 +0000366 case Type::FloatTyID:
367 case Type::DoubleTyID: return cFP; // Floating Point is #3
Chris Lattner3e130a22003-01-13 00:32:26 +0000368
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000369 case Type::LongTyID:
Chris Lattner3e130a22003-01-13 00:32:26 +0000370 case Type::ULongTyID: return cLong; // Longs are class #4
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000371 default:
372 assert(0 && "Invalid type to getClass!");
Chris Lattner43189d12002-11-17 20:07:45 +0000373 return cByte; // not reached
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000374 }
375}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000376
Chris Lattner6b993cc2002-12-15 08:02:15 +0000377// getClassB - Just like getClass, but treat boolean values as bytes.
378static inline TypeClass getClassB(const Type *Ty) {
379 if (Ty == Type::BoolTy) return cByte;
380 return getClass(Ty);
381}
382
Chris Lattner06925362002-11-17 21:56:38 +0000383
Chris Lattnerc5291f52002-10-27 21:16:59 +0000384/// copyConstantToRegister - Output the instructions required to put the
385/// specified constant into the specified register.
386///
Chris Lattner8a307e82002-12-16 19:32:50 +0000387void ISel::copyConstantToRegister(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000388 MachineBasicBlock::iterator IP,
Chris Lattner8a307e82002-12-16 19:32:50 +0000389 Constant *C, unsigned R) {
Chris Lattnerc0812d82002-12-13 06:56:29 +0000390 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000391 unsigned Class = 0;
392 switch (CE->getOpcode()) {
393 case Instruction::GetElementPtr:
Brian Gaeke68b1edc2002-12-16 04:23:29 +0000394 emitGEPOperation(MBB, IP, CE->getOperand(0),
Chris Lattner333b2fa2002-12-13 10:09:43 +0000395 CE->op_begin()+1, CE->op_end(), R);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000396 return;
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000397 case Instruction::Cast:
Chris Lattner548f61d2003-04-23 17:22:12 +0000398 emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R);
Chris Lattner4b12cde2003-04-21 21:33:44 +0000399 return;
Chris Lattnerc0812d82002-12-13 06:56:29 +0000400
Chris Lattnerb515f6d2003-05-08 20:49:25 +0000401 case Instruction::Xor: ++Class; // FALL THROUGH
402 case Instruction::Or: ++Class; // FALL THROUGH
403 case Instruction::And: ++Class; // FALL THROUGH
404 case Instruction::Sub: ++Class; // FALL THROUGH
405 case Instruction::Add:
406 emitSimpleBinaryOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
407 Class, R);
408 return;
409
Chris Lattnercadff442003-10-23 17:21:43 +0000410 case Instruction::Mul: {
411 unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP);
412 unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP);
413 doMultiply(MBB, IP, R, CE->getType(), Op0Reg, Op1Reg);
414 return;
415 }
416 case Instruction::Div:
417 case Instruction::Rem: {
418 unsigned Op0Reg = getReg(CE->getOperand(0), MBB, IP);
419 unsigned Op1Reg = getReg(CE->getOperand(1), MBB, IP);
420 emitDivRemOperation(MBB, IP, Op0Reg, Op1Reg,
421 CE->getOpcode() == Instruction::Div,
422 CE->getType(), R);
423 return;
424 }
425
Chris Lattner58c41fe2003-08-24 19:19:47 +0000426 case Instruction::SetNE:
427 case Instruction::SetEQ:
428 case Instruction::SetLT:
429 case Instruction::SetGT:
430 case Instruction::SetLE:
431 case Instruction::SetGE:
432 emitSetCCOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
433 CE->getOpcode(), R);
434 return;
435
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000436 case Instruction::Shl:
437 case Instruction::Shr:
438 emitShiftOperation(MBB, IP, CE->getOperand(0), CE->getOperand(1),
Brian Gaekedfcc9cf2003-11-22 06:49:41 +0000439 CE->getOpcode() == Instruction::Shl, CE->getType(), R);
440 return;
Brian Gaeke2dd3e1b2003-11-22 05:18:35 +0000441
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();
Chris Lattner6e173a02004-02-17 06:16:44 +0000454 BMI(MBB, IP, X86::MOVri32, 1, R).addZImm(Val & 0xFFFFFFFF);
455 BMI(MBB, IP, X86::MOVri32, 1, R+1).addZImm(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[] = {
Chris Lattner6e173a02004-02-17 06:16:44 +0000462 X86::MOVri8, X86::MOVri16, X86::MOVri32
Chris Lattnerb1761fc2002-11-02 01:15:18 +0000463 };
464
Chris Lattner6b993cc2002-12-15 08:02:15 +0000465 if (C->getType() == Type::BoolTy) {
Chris Lattner6e173a02004-02-17 06:16:44 +0000466 BMI(MBB, IP, X86::MOVri8, 1, R).addZImm(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);
469 BMI(MBB, IP, IntegralOpcodeTab[Class], 1, R).addZImm(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 Lattner94af4142002-12-25 05:13:53 +0000473 BMI(MBB, IP, X86::FLD0, 0, R);
Chris Lattneraf703622004-02-02 18:56:30 +0000474 else if (CFP->isExactlyValue(+1.0))
Chris Lattner94af4142002-12-25 05:13:53 +0000475 BMI(MBB, IP, X86::FLD1, 0, R);
476 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!");
483 unsigned LoadOpcode = Ty == Type::FloatTy ? X86::FLDr32 : X86::FLDr64;
484 addConstantPoolReference(BMI(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.
Chris Lattner6e173a02004-02-17 06:16:44 +0000489 BMI(MBB, IP, X86::MOVri32, 1, R).addZImm(0);
Chris Lattnerc0812d82002-12-13 06:56:29 +0000490 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Chris Lattner7ca04092004-02-22 17:35:42 +0000491 BMI(MBB, IP, X86::MOVri32, 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);
Chris Lattnere87331d2004-02-17 06:28:19 +0000520 addFrameReference(BuildMI(BB, X86::MOVrm8, 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);
Chris Lattnere87331d2004-02-17 06:28:19 +0000524 addFrameReference(BuildMI(BB, X86::MOVrm16, 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);
Chris Lattnere87331d2004-02-17 06:28:19 +0000528 addFrameReference(BuildMI(BB, X86::MOVrm32, 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);
Chris Lattnere87331d2004-02-17 06:28:19 +0000532 addFrameReference(BuildMI(BB, X86::MOVrm32, 4, Reg), FI);
533 addFrameReference(BuildMI(BB, X86::MOVrm32, 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) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000539 Opcode = X86::FLDr32;
540 FI = MFI->CreateFixedObject(4, ArgOffset);
Chris Lattner065faeb2002-12-28 20:24:02 +0000541 } else {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000542 Opcode = X86::FLDr64;
543 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;
571 MachineBasicBlock *MBB = MBBMap[I];
572
573 // Loop over all of the PHI nodes in the LLVM basic block...
Chris Lattner986618e2004-02-22 19:47:26 +0000574 MachineBasicBlock::iterator instr = 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);
580 MachineInstr *PhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000581 MBB->insert(instr, PhiMI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000582
583 MachineInstr *LongPhiMI = 0;
584 if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000585 LongPhiMI = BuildMI(X86::PHI, PN->getNumOperands(), PHIReg+1);
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000586 MBB->insert(instr, LongPhiMI);
Chris Lattner3e130a22003-01-13 00:32:26 +0000587 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000588
Chris Lattnera6e73f12003-05-12 14:22:21 +0000589 // PHIValues - Map of blocks to incoming virtual registers. We use this
590 // so that we only initialize one incoming value for a particular block,
591 // even if the block has multiple entries in the PHI node.
592 //
593 std::map<MachineBasicBlock*, unsigned> PHIValues;
594
Chris Lattner333b2fa2002-12-13 10:09:43 +0000595 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
596 MachineBasicBlock *PredMBB = MBBMap[PN->getIncomingBlock(i)];
Chris Lattnera6e73f12003-05-12 14:22:21 +0000597 unsigned ValReg;
598 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
599 PHIValues.lower_bound(PredMBB);
Chris Lattner333b2fa2002-12-13 10:09:43 +0000600
Chris Lattnera6e73f12003-05-12 14:22:21 +0000601 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
602 // We already inserted an initialization of the register for this
603 // predecessor. Recycle it.
604 ValReg = EntryIt->second;
605
606 } else {
Chris Lattnera81fc682003-10-19 00:26:11 +0000607 // Get the incoming value into a virtual register.
Chris Lattnera6e73f12003-05-12 14:22:21 +0000608 //
Chris Lattnera81fc682003-10-19 00:26:11 +0000609 Value *Val = PN->getIncomingValue(i);
610
611 // If this is a constant or GlobalValue, we may have to insert code
612 // into the basic block to compute it into a virtual register.
613 if (isa<Constant>(Val) || isa<GlobalValue>(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!
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000621 while (PI != PredMBB->end() && PI->getOpcode() == X86::PHI)
Chris Lattnera81fc682003-10-19 00:26:11 +0000622 ++PI;
623
624 ValReg = getReg(Val, PredMBB, PI);
625 } else {
626 ValReg = getReg(Val);
627 }
Chris Lattnera6e73f12003-05-12 14:22:21 +0000628
629 // Remember that we inserted a value for this PHI for this predecessor
630 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
631 }
632
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000633 PhiMI->addRegOperand(ValReg);
Chris Lattner3e130a22003-01-13 00:32:26 +0000634 PhiMI->addMachineBasicBlockOperand(PredMBB);
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000635 if (LongPhiMI) {
636 LongPhiMI->addRegOperand(ValReg+1);
637 LongPhiMI->addMachineBasicBlockOperand(PredMBB);
638 }
Chris Lattner333b2fa2002-12-13 10:09:43 +0000639 }
640 }
641 }
642}
643
Chris Lattner986618e2004-02-22 19:47:26 +0000644/// RequiresFPRegKill - The floating point stackifier pass cannot insert
645/// compensation code on critical edges. As such, it requires that we kill all
646/// FP registers on the exit from any blocks that either ARE critical edges, or
647/// branch to a block that has incoming critical edges.
648///
649/// Note that this kill instruction will eventually be eliminated when
650/// restrictions in the stackifier are relaxed.
651///
652static bool RequiresFPRegKill(const BasicBlock *BB) {
653#if 0
654 for (succ_const_iterator SI = succ_begin(BB), E = succ_end(BB); SI!=E; ++SI) {
655 const BasicBlock *Succ = *SI;
656 pred_const_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
657 ++PI; // Block have at least one predecessory
658 if (PI != PE) { // If it has exactly one, this isn't crit edge
659 // If this block has more than one predecessor, check all of the
660 // predecessors to see if they have multiple successors. If so, then the
661 // block we are analyzing needs an FPRegKill.
662 for (PI = pred_begin(Succ); PI != PE; ++PI) {
663 const BasicBlock *Pred = *PI;
664 succ_const_iterator SI2 = succ_begin(Pred);
665 ++SI2; // There must be at least one successor of this block.
666 if (SI2 != succ_end(Pred))
667 return true; // Yes, we must insert the kill on this edge.
668 }
669 }
670 }
671 // If we got this far, there is no need to insert the kill instruction.
672 return false;
673#else
674 return true;
675#endif
676}
677
678// InsertFPRegKills - Insert FP_REG_KILL instructions into basic blocks that
679// need them. This only occurs due to the floating point stackifier not being
680// aggressive enough to handle arbitrary global stackification.
681//
682// Currently we insert an FP_REG_KILL instruction into each block that uses or
683// defines a floating point virtual register.
684//
685// When the global register allocators (like linear scan) finally update live
686// variable analysis, we can keep floating point values in registers across
687// portions of the CFG that do not involve critical edges. This will be a big
688// win, but we are waiting on the global allocators before we can do this.
689//
690// With a bit of work, the floating point stackifier pass can be enhanced to
691// break critical edges as needed (to make a place to put compensation code),
692// but this will require some infrastructure improvements as well.
693//
694void ISel::InsertFPRegKills() {
695 SSARegMap &RegMap = *F->getSSARegMap();
Chris Lattner986618e2004-02-22 19:47:26 +0000696
697 for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Chris Lattner986618e2004-02-22 19:47:26 +0000698 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
699 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
700 if (I->getOperand(i).isRegister()) {
701 unsigned Reg = I->getOperand(i).getReg();
702 if (MRegisterInfo::isVirtualRegister(Reg))
Chris Lattner65cf42d2004-02-23 07:29:45 +0000703 if (RegMap.getRegClass(Reg)->getSize() == 10)
704 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000705 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000706
707 // If we haven't found an FP register use or def in this basic block, check
708 // to see if any of our successors has an FP PHI node, which will cause a
709 // copy to be inserted into this block.
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000710 for (succ_const_iterator SI = succ_begin(BB->getBasicBlock()),
711 E = succ_end(BB->getBasicBlock()); SI != E; ++SI) {
712 MachineBasicBlock *SBB = MBBMap[*SI];
713 for (MachineBasicBlock::iterator I = SBB->begin();
714 I != SBB->end() && I->getOpcode() == X86::PHI; ++I) {
715 if (RegMap.getRegClass(I->getOperand(0).getReg())->getSize() == 10)
716 goto UsesFPReg;
Chris Lattner986618e2004-02-22 19:47:26 +0000717 }
Chris Lattnerfbc39d52004-02-23 07:42:19 +0000718 }
Chris Lattner65cf42d2004-02-23 07:29:45 +0000719 continue;
720 UsesFPReg:
721 // Okay, this block uses an FP register. If the block has successors (ie,
722 // it's not an unwind/return), insert the FP_REG_KILL instruction.
723 if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
724 RequiresFPRegKill(BB->getBasicBlock())) {
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000725 BMI(BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner65cf42d2004-02-23 07:29:45 +0000726 ++NumFPKill;
Chris Lattner986618e2004-02-22 19:47:26 +0000727 }
728 }
729}
730
731
Chris Lattner6d40c192003-01-16 16:43:00 +0000732// canFoldSetCCIntoBranch - Return the setcc instruction if we can fold it into
733// the conditional branch instruction which is the only user of the cc
734// instruction. This is the case if the conditional branch is the only user of
735// the setcc, and if the setcc is in the same basic block as the conditional
736// branch. We also don't handle long arguments below, so we reject them here as
737// well.
738//
739static SetCondInst *canFoldSetCCIntoBranch(Value *V) {
740 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
Chris Lattnerfd059242003-10-15 16:48:29 +0000741 if (SCI->hasOneUse() && isa<BranchInst>(SCI->use_back()) &&
Chris Lattner6d40c192003-01-16 16:43:00 +0000742 SCI->getParent() == cast<BranchInst>(SCI->use_back())->getParent()) {
743 const Type *Ty = SCI->getOperand(0)->getType();
744 if (Ty != Type::LongTy && Ty != Type::ULongTy)
745 return SCI;
746 }
747 return 0;
748}
Chris Lattner333b2fa2002-12-13 10:09:43 +0000749
Chris Lattner6d40c192003-01-16 16:43:00 +0000750// Return a fixed numbering for setcc instructions which does not depend on the
751// order of the opcodes.
752//
753static unsigned getSetCCNumber(unsigned Opcode) {
754 switch(Opcode) {
755 default: assert(0 && "Unknown setcc instruction!");
756 case Instruction::SetEQ: return 0;
757 case Instruction::SetNE: return 1;
758 case Instruction::SetLT: return 2;
Chris Lattner55f6fab2003-01-16 18:07:23 +0000759 case Instruction::SetGE: return 3;
760 case Instruction::SetGT: return 4;
761 case Instruction::SetLE: return 5;
Chris Lattner6d40c192003-01-16 16:43:00 +0000762 }
763}
Chris Lattner06925362002-11-17 21:56:38 +0000764
Chris Lattner6d40c192003-01-16 16:43:00 +0000765// LLVM -> X86 signed X86 unsigned
766// ----- ---------- ------------
767// seteq -> sete sete
768// setne -> setne setne
769// setlt -> setl setb
Chris Lattner55f6fab2003-01-16 18:07:23 +0000770// setge -> setge setae
Chris Lattner6d40c192003-01-16 16:43:00 +0000771// setgt -> setg seta
772// setle -> setle setbe
Chris Lattnerb2acc512003-10-19 21:09:10 +0000773// ----
774// sets // Used by comparison with 0 optimization
775// setns
776static const unsigned SetCCOpcodeTab[2][8] = {
777 { X86::SETEr, X86::SETNEr, X86::SETBr, X86::SETAEr, X86::SETAr, X86::SETBEr,
778 0, 0 },
779 { X86::SETEr, X86::SETNEr, X86::SETLr, X86::SETGEr, X86::SETGr, X86::SETLEr,
780 X86::SETSr, X86::SETNSr },
Chris Lattner6d40c192003-01-16 16:43:00 +0000781};
782
Chris Lattnerb2acc512003-10-19 21:09:10 +0000783// EmitComparison - This function emits a comparison of the two operands,
784// returning the extended setcc code to use.
785unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
786 MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000787 MachineBasicBlock::iterator IP) {
Brian Gaeke1749d632002-11-07 17:59:21 +0000788 // The arguments are already supposed to be of the same type.
Chris Lattner6d40c192003-01-16 16:43:00 +0000789 const Type *CompTy = Op0->getType();
Chris Lattner3e130a22003-01-13 00:32:26 +0000790 unsigned Class = getClassB(CompTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000791 unsigned Op0r = getReg(Op0, MBB, IP);
Chris Lattner333864d2003-06-05 19:30:30 +0000792
793 // Special case handling of: cmp R, i
794 if (Class == cByte || Class == cShort || Class == cInt)
795 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000796 uint64_t Op1v = cast<ConstantInt>(CI)->getRawValue();
797
Chris Lattner333864d2003-06-05 19:30:30 +0000798 // Mask off any upper bits of the constant, if there are any...
799 Op1v &= (1ULL << (8 << Class)) - 1;
800
Chris Lattnerb2acc512003-10-19 21:09:10 +0000801 // If this is a comparison against zero, emit more efficient code. We
802 // can't handle unsigned comparisons against zero unless they are == or
803 // !=. These should have been strength reduced already anyway.
804 if (Op1v == 0 && (CompTy->isSigned() || OpNum < 2)) {
805 static const unsigned TESTTab[] = {
806 X86::TESTrr8, X86::TESTrr16, X86::TESTrr32
807 };
808 BMI(MBB, IP, TESTTab[Class], 2).addReg(Op0r).addReg(Op0r);
809
810 if (OpNum == 2) return 6; // Map jl -> js
811 if (OpNum == 3) return 7; // Map jg -> jns
812 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000813 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000814
815 static const unsigned CMPTab[] = {
816 X86::CMPri8, X86::CMPri16, X86::CMPri32
817 };
818
819 BMI(MBB, IP, CMPTab[Class], 2).addReg(Op0r).addZImm(Op1v);
820 return OpNum;
Chris Lattner333864d2003-06-05 19:30:30 +0000821 }
822
Chris Lattner9f08a922004-02-03 18:54:04 +0000823 // Special case handling of comparison against +/- 0.0
824 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op1))
825 if (CFP->isExactlyValue(+0.0) || CFP->isExactlyValue(-0.0)) {
826 BMI(MBB, IP, X86::FTST, 1).addReg(Op0r);
827 BMI(MBB, IP, X86::FNSTSWr8, 0);
828 BMI(MBB, IP, X86::SAHF, 1);
829 return OpNum;
830 }
831
Chris Lattner58c41fe2003-08-24 19:19:47 +0000832 unsigned Op1r = getReg(Op1, MBB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +0000833 switch (Class) {
834 default: assert(0 && "Unknown type class!");
835 // Emit: cmp <var1>, <var2> (do the comparison). We can
836 // compare 8-bit with 8-bit, 16-bit with 16-bit, 32-bit with
837 // 32-bit.
838 case cByte:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000839 BMI(MBB, IP, X86::CMPrr8, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000840 break;
841 case cShort:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000842 BMI(MBB, IP, X86::CMPrr16, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000843 break;
844 case cInt:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000845 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r);
Chris Lattner3e130a22003-01-13 00:32:26 +0000846 break;
847 case cFP:
Chris Lattner58c41fe2003-08-24 19:19:47 +0000848 BMI(MBB, IP, X86::FpUCOM, 2).addReg(Op0r).addReg(Op1r);
849 BMI(MBB, IP, X86::FNSTSWr8, 0);
850 BMI(MBB, IP, X86::SAHF, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +0000851 break;
852
853 case cLong:
854 if (OpNum < 2) { // seteq, setne
855 unsigned LoTmp = makeAnotherReg(Type::IntTy);
856 unsigned HiTmp = makeAnotherReg(Type::IntTy);
857 unsigned FinalTmp = makeAnotherReg(Type::IntTy);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000858 BMI(MBB, IP, X86::XORrr32, 2, LoTmp).addReg(Op0r).addReg(Op1r);
859 BMI(MBB, IP, X86::XORrr32, 2, HiTmp).addReg(Op0r+1).addReg(Op1r+1);
860 BMI(MBB, IP, X86::ORrr32, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp);
Chris Lattner3e130a22003-01-13 00:32:26 +0000861 break; // Allow the sete or setne to be generated from flags set by OR
862 } else {
863 // Emit a sequence of code which compares the high and low parts once
864 // each, then uses a conditional move to handle the overflow case. For
865 // example, a setlt for long would generate code like this:
866 //
867 // AL = lo(op1) < lo(op2) // Signedness depends on operands
868 // BL = hi(op1) < hi(op2) // Always unsigned comparison
869 // dest = hi(op1) == hi(op2) ? AL : BL;
870 //
871
Chris Lattner6d40c192003-01-16 16:43:00 +0000872 // FIXME: This would be much better if we had hierarchical register
Chris Lattner3e130a22003-01-13 00:32:26 +0000873 // classes! Until then, hardcode registers so that we can deal with their
874 // aliases (because we don't have conditional byte moves).
875 //
Chris Lattner58c41fe2003-08-24 19:19:47 +0000876 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r).addReg(Op1r);
877 BMI(MBB, IP, SetCCOpcodeTab[0][OpNum], 0, X86::AL);
878 BMI(MBB, IP, X86::CMPrr32, 2).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000879 BMI(MBB, IP, SetCCOpcodeTab[CompTy->isSigned()][OpNum], 0, X86::BL);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000880 BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::BH);
881 BMI(MBB, IP, X86::IMPLICIT_DEF, 0, X86::AH);
882 BMI(MBB, IP, X86::CMOVErr16, 2, X86::BX).addReg(X86::BX).addReg(X86::AX);
Chris Lattner6d40c192003-01-16 16:43:00 +0000883 // NOTE: visitSetCondInst knows that the value is dumped into the BL
884 // register at this point for long values...
Chris Lattnerb2acc512003-10-19 21:09:10 +0000885 return OpNum;
Chris Lattner3e130a22003-01-13 00:32:26 +0000886 }
887 }
Chris Lattnerb2acc512003-10-19 21:09:10 +0000888 return OpNum;
Chris Lattner6d40c192003-01-16 16:43:00 +0000889}
Chris Lattner3e130a22003-01-13 00:32:26 +0000890
Chris Lattner6d40c192003-01-16 16:43:00 +0000891
892/// SetCC instructions - Here we just emit boilerplate code to set a byte-sized
893/// register, then move it to wherever the result should be.
894///
895void ISel::visitSetCondInst(SetCondInst &I) {
896 if (canFoldSetCCIntoBranch(&I)) return; // Fold this into a branch...
897
Chris Lattner6d40c192003-01-16 16:43:00 +0000898 unsigned DestReg = getReg(I);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000899 MachineBasicBlock::iterator MII = BB->end();
900 emitSetCCOperation(BB, MII, I.getOperand(0), I.getOperand(1), I.getOpcode(),
901 DestReg);
902}
Chris Lattner6d40c192003-01-16 16:43:00 +0000903
Chris Lattner58c41fe2003-08-24 19:19:47 +0000904/// emitSetCCOperation - Common code shared between visitSetCondInst and
905/// constant expression support.
906void ISel::emitSetCCOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +0000907 MachineBasicBlock::iterator IP,
Chris Lattner58c41fe2003-08-24 19:19:47 +0000908 Value *Op0, Value *Op1, unsigned Opcode,
909 unsigned TargetReg) {
910 unsigned OpNum = getSetCCNumber(Opcode);
Chris Lattnerb2acc512003-10-19 21:09:10 +0000911 OpNum = EmitComparison(OpNum, Op0, Op1, MBB, IP);
Chris Lattner58c41fe2003-08-24 19:19:47 +0000912
Chris Lattnerb2acc512003-10-19 21:09:10 +0000913 const Type *CompTy = Op0->getType();
914 unsigned CompClass = getClassB(CompTy);
915 bool isSigned = CompTy->isSigned() && CompClass != cFP;
916
917 if (CompClass != cLong || OpNum < 2) {
Chris Lattner6d40c192003-01-16 16:43:00 +0000918 // Handle normal comparisons with a setcc instruction...
Chris Lattner58c41fe2003-08-24 19:19:47 +0000919 BMI(MBB, IP, SetCCOpcodeTab[isSigned][OpNum], 0, TargetReg);
Chris Lattner6d40c192003-01-16 16:43:00 +0000920 } else {
921 // Handle long comparisons by copying the value which is already in BL into
922 // the register we want...
Chris Lattner58c41fe2003-08-24 19:19:47 +0000923 BMI(MBB, IP, X86::MOVrr8, 1, TargetReg).addReg(X86::BL);
Chris Lattner6d40c192003-01-16 16:43:00 +0000924 }
Brian Gaeke1749d632002-11-07 17:59:21 +0000925}
Chris Lattner51b49a92002-11-02 19:45:49 +0000926
Chris Lattner58c41fe2003-08-24 19:19:47 +0000927
928
929
Brian Gaekec2505982002-11-30 11:57:28 +0000930/// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
931/// operand, in the specified target register.
Chris Lattner3e130a22003-01-13 00:32:26 +0000932void ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
933 bool isUnsigned = VR.Ty->isUnsigned();
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000934
935 // Make sure we have the register number for this value...
936 unsigned Reg = VR.Val ? getReg(VR.Val) : VR.Reg;
937
Chris Lattner3e130a22003-01-13 00:32:26 +0000938 switch (getClassB(VR.Ty)) {
Chris Lattner94af4142002-12-25 05:13:53 +0000939 case cByte:
940 // Extend value into target register (8->32)
941 if (isUnsigned)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000942 BuildMI(BB, X86::MOVZXr32r8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000943 else
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000944 BuildMI(BB, X86::MOVSXr32r8, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000945 break;
946 case cShort:
947 // Extend value into target register (16->32)
948 if (isUnsigned)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000949 BuildMI(BB, X86::MOVZXr32r16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000950 else
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000951 BuildMI(BB, X86::MOVSXr32r16, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000952 break;
953 case cInt:
954 // Move value into target register (32->32)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +0000955 BuildMI(BB, X86::MOVrr32, 1, targetReg).addReg(Reg);
Chris Lattner94af4142002-12-25 05:13:53 +0000956 break;
957 default:
958 assert(0 && "Unpromotable operand class in promote32");
959 }
Brian Gaekec2505982002-11-30 11:57:28 +0000960}
Chris Lattnerc5291f52002-10-27 21:16:59 +0000961
Chris Lattner72614082002-10-25 22:55:53 +0000962/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
963/// we have the following possibilities:
964///
965/// ret void: No return value, simply emit a 'ret' instruction
966/// ret sbyte, ubyte : Extend value into EAX and return
967/// ret short, ushort: Extend value into EAX and return
968/// ret int, uint : Move value into EAX and return
969/// ret pointer : Move value into EAX and return
Chris Lattner06925362002-11-17 21:56:38 +0000970/// ret long, ulong : Move value into EAX/EDX and return
971/// ret float/double : Top of FP stack
Chris Lattner72614082002-10-25 22:55:53 +0000972///
Chris Lattner3e130a22003-01-13 00:32:26 +0000973void ISel::visitReturnInst(ReturnInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +0000974 if (I.getNumOperands() == 0) {
975 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
976 return;
977 }
978
979 Value *RetVal = I.getOperand(0);
Chris Lattner3e130a22003-01-13 00:32:26 +0000980 unsigned RetReg = getReg(RetVal);
981 switch (getClassB(RetVal->getType())) {
Chris Lattner94af4142002-12-25 05:13:53 +0000982 case cByte: // integral return values: extend or move into EAX and return
983 case cShort:
984 case cInt:
Chris Lattner3e130a22003-01-13 00:32:26 +0000985 promote32(X86::EAX, ValueRecord(RetReg, RetVal->getType()));
Chris Lattnerdbd73722003-05-06 21:32:22 +0000986 // Declare that EAX is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000987 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +0000988 break;
989 case cFP: // Floats & Doubles: Return in ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +0000990 BuildMI(BB, X86::FpSETRESULT, 1).addReg(RetReg);
Chris Lattnerdbd73722003-05-06 21:32:22 +0000991 // Declare that top-of-stack is live on exit
Chris Lattnerc2489032003-05-07 19:21:28 +0000992 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
Chris Lattner94af4142002-12-25 05:13:53 +0000993 break;
994 case cLong:
Chris Lattner3e130a22003-01-13 00:32:26 +0000995 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(RetReg);
996 BuildMI(BB, X86::MOVrr32, 1, X86::EDX).addReg(RetReg+1);
Chris Lattnerdbd73722003-05-06 21:32:22 +0000997 // Declare that EAX & EDX are live on exit
Misha Brukmanc8893fc2003-10-23 16:22:08 +0000998 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
999 .addReg(X86::ESP);
Chris Lattner3e130a22003-01-13 00:32:26 +00001000 break;
Chris Lattner94af4142002-12-25 05:13:53 +00001001 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001002 visitInstruction(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001003 }
Chris Lattner43189d12002-11-17 20:07:45 +00001004 // Emit a 'ret' instruction
Chris Lattner94af4142002-12-25 05:13:53 +00001005 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +00001006}
1007
Chris Lattner55f6fab2003-01-16 18:07:23 +00001008// getBlockAfter - Return the basic block which occurs lexically after the
1009// specified one.
1010static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1011 Function::iterator I = BB; ++I; // Get iterator to next block
1012 return I != BB->getParent()->end() ? &*I : 0;
1013}
1014
Chris Lattner51b49a92002-11-02 19:45:49 +00001015/// visitBranchInst - Handle conditional and unconditional branches here. Note
1016/// that since code layout is frozen at this point, that if we are trying to
1017/// jump to a block that is the immediate successor of the current block, we can
Chris Lattner6d40c192003-01-16 16:43:00 +00001018/// just make a fall-through (but we don't currently).
Chris Lattner51b49a92002-11-02 19:45:49 +00001019///
Chris Lattner94af4142002-12-25 05:13:53 +00001020void ISel::visitBranchInst(BranchInst &BI) {
Chris Lattner55f6fab2003-01-16 18:07:23 +00001021 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1022
1023 if (!BI.isConditional()) { // Unconditional branch?
Chris Lattnercf93cdd2004-01-30 22:13:44 +00001024 if (BI.getSuccessor(0) != NextBB)
Chris Lattner55f6fab2003-01-16 18:07:23 +00001025 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
Chris Lattner6d40c192003-01-16 16:43:00 +00001026 return;
1027 }
1028
1029 // See if we can fold the setcc into the branch itself...
1030 SetCondInst *SCI = canFoldSetCCIntoBranch(BI.getCondition());
1031 if (SCI == 0) {
1032 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1033 // computed some other way...
Chris Lattner065faeb2002-12-28 20:24:02 +00001034 unsigned condReg = getReg(BI.getCondition());
Chris Lattner94af4142002-12-25 05:13:53 +00001035 BuildMI(BB, X86::CMPri8, 2).addReg(condReg).addZImm(0);
Chris Lattner55f6fab2003-01-16 18:07:23 +00001036 if (BI.getSuccessor(1) == NextBB) {
1037 if (BI.getSuccessor(0) != NextBB)
1038 BuildMI(BB, X86::JNE, 1).addPCDisp(BI.getSuccessor(0));
1039 } else {
1040 BuildMI(BB, X86::JE, 1).addPCDisp(BI.getSuccessor(1));
1041
1042 if (BI.getSuccessor(0) != NextBB)
1043 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(0));
1044 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001045 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001046 }
Chris Lattner6d40c192003-01-16 16:43:00 +00001047
1048 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
Chris Lattner58c41fe2003-08-24 19:19:47 +00001049 MachineBasicBlock::iterator MII = BB->end();
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001050 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001051
1052 const Type *CompTy = SCI->getOperand(0)->getType();
1053 bool isSigned = CompTy->isSigned() && getClassB(CompTy) != cFP;
Chris Lattner6d40c192003-01-16 16:43:00 +00001054
Chris Lattnerb2acc512003-10-19 21:09:10 +00001055
Chris Lattner6d40c192003-01-16 16:43:00 +00001056 // LLVM -> X86 signed X86 unsigned
1057 // ----- ---------- ------------
1058 // seteq -> je je
1059 // setne -> jne jne
1060 // setlt -> jl jb
Chris Lattner55f6fab2003-01-16 18:07:23 +00001061 // setge -> jge jae
Chris Lattner6d40c192003-01-16 16:43:00 +00001062 // setgt -> jg ja
1063 // setle -> jle jbe
Chris Lattnerb2acc512003-10-19 21:09:10 +00001064 // ----
1065 // js // Used by comparison with 0 optimization
1066 // jns
1067
1068 static const unsigned OpcodeTab[2][8] = {
1069 { X86::JE, X86::JNE, X86::JB, X86::JAE, X86::JA, X86::JBE, 0, 0 },
1070 { X86::JE, X86::JNE, X86::JL, X86::JGE, X86::JG, X86::JLE,
1071 X86::JS, X86::JNS },
Chris Lattner6d40c192003-01-16 16:43:00 +00001072 };
1073
Chris Lattner55f6fab2003-01-16 18:07:23 +00001074 if (BI.getSuccessor(0) != NextBB) {
1075 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(0));
1076 if (BI.getSuccessor(1) != NextBB)
1077 BuildMI(BB, X86::JMP, 1).addPCDisp(BI.getSuccessor(1));
1078 } else {
1079 // Change to the inverse condition...
1080 if (BI.getSuccessor(1) != NextBB) {
1081 OpNum ^= 1;
1082 BuildMI(BB, OpcodeTab[isSigned][OpNum], 1).addPCDisp(BI.getSuccessor(1));
1083 }
1084 }
Chris Lattner2df035b2002-11-02 19:27:56 +00001085}
1086
Chris Lattner3e130a22003-01-13 00:32:26 +00001087
1088/// doCall - This emits an abstract call instruction, setting up the arguments
1089/// and the return value as appropriate. For the actual function call itself,
1090/// it inserts the specified CallMI instruction into the stream.
1091///
1092void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001093 const std::vector<ValueRecord> &Args) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001094
Chris Lattner065faeb2002-12-28 20:24:02 +00001095 // Count how many bytes are to be pushed on the stack...
1096 unsigned NumBytes = 0;
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001097
Chris Lattner3e130a22003-01-13 00:32:26 +00001098 if (!Args.empty()) {
1099 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1100 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001101 case cByte: case cShort: case cInt:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001102 NumBytes += 4; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001103 case cLong:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001104 NumBytes += 8; break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001105 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001106 NumBytes += Args[i].Ty == Type::FloatTy ? 4 : 8;
1107 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001108 default: assert(0 && "Unknown class!");
1109 }
1110
1111 // Adjust the stack pointer for the new arguments...
1112 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(NumBytes);
1113
1114 // Arguments go on the stack in reverse order, as specified by the ABI.
1115 unsigned ArgOffset = 0;
Chris Lattner3e130a22003-01-13 00:32:26 +00001116 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001117 unsigned ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
Chris Lattner3e130a22003-01-13 00:32:26 +00001118 switch (getClassB(Args[i].Ty)) {
Chris Lattner065faeb2002-12-28 20:24:02 +00001119 case cByte:
1120 case cShort: {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001121 // Promote arg to 32 bits wide into a temporary register...
1122 unsigned R = makeAnotherReg(Type::UIntTy);
1123 promote32(R, Args[i]);
Chris Lattnere87331d2004-02-17 06:28:19 +00001124 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001125 X86::ESP, ArgOffset).addReg(R);
1126 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001127 }
1128 case cInt:
Chris Lattnere87331d2004-02-17 06:28:19 +00001129 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001130 X86::ESP, ArgOffset).addReg(ArgReg);
1131 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001132 case cLong:
Chris Lattnere87331d2004-02-17 06:28:19 +00001133 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001134 X86::ESP, ArgOffset).addReg(ArgReg);
Chris Lattnere87331d2004-02-17 06:28:19 +00001135 addRegOffset(BuildMI(BB, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001136 X86::ESP, ArgOffset+4).addReg(ArgReg+1);
1137 ArgOffset += 4; // 8 byte entry, not 4.
1138 break;
1139
Chris Lattner065faeb2002-12-28 20:24:02 +00001140 case cFP:
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001141 if (Args[i].Ty == Type::FloatTy) {
1142 addRegOffset(BuildMI(BB, X86::FSTr32, 5),
1143 X86::ESP, ArgOffset).addReg(ArgReg);
1144 } else {
1145 assert(Args[i].Ty == Type::DoubleTy && "Unknown FP type!");
1146 addRegOffset(BuildMI(BB, X86::FSTr64, 5),
1147 X86::ESP, ArgOffset).addReg(ArgReg);
1148 ArgOffset += 4; // 8 byte entry, not 4.
1149 }
1150 break;
Chris Lattner065faeb2002-12-28 20:24:02 +00001151
Chris Lattner3e130a22003-01-13 00:32:26 +00001152 default: assert(0 && "Unknown class!");
Chris Lattner065faeb2002-12-28 20:24:02 +00001153 }
1154 ArgOffset += 4;
Chris Lattner94af4142002-12-25 05:13:53 +00001155 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001156 } else {
1157 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addZImm(0);
Chris Lattner94af4142002-12-25 05:13:53 +00001158 }
Chris Lattner6e49a4b2002-12-13 14:13:27 +00001159
Chris Lattner3e130a22003-01-13 00:32:26 +00001160 BB->push_back(CallMI);
Misha Brukman0d2cf3a2002-12-04 19:22:53 +00001161
Chris Lattner065faeb2002-12-28 20:24:02 +00001162 BuildMI(BB, X86::ADJCALLSTACKUP, 1).addZImm(NumBytes);
Chris Lattnera3243642002-12-04 23:45:28 +00001163
1164 // If there is a return value, scavenge the result from the location the call
1165 // leaves it in...
1166 //
Chris Lattner3e130a22003-01-13 00:32:26 +00001167 if (Ret.Ty != Type::VoidTy) {
1168 unsigned DestClass = getClassB(Ret.Ty);
1169 switch (DestClass) {
Brian Gaeke20244b72002-12-12 15:33:40 +00001170 case cByte:
1171 case cShort:
1172 case cInt: {
1173 // Integral results are in %eax, or the appropriate portion
1174 // thereof.
1175 static const unsigned regRegMove[] = {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001176 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32
Brian Gaeke20244b72002-12-12 15:33:40 +00001177 };
1178 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX };
Chris Lattner3e130a22003-01-13 00:32:26 +00001179 BuildMI(BB, regRegMove[DestClass], 1, Ret.Reg).addReg(AReg[DestClass]);
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001180 break;
Brian Gaeke20244b72002-12-12 15:33:40 +00001181 }
Chris Lattner94af4142002-12-25 05:13:53 +00001182 case cFP: // Floating-point return values live in %ST(0)
Chris Lattner3e130a22003-01-13 00:32:26 +00001183 BuildMI(BB, X86::FpGETRESULT, 1, Ret.Reg);
Brian Gaeke20244b72002-12-12 15:33:40 +00001184 break;
Chris Lattner3e130a22003-01-13 00:32:26 +00001185 case cLong: // Long values are left in EDX:EAX
1186 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg).addReg(X86::EAX);
1187 BuildMI(BB, X86::MOVrr32, 1, Ret.Reg+1).addReg(X86::EDX);
1188 break;
1189 default: assert(0 && "Unknown class!");
Chris Lattner4fa1acc2002-12-04 23:50:28 +00001190 }
Chris Lattnera3243642002-12-04 23:45:28 +00001191 }
Brian Gaekefa8d5712002-11-22 11:07:01 +00001192}
Chris Lattner2df035b2002-11-02 19:27:56 +00001193
Chris Lattner3e130a22003-01-13 00:32:26 +00001194
1195/// visitCallInst - Push args on stack and do a procedure call instruction.
1196void ISel::visitCallInst(CallInst &CI) {
1197 MachineInstr *TheCall;
1198 if (Function *F = CI.getCalledFunction()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001199 // Is it an intrinsic function call?
Brian Gaeked0fde302003-11-11 22:41:34 +00001200 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001201 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1202 return;
1203 }
1204
Chris Lattner3e130a22003-01-13 00:32:26 +00001205 // Emit a CALL instruction with PC-relative displacement.
1206 TheCall = BuildMI(X86::CALLpcrel32, 1).addGlobalAddress(F, true);
1207 } else { // Emit an indirect call...
1208 unsigned Reg = getReg(CI.getCalledValue());
1209 TheCall = BuildMI(X86::CALLr32, 1).addReg(Reg);
1210 }
1211
1212 std::vector<ValueRecord> Args;
1213 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001214 Args.push_back(ValueRecord(CI.getOperand(i)));
Chris Lattner3e130a22003-01-13 00:32:26 +00001215
1216 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1217 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001218}
Chris Lattner3e130a22003-01-13 00:32:26 +00001219
Chris Lattneraeb54b82003-08-28 21:23:43 +00001220
Chris Lattner44827152003-12-28 09:47:19 +00001221/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1222/// function, lowering any calls to unknown intrinsic functions into the
1223/// equivalent LLVM code.
1224void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1225 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1226 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1227 if (CallInst *CI = dyn_cast<CallInst>(I++))
1228 if (Function *F = CI->getCalledFunction())
1229 switch (F->getIntrinsicID()) {
Chris Lattneraed386e2003-12-28 09:53:23 +00001230 case Intrinsic::not_intrinsic:
Chris Lattner44827152003-12-28 09:47:19 +00001231 case Intrinsic::va_start:
1232 case Intrinsic::va_copy:
1233 case Intrinsic::va_end:
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001234 case Intrinsic::returnaddress:
1235 case Intrinsic::frameaddress:
Chris Lattner915e5e52004-02-12 17:53:22 +00001236 case Intrinsic::memcpy:
Chris Lattner2a0f2242004-02-14 04:46:05 +00001237 case Intrinsic::memset:
Chris Lattner44827152003-12-28 09:47:19 +00001238 // We directly implement these intrinsics
1239 break;
1240 default:
1241 // All other intrinsic calls we must lower.
1242 Instruction *Before = CI->getPrev();
Chris Lattnerf70e0c22003-12-28 21:23:38 +00001243 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
Chris Lattner44827152003-12-28 09:47:19 +00001244 if (Before) { // Move iterator to instruction after call
1245 I = Before; ++I;
1246 } else {
1247 I = BB->begin();
1248 }
1249 }
1250
1251}
1252
Brian Gaeked0fde302003-11-11 22:41:34 +00001253void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnereca195e2003-05-08 19:44:13 +00001254 unsigned TmpReg1, TmpReg2;
1255 switch (ID) {
Brian Gaeked0fde302003-11-11 22:41:34 +00001256 case Intrinsic::va_start:
Chris Lattnereca195e2003-05-08 19:44:13 +00001257 // Get the address of the first vararg value...
Chris Lattner73815062003-10-18 05:56:40 +00001258 TmpReg1 = getReg(CI);
Chris Lattnereca195e2003-05-08 19:44:13 +00001259 addFrameReference(BuildMI(BB, X86::LEAr32, 5, TmpReg1), VarArgsFrameIndex);
Chris Lattnereca195e2003-05-08 19:44:13 +00001260 return;
1261
Brian Gaeked0fde302003-11-11 22:41:34 +00001262 case Intrinsic::va_copy:
Chris Lattner73815062003-10-18 05:56:40 +00001263 TmpReg1 = getReg(CI);
1264 TmpReg2 = getReg(CI.getOperand(1));
1265 BuildMI(BB, X86::MOVrr32, 1, TmpReg1).addReg(TmpReg2);
Chris Lattnereca195e2003-05-08 19:44:13 +00001266 return;
Brian Gaeked0fde302003-11-11 22:41:34 +00001267 case Intrinsic::va_end: return; // Noop on X86
Chris Lattnereca195e2003-05-08 19:44:13 +00001268
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001269 case Intrinsic::returnaddress:
1270 case Intrinsic::frameaddress:
1271 TmpReg1 = getReg(CI);
1272 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1273 if (ID == Intrinsic::returnaddress) {
1274 // Just load the return address
Chris Lattnere87331d2004-02-17 06:28:19 +00001275 addFrameReference(BuildMI(BB, X86::MOVrm32, 4, TmpReg1),
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001276 ReturnAddressIndex);
1277 } else {
1278 addFrameReference(BuildMI(BB, X86::LEAr32, 4, TmpReg1),
1279 ReturnAddressIndex, -4);
1280 }
1281 } else {
1282 // Values other than zero are not implemented yet.
Chris Lattner6e173a02004-02-17 06:16:44 +00001283 BuildMI(BB, X86::MOVri32, 1, TmpReg1).addZImm(0);
Chris Lattner0e5b79c2004-02-15 01:04:03 +00001284 }
1285 return;
1286
Chris Lattner915e5e52004-02-12 17:53:22 +00001287 case Intrinsic::memcpy: {
1288 assert(CI.getNumOperands() == 5 && "Illegal llvm.memcpy call!");
1289 unsigned Align = 1;
1290 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1291 Align = AlignC->getRawValue();
1292 if (Align == 0) Align = 1;
1293 }
1294
1295 // Turn the byte code into # iterations
Chris Lattner07122832004-02-13 23:36:47 +00001296 unsigned ByteReg;
Chris Lattner915e5e52004-02-12 17:53:22 +00001297 unsigned CountReg;
Chris Lattner2a0f2242004-02-14 04:46:05 +00001298 unsigned Opcode;
Chris Lattner915e5e52004-02-12 17:53:22 +00001299 switch (Align & 3) {
1300 case 2: // WORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001301 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1302 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
1303 } else {
1304 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001305 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(1);
Chris Lattner07122832004-02-13 23:36:47 +00001306 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001307 Opcode = X86::REP_MOVSW;
Chris Lattner915e5e52004-02-12 17:53:22 +00001308 break;
1309 case 0: // DWORD aligned
Chris Lattner07122832004-02-13 23:36:47 +00001310 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
1311 CountReg = getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
1312 } else {
1313 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001314 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(2);
Chris Lattner07122832004-02-13 23:36:47 +00001315 }
Chris Lattner2a0f2242004-02-14 04:46:05 +00001316 Opcode = X86::REP_MOVSD;
Chris Lattner915e5e52004-02-12 17:53:22 +00001317 break;
1318 case 1: // BYTE aligned
1319 case 3: // BYTE aligned
Chris Lattner07122832004-02-13 23:36:47 +00001320 CountReg = getReg(CI.getOperand(3));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001321 Opcode = X86::REP_MOVSB;
Chris Lattner915e5e52004-02-12 17:53:22 +00001322 break;
1323 }
1324
1325 // No matter what the alignment is, we put the source in ESI, the
1326 // destination in EDI, and the count in ECX.
1327 TmpReg1 = getReg(CI.getOperand(1));
1328 TmpReg2 = getReg(CI.getOperand(2));
1329 BuildMI(BB, X86::MOVrr32, 1, X86::ECX).addReg(CountReg);
1330 BuildMI(BB, X86::MOVrr32, 1, X86::EDI).addReg(TmpReg1);
1331 BuildMI(BB, X86::MOVrr32, 1, X86::ESI).addReg(TmpReg2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001332 BuildMI(BB, Opcode, 0);
1333 return;
1334 }
1335 case Intrinsic::memset: {
1336 assert(CI.getNumOperands() == 5 && "Illegal llvm.memset call!");
1337 unsigned Align = 1;
1338 if (ConstantInt *AlignC = dyn_cast<ConstantInt>(CI.getOperand(4))) {
1339 Align = AlignC->getRawValue();
1340 if (Align == 0) Align = 1;
Chris Lattner915e5e52004-02-12 17:53:22 +00001341 }
1342
Chris Lattner2a0f2242004-02-14 04:46:05 +00001343 // Turn the byte code into # iterations
1344 unsigned ByteReg;
1345 unsigned CountReg;
1346 unsigned Opcode;
1347 if (ConstantInt *ValC = dyn_cast<ConstantInt>(CI.getOperand(2))) {
1348 unsigned Val = ValC->getRawValue() & 255;
1349
1350 // If the value is a constant, then we can potentially use larger copies.
1351 switch (Align & 3) {
1352 case 2: // WORD aligned
1353 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001354 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/2));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001355 } else {
1356 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001357 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(1);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001358 }
Chris Lattner6e173a02004-02-17 06:16:44 +00001359 BuildMI(BB, X86::MOVri16, 1, X86::AX).addZImm((Val << 8) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001360 Opcode = X86::REP_STOSW;
1361 break;
1362 case 0: // DWORD aligned
1363 if (ConstantInt *I = dyn_cast<ConstantInt>(CI.getOperand(3))) {
Chris Lattner300d0ed2004-02-14 06:00:36 +00001364 CountReg =getReg(ConstantUInt::get(Type::UIntTy, I->getRawValue()/4));
Chris Lattner2a0f2242004-02-14 04:46:05 +00001365 } else {
1366 CountReg = makeAnotherReg(Type::IntTy);
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001367 BuildMI(BB, X86::SHRri32, 2, CountReg).addReg(ByteReg).addZImm(2);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001368 }
1369 Val = (Val << 8) | Val;
Chris Lattner6e173a02004-02-17 06:16:44 +00001370 BuildMI(BB, X86::MOVri32, 1, X86::EAX).addZImm((Val << 16) | Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001371 Opcode = X86::REP_STOSD;
1372 break;
1373 case 1: // BYTE aligned
1374 case 3: // BYTE aligned
1375 CountReg = getReg(CI.getOperand(3));
Chris Lattner6e173a02004-02-17 06:16:44 +00001376 BuildMI(BB, X86::MOVri8, 1, X86::AL).addZImm(Val);
Chris Lattner2a0f2242004-02-14 04:46:05 +00001377 Opcode = X86::REP_STOSB;
1378 break;
1379 }
1380 } else {
1381 // If it's not a constant value we are storing, just fall back. We could
1382 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
1383 unsigned ValReg = getReg(CI.getOperand(2));
1384 BuildMI(BB, X86::MOVrr8, 1, X86::AL).addReg(ValReg);
1385 CountReg = getReg(CI.getOperand(3));
1386 Opcode = X86::REP_STOSB;
1387 }
1388
1389 // No matter what the alignment is, we put the source in ESI, the
1390 // destination in EDI, and the count in ECX.
1391 TmpReg1 = getReg(CI.getOperand(1));
1392 //TmpReg2 = getReg(CI.getOperand(2));
1393 BuildMI(BB, X86::MOVrr32, 1, X86::ECX).addReg(CountReg);
1394 BuildMI(BB, X86::MOVrr32, 1, X86::EDI).addReg(TmpReg1);
1395 BuildMI(BB, Opcode, 0);
Chris Lattner915e5e52004-02-12 17:53:22 +00001396 return;
1397 }
1398
Chris Lattner44827152003-12-28 09:47:19 +00001399 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
Chris Lattnereca195e2003-05-08 19:44:13 +00001400 }
1401}
1402
1403
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001404/// visitSimpleBinary - Implement simple binary operators for integral types...
1405/// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1406/// Xor.
1407void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1408 unsigned DestReg = getReg(B);
1409 MachineBasicBlock::iterator MI = BB->end();
1410 emitSimpleBinaryOperation(BB, MI, B.getOperand(0), B.getOperand(1),
1411 OperatorClass, DestReg);
1412}
Chris Lattner3e130a22003-01-13 00:32:26 +00001413
Chris Lattnerb2acc512003-10-19 21:09:10 +00001414/// emitSimpleBinaryOperation - Implement simple binary operators for integral
1415/// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1416/// Or, 4 for Xor.
Chris Lattner68aad932002-11-02 20:13:22 +00001417///
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001418/// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1419/// and constant expression support.
Chris Lattnerb2acc512003-10-19 21:09:10 +00001420///
1421void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001422 MachineBasicBlock::iterator IP,
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001423 Value *Op0, Value *Op1,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001424 unsigned OperatorClass, unsigned DestReg) {
Chris Lattnerb515f6d2003-05-08 20:49:25 +00001425 unsigned Class = getClassB(Op0->getType());
Chris Lattnerb2acc512003-10-19 21:09:10 +00001426
1427 // sub 0, X -> neg X
1428 if (OperatorClass == 1 && Class != cLong)
Chris Lattneraf703622004-02-02 18:56:30 +00001429 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001430 if (CI->isNullValue()) {
1431 unsigned op1Reg = getReg(Op1, MBB, IP);
1432 switch (Class) {
1433 default: assert(0 && "Unknown class for this function!");
1434 case cByte:
1435 BMI(MBB, IP, X86::NEGr8, 1, DestReg).addReg(op1Reg);
1436 return;
1437 case cShort:
1438 BMI(MBB, IP, X86::NEGr16, 1, DestReg).addReg(op1Reg);
1439 return;
1440 case cInt:
1441 BMI(MBB, IP, X86::NEGr32, 1, DestReg).addReg(op1Reg);
1442 return;
1443 }
1444 }
Chris Lattner9f8fd6d2004-02-02 19:31:38 +00001445 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(Op0))
1446 if (CFP->isExactlyValue(-0.0)) {
1447 // -0.0 - X === -X
1448 unsigned op1Reg = getReg(Op1, MBB, IP);
1449 BMI(MBB, IP, X86::FCHS, 1, DestReg).addReg(op1Reg);
1450 return;
1451 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001452
Chris Lattner35333e12003-06-05 18:28:55 +00001453 if (!isa<ConstantInt>(Op1) || Class == cLong) {
1454 static const unsigned OpcodeTab[][4] = {
1455 // Arithmetic operators
1456 { X86::ADDrr8, X86::ADDrr16, X86::ADDrr32, X86::FpADD }, // ADD
1457 { X86::SUBrr8, X86::SUBrr16, X86::SUBrr32, X86::FpSUB }, // SUB
1458
1459 // Bitwise operators
1460 { X86::ANDrr8, X86::ANDrr16, X86::ANDrr32, 0 }, // AND
1461 { X86:: ORrr8, X86:: ORrr16, X86:: ORrr32, 0 }, // OR
1462 { X86::XORrr8, X86::XORrr16, X86::XORrr32, 0 }, // XOR
Chris Lattner3e130a22003-01-13 00:32:26 +00001463 };
Chris Lattner35333e12003-06-05 18:28:55 +00001464
1465 bool isLong = false;
1466 if (Class == cLong) {
1467 isLong = true;
1468 Class = cInt; // Bottom 32 bits are handled just like ints
1469 }
1470
1471 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1472 assert(Opcode && "Floating point arguments to logical inst?");
Chris Lattnerb2acc512003-10-19 21:09:10 +00001473 unsigned Op0r = getReg(Op0, MBB, IP);
1474 unsigned Op1r = getReg(Op1, MBB, IP);
1475 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner35333e12003-06-05 18:28:55 +00001476
1477 if (isLong) { // Handle the upper 32 bits of long values...
1478 static const unsigned TopTab[] = {
1479 X86::ADCrr32, X86::SBBrr32, X86::ANDrr32, X86::ORrr32, X86::XORrr32
1480 };
Chris Lattnerb2acc512003-10-19 21:09:10 +00001481 BMI(MBB, IP, TopTab[OperatorClass], 2,
1482 DestReg+1).addReg(Op0r+1).addReg(Op1r+1);
Chris Lattner35333e12003-06-05 18:28:55 +00001483 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001484 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001485 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001486
1487 // Special case: op Reg, <const>
1488 ConstantInt *Op1C = cast<ConstantInt>(Op1);
1489 unsigned Op0r = getReg(Op0, MBB, IP);
1490
1491 // xor X, -1 -> not X
1492 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1493 static unsigned const NOTTab[] = { X86::NOTr8, X86::NOTr16, X86::NOTr32 };
1494 BMI(MBB, IP, NOTTab[Class], 1, DestReg).addReg(Op0r);
1495 return;
1496 }
1497
1498 // add X, -1 -> dec X
1499 if (OperatorClass == 0 && Op1C->isAllOnesValue()) {
1500 static unsigned const DECTab[] = { X86::DECr8, X86::DECr16, X86::DECr32 };
1501 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1502 return;
1503 }
1504
1505 // add X, 1 -> inc X
1506 if (OperatorClass == 0 && Op1C->equalsInt(1)) {
1507 static unsigned const DECTab[] = { X86::INCr8, X86::INCr16, X86::INCr32 };
1508 BMI(MBB, IP, DECTab[Class], 1, DestReg).addReg(Op0r);
1509 return;
1510 }
1511
1512 static const unsigned OpcodeTab[][3] = {
1513 // Arithmetic operators
1514 { X86::ADDri8, X86::ADDri16, X86::ADDri32 }, // ADD
1515 { X86::SUBri8, X86::SUBri16, X86::SUBri32 }, // SUB
1516
1517 // Bitwise operators
1518 { X86::ANDri8, X86::ANDri16, X86::ANDri32 }, // AND
1519 { X86:: ORri8, X86:: ORri16, X86:: ORri32 }, // OR
1520 { X86::XORri8, X86::XORri16, X86::XORri32 }, // XOR
1521 };
1522
1523 assert(Class < 3 && "General code handles 64-bit integer types!");
1524 unsigned Opcode = OpcodeTab[OperatorClass][Class];
1525 uint64_t Op1v = cast<ConstantInt>(Op1C)->getRawValue();
1526
1527 // Mask off any upper bits of the constant, if there are any...
1528 Op1v &= (1ULL << (8 << Class)) - 1;
1529 BMI(MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addZImm(Op1v);
Chris Lattnere2954c82002-11-02 20:04:26 +00001530}
1531
Chris Lattner3e130a22003-01-13 00:32:26 +00001532/// doMultiply - Emit appropriate instructions to multiply together the
1533/// registers op0Reg and op1Reg, and put the result in DestReg. The type of the
1534/// result should be given as DestTy.
1535///
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001536void ISel::doMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
Chris Lattner3e130a22003-01-13 00:32:26 +00001537 unsigned DestReg, const Type *DestTy,
Chris Lattner8a307e82002-12-16 19:32:50 +00001538 unsigned op0Reg, unsigned op1Reg) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001539 unsigned Class = getClass(DestTy);
Chris Lattner94af4142002-12-25 05:13:53 +00001540 switch (Class) {
1541 case cFP: // Floating point multiply
Chris Lattner3e130a22003-01-13 00:32:26 +00001542 BMI(BB, MBBI, X86::FpMUL, 2, DestReg).addReg(op0Reg).addReg(op1Reg);
Chris Lattner94af4142002-12-25 05:13:53 +00001543 return;
Chris Lattner0f1c4612003-06-21 17:16:58 +00001544 case cInt:
1545 case cShort:
Chris Lattnerc01d1232003-10-20 03:42:58 +00001546 BMI(BB, MBBI, Class == cInt ? X86::IMULrr32 : X86::IMULrr16, 2, DestReg)
Chris Lattner0f1c4612003-06-21 17:16:58 +00001547 .addReg(op0Reg).addReg(op1Reg);
1548 return;
1549 case cByte:
1550 // Must use the MUL instruction, which forces use of AL...
1551 BMI(MBB, MBBI, X86::MOVrr8, 1, X86::AL).addReg(op0Reg);
1552 BMI(MBB, MBBI, X86::MULr8, 1).addReg(op1Reg);
1553 BMI(MBB, MBBI, X86::MOVrr8, 1, DestReg).addReg(X86::AL);
1554 return;
Chris Lattner94af4142002-12-25 05:13:53 +00001555 default:
Chris Lattner3e130a22003-01-13 00:32:26 +00001556 case cLong: assert(0 && "doMultiply cannot operate on LONG values!");
Chris Lattner94af4142002-12-25 05:13:53 +00001557 }
Brian Gaeke20244b72002-12-12 15:33:40 +00001558}
1559
Chris Lattnerb2acc512003-10-19 21:09:10 +00001560// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1561// returns zero when the input is not exactly a power of two.
1562static unsigned ExactLog2(unsigned Val) {
1563 if (Val == 0) return 0;
1564 unsigned Count = 0;
1565 while (Val != 1) {
1566 if (Val & 1) return 0;
1567 Val >>= 1;
1568 ++Count;
1569 }
1570 return Count+1;
1571}
1572
1573void ISel::doMultiplyConst(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001574 MachineBasicBlock::iterator IP,
Chris Lattnerb2acc512003-10-19 21:09:10 +00001575 unsigned DestReg, const Type *DestTy,
1576 unsigned op0Reg, unsigned ConstRHS) {
1577 unsigned Class = getClass(DestTy);
1578
1579 // If the element size is exactly a power of 2, use a shift to get it.
1580 if (unsigned Shift = ExactLog2(ConstRHS)) {
1581 switch (Class) {
1582 default: assert(0 && "Unknown class for this function!");
1583 case cByte:
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001584 BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001585 return;
1586 case cShort:
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001587 BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001588 return;
1589 case cInt:
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001590 BMI(MBB, IP, X86::SHLri32, 2, DestReg).addReg(op0Reg).addZImm(Shift-1);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001591 return;
1592 }
1593 }
Chris Lattnerc01d1232003-10-20 03:42:58 +00001594
1595 if (Class == cShort) {
Chris Lattner55b54812004-02-17 04:26:43 +00001596 BMI(MBB, IP, X86::IMULrri16, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00001597 return;
1598 } else if (Class == cInt) {
Chris Lattner55b54812004-02-17 04:26:43 +00001599 BMI(MBB, IP, X86::IMULrri32, 2, DestReg).addReg(op0Reg).addZImm(ConstRHS);
Chris Lattnerc01d1232003-10-20 03:42:58 +00001600 return;
1601 }
Chris Lattnerb2acc512003-10-19 21:09:10 +00001602
1603 // Most general case, emit a normal multiply...
Chris Lattner6e173a02004-02-17 06:16:44 +00001604 static const unsigned MOVriTab[] = {
1605 X86::MOVri8, X86::MOVri16, X86::MOVri32
Chris Lattnerb2acc512003-10-19 21:09:10 +00001606 };
1607
1608 unsigned TmpReg = makeAnotherReg(DestTy);
Chris Lattner6e173a02004-02-17 06:16:44 +00001609 BMI(MBB, IP, MOVriTab[Class], 1, TmpReg).addZImm(ConstRHS);
Chris Lattnerb2acc512003-10-19 21:09:10 +00001610
1611 // Emit a MUL to multiply the register holding the index by
1612 // elementSize, putting the result in OffsetReg.
1613 doMultiply(MBB, IP, DestReg, DestTy, op0Reg, TmpReg);
1614}
1615
Chris Lattnerca9671d2002-11-02 20:28:58 +00001616/// visitMul - Multiplies are not simple binary operators because they must deal
1617/// with the EAX register explicitly.
1618///
1619void ISel::visitMul(BinaryOperator &I) {
Chris Lattner202a2d02002-12-13 13:07:42 +00001620 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00001621 unsigned DestReg = getReg(I);
1622
1623 // Simple scalar multiply?
1624 if (I.getType() != Type::LongTy && I.getType() != Type::ULongTy) {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001625 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
1626 unsigned Val = (unsigned)CI->getRawValue(); // Cannot be 64-bit constant
1627 MachineBasicBlock::iterator MBBI = BB->end();
1628 doMultiplyConst(BB, MBBI, DestReg, I.getType(), Op0Reg, Val);
1629 } else {
1630 unsigned Op1Reg = getReg(I.getOperand(1));
1631 MachineBasicBlock::iterator MBBI = BB->end();
1632 doMultiply(BB, MBBI, DestReg, I.getType(), Op0Reg, Op1Reg);
1633 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001634 } else {
Chris Lattnerb2acc512003-10-19 21:09:10 +00001635 unsigned Op1Reg = getReg(I.getOperand(1));
1636
Chris Lattner3e130a22003-01-13 00:32:26 +00001637 // Long value. We have to do things the hard way...
1638 // Multiply the two low parts... capturing carry into EDX
1639 BuildMI(BB, X86::MOVrr32, 1, X86::EAX).addReg(Op0Reg);
1640 BuildMI(BB, X86::MULr32, 1).addReg(Op1Reg); // AL*BL
1641
1642 unsigned OverflowReg = makeAnotherReg(Type::UIntTy);
1643 BuildMI(BB, X86::MOVrr32, 1, DestReg).addReg(X86::EAX); // AL*BL
1644 BuildMI(BB, X86::MOVrr32, 1, OverflowReg).addReg(X86::EDX); // AL*BL >> 32
1645
1646 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001647 unsigned AHBLReg = makeAnotherReg(Type::UIntTy); // AH*BL
Chris Lattnerc01d1232003-10-20 03:42:58 +00001648 BMI(BB, MBBI, X86::IMULrr32, 2, AHBLReg).addReg(Op0Reg+1).addReg(Op1Reg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001649
1650 unsigned AHBLplusOverflowReg = makeAnotherReg(Type::UIntTy);
1651 BuildMI(BB, X86::ADDrr32, 2, // AH*BL+(AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001652 AHBLplusOverflowReg).addReg(AHBLReg).addReg(OverflowReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001653
1654 MBBI = BB->end();
Chris Lattner034acf02003-06-21 18:15:27 +00001655 unsigned ALBHReg = makeAnotherReg(Type::UIntTy); // AL*BH
Chris Lattnerc01d1232003-10-20 03:42:58 +00001656 BMI(BB, MBBI, X86::IMULrr32, 2, ALBHReg).addReg(Op0Reg).addReg(Op1Reg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00001657
1658 BuildMI(BB, X86::ADDrr32, 2, // AL*BH + AH*BL + (AL*BL >> 32)
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001659 DestReg+1).addReg(AHBLplusOverflowReg).addReg(ALBHReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001660 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001661}
Chris Lattnerca9671d2002-11-02 20:28:58 +00001662
Chris Lattner06925362002-11-17 21:56:38 +00001663
Chris Lattnerf01729e2002-11-02 20:54:46 +00001664/// visitDivRem - Handle division and remainder instructions... these
1665/// instruction both require the same instructions to be generated, they just
1666/// select the result from a different register. Note that both of these
1667/// instructions work differently for signed and unsigned operands.
1668///
1669void ISel::visitDivRem(BinaryOperator &I) {
Chris Lattnercadff442003-10-23 17:21:43 +00001670 unsigned Op0Reg = getReg(I.getOperand(0));
1671 unsigned Op1Reg = getReg(I.getOperand(1));
1672 unsigned ResultReg = getReg(I);
Chris Lattner94af4142002-12-25 05:13:53 +00001673
Chris Lattnercadff442003-10-23 17:21:43 +00001674 MachineBasicBlock::iterator IP = BB->end();
1675 emitDivRemOperation(BB, IP, Op0Reg, Op1Reg, I.getOpcode() == Instruction::Div,
1676 I.getType(), ResultReg);
1677}
1678
1679void ISel::emitDivRemOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001680 MachineBasicBlock::iterator IP,
Chris Lattnercadff442003-10-23 17:21:43 +00001681 unsigned Op0Reg, unsigned Op1Reg, bool isDiv,
1682 const Type *Ty, unsigned ResultReg) {
1683 unsigned Class = getClass(Ty);
Chris Lattner94af4142002-12-25 05:13:53 +00001684 switch (Class) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001685 case cFP: // Floating point divide
Chris Lattnercadff442003-10-23 17:21:43 +00001686 if (isDiv) {
Chris Lattner62b767b2003-11-18 17:47:05 +00001687 BMI(BB, IP, X86::FpDIV, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00001688 } else { // Floating point remainder...
Chris Lattner3e130a22003-01-13 00:32:26 +00001689 MachineInstr *TheCall =
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001690 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol("fmod", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00001691 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00001692 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
1693 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00001694 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args);
1695 }
Chris Lattner94af4142002-12-25 05:13:53 +00001696 return;
Chris Lattner3e130a22003-01-13 00:32:26 +00001697 case cLong: {
1698 static const char *FnName[] =
1699 { "__moddi3", "__divdi3", "__umoddi3", "__udivdi3" };
1700
Chris Lattnercadff442003-10-23 17:21:43 +00001701 unsigned NameIdx = Ty->isUnsigned()*2 + isDiv;
Chris Lattner3e130a22003-01-13 00:32:26 +00001702 MachineInstr *TheCall =
1703 BuildMI(X86::CALLpcrel32, 1).addExternalSymbol(FnName[NameIdx], true);
1704
1705 std::vector<ValueRecord> Args;
Chris Lattnercadff442003-10-23 17:21:43 +00001706 Args.push_back(ValueRecord(Op0Reg, Type::LongTy));
1707 Args.push_back(ValueRecord(Op1Reg, Type::LongTy));
Chris Lattner3e130a22003-01-13 00:32:26 +00001708 doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args);
1709 return;
1710 }
1711 case cByte: case cShort: case cInt:
Misha Brukmancf00c4a2003-10-10 17:57:28 +00001712 break; // Small integrals, handled below...
Chris Lattner3e130a22003-01-13 00:32:26 +00001713 default: assert(0 && "Unknown class!");
Chris Lattner94af4142002-12-25 05:13:53 +00001714 }
Chris Lattnerf01729e2002-11-02 20:54:46 +00001715
1716 static const unsigned Regs[] ={ X86::AL , X86::AX , X86::EAX };
1717 static const unsigned MovOpcode[]={ X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001718 static const unsigned SarOpcode[]={ X86::SARri8, X86::SARri16, X86::SARri32 };
Chris Lattner6e173a02004-02-17 06:16:44 +00001719 static const unsigned ClrOpcode[]={ X86::MOVri8, X86::MOVri16, X86::MOVri32 };
Chris Lattnerf01729e2002-11-02 20:54:46 +00001720 static const unsigned ExtRegs[] ={ X86::AH , X86::DX , X86::EDX };
1721
1722 static const unsigned DivOpcode[][4] = {
Chris Lattner3e130a22003-01-13 00:32:26 +00001723 { X86::DIVr8 , X86::DIVr16 , X86::DIVr32 , 0 }, // Unsigned division
1724 { X86::IDIVr8, X86::IDIVr16, X86::IDIVr32, 0 }, // Signed division
Chris Lattnerf01729e2002-11-02 20:54:46 +00001725 };
1726
Chris Lattnercadff442003-10-23 17:21:43 +00001727 bool isSigned = Ty->isSigned();
Chris Lattnerf01729e2002-11-02 20:54:46 +00001728 unsigned Reg = Regs[Class];
1729 unsigned ExtReg = ExtRegs[Class];
Chris Lattnerf01729e2002-11-02 20:54:46 +00001730
1731 // Put the first operand into one of the A registers...
Chris Lattner62b767b2003-11-18 17:47:05 +00001732 BMI(BB, IP, MovOpcode[Class], 1, Reg).addReg(Op0Reg);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001733
1734 if (isSigned) {
1735 // Emit a sign extension instruction...
Chris Lattnercadff442003-10-23 17:21:43 +00001736 unsigned ShiftResult = makeAnotherReg(Ty);
Chris Lattner62b767b2003-11-18 17:47:05 +00001737 BMI(BB, IP, SarOpcode[Class], 2, ShiftResult).addReg(Op0Reg).addZImm(31);
1738 BMI(BB, IP, MovOpcode[Class], 1, ExtReg).addReg(ShiftResult);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001739 } else {
Alkis Evlogimenosf998a7e2004-01-12 07:22:45 +00001740 // If unsigned, emit a zeroing instruction... (reg = 0)
1741 BMI(BB, IP, ClrOpcode[Class], 2, ExtReg).addZImm(0);
Chris Lattnerf01729e2002-11-02 20:54:46 +00001742 }
1743
Chris Lattner06925362002-11-17 21:56:38 +00001744 // Emit the appropriate divide or remainder instruction...
Chris Lattner62b767b2003-11-18 17:47:05 +00001745 BMI(BB, IP, DivOpcode[isSigned][Class], 1).addReg(Op1Reg);
Chris Lattner06925362002-11-17 21:56:38 +00001746
Chris Lattnerf01729e2002-11-02 20:54:46 +00001747 // Figure out which register we want to pick the result out of...
Chris Lattnercadff442003-10-23 17:21:43 +00001748 unsigned DestReg = isDiv ? Reg : ExtReg;
Chris Lattnerf01729e2002-11-02 20:54:46 +00001749
Chris Lattnerf01729e2002-11-02 20:54:46 +00001750 // Put the result into the destination register...
Chris Lattner62b767b2003-11-18 17:47:05 +00001751 BMI(BB, IP, MovOpcode[Class], 1, ResultReg).addReg(DestReg);
Chris Lattnerca9671d2002-11-02 20:28:58 +00001752}
Chris Lattnere2954c82002-11-02 20:04:26 +00001753
Chris Lattner06925362002-11-17 21:56:38 +00001754
Brian Gaekea1719c92002-10-31 23:03:59 +00001755/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1756/// for constant immediate shift values, and for constant immediate
1757/// shift values equal to 1. Even the general case is sort of special,
1758/// because the shift amount has to be in CL, not just any old register.
1759///
Chris Lattner3e130a22003-01-13 00:32:26 +00001760void ISel::visitShiftInst(ShiftInst &I) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001761 MachineBasicBlock::iterator IP = BB->end ();
1762 emitShiftOperation (BB, IP, I.getOperand (0), I.getOperand (1),
1763 I.getOpcode () == Instruction::Shl, I.getType (),
1764 getReg (I));
1765}
1766
1767/// emitShiftOperation - Common code shared between visitShiftInst and
1768/// constant expression support.
1769void ISel::emitShiftOperation(MachineBasicBlock *MBB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00001770 MachineBasicBlock::iterator IP,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001771 Value *Op, Value *ShiftAmount, bool isLeftShift,
1772 const Type *ResultTy, unsigned DestReg) {
1773 unsigned SrcReg = getReg (Op, MBB, IP);
1774 bool isSigned = ResultTy->isSigned ();
1775 unsigned Class = getClass (ResultTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00001776
1777 static const unsigned ConstantOperand[][4] = {
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001778 { X86::SHRri8, X86::SHRri16, X86::SHRri32, X86::SHRDri32 }, // SHR
1779 { X86::SARri8, X86::SARri16, X86::SARri32, X86::SHRDri32 }, // SAR
1780 { X86::SHLri8, X86::SHLri16, X86::SHLri32, X86::SHLDri32 }, // SHL
1781 { X86::SHLri8, X86::SHLri16, X86::SHLri32, X86::SHLDri32 }, // SAL = SHL
Chris Lattner3e130a22003-01-13 00:32:26 +00001782 };
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001783
Chris Lattner3e130a22003-01-13 00:32:26 +00001784 static const unsigned NonConstantOperand[][4] = {
1785 { X86::SHRrr8, X86::SHRrr16, X86::SHRrr32 }, // SHR
1786 { X86::SARrr8, X86::SARrr16, X86::SARrr32 }, // SAR
1787 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SHL
1788 { X86::SHLrr8, X86::SHLrr16, X86::SHLrr32 }, // SAL = SHL
1789 };
Chris Lattner796df732002-11-02 00:44:25 +00001790
Chris Lattner3e130a22003-01-13 00:32:26 +00001791 // Longs, as usual, are handled specially...
1792 if (Class == cLong) {
1793 // If we have a constant shift, we can generate much more efficient code
1794 // than otherwise...
1795 //
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001796 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001797 unsigned Amount = CUI->getValue();
1798 if (Amount < 32) {
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001799 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
1800 if (isLeftShift) {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001801 BMI(MBB, IP, Opc[3], 3,
1802 DestReg+1).addReg(SrcReg+1).addReg(SrcReg).addZImm(Amount);
1803 BMI(MBB, IP, Opc[2], 2, DestReg).addReg(SrcReg).addZImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001804 } else {
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001805 BMI(MBB, IP, Opc[3], 3,
1806 DestReg).addReg(SrcReg ).addReg(SrcReg+1).addZImm(Amount);
1807 BMI(MBB, IP, Opc[2], 2, DestReg+1).addReg(SrcReg+1).addZImm(Amount);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001808 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001809 } else { // Shifting more than 32 bits
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001810 Amount -= 32;
1811 if (isLeftShift) {
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001812 BMI(MBB, IP, X86::SHLri32, 2,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001813 DestReg + 1).addReg(SrcReg).addZImm(Amount);
Chris Lattner6e173a02004-02-17 06:16:44 +00001814 BMI(MBB, IP, X86::MOVri32, 1,
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001815 DestReg).addZImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001816 } else {
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001817 unsigned Opcode = isSigned ? X86::SARri32 : X86::SHRri32;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001818 BMI(MBB, IP, Opcode, 2, DestReg).addReg(SrcReg+1).addZImm(Amount);
Chris Lattner6e173a02004-02-17 06:16:44 +00001819 BMI(MBB, IP, X86::MOVri32, 1, DestReg+1).addZImm(0);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00001820 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001821 }
1822 } else {
Chris Lattner9171ef52003-06-01 01:56:54 +00001823 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1824
1825 if (!isLeftShift && isSigned) {
1826 // If this is a SHR of a Long, then we need to do funny sign extension
1827 // stuff. TmpReg gets the value to use as the high-part if we are
1828 // shifting more than 32 bits.
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00001829 BMI(MBB, IP, X86::SARri32, 2, TmpReg).addReg(SrcReg).addZImm(31);
Chris Lattner9171ef52003-06-01 01:56:54 +00001830 } else {
1831 // Other shifts use a fixed zero value if the shift is more than 32
1832 // bits.
Chris Lattner6e173a02004-02-17 06:16:44 +00001833 BMI(MBB, IP, X86::MOVri32, 1, TmpReg).addZImm(0);
Chris Lattner9171ef52003-06-01 01:56:54 +00001834 }
1835
1836 // Initialize CL with the shift amount...
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001837 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
1838 BMI(MBB, IP, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001839
1840 unsigned TmpReg2 = makeAnotherReg(Type::IntTy);
1841 unsigned TmpReg3 = makeAnotherReg(Type::IntTy);
1842 if (isLeftShift) {
1843 // TmpReg2 = shld inHi, inLo
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001844 BMI(MBB, IP, X86::SHLDrr32, 2, TmpReg2).addReg(SrcReg+1).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001845 // TmpReg3 = shl inLo, CL
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001846 BMI(MBB, IP, X86::SHLrr32, 1, TmpReg3).addReg(SrcReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001847
1848 // Set the flags to indicate whether the shift was by more than 32 bits.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001849 BMI(MBB, IP, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00001850
1851 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001852 BMI(MBB, IP, X86::CMOVNErr32, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00001853 DestReg+1).addReg(TmpReg2).addReg(TmpReg3);
1854 // DestLo = (>32) ? TmpReg : TmpReg3;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001855 BMI(MBB, IP, X86::CMOVNErr32, 2,
1856 DestReg).addReg(TmpReg3).addReg(TmpReg);
Chris Lattner9171ef52003-06-01 01:56:54 +00001857 } else {
1858 // TmpReg2 = shrd inLo, inHi
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001859 BMI(MBB, IP, X86::SHRDrr32, 2, TmpReg2).addReg(SrcReg).addReg(SrcReg+1);
Chris Lattner9171ef52003-06-01 01:56:54 +00001860 // TmpReg3 = s[ah]r inHi, CL
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001861 BMI(MBB, IP, isSigned ? X86::SARrr32 : X86::SHRrr32, 1, TmpReg3)
Chris Lattner9171ef52003-06-01 01:56:54 +00001862 .addReg(SrcReg+1);
1863
1864 // Set the flags to indicate whether the shift was by more than 32 bits.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001865 BMI(MBB, IP, X86::TESTri8, 2).addReg(X86::CL).addZImm(32);
Chris Lattner9171ef52003-06-01 01:56:54 +00001866
1867 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001868 BMI(MBB, IP, X86::CMOVNErr32, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00001869 DestReg).addReg(TmpReg2).addReg(TmpReg3);
1870
1871 // DestHi = (>32) ? TmpReg : TmpReg3;
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001872 BMI(MBB, IP, X86::CMOVNErr32, 2,
Chris Lattner9171ef52003-06-01 01:56:54 +00001873 DestReg+1).addReg(TmpReg3).addReg(TmpReg);
1874 }
Brian Gaekea1719c92002-10-31 23:03:59 +00001875 }
Chris Lattner3e130a22003-01-13 00:32:26 +00001876 return;
1877 }
Chris Lattnere9913f22002-11-02 01:41:55 +00001878
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001879 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00001880 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
1881 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001882
Chris Lattner3e130a22003-01-13 00:32:26 +00001883 const unsigned *Opc = ConstantOperand[isLeftShift*2+isSigned];
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001884 BMI(MBB, IP, Opc[Class], 2,
1885 DestReg).addReg(SrcReg).addZImm(CUI->getValue());
Chris Lattner3e130a22003-01-13 00:32:26 +00001886 } else { // The shift amount is non-constant.
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001887 unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP);
1888 BMI(MBB, IP, X86::MOVrr8, 1, X86::CL).addReg(ShiftAmountReg);
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001889
Chris Lattner3e130a22003-01-13 00:32:26 +00001890 const unsigned *Opc = NonConstantOperand[isLeftShift*2+isSigned];
Brian Gaekedfcc9cf2003-11-22 06:49:41 +00001891 BMI(MBB, IP, Opc[Class], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00001892 }
1893}
Chris Lattnerb1761fc2002-11-02 01:15:18 +00001894
Chris Lattner3e130a22003-01-13 00:32:26 +00001895
Chris Lattner6fc3c522002-11-17 21:11:55 +00001896/// visitLoadInst - Implement LLVM load instructions in terms of the x86 'mov'
Chris Lattnere8f0d922002-12-24 00:03:11 +00001897/// instruction. The load and store instructions are the only place where we
1898/// need to worry about the memory layout of the target machine.
Chris Lattner6fc3c522002-11-17 21:11:55 +00001899///
1900void ISel::visitLoadInst(LoadInst &I) {
Chris Lattner94af4142002-12-25 05:13:53 +00001901 unsigned DestReg = getReg(I);
Chris Lattnerb6bac512004-02-25 06:13:04 +00001902 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
1903 Value *Addr = I.getOperand(0);
1904 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
1905 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
1906 BaseReg, Scale, IndexReg, Disp))
1907 Addr = 0; // Address is consumed!
1908 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
1909 if (CE->getOpcode() == Instruction::GetElementPtr)
1910 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
1911 BaseReg, Scale, IndexReg, Disp))
1912 Addr = 0;
1913 }
1914
1915 if (Addr) {
1916 // If it's not foldable, reset addr mode.
1917 BaseReg = getReg(Addr);
1918 Scale = 1; IndexReg = 0; Disp = 0;
1919 }
Chris Lattnere8f0d922002-12-24 00:03:11 +00001920
Brian Gaekebfedb912003-07-17 21:30:06 +00001921 unsigned Class = getClassB(I.getType());
Chris Lattner6ac1d712003-10-20 04:48:06 +00001922 if (Class == cLong) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00001923 addFullAddress(BuildMI(BB, X86::MOVrm32, 4, DestReg),
1924 BaseReg, Scale, IndexReg, Disp);
1925 addFullAddress(BuildMI(BB, X86::MOVrm32, 4, DestReg+1),
1926 BaseReg, Scale, IndexReg, Disp+4);
Chris Lattner94af4142002-12-25 05:13:53 +00001927 return;
1928 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00001929
Chris Lattner6ac1d712003-10-20 04:48:06 +00001930 static const unsigned Opcodes[] = {
Chris Lattnere87331d2004-02-17 06:28:19 +00001931 X86::MOVrm8, X86::MOVrm16, X86::MOVrm32, X86::FLDr32
Chris Lattner3e130a22003-01-13 00:32:26 +00001932 };
Chris Lattner6ac1d712003-10-20 04:48:06 +00001933 unsigned Opcode = Opcodes[Class];
1934 if (I.getType() == Type::DoubleTy) Opcode = X86::FLDr64;
Chris Lattnerb6bac512004-02-25 06:13:04 +00001935 addFullAddress(BuildMI(BB, Opcode, 4, DestReg),
1936 BaseReg, Scale, IndexReg, Disp);
Chris Lattner3e130a22003-01-13 00:32:26 +00001937}
1938
Chris Lattner6fc3c522002-11-17 21:11:55 +00001939/// visitStoreInst - Implement LLVM store instructions in terms of the x86 'mov'
1940/// instruction.
1941///
1942void ISel::visitStoreInst(StoreInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00001943 unsigned BaseReg = 0, Scale = 1, IndexReg = 0, Disp = 0;
1944 Value *Addr = I.getOperand(1);
1945 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) {
1946 if (isGEPFoldable(BB, GEP->getOperand(0), GEP->op_begin()+1, GEP->op_end(),
1947 BaseReg, Scale, IndexReg, Disp))
1948 Addr = 0; // Address is consumed!
1949 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
1950 if (CE->getOpcode() == Instruction::GetElementPtr)
1951 if (isGEPFoldable(BB, CE->getOperand(0), CE->op_begin()+1, CE->op_end(),
1952 BaseReg, Scale, IndexReg, Disp))
1953 Addr = 0;
1954 }
1955
1956 if (Addr) {
1957 // If it's not foldable, reset addr mode.
1958 BaseReg = getReg(Addr);
1959 Scale = 1; IndexReg = 0; Disp = 0;
1960 }
1961
Chris Lattner6c09db22003-10-20 04:11:23 +00001962 const Type *ValTy = I.getOperand(0)->getType();
1963 unsigned Class = getClassB(ValTy);
Chris Lattner6ac1d712003-10-20 04:48:06 +00001964
Chris Lattner5a830962004-02-25 02:56:58 +00001965 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(0))) {
1966 uint64_t Val = CI->getRawValue();
1967 if (Class == cLong) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00001968 addFullAddress(BuildMI(BB, X86::MOVmi32, 5),
1969 BaseReg, Scale, IndexReg, Disp).addZImm(Val & ~0U);
1970 addFullAddress(BuildMI(BB, X86::MOVmi32, 5),
1971 BaseReg, Scale, IndexReg, Disp+4).addZImm(Val>>32);
Chris Lattner5a830962004-02-25 02:56:58 +00001972 } else {
1973 static const unsigned Opcodes[] = {
1974 X86::MOVmi8, X86::MOVmi16, X86::MOVmi32
1975 };
1976 unsigned Opcode = Opcodes[Class];
Chris Lattnerb6bac512004-02-25 06:13:04 +00001977 addFullAddress(BuildMI(BB, Opcode, 5),
1978 BaseReg, Scale, IndexReg, Disp).addZImm(Val);
Chris Lattner5a830962004-02-25 02:56:58 +00001979 }
1980 } else if (ConstantBool *CB = dyn_cast<ConstantBool>(I.getOperand(0))) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00001981 addFullAddress(BuildMI(BB, X86::MOVmi8, 5),
1982 BaseReg, Scale, IndexReg, Disp).addZImm(CB->getValue());
Chris Lattner5a830962004-02-25 02:56:58 +00001983 } else {
1984 if (Class == cLong) {
1985 unsigned ValReg = getReg(I.getOperand(0));
Chris Lattnerb6bac512004-02-25 06:13:04 +00001986 addFullAddress(BuildMI(BB, X86::MOVmr32, 5),
1987 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
1988 addFullAddress(BuildMI(BB, X86::MOVmr32, 5),
1989 BaseReg, Scale, IndexReg, Disp+4).addReg(ValReg+1);
Chris Lattner5a830962004-02-25 02:56:58 +00001990 } else {
1991 unsigned ValReg = getReg(I.getOperand(0));
1992 static const unsigned Opcodes[] = {
1993 X86::MOVmr8, X86::MOVmr16, X86::MOVmr32, X86::FSTr32
1994 };
1995 unsigned Opcode = Opcodes[Class];
1996 if (ValTy == Type::DoubleTy) Opcode = X86::FSTr64;
Chris Lattnerb6bac512004-02-25 06:13:04 +00001997 addFullAddress(BuildMI(BB, Opcode, 1+4),
1998 BaseReg, Scale, IndexReg, Disp).addReg(ValReg);
Chris Lattner5a830962004-02-25 02:56:58 +00001999 }
Chris Lattner94af4142002-12-25 05:13:53 +00002000 }
Chris Lattner6fc3c522002-11-17 21:11:55 +00002001}
2002
2003
Brian Gaekec11232a2002-11-26 10:43:30 +00002004/// visitCastInst - Here we have various kinds of copying with or without
2005/// sign extension going on.
Chris Lattner3e130a22003-01-13 00:32:26 +00002006void ISel::visitCastInst(CastInst &CI) {
Chris Lattnerf5854472003-06-21 16:01:24 +00002007 Value *Op = CI.getOperand(0);
2008 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2009 // of the case are GEP instructions, then the cast does not need to be
2010 // generated explicitly, it will be folded into the GEP.
2011 if (CI.getType() == Type::LongTy &&
2012 (Op->getType() == Type::IntTy || Op->getType() == Type::UIntTy)) {
2013 bool AllUsesAreGEPs = true;
2014 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2015 if (!isa<GetElementPtrInst>(*I)) {
2016 AllUsesAreGEPs = false;
2017 break;
2018 }
2019
2020 // No need to codegen this cast if all users are getelementptr instrs...
2021 if (AllUsesAreGEPs) return;
2022 }
2023
Chris Lattner548f61d2003-04-23 17:22:12 +00002024 unsigned DestReg = getReg(CI);
2025 MachineBasicBlock::iterator MI = BB->end();
Chris Lattnerf5854472003-06-21 16:01:24 +00002026 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
Chris Lattner548f61d2003-04-23 17:22:12 +00002027}
2028
2029/// emitCastOperation - Common code shared between visitCastInst and
2030/// constant expression cast support.
2031void ISel::emitCastOperation(MachineBasicBlock *BB,
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002032 MachineBasicBlock::iterator IP,
Chris Lattner548f61d2003-04-23 17:22:12 +00002033 Value *Src, const Type *DestTy,
2034 unsigned DestReg) {
Chris Lattner3907d112003-04-23 17:57:48 +00002035 unsigned SrcReg = getReg(Src, BB, IP);
Chris Lattner3e130a22003-01-13 00:32:26 +00002036 const Type *SrcTy = Src->getType();
2037 unsigned SrcClass = getClassB(SrcTy);
Chris Lattner3e130a22003-01-13 00:32:26 +00002038 unsigned DestClass = getClassB(DestTy);
Chris Lattner7d255892002-12-13 11:31:59 +00002039
Chris Lattner3e130a22003-01-13 00:32:26 +00002040 // Implement casts to bool by using compare on the operand followed by set if
2041 // not zero on the result.
2042 if (DestTy == Type::BoolTy) {
Chris Lattner20772542003-06-01 03:38:24 +00002043 switch (SrcClass) {
2044 case cByte:
2045 BMI(BB, IP, X86::TESTrr8, 2).addReg(SrcReg).addReg(SrcReg);
2046 break;
2047 case cShort:
2048 BMI(BB, IP, X86::TESTrr16, 2).addReg(SrcReg).addReg(SrcReg);
2049 break;
2050 case cInt:
2051 BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg).addReg(SrcReg);
2052 break;
2053 case cLong: {
2054 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2055 BMI(BB, IP, X86::ORrr32, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
2056 break;
2057 }
2058 case cFP:
Chris Lattner311ca2e2004-02-23 03:21:41 +00002059 BMI(BB, IP, X86::FTST, 1).addReg(SrcReg);
2060 BMI(BB, IP, X86::FNSTSWr8, 0);
2061 BMI(BB, IP, X86::SAHF, 1);
2062 break;
Chris Lattner20772542003-06-01 03:38:24 +00002063 }
2064
2065 // If the zero flag is not set, then the value is true, set the byte to
2066 // true.
Chris Lattner548f61d2003-04-23 17:22:12 +00002067 BMI(BB, IP, X86::SETNEr, 1, DestReg);
Chris Lattner94af4142002-12-25 05:13:53 +00002068 return;
2069 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002070
2071 static const unsigned RegRegMove[] = {
2072 X86::MOVrr8, X86::MOVrr16, X86::MOVrr32, X86::FpMOV, X86::MOVrr32
2073 };
2074
2075 // Implement casts between values of the same type class (as determined by
2076 // getClass) by using a register-to-register move.
2077 if (SrcClass == DestClass) {
2078 if (SrcClass <= cInt || (SrcClass == cFP && SrcTy == DestTy)) {
Chris Lattner548f61d2003-04-23 17:22:12 +00002079 BMI(BB, IP, RegRegMove[SrcClass], 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002080 } else if (SrcClass == cFP) {
2081 if (SrcTy == Type::FloatTy) { // double -> float
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002082 assert(DestTy == Type::DoubleTy && "Unknown cFP member!");
2083 BMI(BB, IP, X86::FpMOV, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002084 } else { // float -> double
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002085 assert(SrcTy == Type::DoubleTy && DestTy == Type::FloatTy &&
2086 "Unknown cFP member!");
2087 // Truncate from double to float by storing to memory as short, then
2088 // reading it back.
2089 unsigned FltAlign = TM.getTargetData().getFloatAlignment();
Chris Lattner3e130a22003-01-13 00:32:26 +00002090 int FrameIdx = F->getFrameInfo()->CreateStackObject(4, FltAlign);
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002091 addFrameReference(BMI(BB, IP, X86::FSTr32, 5), FrameIdx).addReg(SrcReg);
2092 addFrameReference(BMI(BB, IP, X86::FLDr32, 5, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002093 }
2094 } else if (SrcClass == cLong) {
Chris Lattner548f61d2003-04-23 17:22:12 +00002095 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
2096 BMI(BB, IP, X86::MOVrr32, 1, DestReg+1).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002097 } else {
Chris Lattnerc53544a2003-05-12 20:16:58 +00002098 assert(0 && "Cannot handle this type of cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002099 abort();
Brian Gaeked474e9c2002-12-06 10:49:33 +00002100 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002101 return;
2102 }
2103
2104 // Handle cast of SMALLER int to LARGER int using a move with sign extension
2105 // or zero extension, depending on whether the source type was signed.
2106 if (SrcClass <= cInt && (DestClass <= cInt || DestClass == cLong) &&
2107 SrcClass < DestClass) {
2108 bool isLong = DestClass == cLong;
2109 if (isLong) DestClass = cInt;
2110
2111 static const unsigned Opc[][4] = {
2112 { X86::MOVSXr16r8, X86::MOVSXr32r8, X86::MOVSXr32r16, X86::MOVrr32 }, // s
2113 { X86::MOVZXr16r8, X86::MOVZXr32r8, X86::MOVZXr32r16, X86::MOVrr32 } // u
2114 };
2115
2116 bool isUnsigned = SrcTy->isUnsigned();
Chris Lattner548f61d2003-04-23 17:22:12 +00002117 BMI(BB, IP, Opc[isUnsigned][SrcClass + DestClass - 1], 1,
2118 DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002119
2120 if (isLong) { // Handle upper 32 bits as appropriate...
2121 if (isUnsigned) // Zero out top bits...
Chris Lattner6e173a02004-02-17 06:16:44 +00002122 BMI(BB, IP, X86::MOVri32, 1, DestReg+1).addZImm(0);
Chris Lattner3e130a22003-01-13 00:32:26 +00002123 else // Sign extend bottom half...
Chris Lattner7ddc3fb2004-02-17 06:24:02 +00002124 BMI(BB, IP, X86::SARri32, 2, DestReg+1).addReg(DestReg).addZImm(31);
Brian Gaeked474e9c2002-12-06 10:49:33 +00002125 }
Chris Lattner3e130a22003-01-13 00:32:26 +00002126 return;
2127 }
2128
2129 // Special case long -> int ...
2130 if (SrcClass == cLong && DestClass == cInt) {
Chris Lattner548f61d2003-04-23 17:22:12 +00002131 BMI(BB, IP, X86::MOVrr32, 1, DestReg).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002132 return;
2133 }
2134
2135 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by a
2136 // move out of AX or AL.
2137 if ((SrcClass <= cInt || SrcClass == cLong) && DestClass <= cInt
2138 && SrcClass > DestClass) {
2139 static const unsigned AReg[] = { X86::AL, X86::AX, X86::EAX, 0, X86::EAX };
Chris Lattner548f61d2003-04-23 17:22:12 +00002140 BMI(BB, IP, RegRegMove[SrcClass], 1, AReg[SrcClass]).addReg(SrcReg);
2141 BMI(BB, IP, RegRegMove[DestClass], 1, DestReg).addReg(AReg[DestClass]);
Chris Lattner3e130a22003-01-13 00:32:26 +00002142 return;
2143 }
2144
2145 // Handle casts from integer to floating point now...
2146 if (DestClass == cFP) {
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002147 // Promote the integer to a type supported by FLD. We do this because there
2148 // are no unsigned FLD instructions, so we must promote an unsigned value to
2149 // a larger signed value, then use FLD on the larger value.
2150 //
2151 const Type *PromoteType = 0;
2152 unsigned PromoteOpcode;
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002153 unsigned RealDestReg = DestReg;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002154 switch (SrcTy->getPrimitiveID()) {
2155 case Type::BoolTyID:
2156 case Type::SByteTyID:
2157 // We don't have the facilities for directly loading byte sized data from
2158 // memory (even signed). Promote it to 16 bits.
2159 PromoteType = Type::ShortTy;
2160 PromoteOpcode = X86::MOVSXr16r8;
2161 break;
2162 case Type::UByteTyID:
2163 PromoteType = Type::ShortTy;
2164 PromoteOpcode = X86::MOVZXr16r8;
2165 break;
2166 case Type::UShortTyID:
2167 PromoteType = Type::IntTy;
2168 PromoteOpcode = X86::MOVZXr32r16;
2169 break;
2170 case Type::UIntTyID: {
2171 // Make a 64 bit temporary... and zero out the top of it...
2172 unsigned TmpReg = makeAnotherReg(Type::LongTy);
2173 BMI(BB, IP, X86::MOVrr32, 1, TmpReg).addReg(SrcReg);
Chris Lattner6e173a02004-02-17 06:16:44 +00002174 BMI(BB, IP, X86::MOVri32, 1, TmpReg+1).addZImm(0);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002175 SrcTy = Type::LongTy;
2176 SrcClass = cLong;
2177 SrcReg = TmpReg;
2178 break;
2179 }
2180 case Type::ULongTyID:
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002181 // Don't fild into the read destination.
2182 DestReg = makeAnotherReg(Type::DoubleTy);
2183 break;
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002184 default: // No promotion needed...
2185 break;
2186 }
2187
2188 if (PromoteType) {
2189 unsigned TmpReg = makeAnotherReg(PromoteType);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002190 unsigned Opc = SrcTy->isSigned() ? X86::MOVSXr16r8 : X86::MOVZXr16r8;
2191 BMI(BB, IP, Opc, 1, TmpReg).addReg(SrcReg);
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002192 SrcTy = PromoteType;
2193 SrcClass = getClass(PromoteType);
Chris Lattner3e130a22003-01-13 00:32:26 +00002194 SrcReg = TmpReg;
2195 }
2196
2197 // Spill the integer to memory and reload it from there...
Chris Lattnerb6bac512004-02-25 06:13:04 +00002198 int FrameIdx =
2199 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
Chris Lattner3e130a22003-01-13 00:32:26 +00002200
2201 if (SrcClass == cLong) {
Chris Lattnere87331d2004-02-17 06:28:19 +00002202 addFrameReference(BMI(BB, IP, X86::MOVmr32, 5), FrameIdx).addReg(SrcReg);
2203 addFrameReference(BMI(BB, IP, X86::MOVmr32, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002204 FrameIdx, 4).addReg(SrcReg+1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002205 } else {
Chris Lattnere87331d2004-02-17 06:28:19 +00002206 static const unsigned Op1[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002207 addFrameReference(BMI(BB, IP, Op1[SrcClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002208 }
2209
2210 static const unsigned Op2[] =
Chris Lattner4d5a50a2003-05-12 20:36:13 +00002211 { 0/*byte*/, X86::FILDr16, X86::FILDr32, 0/*FP*/, X86::FILDr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002212 addFrameReference(BMI(BB, IP, Op2[SrcClass], 5, DestReg), FrameIdx);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002213
2214 // We need special handling for unsigned 64-bit integer sources. If the
2215 // input number has the "sign bit" set, then we loaded it incorrectly as a
2216 // negative 64-bit number. In this case, add an offset value.
2217 if (SrcTy == Type::ULongTy) {
2218 // Emit a test instruction to see if the dynamic input value was signed.
2219 BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg+1).addReg(SrcReg+1);
2220
Chris Lattnerb6bac512004-02-25 06:13:04 +00002221 // If the sign bit is set, get a pointer to an offset, otherwise get a
2222 // pointer to a zero.
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002223 MachineConstantPool *CP = F->getConstantPool();
2224 unsigned Zero = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002225 Constant *Null = Constant::getNullValue(Type::UIntTy);
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002226 addConstantPoolReference(BMI(BB, IP, X86::LEAr32, 5, Zero),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002227 CP->getConstantPoolIndex(Null));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002228 unsigned Offset = makeAnotherReg(Type::IntTy);
Chris Lattnerb6bac512004-02-25 06:13:04 +00002229 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
2230
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002231 addConstantPoolReference(BMI(BB, IP, X86::LEAr32, 5, Offset),
Chris Lattnerb6bac512004-02-25 06:13:04 +00002232 CP->getConstantPoolIndex(OffsetCst));
Chris Lattnerbaa58a52004-02-23 03:10:10 +00002233 unsigned Addr = makeAnotherReg(Type::IntTy);
2234 BMI(BB, IP, X86::CMOVSrr32, 2, Addr).addReg(Zero).addReg(Offset);
2235
2236 // Load the constant for an add. FIXME: this could make an 'fadd' that
2237 // reads directly from memory, but we don't support these yet.
2238 unsigned ConstReg = makeAnotherReg(Type::DoubleTy);
2239 addDirectMem(BMI(BB, IP, X86::FLDr32, 4, ConstReg), Addr);
2240
2241 BMI(BB, IP, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(DestReg);
2242 }
2243
Chris Lattner3e130a22003-01-13 00:32:26 +00002244 return;
2245 }
2246
2247 // Handle casts from floating point to integer now...
2248 if (SrcClass == cFP) {
2249 // Change the floating point control register to use "round towards zero"
2250 // mode when truncating to an integer value.
2251 //
2252 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Chris Lattner548f61d2003-04-23 17:22:12 +00002253 addFrameReference(BMI(BB, IP, X86::FNSTCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002254
2255 // Load the old value of the high byte of the control word...
2256 unsigned HighPartOfCW = makeAnotherReg(Type::UByteTy);
Chris Lattnere87331d2004-02-17 06:28:19 +00002257 addFrameReference(BMI(BB, IP, X86::MOVrm8, 4, HighPartOfCW), CWFrameIdx, 1);
Chris Lattner3e130a22003-01-13 00:32:26 +00002258
2259 // Set the high part to be round to zero...
Chris Lattner6e173a02004-02-17 06:16:44 +00002260 addFrameReference(BMI(BB, IP, X86::MOVmi8, 5), CWFrameIdx, 1).addZImm(12);
Chris Lattner3e130a22003-01-13 00:32:26 +00002261
2262 // Reload the modified control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00002263 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002264
2265 // Restore the memory image of control word to original value
Chris Lattnere87331d2004-02-17 06:28:19 +00002266 addFrameReference(BMI(BB, IP, X86::MOVmr8, 5),
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002267 CWFrameIdx, 1).addReg(HighPartOfCW);
Chris Lattner3e130a22003-01-13 00:32:26 +00002268
2269 // We don't have the facilities for directly storing byte sized data to
2270 // memory. Promote it to 16 bits. We also must promote unsigned values to
2271 // larger classes because we only have signed FP stores.
2272 unsigned StoreClass = DestClass;
2273 const Type *StoreTy = DestTy;
2274 if (StoreClass == cByte || DestTy->isUnsigned())
2275 switch (StoreClass) {
2276 case cByte: StoreTy = Type::ShortTy; StoreClass = cShort; break;
2277 case cShort: StoreTy = Type::IntTy; StoreClass = cInt; break;
2278 case cInt: StoreTy = Type::LongTy; StoreClass = cLong; break;
Brian Gaeked4615052003-07-18 20:23:43 +00002279 // The following treatment of cLong may not be perfectly right,
2280 // but it survives chains of casts of the form
2281 // double->ulong->double.
2282 case cLong: StoreTy = Type::LongTy; StoreClass = cLong; break;
Chris Lattner3e130a22003-01-13 00:32:26 +00002283 default: assert(0 && "Unknown store class!");
2284 }
2285
2286 // Spill the integer to memory and reload it from there...
2287 int FrameIdx =
2288 F->getFrameInfo()->CreateStackObject(StoreTy, TM.getTargetData());
2289
2290 static const unsigned Op1[] =
2291 { 0, X86::FISTr16, X86::FISTr32, 0, X86::FISTPr64 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002292 addFrameReference(BMI(BB, IP, Op1[StoreClass], 5), FrameIdx).addReg(SrcReg);
Chris Lattner3e130a22003-01-13 00:32:26 +00002293
2294 if (DestClass == cLong) {
Chris Lattnere87331d2004-02-17 06:28:19 +00002295 addFrameReference(BMI(BB, IP, X86::MOVrm32, 4, DestReg), FrameIdx);
2296 addFrameReference(BMI(BB, IP, X86::MOVrm32, 4, DestReg+1), FrameIdx, 4);
Chris Lattner3e130a22003-01-13 00:32:26 +00002297 } else {
Chris Lattnere87331d2004-02-17 06:28:19 +00002298 static const unsigned Op2[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
Chris Lattner548f61d2003-04-23 17:22:12 +00002299 addFrameReference(BMI(BB, IP, Op2[DestClass], 4, DestReg), FrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002300 }
2301
2302 // Reload the original control word now...
Chris Lattner548f61d2003-04-23 17:22:12 +00002303 addFrameReference(BMI(BB, IP, X86::FLDCWm16, 4), CWFrameIdx);
Chris Lattner3e130a22003-01-13 00:32:26 +00002304 return;
2305 }
2306
Brian Gaeked474e9c2002-12-06 10:49:33 +00002307 // Anything we haven't handled already, we can't (yet) handle at all.
Chris Lattnerc53544a2003-05-12 20:16:58 +00002308 assert(0 && "Unhandled cast instruction!");
Chris Lattner548f61d2003-04-23 17:22:12 +00002309 abort();
Brian Gaekefa8d5712002-11-22 11:07:01 +00002310}
Brian Gaekea1719c92002-10-31 23:03:59 +00002311
Chris Lattner73815062003-10-18 05:56:40 +00002312/// visitVANextInst - Implement the va_next instruction...
Chris Lattnereca195e2003-05-08 19:44:13 +00002313///
Chris Lattner73815062003-10-18 05:56:40 +00002314void ISel::visitVANextInst(VANextInst &I) {
2315 unsigned VAList = getReg(I.getOperand(0));
Chris Lattnereca195e2003-05-08 19:44:13 +00002316 unsigned DestReg = getReg(I);
2317
Chris Lattnereca195e2003-05-08 19:44:13 +00002318 unsigned Size;
Chris Lattner73815062003-10-18 05:56:40 +00002319 switch (I.getArgType()->getPrimitiveID()) {
Chris Lattnereca195e2003-05-08 19:44:13 +00002320 default:
2321 std::cerr << I;
Chris Lattner73815062003-10-18 05:56:40 +00002322 assert(0 && "Error: bad type for va_next instruction!");
Chris Lattnereca195e2003-05-08 19:44:13 +00002323 return;
2324 case Type::PointerTyID:
2325 case Type::UIntTyID:
2326 case Type::IntTyID:
2327 Size = 4;
Chris Lattnereca195e2003-05-08 19:44:13 +00002328 break;
2329 case Type::ULongTyID:
2330 case Type::LongTyID:
Chris Lattnereca195e2003-05-08 19:44:13 +00002331 case Type::DoubleTyID:
2332 Size = 8;
Chris Lattnereca195e2003-05-08 19:44:13 +00002333 break;
2334 }
2335
2336 // Increment the VAList pointer...
Chris Lattner73815062003-10-18 05:56:40 +00002337 BuildMI(BB, X86::ADDri32, 2, DestReg).addReg(VAList).addZImm(Size);
2338}
Chris Lattnereca195e2003-05-08 19:44:13 +00002339
Chris Lattner73815062003-10-18 05:56:40 +00002340void ISel::visitVAArgInst(VAArgInst &I) {
2341 unsigned VAList = getReg(I.getOperand(0));
2342 unsigned DestReg = getReg(I);
2343
2344 switch (I.getType()->getPrimitiveID()) {
2345 default:
2346 std::cerr << I;
2347 assert(0 && "Error: bad type for va_next instruction!");
2348 return;
2349 case Type::PointerTyID:
2350 case Type::UIntTyID:
2351 case Type::IntTyID:
Chris Lattnere87331d2004-02-17 06:28:19 +00002352 addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), VAList);
Chris Lattner73815062003-10-18 05:56:40 +00002353 break;
2354 case Type::ULongTyID:
2355 case Type::LongTyID:
Chris Lattnere87331d2004-02-17 06:28:19 +00002356 addDirectMem(BuildMI(BB, X86::MOVrm32, 4, DestReg), VAList);
2357 addRegOffset(BuildMI(BB, X86::MOVrm32, 4, DestReg+1), VAList, 4);
Chris Lattner73815062003-10-18 05:56:40 +00002358 break;
2359 case Type::DoubleTyID:
2360 addDirectMem(BuildMI(BB, X86::FLDr64, 4, DestReg), VAList);
2361 break;
2362 }
Chris Lattnereca195e2003-05-08 19:44:13 +00002363}
2364
2365
Chris Lattner3e130a22003-01-13 00:32:26 +00002366void ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00002367 // If this GEP instruction will be folded into all of its users, we don't need
2368 // to explicitly calculate it!
2369 unsigned A, B, C, D;
2370 if (isGEPFoldable(0, I.getOperand(0), I.op_begin()+1, I.op_end(), A,B,C,D)) {
2371 // Check all of the users of the instruction to see if they are loads and
2372 // stores.
2373 bool AllWillFold = true;
2374 for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E; ++UI)
2375 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Load)
2376 if (cast<Instruction>(*UI)->getOpcode() != Instruction::Store ||
2377 cast<Instruction>(*UI)->getOperand(0) == &I) {
2378 AllWillFold = false;
2379 break;
2380 }
2381
2382 // If the instruction is foldable, and will be folded into all users, don't
2383 // emit it!
2384 if (AllWillFold) return;
2385 }
2386
Chris Lattner3e130a22003-01-13 00:32:26 +00002387 unsigned outputReg = getReg(I);
Chris Lattner827832c2004-02-22 17:05:38 +00002388 emitGEPOperation(BB, BB->end(), I.getOperand(0),
Brian Gaeke68b1edc2002-12-16 04:23:29 +00002389 I.op_begin()+1, I.op_end(), outputReg);
Chris Lattnerc0812d82002-12-13 06:56:29 +00002390}
2391
Chris Lattner985fe3d2004-02-25 03:45:50 +00002392/// getGEPIndex - Inspect the getelementptr operands specified with GEPOps and
2393/// GEPTypes (the derived types being stepped through at each level). On return
2394/// from this function, if some indexes of the instruction are representable as
2395/// an X86 lea instruction, the machine operands are put into the Ops
2396/// instruction and the consumed indexes are poped from the GEPOps/GEPTypes
2397/// lists. Otherwise, GEPOps.size() is returned. If this returns a an
2398/// addressing mode that only partially consumes the input, the BaseReg input of
2399/// the addressing mode must be left free.
2400///
2401/// Note that there is one fewer entry in GEPTypes than there is in GEPOps.
2402///
Chris Lattnerb6bac512004-02-25 06:13:04 +00002403void ISel::getGEPIndex(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
2404 std::vector<Value*> &GEPOps,
2405 std::vector<const Type*> &GEPTypes, unsigned &BaseReg,
2406 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
2407 const TargetData &TD = TM.getTargetData();
2408
Chris Lattner985fe3d2004-02-25 03:45:50 +00002409 // Clear out the state we are working with...
Chris Lattnerb6bac512004-02-25 06:13:04 +00002410 BaseReg = 0; // No base register
2411 Scale = 1; // Unit scale
2412 IndexReg = 0; // No index register
2413 Disp = 0; // No displacement
2414
Chris Lattner985fe3d2004-02-25 03:45:50 +00002415 // While there are GEP indexes that can be folded into the current address,
2416 // keep processing them.
2417 while (!GEPTypes.empty()) {
2418 if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
2419 // It's a struct access. CUI is the index into the structure,
2420 // which names the field. This index must have unsigned type.
2421 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
2422
2423 // Use the TargetData structure to pick out what the layout of the
2424 // structure is in memory. Since the structure index must be constant, we
2425 // can get its value and use it to find the right byte offset from the
2426 // StructLayout class's list of structure member offsets.
Chris Lattnerb6bac512004-02-25 06:13:04 +00002427 Disp += TD.getStructLayout(StTy)->MemberOffsets[CUI->getValue()];
Chris Lattner985fe3d2004-02-25 03:45:50 +00002428 GEPOps.pop_back(); // Consume a GEP operand
2429 GEPTypes.pop_back();
2430 } else {
2431 // It's an array or pointer access: [ArraySize x ElementType].
2432 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
2433 Value *idx = GEPOps.back();
2434
2435 // idx is the index into the array. Unlike with structure
2436 // indices, we may not know its actual value at code-generation
2437 // time.
2438 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
2439
2440 // If idx is a constant, fold it into the offset.
2441 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattnerb6bac512004-02-25 06:13:04 +00002442 Disp += TD.getTypeSize(SqTy->getElementType())*CSI->getValue();
Chris Lattner985fe3d2004-02-25 03:45:50 +00002443 } else {
2444 // If we can't handle it, return.
2445 return;
2446 }
2447
2448 GEPOps.pop_back(); // Consume a GEP operand
2449 GEPTypes.pop_back();
2450 }
2451 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00002452
2453 // GEPTypes is empty, which means we have a single operand left. See if we
2454 // can set it as the base register.
2455 //
2456 // FIXME: When addressing modes are more powerful/correct, we could load
2457 // global addresses directly as 32-bit immediates.
2458 assert(BaseReg == 0);
2459 BaseReg = MBB ? getReg(GEPOps[0], MBB, IP) : 0;
2460 GEPOps.pop_back(); // Consume the last GEP operand
Chris Lattner985fe3d2004-02-25 03:45:50 +00002461}
2462
2463
Chris Lattnerb6bac512004-02-25 06:13:04 +00002464/// isGEPFoldable - Return true if the specified GEP can be completely
2465/// folded into the addressing mode of a load/store or lea instruction.
2466bool ISel::isGEPFoldable(MachineBasicBlock *MBB,
2467 Value *Src, User::op_iterator IdxBegin,
2468 User::op_iterator IdxEnd, unsigned &BaseReg,
2469 unsigned &Scale, unsigned &IndexReg, unsigned &Disp) {
Chris Lattner7ca04092004-02-22 17:35:42 +00002470 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
2471 Src = CPR->getValue();
2472
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002473 std::vector<Value*> GEPOps;
2474 GEPOps.resize(IdxEnd-IdxBegin+1);
2475 GEPOps[0] = Src;
2476 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
2477
2478 std::vector<const Type*> GEPTypes;
2479 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
2480 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
2481
Chris Lattnerb6bac512004-02-25 06:13:04 +00002482 MachineBasicBlock::iterator IP;
2483 if (MBB) IP = MBB->end();
2484 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
2485
2486 // We can fold it away iff the getGEPIndex call eliminated all operands.
2487 return GEPOps.empty();
2488}
2489
2490void ISel::emitGEPOperation(MachineBasicBlock *MBB,
2491 MachineBasicBlock::iterator IP,
2492 Value *Src, User::op_iterator IdxBegin,
2493 User::op_iterator IdxEnd, unsigned TargetReg) {
2494 const TargetData &TD = TM.getTargetData();
2495 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Src))
2496 Src = CPR->getValue();
2497
2498 std::vector<Value*> GEPOps;
2499 GEPOps.resize(IdxEnd-IdxBegin+1);
2500 GEPOps[0] = Src;
2501 std::copy(IdxBegin, IdxEnd, GEPOps.begin()+1);
2502
2503 std::vector<const Type*> GEPTypes;
2504 GEPTypes.assign(gep_type_begin(Src->getType(), IdxBegin, IdxEnd),
2505 gep_type_end(Src->getType(), IdxBegin, IdxEnd));
Chris Lattner985fe3d2004-02-25 03:45:50 +00002506
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002507 // Keep emitting instructions until we consume the entire GEP instruction.
2508 while (!GEPOps.empty()) {
2509 unsigned OldSize = GEPOps.size();
Chris Lattnerb6bac512004-02-25 06:13:04 +00002510 unsigned BaseReg, Scale, IndexReg, Disp;
2511 getGEPIndex(MBB, IP, GEPOps, GEPTypes, BaseReg, Scale, IndexReg, Disp);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002512
Chris Lattner985fe3d2004-02-25 03:45:50 +00002513 if (GEPOps.size() != OldSize) {
2514 // getGEPIndex consumed some of the input. Build an LEA instruction here.
Chris Lattnerb6bac512004-02-25 06:13:04 +00002515 unsigned NextTarget = 0;
2516 if (!GEPOps.empty()) {
2517 assert(BaseReg == 0 &&
2518 "getGEPIndex should have left the base register open for chaining!");
2519 NextTarget = BaseReg = makeAnotherReg(Type::UIntTy);
Chris Lattner985fe3d2004-02-25 03:45:50 +00002520 }
Chris Lattnerb6bac512004-02-25 06:13:04 +00002521
2522 if (IndexReg == 0 && Disp == 0)
2523 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg);
2524 else
2525 addFullAddress(BMI(MBB, IP, X86::LEAr32, 5, TargetReg),
2526 BaseReg, Scale, IndexReg, Disp);
2527 --IP;
2528 TargetReg = NextTarget;
Chris Lattner985fe3d2004-02-25 03:45:50 +00002529 } else if (GEPTypes.empty()) {
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002530 // The getGEPIndex operation didn't want to build an LEA. Check to see if
2531 // all operands are consumed but the base pointer. If so, just load it
2532 // into the register.
Chris Lattner7ca04092004-02-22 17:35:42 +00002533 if (GlobalValue *GV = dyn_cast<GlobalValue>(GEPOps[0])) {
2534 BMI(MBB, IP, X86::MOVri32, 1, TargetReg).addGlobalAddress(GV);
2535 } else {
2536 unsigned BaseReg = getReg(GEPOps[0], MBB, IP);
2537 BMI(MBB, IP, X86::MOVrr32, 1, TargetReg).addReg(BaseReg);
2538 }
2539 break; // we are now done
Chris Lattnerb6bac512004-02-25 06:13:04 +00002540
2541 } else if (const StructType *StTy = dyn_cast<StructType>(GEPTypes.back())) {
2542 // It's a struct access. CUI is the index into the structure,
2543 // which names the field. This index must have unsigned type.
2544 const ConstantUInt *CUI = cast<ConstantUInt>(GEPOps.back());
2545 GEPOps.pop_back(); // Consume a GEP operand
2546 GEPTypes.pop_back();
2547
2548 // Use the TargetData structure to pick out what the layout of the
2549 // structure is in memory. Since the structure index must be constant, we
2550 // can get its value and use it to find the right byte offset from the
2551 // StructLayout class's list of structure member offsets.
2552 unsigned idxValue = CUI->getValue();
2553 unsigned FieldOff = TD.getStructLayout(StTy)->MemberOffsets[idxValue];
2554 if (FieldOff) {
2555 unsigned Reg = makeAnotherReg(Type::UIntTy);
2556 // Emit an ADD to add FieldOff to the basePtr.
2557 BMI(MBB, IP, X86::ADDri32, 2, TargetReg).addReg(Reg).addZImm(FieldOff);
2558 --IP; // Insert the next instruction before this one.
2559 TargetReg = Reg; // Codegen the rest of the GEP into this
2560 }
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002561 } else {
Brian Gaeke20244b72002-12-12 15:33:40 +00002562 // It's an array or pointer access: [ArraySize x ElementType].
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002563 const SequentialType *SqTy = cast<SequentialType>(GEPTypes.back());
2564 Value *idx = GEPOps.back();
2565 GEPOps.pop_back(); // Consume a GEP operand
2566 GEPTypes.pop_back();
Chris Lattner8a307e82002-12-16 19:32:50 +00002567
Brian Gaeke20244b72002-12-12 15:33:40 +00002568 // idx is the index into the array. Unlike with structure
2569 // indices, we may not know its actual value at code-generation
2570 // time.
Chris Lattner8a307e82002-12-16 19:32:50 +00002571 assert(idx->getType() == Type::LongTy && "Bad GEP array index!");
2572
Chris Lattnerf5854472003-06-21 16:01:24 +00002573 // Most GEP instructions use a [cast (int/uint) to LongTy] as their
2574 // operand on X86. Handle this case directly now...
2575 if (CastInst *CI = dyn_cast<CastInst>(idx))
2576 if (CI->getOperand(0)->getType() == Type::IntTy ||
2577 CI->getOperand(0)->getType() == Type::UIntTy)
2578 idx = CI->getOperand(0);
2579
Chris Lattner3e130a22003-01-13 00:32:26 +00002580 // We want to add BaseReg to(idxReg * sizeof ElementType). First, we
Chris Lattner8a307e82002-12-16 19:32:50 +00002581 // must find the size of the pointed-to type (Not coincidentally, the next
2582 // type is the type of the elements in the array).
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002583 const Type *ElTy = SqTy->getElementType();
2584 unsigned elementSize = TD.getTypeSize(ElTy);
Chris Lattner8a307e82002-12-16 19:32:50 +00002585
2586 // If idxReg is a constant, we don't need to perform the multiply!
2587 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(idx)) {
Chris Lattner3e130a22003-01-13 00:32:26 +00002588 if (!CSI->isNullValue()) {
Chris Lattner8a307e82002-12-16 19:32:50 +00002589 unsigned Offset = elementSize*CSI->getValue();
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002590 unsigned Reg = makeAnotherReg(Type::UIntTy);
2591 BMI(MBB, IP, X86::ADDri32, 2, TargetReg).addReg(Reg).addZImm(Offset);
2592 --IP; // Insert the next instruction before this one.
2593 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002594 }
2595 } else if (elementSize == 1) {
2596 // If the element size is 1, we don't have to multiply, just add
2597 unsigned idxReg = getReg(idx, MBB, IP);
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002598 unsigned Reg = makeAnotherReg(Type::UIntTy);
2599 BMI(MBB, IP, X86::ADDrr32, 2, TargetReg).addReg(Reg).addReg(idxReg);
2600 --IP; // Insert the next instruction before this one.
2601 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002602 } else {
2603 unsigned idxReg = getReg(idx, MBB, IP);
2604 unsigned OffsetReg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002605
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002606 // Make sure we can back the iterator up to point to the first
2607 // instruction emitted.
2608 MachineBasicBlock::iterator BeforeIt = IP;
2609 if (IP == MBB->begin())
2610 BeforeIt = MBB->end();
2611 else
2612 --BeforeIt;
Chris Lattnerb2acc512003-10-19 21:09:10 +00002613 doMultiplyConst(MBB, IP, OffsetReg, Type::IntTy, idxReg, elementSize);
2614
Chris Lattner8a307e82002-12-16 19:32:50 +00002615 // Emit an ADD to add OffsetReg to the basePtr.
Chris Lattner3f1e8e72004-02-22 07:04:00 +00002616 unsigned Reg = makeAnotherReg(Type::UIntTy);
2617 BMI(MBB, IP, X86::ADDrr32, 2, TargetReg).addReg(Reg).addReg(OffsetReg);
2618
2619 // Step to the first instruction of the multiply.
2620 if (BeforeIt == MBB->end())
2621 IP = MBB->begin();
2622 else
2623 IP = ++BeforeIt;
2624
2625 TargetReg = Reg; // Codegen the rest of the GEP into this
Chris Lattner8a307e82002-12-16 19:32:50 +00002626 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002627 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002628 }
Brian Gaeke20244b72002-12-12 15:33:40 +00002629}
2630
2631
Chris Lattner065faeb2002-12-28 20:24:02 +00002632/// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2633/// frame manager, otherwise do it the hard way.
2634///
2635void ISel::visitAllocaInst(AllocaInst &I) {
Brian Gaekee48ec012002-12-13 06:46:31 +00002636 // Find the data size of the alloca inst's getAllocatedType.
Chris Lattner065faeb2002-12-28 20:24:02 +00002637 const Type *Ty = I.getAllocatedType();
2638 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2639
2640 // If this is a fixed size alloca in the entry block for the function,
2641 // statically stack allocate the space.
2642 //
2643 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(I.getArraySize())) {
2644 if (I.getParent() == I.getParent()->getParent()->begin()) {
2645 TySize *= CUI->getValue(); // Get total allocated size...
2646 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
2647
2648 // Create a new stack object using the frame manager...
2649 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
2650 addFrameReference(BuildMI(BB, X86::LEAr32, 5, getReg(I)), FrameIdx);
2651 return;
2652 }
2653 }
2654
2655 // Create a register to hold the temporary result of multiplying the type size
2656 // constant by the variable amount.
2657 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2658 unsigned SrcReg1 = getReg(I.getArraySize());
Chris Lattner065faeb2002-12-28 20:24:02 +00002659
2660 // TotalSizeReg = mul <numelements>, <TypeSize>
2661 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002662 doMultiplyConst(BB, MBBI, TotalSizeReg, Type::UIntTy, SrcReg1, TySize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002663
2664 // AddedSize = add <TotalSizeReg>, 15
2665 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2666 BuildMI(BB, X86::ADDri32, 2, AddedSizeReg).addReg(TotalSizeReg).addZImm(15);
2667
2668 // AlignedSize = and <AddedSize>, ~15
2669 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2670 BuildMI(BB, X86::ANDri32, 2, AlignedSize).addReg(AddedSizeReg).addZImm(~15);
2671
Brian Gaekee48ec012002-12-13 06:46:31 +00002672 // Subtract size from stack pointer, thereby allocating some space.
Chris Lattner3e130a22003-01-13 00:32:26 +00002673 BuildMI(BB, X86::SUBrr32, 2, X86::ESP).addReg(X86::ESP).addReg(AlignedSize);
Chris Lattner065faeb2002-12-28 20:24:02 +00002674
Brian Gaekee48ec012002-12-13 06:46:31 +00002675 // Put a pointer to the space into the result register, by copying
2676 // the stack pointer.
Chris Lattner065faeb2002-12-28 20:24:02 +00002677 BuildMI(BB, X86::MOVrr32, 1, getReg(I)).addReg(X86::ESP);
2678
Misha Brukman48196b32003-05-03 02:18:17 +00002679 // Inform the Frame Information that we have just allocated a variable-sized
Chris Lattner065faeb2002-12-28 20:24:02 +00002680 // object.
2681 F->getFrameInfo()->CreateVariableSizedObject();
Brian Gaeke20244b72002-12-12 15:33:40 +00002682}
Chris Lattner3e130a22003-01-13 00:32:26 +00002683
2684/// visitMallocInst - Malloc instructions are code generated into direct calls
2685/// to the library malloc.
2686///
2687void ISel::visitMallocInst(MallocInst &I) {
2688 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2689 unsigned Arg;
2690
2691 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2692 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2693 } else {
2694 Arg = makeAnotherReg(Type::UIntTy);
Chris Lattnerb2acc512003-10-19 21:09:10 +00002695 unsigned Op0Reg = getReg(I.getOperand(0));
Chris Lattner3e130a22003-01-13 00:32:26 +00002696 MachineBasicBlock::iterator MBBI = BB->end();
Chris Lattnerb2acc512003-10-19 21:09:10 +00002697 doMultiplyConst(BB, MBBI, Arg, Type::UIntTy, Op0Reg, AllocSize);
Chris Lattner3e130a22003-01-13 00:32:26 +00002698 }
2699
2700 std::vector<ValueRecord> Args;
2701 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2702 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002703 1).addExternalSymbol("malloc", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002704 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args);
2705}
2706
2707
2708/// visitFreeInst - Free instructions are code gen'd to call the free libc
2709/// function.
2710///
2711void ISel::visitFreeInst(FreeInst &I) {
2712 std::vector<ValueRecord> Args;
Chris Lattner5e2cb8b2003-08-04 02:12:48 +00002713 Args.push_back(ValueRecord(I.getOperand(0)));
Chris Lattner3e130a22003-01-13 00:32:26 +00002714 MachineInstr *TheCall = BuildMI(X86::CALLpcrel32,
Misha Brukmanc8893fc2003-10-23 16:22:08 +00002715 1).addExternalSymbol("free", true);
Chris Lattner3e130a22003-01-13 00:32:26 +00002716 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args);
2717}
2718
Chris Lattnerd281de22003-07-26 23:49:58 +00002719/// createX86SimpleInstructionSelector - This pass converts an LLVM function
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00002720/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +00002721/// generated code sucks but the implementation is nice and simple.
2722///
Chris Lattnerf70e0c22003-12-28 21:23:38 +00002723FunctionPass *llvm::createX86SimpleInstructionSelector(TargetMachine &TM) {
2724 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +00002725}