blob: 3bae6a51522076ff6d2e5320d88a1c805fb7fdcf [file] [log] [blame]
Chris Lattner1c809c52004-02-29 00:27:00 +00001//===-- InstSelectSimple.cpp - A simple instruction selector for SparcV8 --===//
2//
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//===----------------------------------------------------------------------===//
9//
10// This file defines a simple peephole instruction selector for the V8 target
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcV8.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000015#include "SparcV8InstrInfo.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000016#include "llvm/Instructions.h"
17#include "llvm/IntrinsicLowering.h"
18#include "llvm/Pass.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000019#include "llvm/Constants.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineFunction.h"
Brian Gaekebc1d27a2004-03-03 23:03:14 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner1c809c52004-02-29 00:27:00 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Support/GetElementPtrTypeIterator.h"
25#include "llvm/Support/InstVisitor.h"
26#include "llvm/Support/CFG.h"
27using namespace llvm;
28
29namespace {
30 struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> {
31 TargetMachine &TM;
32 MachineFunction *F; // The function we are compiling into
33 MachineBasicBlock *BB; // The current MBB we are compiling
34
35 std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs
36
37 // MBBMap - Mapping between LLVM BB -> Machine BB
38 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
39
40 V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {}
41
42 /// runOnFunction - Top level implementation of instruction selection for
43 /// the entire function.
44 ///
45 bool runOnFunction(Function &Fn);
46
47 virtual const char *getPassName() const {
48 return "SparcV8 Simple Instruction Selection";
49 }
50
51 /// visitBasicBlock - This method is called when we are visiting a new basic
52 /// block. This simply creates a new MachineBasicBlock to emit code into
53 /// and adds it to the current MachineFunction. Subsequent visit* for
54 /// instructions will be invoked for all instructions in the basic block.
55 ///
56 void visitBasicBlock(BasicBlock &LLVM_BB) {
57 BB = MBBMap[&LLVM_BB];
58 }
59
Brian Gaekebc1d27a2004-03-03 23:03:14 +000060 void visitBinaryOperator(BinaryOperator &I);
Brian Gaekef7e44ef2004-04-02 20:53:33 +000061 void visitCallInst(CallInst &I);
62 void visitReturnInst(ReturnInst &RI);
Chris Lattner1c809c52004-02-29 00:27:00 +000063
64 void visitInstruction(Instruction &I) {
65 std::cerr << "Unhandled instruction: " << I;
66 abort();
67 }
68
69 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
70 /// function, lowering any calls to unknown intrinsic functions into the
71 /// equivalent LLVM code.
72 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000073 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
74
Brian Gaekebc1d27a2004-03-03 23:03:14 +000075 /// copyConstantToRegister - Output the instructions required to put the
76 /// specified constant into the specified register.
77 ///
78 void copyConstantToRegister(MachineBasicBlock *MBB,
79 MachineBasicBlock::iterator IP,
80 Constant *C, unsigned R);
81
82 /// makeAnotherReg - This method returns the next register number we haven't
83 /// yet used.
84 ///
85 /// Long values are handled somewhat specially. They are always allocated
86 /// as pairs of 32 bit integer values. The register number returned is the
87 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
88 /// of the long value.
89 ///
90 unsigned makeAnotherReg(const Type *Ty) {
91 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
92 "Current target doesn't have SparcV8 reg info??");
93 const SparcV8RegisterInfo *MRI =
94 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
95 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
96 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
97 // Create the lower part
98 F->getSSARegMap()->createVirtualRegister(RC);
99 // Create the upper part.
100 return F->getSSARegMap()->createVirtualRegister(RC)-1;
101 }
102
103 // Add the mapping of regnumber => reg class to MachineFunction
104 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
105 return F->getSSARegMap()->createVirtualRegister(RC);
106 }
107
108 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
109 unsigned getReg(Value *V) {
110 // Just append to the end of the current bb.
111 MachineBasicBlock::iterator It = BB->end();
112 return getReg(V, BB, It);
113 }
114 unsigned getReg(Value *V, MachineBasicBlock *MBB,
115 MachineBasicBlock::iterator IPt) {
116 unsigned &Reg = RegMap[V];
117 if (Reg == 0) {
118 Reg = makeAnotherReg(V->getType());
119 RegMap[V] = Reg;
120 }
121 // If this operand is a constant, emit the code to copy the constant into
122 // the register here...
123 //
124 if (Constant *C = dyn_cast<Constant>(V)) {
125 copyConstantToRegister(MBB, IPt, C, Reg);
126 RegMap.erase(V); // Assign a new name to this constant if ref'd again
127 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
128 // Move the address of the global into the register
Brian Gaekecf471982004-03-09 04:49:13 +0000129 unsigned TmpReg = makeAnotherReg(V->getType());
130 BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV);
131 BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg)
132 .addGlobalAddress (GV);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000133 RegMap.erase(V); // Assign a new name to this address if ref'd again
134 }
135
136 return Reg;
137 }
138
Chris Lattner1c809c52004-02-29 00:27:00 +0000139 };
140}
141
142FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
143 return new V8ISel(TM);
144}
145
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000146enum TypeClass {
Brian Gaekef57e3642004-03-16 22:37:11 +0000147 cByte, cShort, cInt, cLong, cFloat, cDouble
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000148};
149
150static TypeClass getClass (const Type *T) {
151 switch (T->getPrimitiveID ()) {
152 case Type::UByteTyID: case Type::SByteTyID: return cByte;
153 case Type::UShortTyID: case Type::ShortTyID: return cShort;
154 case Type::UIntTyID: case Type::IntTyID: return cInt;
Brian Gaekef57e3642004-03-16 22:37:11 +0000155 case Type::ULongTyID: case Type::LongTyID: return cLong;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000156 case Type::FloatTyID: return cFloat;
157 case Type::DoubleTyID: return cDouble;
158 default:
159 assert (0 && "Type of unknown class passed to getClass?");
160 return cByte;
161 }
162}
163
164/// copyConstantToRegister - Output the instructions required to put the
165/// specified constant into the specified register.
166///
167void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
168 MachineBasicBlock::iterator IP,
169 Constant *C, unsigned R) {
Brian Gaeke775158d2004-03-04 04:37:45 +0000170 if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000171 unsigned Class = getClass(C->getType());
Brian Gaekee8061732004-03-04 00:56:25 +0000172 switch (Class) {
173 case cByte:
174 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm ((uint8_t) CI->getRawValue ());
175 return;
176 case cShort: {
177 unsigned TmpReg = makeAnotherReg (C->getType ());
178 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (((uint16_t) CI->getRawValue ()) >> 10);
179 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (((uint16_t) CI->getRawValue ()) & 0x03ff);
180 return;
181 }
182 case cInt: {
183 unsigned TmpReg = makeAnotherReg (C->getType ());
184 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (((uint32_t) CI->getRawValue ()) >> 10);
185 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (((uint32_t) CI->getRawValue ()) & 0x03ff);
186 return;
187 }
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000188 case cLong: {
189 unsigned TmpReg = makeAnotherReg (Type::UIntTy);
190 uint32_t topHalf, bottomHalf;
191 topHalf = (uint32_t) (CI->getRawValue () >> 32);
192 bottomHalf = (uint32_t) (CI->getRawValue () & 0x0ffffffffULL);
193 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (topHalf >> 10);
194 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (topHalf & 0x03ff);
195 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (bottomHalf >> 10);
196 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (bottomHalf & 0x03ff);
197 return;
198 }
Brian Gaekee8061732004-03-04 00:56:25 +0000199 default:
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000200 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000201 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000202 return;
203 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000204 }
205
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000206 std::cerr << "Offending constant: " << *C << "\n";
Brian Gaeke775158d2004-03-04 04:37:45 +0000207 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000208}
Chris Lattner1c809c52004-02-29 00:27:00 +0000209
210bool V8ISel::runOnFunction(Function &Fn) {
211 // First pass over the function, lower any unknown intrinsic functions
212 // with the IntrinsicLowering class.
213 LowerUnknownIntrinsicFunctionCalls(Fn);
214
215 F = &MachineFunction::construct(&Fn, TM);
216
217 // Create all of the machine basic blocks for the function...
218 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
219 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
220
221 BB = &F->front();
222
223 // Set up a frame object for the return address. This is used by the
224 // llvm.returnaddress & llvm.frameaddress intrinisics.
225 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
226
227 // Copy incoming arguments off of the stack and out of fixed registers.
228 //LoadArgumentsToVirtualRegs(Fn);
229
230 // Instruction select everything except PHI nodes
231 visit(Fn);
232
233 // Select the PHI nodes
234 //SelectPHINodes();
235
236 RegMap.clear();
237 MBBMap.clear();
238 F = 0;
239 // We always build a machine code representation for the function
240 return true;
241}
242
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000243void V8ISel::visitCallInst(CallInst &I) {
244 assert (I.getNumOperands () == 1 && "Can't handle call args yet");
Brian Gaekeea8494b2004-04-06 22:09:23 +0000245 unsigned DestReg = getReg (I);
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000246 BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0));
Brian Gaekeea8494b2004-04-06 22:09:23 +0000247 if (I.getType ()->getPrimitiveID () == Type::VoidTyID)
248 return;
249 // Deal w/ return value
250 switch (getClass (I.getType ())) {
251 case cByte:
252 case cShort:
253 case cInt:
254 // Schlep it over into the destination register
255 BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0);
256 break;
257 default:
258 visitInstruction (I);
259 return;
260 }
Brian Gaekef7e44ef2004-04-02 20:53:33 +0000261}
Chris Lattner1c809c52004-02-29 00:27:00 +0000262
263void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000264 if (I.getNumOperands () == 1) {
265 unsigned RetValReg = getReg (I.getOperand (0));
266 switch (getClass (I.getOperand (0)->getType ())) {
267 case cByte:
268 case cShort:
269 case cInt:
270 // Schlep it over into i0 (where it will become o0 after restore).
271 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
272 break;
273 default:
274 visitInstruction (I);
275 return;
276 }
277 } else if (I.getNumOperands () != 1) {
278 visitInstruction (I);
Chris Lattner1c809c52004-02-29 00:27:00 +0000279 }
Brian Gaeke08f64c32004-03-06 05:32:28 +0000280 // Just emit a 'retl' instruction to return.
281 BuildMI(BB, V8::RETL, 0);
282 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000283}
284
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000285void V8ISel::visitBinaryOperator (BinaryOperator &I) {
286 unsigned DestReg = getReg (I);
287 unsigned Op0Reg = getReg (I.getOperand (0));
288 unsigned Op1Reg = getReg (I.getOperand (1));
289
290 unsigned ResultReg = makeAnotherReg (I.getType ());
Chris Lattner22ede702004-04-07 04:06:46 +0000291 unsigned OpCase = ~0;
292
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000293 // FIXME: support long, ulong, fp.
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000294 switch (I.getOpcode ()) {
Chris Lattner22ede702004-04-07 04:06:46 +0000295 case Instruction::Add: OpCase = 0; break;
296 case Instruction::Sub: OpCase = 1; break;
297 case Instruction::Mul: OpCase = 2; break;
298 case Instruction::And: OpCase = 3; break;
299 case Instruction::Or: OpCase = 4; break;
300 case Instruction::Xor: OpCase = 5; break;
301
302 case Instruction::Div:
303 case Instruction::Rem: {
304 unsigned Dest = ResultReg;
305 if (I.getOpcode() == Instruction::Rem)
306 Dest = makeAnotherReg(I.getType());
307
308 // FIXME: this is probably only right for 32 bit operands.
309 if (I.getType ()->isSigned()) {
310 unsigned Tmp = makeAnotherReg (I.getType ());
311 // Sign extend into the Y register
312 BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31);
313 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0);
314 BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
315 } else {
316 // Zero extend into the Y register, ie, just set it to zero
317 BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0);
318 BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaeke2d4fa8f2004-04-07 04:00:49 +0000319 }
Chris Lattner22ede702004-04-07 04:06:46 +0000320
321 if (I.getOpcode() == Instruction::Rem) {
322 unsigned Tmp = makeAnotherReg (I.getType ());
323 BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg);
324 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp);
Brian Gaekef57e3642004-03-16 22:37:11 +0000325 }
Chris Lattner22ede702004-04-07 04:06:46 +0000326 break;
327 }
328 default:
329 visitInstruction (I);
330 return;
331 }
332
333 if (OpCase != ~0U) {
334 static const unsigned Opcodes[] = {
335 V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr
336 };
337 BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000338 }
339
340 switch (getClass (I.getType ())) {
341 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000342 if (I.getType ()->isSigned ()) { // add byte
343 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
344 } else { // add ubyte
345 unsigned TmpReg = makeAnotherReg (I.getType ());
346 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
347 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
348 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000349 break;
350 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000351 if (I.getType ()->isSigned ()) { // add short
352 unsigned TmpReg = makeAnotherReg (I.getType ());
353 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
354 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
355 } else { // add ushort
356 unsigned TmpReg = makeAnotherReg (I.getType ());
Brian Gaeke6d339f92004-03-16 22:45:42 +0000357 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
358 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16);
Brian Gaeke08f64c32004-03-06 05:32:28 +0000359 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000360 break;
361 case cInt:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000362 BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (ResultReg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000363 break;
364 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000365 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000366 return;
367 }
368}
369
Chris Lattner1c809c52004-02-29 00:27:00 +0000370
371/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
372/// function, lowering any calls to unknown intrinsic functions into the
373/// equivalent LLVM code.
374void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
375 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
376 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
377 if (CallInst *CI = dyn_cast<CallInst>(I++))
378 if (Function *F = CI->getCalledFunction())
379 switch (F->getIntrinsicID()) {
380 case Intrinsic::not_intrinsic: break;
381 default:
382 // All other intrinsic calls we must lower.
383 Instruction *Before = CI->getPrev();
384 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
385 if (Before) { // Move iterator to instruction after call
386 I = Before; ++I;
387 } else {
388 I = BB->begin();
389 }
390 }
391}
392
393
394void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
395 unsigned TmpReg1, TmpReg2;
396 switch (ID) {
397 default: assert(0 && "Intrinsic not supported!");
398 }
399}