blob: 44705586da935807ca83473a7f2a35f1a2d05229 [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for x86 ------===//
2//
3// This file defines a simple peephole instruction selector for the x86 platform
4//
5//===----------------------------------------------------------------------===//
6
7#include "X86.h"
Chris Lattner055c9652002-10-29 21:05:24 +00008#include "X86InstrInfo.h"
Chris Lattner72614082002-10-25 22:55:53 +00009#include "llvm/Function.h"
10#include "llvm/iTerminators.h"
Brian Gaekea1719c92002-10-31 23:03:59 +000011#include "llvm/iOther.h"
Chris Lattner72614082002-10-25 22:55:53 +000012#include "llvm/Type.h"
Chris Lattnerc5291f52002-10-27 21:16:59 +000013#include "llvm/Constants.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000014#include "llvm/Pass.h"
Chris Lattner341a9372002-10-29 17:43:55 +000015#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner72614082002-10-25 22:55:53 +000017#include "llvm/Support/InstVisitor.h"
18#include <map>
19
20namespace {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000021 struct ISel : public FunctionPass, InstVisitor<ISel> {
22 TargetMachine &TM;
Chris Lattner341a9372002-10-29 17:43:55 +000023 MachineFunction *F; // The function we are compiling into
24 MachineBasicBlock *BB; // The current MBB we are compiling
Chris Lattner72614082002-10-25 22:55:53 +000025
26 unsigned CurReg;
27 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
28
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000029 ISel(TargetMachine &tm)
30 : TM(tm), F(0), BB(0), CurReg(MRegisterInfo::FirstVirtualRegister) {}
Chris Lattner72614082002-10-25 22:55:53 +000031
32 /// runOnFunction - Top level implementation of instruction selection for
33 /// the entire function.
34 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000035 bool runOnFunction(Function &Fn) {
Chris Lattner36b36032002-10-29 23:40:58 +000036 F = &MachineFunction::construct(&Fn, TM);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000037 visit(Fn);
Chris Lattner72614082002-10-25 22:55:53 +000038 RegMap.clear();
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000039 F = 0;
Chris Lattner72614082002-10-25 22:55:53 +000040 return false; // We never modify the LLVM itself.
41 }
42
43 /// visitBasicBlock - This method is called when we are visiting a new basic
Chris Lattner33f53b52002-10-29 20:48:56 +000044 /// block. This simply creates a new MachineBasicBlock to emit code into
45 /// and adds it to the current MachineFunction. Subsequent visit* for
46 /// instructions will be invoked for all instructions in the basic block.
Chris Lattner72614082002-10-25 22:55:53 +000047 ///
48 void visitBasicBlock(BasicBlock &LLVM_BB) {
Chris Lattner42c77862002-10-30 00:47:40 +000049 BB = new MachineBasicBlock(&LLVM_BB);
Chris Lattner72614082002-10-25 22:55:53 +000050 // FIXME: Use the auto-insert form when it's available
51 F->getBasicBlockList().push_back(BB);
52 }
53
54 // Visitation methods for various instructions. These methods simply emit
55 // fixed X86 code for each instruction.
56 //
57 void visitReturnInst(ReturnInst &RI);
58 void visitAdd(BinaryOperator &B);
Brian Gaekea1719c92002-10-31 23:03:59 +000059 void visitShiftInst(ShiftInst &I);
Chris Lattner72614082002-10-25 22:55:53 +000060
61 void visitInstruction(Instruction &I) {
62 std::cerr << "Cannot instruction select: " << I;
63 abort();
64 }
65
Chris Lattnerc5291f52002-10-27 21:16:59 +000066
67 /// copyConstantToRegister - Output the instructions required to put the
68 /// specified constant into the specified register.
69 ///
70 void copyConstantToRegister(Constant *C, unsigned Reg);
71
Chris Lattner72614082002-10-25 22:55:53 +000072 /// getReg - This method turns an LLVM value into a register number. This
73 /// is guaranteed to produce the same register number for a particular value
74 /// every time it is queried.
75 ///
76 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
77 unsigned getReg(Value *V) {
78 unsigned &Reg = RegMap[V];
79 if (Reg == 0)
80 Reg = CurReg++;
81
Chris Lattner6f8fd252002-10-27 21:23:43 +000082 // If this operand is a constant, emit the code to copy the constant into
83 // the register here...
84 //
Chris Lattnerc5291f52002-10-27 21:16:59 +000085 if (Constant *C = dyn_cast<Constant>(V))
86 copyConstantToRegister(C, Reg);
87
Chris Lattner72614082002-10-25 22:55:53 +000088 return Reg;
89 }
Chris Lattner72614082002-10-25 22:55:53 +000090 };
91}
92
Chris Lattnerc5291f52002-10-27 21:16:59 +000093
94/// copyConstantToRegister - Output the instructions required to put the
95/// specified constant into the specified register.
96///
97void ISel::copyConstantToRegister(Constant *C, unsigned R) {
98 assert (!isa<ConstantExpr>(C) && "Constant expressions not yet handled!\n");
99
100 switch (C->getType()->getPrimitiveID()) {
101 case Type::SByteTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000102 BuildMI(BB, X86::MOVir8, 1, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000103 break;
104 case Type::UByteTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000105 BuildMI(BB, X86::MOVir8, 1, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000106 break;
107 case Type::ShortTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000108 BuildMI(BB, X86::MOVir16, 1, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000109 break;
110 case Type::UShortTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000111 BuildMI(BB, X86::MOVir16, 1, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000112 break;
113 case Type::IntTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000114 BuildMI(BB, X86::MOVir32, 1, R).addSImm(cast<ConstantSInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000115 break;
116 case Type::UIntTyID:
Chris Lattner8548ee72002-10-30 01:49:01 +0000117 BuildMI(BB, X86::MOVir32, 1, R).addZImm(cast<ConstantUInt>(C)->getValue());
Chris Lattnerc5291f52002-10-27 21:16:59 +0000118 break;
119 default: assert(0 && "Type not handled yet!");
120 }
121}
122
123
Chris Lattner72614082002-10-25 22:55:53 +0000124/// 'ret' instruction - Here we are interested in meeting the x86 ABI. As such,
125/// we have the following possibilities:
126///
127/// ret void: No return value, simply emit a 'ret' instruction
128/// ret sbyte, ubyte : Extend value into EAX and return
129/// ret short, ushort: Extend value into EAX and return
130/// ret int, uint : Move value into EAX and return
131/// ret pointer : Move value into EAX and return
132/// ret long, ulong : Move value into EAX/EDX (?) and return
133/// ret float/double : ? Top of FP stack? XMM0?
134///
135void ISel::visitReturnInst(ReturnInst &I) {
136 if (I.getNumOperands() != 0) { // Not 'ret void'?
137 // Move result into a hard register... then emit a ret
138 visitInstruction(I); // abort
139 }
140
141 // Emit a simple 'ret' instruction... appending it to the end of the basic
142 // block
Chris Lattner341a9372002-10-29 17:43:55 +0000143 BuildMI(BB, X86::RET, 0);
Chris Lattner72614082002-10-25 22:55:53 +0000144}
145
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000146/// SimpleLog2 - Compute and return Log2 of the input, valid only for inputs 1,
147/// 2, 4, & 8. Used to convert operand size into dense classes.
148///
149static inline unsigned SimpleLog2(unsigned N) {
150 switch (N) {
151 case 1: return 0;
152 case 2: return 1;
153 case 4: return 2;
154 case 8: return 3;
155 default: assert(0 && "Invalid operand to SimpleLog2!");
156 }
157 return 0; // not reached
158}
159
Brian Gaekea1719c92002-10-31 23:03:59 +0000160/// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
161/// for constant immediate shift values, and for constant immediate
162/// shift values equal to 1. Even the general case is sort of special,
163/// because the shift amount has to be in CL, not just any old register.
164///
165void
166ISel::visitShiftInst (ShiftInst & I)
167{
168 unsigned Op0r = getReg (I.getOperand (0));
169 unsigned DestReg = getReg (I);
Brian Gaekea1719c92002-10-31 23:03:59 +0000170 bool isRightShift = (I.getOpcode () == Instruction::Shr);
171 bool isOperandUnsigned = I.getType ()->isUnsigned ();
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000172 unsigned OperandClass = SimpleLog2(I.getType()->getPrimitiveSize());
Chris Lattner796df732002-11-02 00:44:25 +0000173
Brian Gaekea1719c92002-10-31 23:03:59 +0000174 if (ConstantUInt *CUI = dyn_cast <ConstantUInt> (I.getOperand (1)))
175 {
Chris Lattner796df732002-11-02 00:44:25 +0000176 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
177 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
178 unsigned char shAmt = CUI->getValue();
179
Brian Gaekea1719c92002-10-31 23:03:59 +0000180 // Emit: <insn> reg, shamt (shift-by-immediate opcode "ir" form.)
181 if (isRightShift)
182 {
183 if (isOperandUnsigned)
184 {
185 // This is a shift right logical (SHR).
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000186 switch (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000187 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000188 case 0:
Brian Gaekea1719c92002-10-31 23:03:59 +0000189 BuildMI (BB, X86::SHRir8, 2,
190 DestReg).addReg (Op0r).addZImm (shAmt);
191 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000192 case 1:
Brian Gaekea1719c92002-10-31 23:03:59 +0000193 BuildMI (BB, X86::SHRir16, 2,
194 DestReg).addReg (Op0r).addZImm (shAmt);
195 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000196 case 2:
Brian Gaekea1719c92002-10-31 23:03:59 +0000197 BuildMI (BB, X86::SHRir32, 2,
198 DestReg).addReg (Op0r).addZImm (shAmt);
199 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000200 case 3:
Brian Gaekea1719c92002-10-31 23:03:59 +0000201 default:
202 visitInstruction (I);
203 break;
204 }
205 }
206 else
207 {
208 // This is a shift right arithmetic (SAR).
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000209 switch (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000210 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000211 case 0:
Brian Gaekea1719c92002-10-31 23:03:59 +0000212 BuildMI (BB, X86::SARir8, 2,
213 DestReg).addReg (Op0r).addZImm (shAmt);
214 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000215 case 1:
Brian Gaekea1719c92002-10-31 23:03:59 +0000216 BuildMI (BB, X86::SARir16, 2,
217 DestReg).addReg (Op0r).addZImm (shAmt);
218 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000219 case 2:
Brian Gaekea1719c92002-10-31 23:03:59 +0000220 BuildMI (BB, X86::SARir32, 2,
221 DestReg).addReg (Op0r).addZImm (shAmt);
222 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000223 case 3:
Brian Gaekea1719c92002-10-31 23:03:59 +0000224 default:
225 visitInstruction (I);
226 break;
227 }
228 }
229 }
230 else
231 {
232 // This is a left shift (SHL).
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000233 switch (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000234 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000235 case 0:
Brian Gaekea1719c92002-10-31 23:03:59 +0000236 BuildMI (BB, X86::SHLir8, 2,
237 DestReg).addReg (Op0r).addZImm (shAmt);
238 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000239 case 1:
Brian Gaekea1719c92002-10-31 23:03:59 +0000240 BuildMI (BB, X86::SHLir16, 2,
241 DestReg).addReg (Op0r).addZImm (shAmt);
242 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000243 case 2:
Brian Gaekea1719c92002-10-31 23:03:59 +0000244 BuildMI (BB, X86::SHLir32, 2,
245 DestReg).addReg (Op0r).addZImm (shAmt);
246 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000247 case 3:
Brian Gaekea1719c92002-10-31 23:03:59 +0000248 default:
249 visitInstruction (I);
250 break;
251 }
252 }
253 }
254 else
255 {
256 // The shift amount is non-constant.
257 //
258 // In fact, you can only shift with a variable shift amount if
259 // that amount is already in the CL register, so we have to put it
260 // there first.
261 //
262 // Get it from the register it's in.
263 unsigned Op1r = getReg (I.getOperand (1));
264 // Emit: move cl, shiftAmount (put the shift amount in CL.)
265 BuildMI (BB, X86::MOVrr8, 2, X86::CL).addReg (Op1r);
266 // Emit: <insn> reg, cl (shift-by-CL opcode; "rr" form.)
267 if (isRightShift)
268 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000269 if (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000270 {
271 // This is a shift right logical (SHR).
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000272 switch (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000273 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000274 case 0:
Brian Gaekea1719c92002-10-31 23:03:59 +0000275 BuildMI (BB, X86::SHRrr8, 2,
276 DestReg).addReg (Op0r).addReg (X86::CL);
277 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000278 case 1:
Brian Gaekea1719c92002-10-31 23:03:59 +0000279 BuildMI (BB, X86::SHRrr16, 2,
280 DestReg).addReg (Op0r).addReg (X86::CL);
281 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000282 case 2:
Brian Gaekea1719c92002-10-31 23:03:59 +0000283 BuildMI (BB, X86::SHRrr32, 2,
284 DestReg).addReg (Op0r).addReg (X86::CL);
285 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000286 case 3:
Brian Gaekea1719c92002-10-31 23:03:59 +0000287 default:
288 visitInstruction (I);
289 break;
290 }
291 }
292 else
293 {
294 // This is a shift right arithmetic (SAR).
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000295 switch (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000296 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000297 case 0:
Brian Gaekea1719c92002-10-31 23:03:59 +0000298 BuildMI (BB, X86::SARrr8, 2,
299 DestReg).addReg (Op0r).addReg (X86::CL);
300 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000301 case 1:
Brian Gaekea1719c92002-10-31 23:03:59 +0000302 BuildMI (BB, X86::SARrr16, 2,
303 DestReg).addReg (Op0r).addReg (X86::CL);
304 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000305 case 2:
Brian Gaekea1719c92002-10-31 23:03:59 +0000306 BuildMI (BB, X86::SARrr32, 2,
307 DestReg).addReg (Op0r).addReg (X86::CL);
308 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000309 case 3:
Brian Gaekea1719c92002-10-31 23:03:59 +0000310 default:
311 visitInstruction (I);
312 break;
313 }
314 }
315 }
316 else
317 {
318 // This is a left shift (SHL).
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000319 switch (OperandClass)
Brian Gaekea1719c92002-10-31 23:03:59 +0000320 {
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000321 case 0:
Brian Gaekea1719c92002-10-31 23:03:59 +0000322 BuildMI (BB, X86::SHLrr8, 2,
323 DestReg).addReg (Op0r).addReg (X86::CL);
324 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000325 case 1:
Brian Gaekea1719c92002-10-31 23:03:59 +0000326 BuildMI (BB, X86::SHLrr16, 2,
327 DestReg).addReg (Op0r).addReg (X86::CL);
328 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000329 case 2:
Brian Gaekea1719c92002-10-31 23:03:59 +0000330 BuildMI (BB, X86::SHLrr32, 2,
331 DestReg).addReg (Op0r).addReg (X86::CL);
332 break;
Chris Lattnerd5a87f82002-11-02 00:49:56 +0000333 case 3:
Brian Gaekea1719c92002-10-31 23:03:59 +0000334 default:
335 visitInstruction (I);
336 break;
337 }
338 }
339 }
340}
341
Chris Lattner72614082002-10-25 22:55:53 +0000342
343/// 'add' instruction - Simply turn this into an x86 reg,reg add instruction.
344void ISel::visitAdd(BinaryOperator &B) {
345 unsigned Op0r = getReg(B.getOperand(0)), Op1r = getReg(B.getOperand(1));
346 unsigned DestReg = getReg(B);
347
348 switch (B.getType()->getPrimitiveSize()) {
349 case 1: // UByte, SByte
Chris Lattner8548ee72002-10-30 01:49:01 +0000350 BuildMI(BB, X86::ADDrr8, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000351 break;
352 case 2: // UShort, Short
Chris Lattner8548ee72002-10-30 01:49:01 +0000353 BuildMI(BB, X86::ADDrr16, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000354 break;
355 case 4: // UInt, Int
Chris Lattner8548ee72002-10-30 01:49:01 +0000356 BuildMI(BB, X86::ADDrr32, 2, DestReg).addReg(Op0r).addReg(Op1r);
Chris Lattner72614082002-10-25 22:55:53 +0000357 break;
Chris Lattner72614082002-10-25 22:55:53 +0000358 case 8: // ULong, Long
Brian Gaekea1719c92002-10-31 23:03:59 +0000359 // Here we have a pair of operands each occupying a pair of registers.
360 // We need to do an ADDrr32 of the least-significant pair immediately
361 // followed by an ADCrr32 (Add with Carry) of the most-significant pair.
362 // I don't know how we are representing these multi-register arguments.
Chris Lattner72614082002-10-25 22:55:53 +0000363 default:
364 visitInstruction(B); // abort
365 }
366}
367
Brian Gaekea1719c92002-10-31 23:03:59 +0000368
369
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000370/// createSimpleX86InstructionSelector - This pass converts an LLVM function
371/// into a machine code representation is a very simple peep-hole fashion. The
Chris Lattner72614082002-10-25 22:55:53 +0000372/// generated code sucks but the implementation is nice and simple.
373///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000374Pass *createSimpleX86InstructionSelector(TargetMachine &TM) {
375 return new ISel(TM);
Chris Lattner72614082002-10-25 22:55:53 +0000376}