blob: ec331ee8fb9f33497e877fdc050e20b19dd79c3f [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);
Chris Lattner1c809c52004-02-29 00:27:00 +000061 void visitReturnInst(ReturnInst &RI);
62
63 void visitInstruction(Instruction &I) {
64 std::cerr << "Unhandled instruction: " << I;
65 abort();
66 }
67
68 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
69 /// function, lowering any calls to unknown intrinsic functions into the
70 /// equivalent LLVM code.
71 void LowerUnknownIntrinsicFunctionCalls(Function &F);
Chris Lattner1c809c52004-02-29 00:27:00 +000072 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI);
73
Brian Gaekebc1d27a2004-03-03 23:03:14 +000074 /// copyConstantToRegister - Output the instructions required to put the
75 /// specified constant into the specified register.
76 ///
77 void copyConstantToRegister(MachineBasicBlock *MBB,
78 MachineBasicBlock::iterator IP,
79 Constant *C, unsigned R);
80
81 /// makeAnotherReg - This method returns the next register number we haven't
82 /// yet used.
83 ///
84 /// Long values are handled somewhat specially. They are always allocated
85 /// as pairs of 32 bit integer values. The register number returned is the
86 /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits
87 /// of the long value.
88 ///
89 unsigned makeAnotherReg(const Type *Ty) {
90 assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) &&
91 "Current target doesn't have SparcV8 reg info??");
92 const SparcV8RegisterInfo *MRI =
93 static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo());
94 if (Ty == Type::LongTy || Ty == Type::ULongTy) {
95 const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy);
96 // Create the lower part
97 F->getSSARegMap()->createVirtualRegister(RC);
98 // Create the upper part.
99 return F->getSSARegMap()->createVirtualRegister(RC)-1;
100 }
101
102 // Add the mapping of regnumber => reg class to MachineFunction
103 const TargetRegisterClass *RC = MRI->getRegClassForType(Ty);
104 return F->getSSARegMap()->createVirtualRegister(RC);
105 }
106
107 unsigned getReg(Value &V) { return getReg (&V); } // allow refs.
108 unsigned getReg(Value *V) {
109 // Just append to the end of the current bb.
110 MachineBasicBlock::iterator It = BB->end();
111 return getReg(V, BB, It);
112 }
113 unsigned getReg(Value *V, MachineBasicBlock *MBB,
114 MachineBasicBlock::iterator IPt) {
115 unsigned &Reg = RegMap[V];
116 if (Reg == 0) {
117 Reg = makeAnotherReg(V->getType());
118 RegMap[V] = Reg;
119 }
120 // If this operand is a constant, emit the code to copy the constant into
121 // the register here...
122 //
123 if (Constant *C = dyn_cast<Constant>(V)) {
124 copyConstantToRegister(MBB, IPt, C, Reg);
125 RegMap.erase(V); // Assign a new name to this constant if ref'd again
126 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
127 // Move the address of the global into the register
128 // X86 does:
129 // BuildMI(*MBB, IPt, V8::ORrr, 2, Reg).addReg(G0).addGlobalAddress(GV);
130 // We need to use SETHI and OR.
131 assert (0 && "Can't move address of global yet");
132 RegMap.erase(V); // Assign a new name to this address if ref'd again
133 }
134
135 return Reg;
136 }
137
Chris Lattner1c809c52004-02-29 00:27:00 +0000138 };
139}
140
141FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) {
142 return new V8ISel(TM);
143}
144
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000145enum TypeClass {
146 cByte, cShort, cInt, cFloat, cDouble
147};
148
149static TypeClass getClass (const Type *T) {
150 switch (T->getPrimitiveID ()) {
151 case Type::UByteTyID: case Type::SByteTyID: return cByte;
152 case Type::UShortTyID: case Type::ShortTyID: return cShort;
153 case Type::UIntTyID: case Type::IntTyID: return cInt;
154 case Type::FloatTyID: return cFloat;
155 case Type::DoubleTyID: return cDouble;
156 default:
157 assert (0 && "Type of unknown class passed to getClass?");
158 return cByte;
159 }
160}
161
162/// copyConstantToRegister - Output the instructions required to put the
163/// specified constant into the specified register.
164///
165void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB,
166 MachineBasicBlock::iterator IP,
167 Constant *C, unsigned R) {
Brian Gaeke775158d2004-03-04 04:37:45 +0000168 if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) {
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000169 unsigned Class = getClass(C->getType());
Brian Gaekee8061732004-03-04 00:56:25 +0000170 switch (Class) {
171 case cByte:
172 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm ((uint8_t) CI->getRawValue ());
173 return;
174 case cShort: {
175 unsigned TmpReg = makeAnotherReg (C->getType ());
176 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (((uint16_t) CI->getRawValue ()) >> 10);
177 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (((uint16_t) CI->getRawValue ()) & 0x03ff);
178 return;
179 }
180 case cInt: {
181 unsigned TmpReg = makeAnotherReg (C->getType ());
182 BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (((uint32_t) CI->getRawValue ()) >> 10);
183 BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg).addImm (((uint32_t) CI->getRawValue ()) & 0x03ff);
184 return;
185 }
186 default:
Brian Gaeke775158d2004-03-04 04:37:45 +0000187 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekee8061732004-03-04 00:56:25 +0000188 return;
189 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000190 }
191
Brian Gaeke775158d2004-03-04 04:37:45 +0000192 assert (0 && "Can't copy this kind of constant into register yet");
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000193}
Chris Lattner1c809c52004-02-29 00:27:00 +0000194
195bool V8ISel::runOnFunction(Function &Fn) {
196 // First pass over the function, lower any unknown intrinsic functions
197 // with the IntrinsicLowering class.
198 LowerUnknownIntrinsicFunctionCalls(Fn);
199
200 F = &MachineFunction::construct(&Fn, TM);
201
202 // Create all of the machine basic blocks for the function...
203 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
204 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
205
206 BB = &F->front();
207
208 // Set up a frame object for the return address. This is used by the
209 // llvm.returnaddress & llvm.frameaddress intrinisics.
210 //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4);
211
212 // Copy incoming arguments off of the stack and out of fixed registers.
213 //LoadArgumentsToVirtualRegs(Fn);
214
215 // Instruction select everything except PHI nodes
216 visit(Fn);
217
218 // Select the PHI nodes
219 //SelectPHINodes();
220
221 RegMap.clear();
222 MBBMap.clear();
223 F = 0;
224 // We always build a machine code representation for the function
225 return true;
226}
227
228
229void V8ISel::visitReturnInst(ReturnInst &I) {
Brian Gaeke08f64c32004-03-06 05:32:28 +0000230 if (I.getNumOperands () == 1) {
231 unsigned RetValReg = getReg (I.getOperand (0));
232 switch (getClass (I.getOperand (0)->getType ())) {
233 case cByte:
234 case cShort:
235 case cInt:
236 // Schlep it over into i0 (where it will become o0 after restore).
237 BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg);
238 break;
239 default:
240 visitInstruction (I);
241 return;
242 }
243 } else if (I.getNumOperands () != 1) {
244 visitInstruction (I);
Chris Lattner1c809c52004-02-29 00:27:00 +0000245 }
Brian Gaeke08f64c32004-03-06 05:32:28 +0000246 // Just emit a 'retl' instruction to return.
247 BuildMI(BB, V8::RETL, 0);
248 return;
Chris Lattner1c809c52004-02-29 00:27:00 +0000249}
250
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000251void V8ISel::visitBinaryOperator (BinaryOperator &I) {
252 unsigned DestReg = getReg (I);
253 unsigned Op0Reg = getReg (I.getOperand (0));
254 unsigned Op1Reg = getReg (I.getOperand (1));
255
256 unsigned ResultReg = makeAnotherReg (I.getType ());
257 switch (I.getOpcode ()) {
258 case Instruction::Add:
259 BuildMI (BB, V8::ADDrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
260 break;
Brian Gaeke775158d2004-03-04 04:37:45 +0000261 case Instruction::Sub:
262 BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg);
263 break;
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000264 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000265 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000266 return;
267 }
268
269 switch (getClass (I.getType ())) {
270 case cByte:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000271 if (I.getType ()->isSigned ()) { // add byte
272 BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff);
273 } else { // add ubyte
274 unsigned TmpReg = makeAnotherReg (I.getType ());
275 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
276 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24);
277 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000278 break;
279 case cShort:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000280 if (I.getType ()->isSigned ()) { // add short
281 unsigned TmpReg = makeAnotherReg (I.getType ());
282 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16);
283 BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16);
284 } else { // add ushort
285 unsigned TmpReg = makeAnotherReg (I.getType ());
286 BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24);
287 BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (24);
288 }
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000289 break;
290 case cInt:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000291 BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (ResultReg);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000292 break;
293 default:
Brian Gaeke08f64c32004-03-06 05:32:28 +0000294 visitInstruction (I);
Brian Gaekebc1d27a2004-03-03 23:03:14 +0000295 return;
296 }
297}
298
Chris Lattner1c809c52004-02-29 00:27:00 +0000299
300/// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
301/// function, lowering any calls to unknown intrinsic functions into the
302/// equivalent LLVM code.
303void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
304 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
305 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
306 if (CallInst *CI = dyn_cast<CallInst>(I++))
307 if (Function *F = CI->getCalledFunction())
308 switch (F->getIntrinsicID()) {
309 case Intrinsic::not_intrinsic: break;
310 default:
311 // All other intrinsic calls we must lower.
312 Instruction *Before = CI->getPrev();
313 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
314 if (Before) { // Move iterator to instruction after call
315 I = Before; ++I;
316 } else {
317 I = BB->begin();
318 }
319 }
320}
321
322
323void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
324 unsigned TmpReg1, TmpReg2;
325 switch (ID) {
326 default: assert(0 && "Intrinsic not supported!");
327 }
328}