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 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 51 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 52 | /// constant expression GEP support. |
| 53 | /// |
| 54 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 55 | Value *Src, User::op_iterator IdxBegin, |
| 56 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 57 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 58 | /// visitBasicBlock - This method is called when we are visiting a new basic |
| 59 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 60 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 61 | /// instructions will be invoked for all instructions in the basic block. |
| 62 | /// |
| 63 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
| 64 | BB = MBBMap[&LLVM_BB]; |
| 65 | } |
| 66 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 67 | void visitBinaryOperator(Instruction &I); |
| 68 | void visitShiftInstruction(Instruction &I) { visitBinaryOperator(I); } |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 69 | void visitSetCondInst(Instruction &I); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 70 | void visitCallInst(CallInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 71 | void visitReturnInst(ReturnInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 72 | void visitBranchInst(BranchInst &I); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 73 | void visitCastInst(CastInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 74 | void visitLoadInst(LoadInst &I); |
| 75 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 76 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
| 77 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 78 | |
| 79 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 80 | |
| 81 | void visitInstruction(Instruction &I) { |
| 82 | std::cerr << "Unhandled instruction: " << I; |
| 83 | abort(); |
| 84 | } |
| 85 | |
| 86 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 87 | /// function, lowering any calls to unknown intrinsic functions into the |
| 88 | /// equivalent LLVM code. |
| 89 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 90 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 91 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 92 | void LoadArgumentsToVirtualRegs(Function *F); |
| 93 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 94 | /// copyConstantToRegister - Output the instructions required to put the |
| 95 | /// specified constant into the specified register. |
| 96 | /// |
| 97 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 98 | MachineBasicBlock::iterator IP, |
| 99 | Constant *C, unsigned R); |
| 100 | |
| 101 | /// makeAnotherReg - This method returns the next register number we haven't |
| 102 | /// yet used. |
| 103 | /// |
| 104 | /// Long values are handled somewhat specially. They are always allocated |
| 105 | /// as pairs of 32 bit integer values. The register number returned is the |
| 106 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 107 | /// of the long value. |
| 108 | /// |
| 109 | unsigned makeAnotherReg(const Type *Ty) { |
| 110 | assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) && |
| 111 | "Current target doesn't have SparcV8 reg info??"); |
| 112 | const SparcV8RegisterInfo *MRI = |
| 113 | static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()); |
| 114 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 115 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 116 | // Create the lower part |
| 117 | F->getSSARegMap()->createVirtualRegister(RC); |
| 118 | // Create the upper part. |
| 119 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 120 | } |
| 121 | |
| 122 | // Add the mapping of regnumber => reg class to MachineFunction |
| 123 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 124 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 125 | } |
| 126 | |
| 127 | unsigned getReg(Value &V) { return getReg (&V); } // allow refs. |
| 128 | unsigned getReg(Value *V) { |
| 129 | // Just append to the end of the current bb. |
| 130 | MachineBasicBlock::iterator It = BB->end(); |
| 131 | return getReg(V, BB, It); |
| 132 | } |
| 133 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 134 | MachineBasicBlock::iterator IPt) { |
| 135 | unsigned &Reg = RegMap[V]; |
| 136 | if (Reg == 0) { |
| 137 | Reg = makeAnotherReg(V->getType()); |
| 138 | RegMap[V] = Reg; |
| 139 | } |
| 140 | // If this operand is a constant, emit the code to copy the constant into |
| 141 | // the register here... |
| 142 | // |
| 143 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 144 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 145 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
| 146 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 147 | // Move the address of the global into the register |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 148 | unsigned TmpReg = makeAnotherReg(V->getType()); |
| 149 | BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV); |
| 150 | BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg) |
| 151 | .addGlobalAddress (GV); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 152 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 153 | } |
| 154 | |
| 155 | return Reg; |
| 156 | } |
| 157 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 158 | }; |
| 159 | } |
| 160 | |
| 161 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 162 | return new V8ISel(TM); |
| 163 | } |
| 164 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 165 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 166 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static TypeClass getClass (const Type *T) { |
| 170 | switch (T->getPrimitiveID ()) { |
| 171 | case Type::UByteTyID: case Type::SByteTyID: return cByte; |
| 172 | case Type::UShortTyID: case Type::ShortTyID: return cShort; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 173 | case Type::PointerTyID: |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 174 | case Type::UIntTyID: case Type::IntTyID: return cInt; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 175 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 176 | case Type::FloatTyID: return cFloat; |
| 177 | case Type::DoubleTyID: return cDouble; |
| 178 | default: |
| 179 | assert (0 && "Type of unknown class passed to getClass?"); |
| 180 | return cByte; |
| 181 | } |
| 182 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 183 | static TypeClass getClassB(const Type *T) { |
| 184 | if (T == Type::BoolTy) return cByte; |
| 185 | return getClass(T); |
| 186 | } |
| 187 | |
| 188 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 189 | |
| 190 | /// copyConstantToRegister - Output the instructions required to put the |
| 191 | /// specified constant into the specified register. |
| 192 | /// |
| 193 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 194 | MachineBasicBlock::iterator IP, |
| 195 | Constant *C, unsigned R) { |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 196 | if (C->getType()->isIntegral ()) { |
| 197 | uint64_t Val; |
| 198 | if (C->getType() == Type::BoolTy) { |
| 199 | Val = (C == ConstantBool::True); |
| 200 | } else { |
| 201 | ConstantInt *CI = dyn_cast<ConstantInt> (C); |
| 202 | Val = CI->getRawValue (); |
| 203 | } |
| 204 | switch (getClassB (C->getType ())) { |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 205 | case cByte: |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 206 | 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] | 207 | return; |
| 208 | case cShort: { |
| 209 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 210 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg) |
| 211 | .addImm (((uint16_t) Val) >> 10); |
| 212 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 213 | .addImm (((uint16_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | case cInt: { |
| 217 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 218 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10); |
| 219 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 220 | .addImm (((uint32_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 223 | case cLong: { |
| 224 | unsigned TmpReg = makeAnotherReg (Type::UIntTy); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 225 | uint32_t topHalf = (uint32_t) (Val >> 32); |
| 226 | uint32_t bottomHalf = (uint32_t)Val; |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 227 | #if 0 // FIXME: This does not appear to be correct; it assigns SSA reg R twice. |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 228 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (topHalf >> 10); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 229 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 230 | .addImm (topHalf & 0x03ff); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 231 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm (bottomHalf >> 10); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 232 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 233 | .addImm (bottomHalf & 0x03ff); |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 234 | #else |
| 235 | std::cerr << "Offending constant: " << *C << "\n"; |
| 236 | assert (0 && "Can't copy this kind of constant into register yet"); |
| 237 | #endif |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 240 | default: |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 241 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 242 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 247 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 248 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 250 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 251 | void V8ISel::LoadArgumentsToVirtualRegs (Function *F) { |
| 252 | unsigned ArgOffset = 0; |
| 253 | static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2, |
| 254 | V8::I3, V8::I4, V8::I5 }; |
| 255 | assert (F->asize () < 7 |
| 256 | && "Can't handle loading excess call args off the stack yet"); |
| 257 | |
| 258 | for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) { |
| 259 | unsigned Reg = getReg(*I); |
| 260 | switch (getClassB(I->getType())) { |
| 261 | case cByte: |
| 262 | case cShort: |
| 263 | case cInt: |
| 264 | BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0) |
| 265 | .addReg (IncomingArgRegs[ArgOffset]); |
| 266 | break; |
| 267 | default: |
| 268 | assert (0 && "Only <=32-bit, integral arguments currently handled"); |
| 269 | return; |
| 270 | } |
| 271 | ++ArgOffset; |
| 272 | } |
| 273 | } |
| 274 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 275 | bool V8ISel::runOnFunction(Function &Fn) { |
| 276 | // First pass over the function, lower any unknown intrinsic functions |
| 277 | // with the IntrinsicLowering class. |
| 278 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 279 | |
| 280 | F = &MachineFunction::construct(&Fn, TM); |
| 281 | |
| 282 | // Create all of the machine basic blocks for the function... |
| 283 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 284 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 285 | |
| 286 | BB = &F->front(); |
| 287 | |
| 288 | // Set up a frame object for the return address. This is used by the |
| 289 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 290 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 291 | |
| 292 | // Copy incoming arguments off of the stack and out of fixed registers. |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 293 | LoadArgumentsToVirtualRegs(&Fn); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 294 | |
| 295 | // Instruction select everything except PHI nodes |
| 296 | visit(Fn); |
| 297 | |
| 298 | // Select the PHI nodes |
| 299 | //SelectPHINodes(); |
| 300 | |
| 301 | RegMap.clear(); |
| 302 | MBBMap.clear(); |
| 303 | F = 0; |
| 304 | // We always build a machine code representation for the function |
| 305 | return true; |
| 306 | } |
| 307 | |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 308 | void V8ISel::visitCastInst(CastInst &I) { |
| 309 | unsigned SrcReg = getReg (I.getOperand (0)); |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 310 | unsigned DestReg = getReg (I); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 311 | const Type *oldTy = I.getOperand (0)->getType (); |
| 312 | const Type *newTy = I.getType (); |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 313 | unsigned oldTyClass = getClassB (oldTy); |
| 314 | unsigned newTyClass = getClassB (newTy); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 315 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame^] | 316 | if (oldTyClass < cLong && newTyClass < cLong) { |
| 317 | if (oldTyClass >= newTyClass) { |
| 318 | // Emit a reg->reg copy to do a equal-size or narrowing cast, |
| 319 | // and do sign/zero extension (necessary if we change signedness). |
| 320 | unsigned TmpReg1 = makeAnotherReg (newTy); |
| 321 | unsigned TmpReg2 = makeAnotherReg (newTy); |
| 322 | BuildMI (BB, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg); |
| 323 | unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy)); |
| 324 | BuildMI (BB, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1); |
| 325 | if (newTy->isSigned ()) { // sign-extend with SRA |
| 326 | BuildMI(BB, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2); |
| 327 | } else { // zero-extend with SRL |
| 328 | BuildMI(BB, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2); |
| 329 | } |
| 330 | } else { |
| 331 | unsigned TmpReg1 = makeAnotherReg (oldTy); |
| 332 | unsigned TmpReg2 = makeAnotherReg (newTy); |
| 333 | unsigned TmpReg3 = makeAnotherReg (newTy); |
| 334 | // Widening integer cast. Make sure it's fully sign/zero-extended |
| 335 | // wrt the input type, then make sure it's fully sign/zero-extended wrt |
| 336 | // the output type. Kind of stupid, but simple... |
| 337 | unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy)); |
| 338 | BuildMI (BB, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg); |
| 339 | if (oldTy->isSigned ()) { // sign-extend with SRA |
| 340 | BuildMI(BB, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1); |
| 341 | } else { // zero-extend with SRL |
| 342 | BuildMI(BB, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1); |
| 343 | } |
| 344 | shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy)); |
| 345 | BuildMI (BB, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2); |
| 346 | if (newTy->isSigned ()) { // sign-extend with SRA |
| 347 | BuildMI(BB, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3); |
| 348 | } else { // zero-extend with SRL |
| 349 | BuildMI(BB, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3); |
| 350 | } |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 351 | } |
| 352 | } else { |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame^] | 353 | std::cerr << "Casts w/ long, fp, double still unsupported: " << I; |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 354 | abort (); |
| 355 | } |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 358 | void V8ISel::visitLoadInst(LoadInst &I) { |
| 359 | unsigned DestReg = getReg (I); |
| 360 | unsigned PtrReg = getReg (I.getOperand (0)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 361 | switch (getClassB (I.getType ())) { |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 362 | case cByte: |
| 363 | if (I.getType ()->isSigned ()) |
| 364 | BuildMI (BB, V8::LDSBmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 365 | else |
| 366 | BuildMI (BB, V8::LDUBmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 367 | return; |
| 368 | case cShort: |
| 369 | if (I.getType ()->isSigned ()) |
| 370 | BuildMI (BB, V8::LDSHmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 371 | else |
| 372 | BuildMI (BB, V8::LDUHmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 373 | return; |
| 374 | case cInt: |
| 375 | BuildMI (BB, V8::LDmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 376 | return; |
| 377 | case cLong: |
| 378 | BuildMI (BB, V8::LDDmr, 1, DestReg).addReg (PtrReg).addSImm(0); |
| 379 | return; |
| 380 | default: |
| 381 | std::cerr << "Load instruction not handled: " << I; |
| 382 | abort (); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void V8ISel::visitStoreInst(StoreInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 388 | Value *SrcVal = I.getOperand (0); |
| 389 | unsigned SrcReg = getReg (SrcVal); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 390 | unsigned PtrReg = getReg (I.getOperand (1)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 391 | switch (getClassB (SrcVal->getType ())) { |
| 392 | case cByte: |
| 393 | BuildMI (BB, V8::STBrm, 1, SrcReg).addReg (PtrReg).addSImm(0); |
| 394 | return; |
| 395 | case cShort: |
| 396 | BuildMI (BB, V8::STHrm, 1, SrcReg).addReg (PtrReg).addSImm(0); |
| 397 | return; |
| 398 | case cInt: |
| 399 | BuildMI (BB, V8::STrm, 1, SrcReg).addReg (PtrReg).addSImm(0); |
| 400 | return; |
| 401 | case cLong: |
| 402 | BuildMI (BB, V8::STDrm, 1, SrcReg).addReg (PtrReg).addSImm(0); |
| 403 | return; |
| 404 | default: |
| 405 | std::cerr << "Store instruction not handled: " << I; |
| 406 | abort (); |
| 407 | return; |
| 408 | } |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 411 | void V8ISel::visitCallInst(CallInst &I) { |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 412 | assert (I.getNumOperands () < 8 |
| 413 | && "Can't handle pushing excess call args on the stack yet"); |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 414 | static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3, |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 415 | V8::O4, V8::O5 }; |
| 416 | for (unsigned i = 1; i < 7; ++i) |
| 417 | if (i < I.getNumOperands ()) { |
| 418 | unsigned ArgReg = getReg (I.getOperand (i)); |
| 419 | // Schlep it over into the incoming arg register |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 420 | BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0) |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 421 | .addReg (ArgReg); |
| 422 | } |
| 423 | |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 424 | unsigned DestReg = getReg (I); |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 425 | BuildMI (BB, V8::CALL, 1).addPCDisp (I.getOperand (0)); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 426 | if (I.getType ()->getPrimitiveID () == Type::VoidTyID) |
| 427 | return; |
| 428 | // Deal w/ return value |
| 429 | switch (getClass (I.getType ())) { |
| 430 | case cByte: |
| 431 | case cShort: |
| 432 | case cInt: |
| 433 | // Schlep it over into the destination register |
| 434 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 435 | break; |
| 436 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 437 | std::cerr << "Return type of call instruction not handled: " << I; |
| 438 | abort (); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 439 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 441 | |
| 442 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 443 | if (I.getNumOperands () == 1) { |
| 444 | unsigned RetValReg = getReg (I.getOperand (0)); |
| 445 | switch (getClass (I.getOperand (0)->getType ())) { |
| 446 | case cByte: |
| 447 | case cShort: |
| 448 | case cInt: |
| 449 | // Schlep it over into i0 (where it will become o0 after restore). |
| 450 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 451 | break; |
| 452 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 453 | std::cerr << "Return instruction of this type not handled: " << I; |
| 454 | abort (); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 455 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 456 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 457 | |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 458 | // Just emit a 'retl' instruction to return. |
| 459 | BuildMI(BB, V8::RETL, 0); |
| 460 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 463 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 464 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 465 | return I != BB->getParent()->end() ? &*I : 0; |
| 466 | } |
| 467 | |
| 468 | /// visitBranchInst - Handles conditional and unconditional branches. |
| 469 | /// |
| 470 | void V8ISel::visitBranchInst(BranchInst &I) { |
| 471 | // Update machine-CFG edges |
| 472 | BB->addSuccessor (MBBMap[I.getSuccessor(0)]); |
| 473 | if (I.isConditional()) |
| 474 | BB->addSuccessor (MBBMap[I.getSuccessor(1)]); |
| 475 | |
| 476 | BasicBlock *NextBB = getBlockAfter(I.getParent()); // BB after current one |
| 477 | |
| 478 | BasicBlock *takenSucc = I.getSuccessor (0); |
| 479 | if (!I.isConditional()) { // Unconditional branch? |
| 480 | if (I.getSuccessor(0) != NextBB) |
| 481 | BuildMI (BB, V8::BA, 1).addPCDisp (takenSucc); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | unsigned CondReg = getReg (I.getCondition ()); |
| 486 | BasicBlock *notTakenSucc = I.getSuccessor (1); |
| 487 | // Set Z condition code if CondReg was false |
| 488 | BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg); |
| 489 | if (notTakenSucc == NextBB) { |
| 490 | if (takenSucc != NextBB) |
| 491 | BuildMI (BB, V8::BNE, 1).addPCDisp (takenSucc); |
| 492 | } else { |
| 493 | BuildMI (BB, V8::BE, 1).addPCDisp (notTakenSucc); |
| 494 | if (takenSucc != NextBB) |
| 495 | BuildMI (BB, V8::BA, 1).addPCDisp (takenSucc); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 500 | /// constant expression GEP support. |
| 501 | /// |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 502 | void V8ISel::emitGEPOperation (MachineBasicBlock *MBB, |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 503 | MachineBasicBlock::iterator IP, |
| 504 | Value *Src, User::op_iterator IdxBegin, |
| 505 | User::op_iterator IdxEnd, unsigned TargetReg) { |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 506 | const TargetData &TD = TM.getTargetData (); |
| 507 | const Type *Ty = Src->getType (); |
| 508 | unsigned basePtrReg = getReg (Src); |
| 509 | |
| 510 | // GEPs have zero or more indices; we must perform a struct access |
| 511 | // or array access for each one. |
| 512 | for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe; |
| 513 | ++oi) { |
| 514 | Value *idx = *oi; |
| 515 | unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy); |
| 516 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 517 | // It's a struct access. idx is the index into the structure, |
| 518 | // which names the field. Use the TargetData structure to |
| 519 | // pick out what the layout of the structure is in memory. |
| 520 | // Use the (constant) structure index's value to find the |
| 521 | // right byte offset from the StructLayout class's list of |
| 522 | // structure member offsets. |
| 523 | unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue (); |
| 524 | unsigned memberOffset = |
| 525 | TD.getStructLayout (StTy)->MemberOffsets[fieldIndex]; |
| 526 | // Emit an ADD to add memberOffset to the basePtr. |
| 527 | BuildMI (*MBB, IP, V8::ADDri, 2, |
| 528 | nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset); |
| 529 | // The next type is the member of the structure selected by the |
| 530 | // index. |
| 531 | Ty = StTy->getElementType (fieldIndex); |
| 532 | } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) { |
| 533 | // It's an array or pointer access: [ArraySize x ElementType]. |
| 534 | // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we |
| 535 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 536 | // type is the type of the elements in the array). |
| 537 | Ty = SqTy->getElementType (); |
| 538 | unsigned elementSize = TD.getTypeSize (Ty); |
| 539 | unsigned idxReg = getReg (idx, MBB, IP); |
| 540 | unsigned OffsetReg = makeAnotherReg (Type::IntTy); |
| 541 | unsigned elementSizeReg = makeAnotherReg (Type::UIntTy); |
| 542 | BuildMI (*MBB, IP, V8::ORri, 2, |
| 543 | elementSizeReg).addZImm (elementSize).addReg (V8::G0); |
| 544 | // Emit a SMUL to multiply the register holding the index by |
| 545 | // elementSize, putting the result in OffsetReg. |
| 546 | BuildMI (*MBB, IP, V8::SMULrr, 2, |
| 547 | OffsetReg).addReg (elementSizeReg).addReg (idxReg); |
| 548 | // Emit an ADD to add OffsetReg to the basePtr. |
| 549 | BuildMI (*MBB, IP, V8::ADDrr, 2, |
| 550 | nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg); |
| 551 | } |
| 552 | basePtrReg = nextBasePtrReg; |
| 553 | } |
| 554 | // After we have processed all the indices, the result is left in |
| 555 | // basePtrReg. Move it to the register where we were expected to |
| 556 | // put the answer. |
| 557 | BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) { |
| 561 | unsigned outputReg = getReg (I); |
| 562 | emitGEPOperation (BB, BB->end (), I.getOperand (0), |
| 563 | I.op_begin ()+1, I.op_end (), outputReg); |
| 564 | } |
| 565 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 566 | void V8ISel::visitBinaryOperator (Instruction &I) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 567 | unsigned DestReg = getReg (I); |
| 568 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 569 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 570 | |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 571 | unsigned ResultReg = DestReg; |
| 572 | if (getClassB(I.getType()) != cInt) |
| 573 | ResultReg = makeAnotherReg (I.getType ()); |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 574 | unsigned OpCase = ~0; |
| 575 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 576 | // FIXME: support long, ulong, fp. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 577 | switch (I.getOpcode ()) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 578 | case Instruction::Add: OpCase = 0; break; |
| 579 | case Instruction::Sub: OpCase = 1; break; |
| 580 | case Instruction::Mul: OpCase = 2; break; |
| 581 | case Instruction::And: OpCase = 3; break; |
| 582 | case Instruction::Or: OpCase = 4; break; |
| 583 | case Instruction::Xor: OpCase = 5; break; |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 584 | case Instruction::Shl: OpCase = 6; break; |
| 585 | case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 586 | |
| 587 | case Instruction::Div: |
| 588 | case Instruction::Rem: { |
| 589 | unsigned Dest = ResultReg; |
| 590 | if (I.getOpcode() == Instruction::Rem) |
| 591 | Dest = makeAnotherReg(I.getType()); |
| 592 | |
| 593 | // FIXME: this is probably only right for 32 bit operands. |
| 594 | if (I.getType ()->isSigned()) { |
| 595 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 596 | // Sign extend into the Y register |
| 597 | BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31); |
| 598 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0); |
| 599 | BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
| 600 | } else { |
| 601 | // Zero extend into the Y register, ie, just set it to zero |
| 602 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0); |
| 603 | BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 605 | |
| 606 | if (I.getOpcode() == Instruction::Rem) { |
| 607 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 608 | BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg); |
| 609 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp); |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 610 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 611 | break; |
| 612 | } |
| 613 | default: |
| 614 | visitInstruction (I); |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | if (OpCase != ~0U) { |
| 619 | static const unsigned Opcodes[] = { |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 620 | V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr, |
| 621 | V8::SLLrr, V8::SRLrr, V8::SRArr |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 622 | }; |
| 623 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | switch (getClass (I.getType ())) { |
| 627 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 628 | if (I.getType ()->isSigned ()) { // add byte |
| 629 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 630 | } else { // add ubyte |
| 631 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 632 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 633 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 634 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 635 | break; |
| 636 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 637 | if (I.getType ()->isSigned ()) { // add short |
| 638 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 639 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 640 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 641 | } else { // add ushort |
| 642 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 643 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 644 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 645 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 646 | break; |
| 647 | case cInt: |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 648 | // Nothing todo here. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 649 | break; |
| 650 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 651 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 652 | return; |
| 653 | } |
| 654 | } |
| 655 | |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 656 | void V8ISel::visitSetCondInst(Instruction &I) { |
| 657 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 658 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 659 | unsigned DestReg = getReg (I); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame^] | 660 | const Type *Ty = I.getOperand (0)->getType (); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 661 | |
| 662 | // Compare the two values. |
| 663 | BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg); |
| 664 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame^] | 665 | unsigned BranchIdx; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 666 | switch (I.getOpcode()) { |
| 667 | default: assert(0 && "Unknown setcc instruction!"); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame^] | 668 | case Instruction::SetEQ: BranchIdx = 0; break; |
| 669 | case Instruction::SetNE: BranchIdx = 1; break; |
| 670 | case Instruction::SetLT: BranchIdx = 2; break; |
| 671 | case Instruction::SetGT: BranchIdx = 3; break; |
| 672 | case Instruction::SetLE: BranchIdx = 4; break; |
| 673 | case Instruction::SetGE: BranchIdx = 5; break; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 674 | } |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame^] | 675 | static unsigned OpcodeTab[12] = { |
| 676 | // LLVM SparcV8 |
| 677 | // unsigned signed |
| 678 | V8::BE, V8::BE, // seteq = be be |
| 679 | V8::BNE, V8::BNE, // setne = bne bne |
| 680 | V8::BCS, V8::BL, // setlt = bcs bl |
| 681 | V8::BGU, V8::BG, // setgt = bgu bg |
| 682 | V8::BLEU, V8::BLE, // setle = bleu ble |
| 683 | V8::BCC, V8::BGE // setge = bcc bge |
| 684 | }; |
| 685 | unsigned Opcode = OpcodeTab[BranchIdx + (Ty->isSigned() ? 1 : 0)]; |
| 686 | MachineBasicBlock *Copy1MBB, *Copy0MBB, *CopyCondMBB; |
| 687 | MachineBasicBlock::iterator IP; |
| 688 | #if 0 |
| 689 | // Cond. Branch from BB --> either Copy1MBB or Copy0MBB --> CopyCondMBB |
| 690 | // Then once we're done with the SetCC, BB = CopyCondMBB. |
| 691 | BasicBlock *LLVM_BB = BB.getBasicBlock (); |
| 692 | unsigned Cond0Reg = makeAnotherReg (I.getType ()); |
| 693 | unsigned Cond1Reg = makeAnotherReg (I.getType ()); |
| 694 | F->getBasicBlockList ().push_back (Copy1MBB = new MachineBasicBlock (LLVM_BB)); |
| 695 | F->getBasicBlockList ().push_back (Copy0MBB = new MachineBasicBlock (LLVM_BB)); |
| 696 | F->getBasicBlockList ().push_back (CopyCondMBB = new MachineBasicBlock (LLVM_BB)); |
| 697 | BuildMI (BB, Opcode, 1).addMBB (Copy1MBB); |
| 698 | BuildMI (BB, V8::BA, 1).addMBB (Copy0MBB); |
| 699 | IP = Copy1MBB->begin (); |
| 700 | BuildMI (*Copy1MBB, IP, V8::ORri, 2, Cond1Reg).addZImm (1).addReg (V8::G0); |
| 701 | BuildMI (*Copy1MBB, IP, V8::BA, 1).addMBB (CopyCondMBB); |
| 702 | IP = Copy0MBB->begin (); |
| 703 | BuildMI (*Copy0MBB, IP, V8::ORri, 2, Cond0Reg).addZImm (0).addReg (V8::G0); |
| 704 | BuildMI (*Copy0MBB, IP, V8::BA, 1).addMBB (CopyCondMBB); |
| 705 | // What should go in CopyCondMBB: PHI, then OR to copy cond. reg to DestReg |
| 706 | #endif |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 707 | visitInstruction(I); |
| 708 | } |
| 709 | |
| 710 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 711 | |
| 712 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 713 | /// function, lowering any calls to unknown intrinsic functions into the |
| 714 | /// equivalent LLVM code. |
| 715 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 716 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 717 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 718 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 719 | if (Function *F = CI->getCalledFunction()) |
| 720 | switch (F->getIntrinsicID()) { |
| 721 | case Intrinsic::not_intrinsic: break; |
| 722 | default: |
| 723 | // All other intrinsic calls we must lower. |
| 724 | Instruction *Before = CI->getPrev(); |
| 725 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 726 | if (Before) { // Move iterator to instruction after call |
| 727 | I = Before; ++I; |
| 728 | } else { |
| 729 | I = BB->begin(); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | |
| 735 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 736 | unsigned TmpReg1, TmpReg2; |
| 737 | switch (ID) { |
| 738 | default: assert(0 && "Intrinsic not supported!"); |
| 739 | } |
| 740 | } |