Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 1 | //===-- 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 Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 15 | #include "SparcV8InstrInfo.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
| 17 | #include "llvm/IntrinsicLowering.h" |
| 18 | #include "llvm/Pass.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 22 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 25 | #include "llvm/Support/InstVisitor.h" |
| 26 | #include "llvm/Support/CFG.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 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 Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 60 | void visitBinaryOperator(BinaryOperator &I); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 61 | 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 Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 72 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 73 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 74 | /// 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 Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 138 | }; |
| 139 | } |
| 140 | |
| 141 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 142 | return new V8ISel(TM); |
| 143 | } |
| 144 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 145 | enum TypeClass { |
| 146 | cByte, cShort, cInt, cFloat, cDouble |
| 147 | }; |
| 148 | |
| 149 | static 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 | /// |
| 165 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 166 | MachineBasicBlock::iterator IP, |
| 167 | Constant *C, unsigned R) { |
| 168 | if (C->getType()->isIntegral()) { |
| 169 | unsigned Class = getClass(C->getType()); |
| 170 | |
| 171 | ConstantInt *CI = cast<ConstantInt>(C); |
| 172 | // cByte: or %g0, <imm>, <dest> |
| 173 | // cShort or cInt: sethi, then or |
| 174 | // BuildMI(*MBB, IP, <opcode>, <#regs>, R).addImm(CI->getRawValue()); |
| 175 | } |
| 176 | |
| 177 | assert (0 && "Can't copy constants into registers yet"); |
| 178 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 179 | |
| 180 | bool V8ISel::runOnFunction(Function &Fn) { |
| 181 | // First pass over the function, lower any unknown intrinsic functions |
| 182 | // with the IntrinsicLowering class. |
| 183 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 184 | |
| 185 | F = &MachineFunction::construct(&Fn, TM); |
| 186 | |
| 187 | // Create all of the machine basic blocks for the function... |
| 188 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 189 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 190 | |
| 191 | BB = &F->front(); |
| 192 | |
| 193 | // Set up a frame object for the return address. This is used by the |
| 194 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 195 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 196 | |
| 197 | // Copy incoming arguments off of the stack and out of fixed registers. |
| 198 | //LoadArgumentsToVirtualRegs(Fn); |
| 199 | |
| 200 | // Instruction select everything except PHI nodes |
| 201 | visit(Fn); |
| 202 | |
| 203 | // Select the PHI nodes |
| 204 | //SelectPHINodes(); |
| 205 | |
| 206 | RegMap.clear(); |
| 207 | MBBMap.clear(); |
| 208 | F = 0; |
| 209 | // We always build a machine code representation for the function |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | void V8ISel::visitReturnInst(ReturnInst &I) { |
| 215 | if (I.getNumOperands() == 0) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 216 | // Just emit a 'jmpl' instruction to return. |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 217 | BuildMI(BB, V8::JMPLi, 2, V8::G0).addZImm(8).addReg(V8::I7); |
| 218 | return; |
| 219 | } |
| 220 | visitInstruction(I); |
| 221 | } |
| 222 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame^] | 223 | void V8ISel::visitBinaryOperator (BinaryOperator &I) { |
| 224 | unsigned DestReg = getReg (I); |
| 225 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 226 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 227 | |
| 228 | unsigned ResultReg = makeAnotherReg (I.getType ()); |
| 229 | switch (I.getOpcode ()) { |
| 230 | case Instruction::Add: |
| 231 | BuildMI (BB, V8::ADDrr, 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
| 232 | break; |
| 233 | default: |
| 234 | visitInstruction (I); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | switch (getClass (I.getType ())) { |
| 239 | case cByte: |
| 240 | if (I.getType ()->isSigned ()) { // add byte |
| 241 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 242 | } else { // add ubyte |
| 243 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 244 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 245 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 246 | } |
| 247 | break; |
| 248 | case cShort: |
| 249 | if (I.getType ()->isSigned ()) { // add short |
| 250 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 251 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 252 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 253 | } else { // add ushort |
| 254 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 255 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 256 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 257 | } |
| 258 | break; |
| 259 | case cInt: |
| 260 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (ResultReg); |
| 261 | break; |
| 262 | default: |
| 263 | visitInstruction (I); |
| 264 | return; |
| 265 | } |
| 266 | } |
| 267 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 268 | |
| 269 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 270 | /// function, lowering any calls to unknown intrinsic functions into the |
| 271 | /// equivalent LLVM code. |
| 272 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 273 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 274 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 275 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 276 | if (Function *F = CI->getCalledFunction()) |
| 277 | switch (F->getIntrinsicID()) { |
| 278 | case Intrinsic::not_intrinsic: break; |
| 279 | default: |
| 280 | // All other intrinsic calls we must lower. |
| 281 | Instruction *Before = CI->getPrev(); |
| 282 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 283 | if (Before) { // Move iterator to instruction after call |
| 284 | I = Before; ++I; |
| 285 | } else { |
| 286 | I = BB->begin(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 293 | unsigned TmpReg1, TmpReg2; |
| 294 | switch (ID) { |
| 295 | default: assert(0 && "Intrinsic not supported!"); |
| 296 | } |
| 297 | } |