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 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 60 | void visitBinaryOperator(Instruction &I); |
| 61 | void visitShiftInstruction(Instruction &I) { visitBinaryOperator(I); } |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 62 | void visitSetCondInst(Instruction &I); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 63 | void visitCallInst(CallInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 64 | void visitReturnInst(ReturnInst &I); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame^] | 65 | void visitCastInst(CastInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 66 | void visitLoadInst(LoadInst &I); |
| 67 | void visitStoreInst(StoreInst &I); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 68 | |
| 69 | void visitInstruction(Instruction &I) { |
| 70 | std::cerr << "Unhandled instruction: " << I; |
| 71 | abort(); |
| 72 | } |
| 73 | |
| 74 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 75 | /// function, lowering any calls to unknown intrinsic functions into the |
| 76 | /// equivalent LLVM code. |
| 77 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 78 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 79 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 80 | void LoadArgumentsToVirtualRegs(Function *F); |
| 81 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 82 | /// copyConstantToRegister - Output the instructions required to put the |
| 83 | /// specified constant into the specified register. |
| 84 | /// |
| 85 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 86 | MachineBasicBlock::iterator IP, |
| 87 | Constant *C, unsigned R); |
| 88 | |
| 89 | /// makeAnotherReg - This method returns the next register number we haven't |
| 90 | /// yet used. |
| 91 | /// |
| 92 | /// Long values are handled somewhat specially. They are always allocated |
| 93 | /// as pairs of 32 bit integer values. The register number returned is the |
| 94 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 95 | /// of the long value. |
| 96 | /// |
| 97 | unsigned makeAnotherReg(const Type *Ty) { |
| 98 | assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) && |
| 99 | "Current target doesn't have SparcV8 reg info??"); |
| 100 | const SparcV8RegisterInfo *MRI = |
| 101 | static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()); |
| 102 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 103 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 104 | // Create the lower part |
| 105 | F->getSSARegMap()->createVirtualRegister(RC); |
| 106 | // Create the upper part. |
| 107 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 108 | } |
| 109 | |
| 110 | // Add the mapping of regnumber => reg class to MachineFunction |
| 111 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 112 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 113 | } |
| 114 | |
| 115 | unsigned getReg(Value &V) { return getReg (&V); } // allow refs. |
| 116 | unsigned getReg(Value *V) { |
| 117 | // Just append to the end of the current bb. |
| 118 | MachineBasicBlock::iterator It = BB->end(); |
| 119 | return getReg(V, BB, It); |
| 120 | } |
| 121 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 122 | MachineBasicBlock::iterator IPt) { |
| 123 | unsigned &Reg = RegMap[V]; |
| 124 | if (Reg == 0) { |
| 125 | Reg = makeAnotherReg(V->getType()); |
| 126 | RegMap[V] = Reg; |
| 127 | } |
| 128 | // If this operand is a constant, emit the code to copy the constant into |
| 129 | // the register here... |
| 130 | // |
| 131 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 132 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 133 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
| 134 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 135 | // Move the address of the global into the register |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 136 | unsigned TmpReg = makeAnotherReg(V->getType()); |
| 137 | BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV); |
| 138 | BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg) |
| 139 | .addGlobalAddress (GV); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 140 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 141 | } |
| 142 | |
| 143 | return Reg; |
| 144 | } |
| 145 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 146 | }; |
| 147 | } |
| 148 | |
| 149 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 150 | return new V8ISel(TM); |
| 151 | } |
| 152 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 153 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 154 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static TypeClass getClass (const Type *T) { |
| 158 | switch (T->getPrimitiveID ()) { |
| 159 | case Type::UByteTyID: case Type::SByteTyID: return cByte; |
| 160 | case Type::UShortTyID: case Type::ShortTyID: return cShort; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 161 | case Type::PointerTyID: |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 162 | case Type::UIntTyID: case Type::IntTyID: return cInt; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 163 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 164 | case Type::FloatTyID: return cFloat; |
| 165 | case Type::DoubleTyID: return cDouble; |
| 166 | default: |
| 167 | assert (0 && "Type of unknown class passed to getClass?"); |
| 168 | return cByte; |
| 169 | } |
| 170 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 171 | static TypeClass getClassB(const Type *T) { |
| 172 | if (T == Type::BoolTy) return cByte; |
| 173 | return getClass(T); |
| 174 | } |
| 175 | |
| 176 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 177 | |
| 178 | /// copyConstantToRegister - Output the instructions required to put the |
| 179 | /// specified constant into the specified register. |
| 180 | /// |
| 181 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 182 | MachineBasicBlock::iterator IP, |
| 183 | Constant *C, unsigned R) { |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 184 | if (ConstantInt *CI = dyn_cast<ConstantInt> (C)) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 185 | unsigned Class = getClass(C->getType()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 186 | uint64_t Val = CI->getRawValue (); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 187 | switch (Class) { |
| 188 | case cByte: |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 189 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm((uint8_t)Val); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 190 | return; |
| 191 | case cShort: { |
| 192 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 193 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg) |
| 194 | .addImm (((uint16_t) Val) >> 10); |
| 195 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 196 | .addImm (((uint16_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | case cInt: { |
| 200 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 201 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10); |
| 202 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 203 | .addImm (((uint32_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 206 | case cLong: { |
| 207 | unsigned TmpReg = makeAnotherReg (Type::UIntTy); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 208 | uint32_t topHalf = (uint32_t) (Val >> 32); |
| 209 | uint32_t bottomHalf = (uint32_t)Val; |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 210 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (topHalf >> 10); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 211 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 212 | .addImm (topHalf & 0x03ff); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 213 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (bottomHalf >> 10); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 214 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 215 | .addImm (bottomHalf & 0x03ff); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 216 | return; |
| 217 | } |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 218 | default: |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 219 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 220 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 225 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 226 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 228 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 229 | void V8ISel::LoadArgumentsToVirtualRegs (Function *F) { |
| 230 | unsigned ArgOffset = 0; |
| 231 | static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2, |
| 232 | V8::I3, V8::I4, V8::I5 }; |
| 233 | assert (F->asize () < 7 |
| 234 | && "Can't handle loading excess call args off the stack yet"); |
| 235 | |
| 236 | for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) { |
| 237 | unsigned Reg = getReg(*I); |
| 238 | switch (getClassB(I->getType())) { |
| 239 | case cByte: |
| 240 | case cShort: |
| 241 | case cInt: |
| 242 | BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0) |
| 243 | .addReg (IncomingArgRegs[ArgOffset]); |
| 244 | break; |
| 245 | default: |
| 246 | assert (0 && "Only <=32-bit, integral arguments currently handled"); |
| 247 | return; |
| 248 | } |
| 249 | ++ArgOffset; |
| 250 | } |
| 251 | } |
| 252 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 253 | bool V8ISel::runOnFunction(Function &Fn) { |
| 254 | // First pass over the function, lower any unknown intrinsic functions |
| 255 | // with the IntrinsicLowering class. |
| 256 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 257 | |
| 258 | F = &MachineFunction::construct(&Fn, TM); |
| 259 | |
| 260 | // Create all of the machine basic blocks for the function... |
| 261 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 262 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 263 | |
| 264 | BB = &F->front(); |
| 265 | |
| 266 | // Set up a frame object for the return address. This is used by the |
| 267 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 268 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 269 | |
| 270 | // Copy incoming arguments off of the stack and out of fixed registers. |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 271 | LoadArgumentsToVirtualRegs(&Fn); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 272 | |
| 273 | // Instruction select everything except PHI nodes |
| 274 | visit(Fn); |
| 275 | |
| 276 | // Select the PHI nodes |
| 277 | //SelectPHINodes(); |
| 278 | |
| 279 | RegMap.clear(); |
| 280 | MBBMap.clear(); |
| 281 | F = 0; |
| 282 | // We always build a machine code representation for the function |
| 283 | return true; |
| 284 | } |
| 285 | |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame^] | 286 | void V8ISel::visitCastInst(CastInst &I) { |
| 287 | unsigned SrcReg = getReg (I.getOperand (0)); |
| 288 | unsigned DestReg = getReg (I.getOperand (0)); |
| 289 | const Type *oldTy = I.getOperand (0)->getType (); |
| 290 | const Type *newTy = I.getType (); |
| 291 | |
| 292 | std::cerr << "Cast instruction not supported: " << I; |
| 293 | abort (); |
| 294 | } |
| 295 | |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 296 | void V8ISel::visitLoadInst(LoadInst &I) { |
| 297 | unsigned DestReg = getReg (I); |
| 298 | unsigned PtrReg = getReg (I.getOperand (0)); |
| 299 | switch (getClass (I.getType ())) { |
| 300 | case cByte: |
| 301 | if (I.getType ()->isSigned ()) |
| 302 | BuildMI (BB, V8::LDSBmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 303 | else |
| 304 | BuildMI (BB, V8::LDUBmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 305 | return; |
| 306 | case cShort: |
| 307 | if (I.getType ()->isSigned ()) |
| 308 | BuildMI (BB, V8::LDSHmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 309 | else |
| 310 | BuildMI (BB, V8::LDUHmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 311 | return; |
| 312 | case cInt: |
| 313 | BuildMI (BB, V8::LDmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 314 | return; |
| 315 | case cLong: |
| 316 | BuildMI (BB, V8::LDDmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 317 | return; |
| 318 | default: |
| 319 | std::cerr << "Load instruction not handled: " << I; |
| 320 | abort (); |
| 321 | return; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void V8ISel::visitStoreInst(StoreInst &I) { |
| 326 | unsigned SrcReg = getReg (I.getOperand (0)); |
| 327 | unsigned PtrReg = getReg (I.getOperand (1)); |
| 328 | std::cerr << "Store instruction not handled: " << I; |
| 329 | abort (); |
| 330 | } |
| 331 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 332 | void V8ISel::visitCallInst(CallInst &I) { |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 333 | assert (I.getNumOperands () < 8 |
| 334 | && "Can't handle pushing excess call args on the stack yet"); |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 335 | static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3, |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 336 | V8::O4, V8::O5 }; |
| 337 | for (unsigned i = 1; i < 7; ++i) |
| 338 | if (i < I.getNumOperands ()) { |
| 339 | unsigned ArgReg = getReg (I.getOperand (i)); |
| 340 | // Schlep it over into the incoming arg register |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 341 | BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0) |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 342 | .addReg (ArgReg); |
| 343 | } |
| 344 | |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 345 | unsigned DestReg = getReg (I); |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 346 | BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0)); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 347 | if (I.getType ()->getPrimitiveID () == Type::VoidTyID) |
| 348 | return; |
| 349 | // Deal w/ return value |
| 350 | switch (getClass (I.getType ())) { |
| 351 | case cByte: |
| 352 | case cShort: |
| 353 | case cInt: |
| 354 | // Schlep it over into the destination register |
| 355 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 356 | break; |
| 357 | default: |
| 358 | visitInstruction (I); |
| 359 | return; |
| 360 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 362 | |
| 363 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 364 | if (I.getNumOperands () == 1) { |
| 365 | unsigned RetValReg = getReg (I.getOperand (0)); |
| 366 | switch (getClass (I.getOperand (0)->getType ())) { |
| 367 | case cByte: |
| 368 | case cShort: |
| 369 | case cInt: |
| 370 | // Schlep it over into i0 (where it will become o0 after restore). |
| 371 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 372 | break; |
| 373 | default: |
| 374 | visitInstruction (I); |
| 375 | return; |
| 376 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 378 | |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 379 | // Just emit a 'retl' instruction to return. |
| 380 | BuildMI(BB, V8::RETL, 0); |
| 381 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 384 | void V8ISel::visitBinaryOperator (Instruction &I) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 385 | unsigned DestReg = getReg (I); |
| 386 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 387 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 388 | |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 389 | unsigned ResultReg = DestReg; |
| 390 | if (getClassB(I.getType()) != cInt) |
| 391 | ResultReg = makeAnotherReg (I.getType ()); |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 392 | unsigned OpCase = ~0; |
| 393 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 394 | // FIXME: support long, ulong, fp. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 395 | switch (I.getOpcode ()) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 396 | case Instruction::Add: OpCase = 0; break; |
| 397 | case Instruction::Sub: OpCase = 1; break; |
| 398 | case Instruction::Mul: OpCase = 2; break; |
| 399 | case Instruction::And: OpCase = 3; break; |
| 400 | case Instruction::Or: OpCase = 4; break; |
| 401 | case Instruction::Xor: OpCase = 5; break; |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 402 | case Instruction::Shl: OpCase = 6; break; |
| 403 | case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 404 | |
| 405 | case Instruction::Div: |
| 406 | case Instruction::Rem: { |
| 407 | unsigned Dest = ResultReg; |
| 408 | if (I.getOpcode() == Instruction::Rem) |
| 409 | Dest = makeAnotherReg(I.getType()); |
| 410 | |
| 411 | // FIXME: this is probably only right for 32 bit operands. |
| 412 | if (I.getType ()->isSigned()) { |
| 413 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 414 | // Sign extend into the Y register |
| 415 | BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31); |
| 416 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0); |
| 417 | BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
| 418 | } else { |
| 419 | // Zero extend into the Y register, ie, just set it to zero |
| 420 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0); |
| 421 | BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 422 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 423 | |
| 424 | if (I.getOpcode() == Instruction::Rem) { |
| 425 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 426 | BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg); |
| 427 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp); |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 428 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | default: |
| 432 | visitInstruction (I); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (OpCase != ~0U) { |
| 437 | static const unsigned Opcodes[] = { |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 438 | V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr, |
| 439 | V8::SLLrr, V8::SRLrr, V8::SRArr |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 440 | }; |
| 441 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | switch (getClass (I.getType ())) { |
| 445 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 446 | if (I.getType ()->isSigned ()) { // add byte |
| 447 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 448 | } else { // add ubyte |
| 449 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 450 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 451 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 452 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 453 | break; |
| 454 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 455 | if (I.getType ()->isSigned ()) { // add short |
| 456 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 457 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 458 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 459 | } else { // add ushort |
| 460 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 461 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 462 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 463 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 464 | break; |
| 465 | case cInt: |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 466 | // Nothing todo here. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 467 | break; |
| 468 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 469 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | } |
| 473 | |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 474 | void V8ISel::visitSetCondInst(Instruction &I) { |
| 475 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 476 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 477 | unsigned DestReg = getReg (I); |
| 478 | |
| 479 | // Compare the two values. |
| 480 | BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg); |
| 481 | |
| 482 | // Put 0 into a register. |
| 483 | //unsigned ZeroReg = makeAnotheRReg(Type::IntTy); |
| 484 | //BuildMI(BB, V8::ORri, 2, ZeroReg).addReg(V8::G0).addReg(V8::G0); |
| 485 | |
| 486 | unsigned Opcode; |
| 487 | switch (I.getOpcode()) { |
| 488 | default: assert(0 && "Unknown setcc instruction!"); |
| 489 | case Instruction::SetEQ: |
| 490 | case Instruction::SetNE: |
| 491 | case Instruction::SetLT: |
| 492 | case Instruction::SetGT: |
| 493 | case Instruction::SetLE: |
| 494 | case Instruction::SetGE: |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 495 | ; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // FIXME: We need either conditional moves like the V9 has (e.g. movge), or we |
| 499 | // need to be able to turn a single LLVM basic block into multiple machine |
| 500 | // code basic blocks. For now, it probably makes sense to emit Sparc V9 |
| 501 | // instructions until the code generator is upgraded. Note that this should |
| 502 | // only happen when the setcc cannot be folded into the branch, but this needs |
| 503 | // to be handled correctly! |
| 504 | |
| 505 | visitInstruction(I); |
| 506 | } |
| 507 | |
| 508 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 509 | |
| 510 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 511 | /// function, lowering any calls to unknown intrinsic functions into the |
| 512 | /// equivalent LLVM code. |
| 513 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 514 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 515 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 516 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 517 | if (Function *F = CI->getCalledFunction()) |
| 518 | switch (F->getIntrinsicID()) { |
| 519 | case Intrinsic::not_intrinsic: break; |
| 520 | default: |
| 521 | // All other intrinsic calls we must lower. |
| 522 | Instruction *Before = CI->getPrev(); |
| 523 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 524 | if (Before) { // Move iterator to instruction after call |
| 525 | I = Before; ++I; |
| 526 | } else { |
| 527 | I = BB->begin(); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | |
| 533 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 534 | unsigned TmpReg1, TmpReg2; |
| 535 | switch (ID) { |
| 536 | default: assert(0 && "Intrinsic not supported!"); |
| 537 | } |
| 538 | } |