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" |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 16 | #include "Support/Debug.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 3048373 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 28 | #include "llvm/Support/InstVisitor.h" |
| 29 | #include "llvm/Support/CFG.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | struct V8ISel : public FunctionPass, public InstVisitor<V8ISel> { |
| 34 | TargetMachine &TM; |
| 35 | MachineFunction *F; // The function we are compiling into |
| 36 | MachineBasicBlock *BB; // The current MBB we are compiling |
| 37 | |
| 38 | std::map<Value*, unsigned> RegMap; // Mapping between Val's and SSA Regs |
| 39 | |
| 40 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 41 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 42 | |
| 43 | V8ISel(TargetMachine &tm) : TM(tm), F(0), BB(0) {} |
| 44 | |
| 45 | /// runOnFunction - Top level implementation of instruction selection for |
| 46 | /// the entire function. |
| 47 | /// |
| 48 | bool runOnFunction(Function &Fn); |
| 49 | |
| 50 | virtual const char *getPassName() const { |
| 51 | return "SparcV8 Simple Instruction Selection"; |
| 52 | } |
| 53 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 54 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 55 | /// constant expression GEP support. |
| 56 | /// |
| 57 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 58 | Value *Src, User::op_iterator IdxBegin, |
| 59 | User::op_iterator IdxEnd, unsigned TargetReg); |
| 60 | |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 61 | /// emitCastOperation - Common code shared between visitCastInst and |
| 62 | /// constant expression cast support. |
| 63 | /// |
| 64 | void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP, |
| 65 | Value *Src, const Type *DestTy, unsigned TargetReg); |
| 66 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 67 | /// visitBasicBlock - This method is called when we are visiting a new basic |
| 68 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 69 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 70 | /// instructions will be invoked for all instructions in the basic block. |
| 71 | /// |
| 72 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
| 73 | BB = MBBMap[&LLVM_BB]; |
| 74 | } |
| 75 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 76 | void visitBinaryOperator(Instruction &I); |
Brian Gaeke | d6a1053 | 2004-06-15 21:09:46 +0000 | [diff] [blame] | 77 | void visitShiftInst (ShiftInst &SI) { visitBinaryOperator (SI); } |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 78 | void visitSetCondInst(Instruction &I); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 79 | void visitCallInst(CallInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 80 | void visitReturnInst(ReturnInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 81 | void visitBranchInst(BranchInst &I); |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 82 | void visitCastInst(CastInst &I); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 83 | void visitLoadInst(LoadInst &I); |
| 84 | void visitStoreInst(StoreInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 85 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
| 86 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 87 | void visitAllocaInst(AllocaInst &I); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 88 | |
| 89 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 90 | |
| 91 | void visitInstruction(Instruction &I) { |
| 92 | std::cerr << "Unhandled instruction: " << I; |
| 93 | abort(); |
| 94 | } |
| 95 | |
| 96 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 97 | /// function, lowering any calls to unknown intrinsic functions into the |
| 98 | /// equivalent LLVM code. |
| 99 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 100 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI); |
| 101 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 102 | void LoadArgumentsToVirtualRegs(Function *F); |
| 103 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 104 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 105 | /// because we have to generate our sources into the source basic blocks, |
| 106 | /// not the current one. |
| 107 | /// |
| 108 | void SelectPHINodes(); |
| 109 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 110 | /// copyConstantToRegister - Output the instructions required to put the |
| 111 | /// specified constant into the specified register. |
| 112 | /// |
| 113 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 114 | MachineBasicBlock::iterator IP, |
| 115 | Constant *C, unsigned R); |
| 116 | |
| 117 | /// makeAnotherReg - This method returns the next register number we haven't |
| 118 | /// yet used. |
| 119 | /// |
| 120 | /// Long values are handled somewhat specially. They are always allocated |
| 121 | /// as pairs of 32 bit integer values. The register number returned is the |
| 122 | /// lower 32 bits of the long value, and the regNum+1 is the upper 32 bits |
| 123 | /// of the long value. |
| 124 | /// |
| 125 | unsigned makeAnotherReg(const Type *Ty) { |
| 126 | assert(dynamic_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()) && |
| 127 | "Current target doesn't have SparcV8 reg info??"); |
| 128 | const SparcV8RegisterInfo *MRI = |
| 129 | static_cast<const SparcV8RegisterInfo*>(TM.getRegisterInfo()); |
| 130 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
| 131 | const TargetRegisterClass *RC = MRI->getRegClassForType(Type::IntTy); |
| 132 | // Create the lower part |
| 133 | F->getSSARegMap()->createVirtualRegister(RC); |
| 134 | // Create the upper part. |
| 135 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 136 | } |
| 137 | |
| 138 | // Add the mapping of regnumber => reg class to MachineFunction |
| 139 | const TargetRegisterClass *RC = MRI->getRegClassForType(Ty); |
| 140 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 141 | } |
| 142 | |
| 143 | unsigned getReg(Value &V) { return getReg (&V); } // allow refs. |
| 144 | unsigned getReg(Value *V) { |
| 145 | // Just append to the end of the current bb. |
| 146 | MachineBasicBlock::iterator It = BB->end(); |
| 147 | return getReg(V, BB, It); |
| 148 | } |
| 149 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 150 | MachineBasicBlock::iterator IPt) { |
| 151 | unsigned &Reg = RegMap[V]; |
| 152 | if (Reg == 0) { |
| 153 | Reg = makeAnotherReg(V->getType()); |
| 154 | RegMap[V] = Reg; |
| 155 | } |
| 156 | // If this operand is a constant, emit the code to copy the constant into |
| 157 | // the register here... |
| 158 | // |
| 159 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 160 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 161 | RegMap.erase(V); // Assign a new name to this constant if ref'd again |
| 162 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 163 | // Move the address of the global into the register |
Brian Gaeke | cf47198 | 2004-03-09 04:49:13 +0000 | [diff] [blame] | 164 | unsigned TmpReg = makeAnotherReg(V->getType()); |
| 165 | BuildMI (*MBB, IPt, V8::SETHIi, 1, TmpReg).addGlobalAddress (GV); |
| 166 | BuildMI (*MBB, IPt, V8::ORri, 2, Reg).addReg (TmpReg) |
| 167 | .addGlobalAddress (GV); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 168 | RegMap.erase(V); // Assign a new name to this address if ref'd again |
| 169 | } |
| 170 | |
| 171 | return Reg; |
| 172 | } |
| 173 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 174 | }; |
| 175 | } |
| 176 | |
| 177 | FunctionPass *llvm::createSparcV8SimpleInstructionSelector(TargetMachine &TM) { |
| 178 | return new V8ISel(TM); |
| 179 | } |
| 180 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 181 | enum TypeClass { |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 182 | cByte, cShort, cInt, cLong, cFloat, cDouble |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static TypeClass getClass (const Type *T) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 186 | switch (T->getTypeID()) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 187 | case Type::UByteTyID: case Type::SByteTyID: return cByte; |
| 188 | case Type::UShortTyID: case Type::ShortTyID: return cShort; |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 189 | case Type::PointerTyID: |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 190 | case Type::UIntTyID: case Type::IntTyID: return cInt; |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 191 | case Type::ULongTyID: case Type::LongTyID: return cLong; |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 192 | case Type::FloatTyID: return cFloat; |
| 193 | case Type::DoubleTyID: return cDouble; |
| 194 | default: |
| 195 | assert (0 && "Type of unknown class passed to getClass?"); |
| 196 | return cByte; |
| 197 | } |
| 198 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 199 | static TypeClass getClassB(const Type *T) { |
| 200 | if (T == Type::BoolTy) return cByte; |
| 201 | return getClass(T); |
| 202 | } |
| 203 | |
| 204 | |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 205 | |
| 206 | /// copyConstantToRegister - Output the instructions required to put the |
| 207 | /// specified constant into the specified register. |
| 208 | /// |
| 209 | void V8ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 210 | MachineBasicBlock::iterator IP, |
| 211 | Constant *C, unsigned R) { |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 212 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 213 | switch (CE->getOpcode()) { |
| 214 | case Instruction::GetElementPtr: |
| 215 | emitGEPOperation(MBB, IP, CE->getOperand(0), |
| 216 | CE->op_begin()+1, CE->op_end(), R); |
| 217 | return; |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 218 | case Instruction::Cast: |
| 219 | emitCastOperation(MBB, IP, CE->getOperand(0), CE->getType(), R); |
| 220 | return; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 221 | default: |
| 222 | std::cerr << "Copying this constant expr not yet handled: " << *CE; |
| 223 | abort(); |
| 224 | } |
| 225 | } |
| 226 | |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 227 | if (C->getType()->isIntegral ()) { |
| 228 | uint64_t Val; |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 229 | unsigned Class = getClassB (C->getType ()); |
| 230 | if (Class == cLong) { |
| 231 | unsigned TmpReg = makeAnotherReg (Type::IntTy); |
| 232 | unsigned TmpReg2 = makeAnotherReg (Type::IntTy); |
| 233 | // Copy the value into the register pair. |
| 234 | // R = top(more-significant) half, R+1 = bottom(less-significant) half |
| 235 | uint64_t Val = cast<ConstantInt>(C)->getRawValue(); |
| 236 | unsigned topHalf = Val & 0xffffffffU; |
| 237 | unsigned bottomHalf = Val >> 32; |
| 238 | unsigned HH = topHalf >> 10; |
| 239 | unsigned HM = topHalf & 0x03ff; |
| 240 | unsigned LM = bottomHalf >> 10; |
| 241 | unsigned LO = bottomHalf & 0x03ff; |
| 242 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(HH); |
| 243 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 244 | .addImm (HM); |
| 245 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg2).addImm(LM); |
| 246 | BuildMI (*MBB, IP, V8::ORri, 2, R+1).addReg (TmpReg2) |
| 247 | .addImm (LO); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | assert(Class <= cInt && "Type not handled yet!"); |
| 252 | |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 253 | if (C->getType() == Type::BoolTy) { |
| 254 | Val = (C == ConstantBool::True); |
| 255 | } else { |
| 256 | ConstantInt *CI = dyn_cast<ConstantInt> (C); |
| 257 | Val = CI->getRawValue (); |
| 258 | } |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 259 | switch (Class) { |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 260 | case cByte: |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 261 | 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] | 262 | return; |
| 263 | case cShort: { |
| 264 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 265 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg) |
| 266 | .addImm (((uint16_t) Val) >> 10); |
| 267 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 268 | .addImm (((uint16_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | case cInt: { |
| 272 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 273 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addImm(((uint32_t)Val) >> 10); |
| 274 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 275 | .addImm (((uint32_t) Val) & 0x03ff); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | default: |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 279 | std::cerr << "Offending constant: " << *C << "\n"; |
Brian Gaeke | 775158d | 2004-03-04 04:37:45 +0000 | [diff] [blame] | 280 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | e806173 | 2004-03-04 00:56:25 +0000 | [diff] [blame] | 281 | return; |
| 282 | } |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 283 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 284 | // We need to spill the constant to memory... |
| 285 | MachineConstantPool *CP = F->getConstantPool(); |
| 286 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 287 | const Type *Ty = CFP->getType(); |
| 288 | |
| 289 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 290 | unsigned LoadOpcode = Ty == Type::FloatTy ? V8::LDFri : V8::LDDFri; |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 291 | BuildMI (*MBB, IP, LoadOpcode, 2, R).addConstantPoolIndex (CPI).addSImm (0); |
Brian Gaeke | 9df9282 | 2004-06-15 19:16:07 +0000 | [diff] [blame] | 292 | } else if (isa<ConstantPointerNull>(C)) { |
| 293 | // Copy zero (null pointer) to the register. |
| 294 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (V8::G0).addImm (0); |
| 295 | } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) { |
| 296 | // Copy it with a SETHI/OR pair; the JIT + asmwriter should recognize |
| 297 | // that SETHI %reg,global == SETHI %reg,%hi(global) and |
| 298 | // OR %reg,global,%reg == OR %reg,%lo(global),%reg. |
| 299 | unsigned TmpReg = makeAnotherReg (C->getType ()); |
| 300 | BuildMI (*MBB, IP, V8::SETHIi, 1, TmpReg).addGlobalAddress (CPR->getValue()); |
| 301 | BuildMI (*MBB, IP, V8::ORri, 2, R).addReg (TmpReg) |
| 302 | .addGlobalAddress (CPR->getValue ()); |
| 303 | } else { |
| 304 | std::cerr << "Offending constant: " << *C << "\n"; |
| 305 | assert (0 && "Can't copy this kind of constant into register yet"); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 306 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 308 | |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 309 | void V8ISel::LoadArgumentsToVirtualRegs (Function *F) { |
| 310 | unsigned ArgOffset = 0; |
| 311 | static const unsigned IncomingArgRegs[] = { V8::I0, V8::I1, V8::I2, |
| 312 | V8::I3, V8::I4, V8::I5 }; |
| 313 | assert (F->asize () < 7 |
| 314 | && "Can't handle loading excess call args off the stack yet"); |
| 315 | |
| 316 | for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) { |
| 317 | unsigned Reg = getReg(*I); |
| 318 | switch (getClassB(I->getType())) { |
| 319 | case cByte: |
| 320 | case cShort: |
| 321 | case cInt: |
| 322 | BuildMI(BB, V8::ORrr, 2, Reg).addReg (V8::G0) |
| 323 | .addReg (IncomingArgRegs[ArgOffset]); |
| 324 | break; |
| 325 | default: |
| 326 | assert (0 && "Only <=32-bit, integral arguments currently handled"); |
| 327 | return; |
| 328 | } |
| 329 | ++ArgOffset; |
| 330 | } |
| 331 | } |
| 332 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 333 | void V8ISel::SelectPHINodes() { |
| 334 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 335 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 336 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 337 | const BasicBlock *BB = I; |
| 338 | MachineBasicBlock &MBB = *MBBMap[I]; |
| 339 | |
| 340 | // Loop over all of the PHI nodes in the LLVM basic block... |
| 341 | MachineBasicBlock::iterator PHIInsertPoint = MBB.begin(); |
| 342 | for (BasicBlock::const_iterator I = BB->begin(); |
| 343 | PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) { |
| 344 | |
| 345 | // Create a new machine instr PHI node, and insert it. |
| 346 | unsigned PHIReg = getReg(*PN); |
| 347 | MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint, |
| 348 | V8::PHI, PN->getNumOperands(), PHIReg); |
| 349 | |
| 350 | MachineInstr *LongPhiMI = 0; |
| 351 | if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) |
| 352 | LongPhiMI = BuildMI(MBB, PHIInsertPoint, |
| 353 | V8::PHI, PN->getNumOperands(), PHIReg+1); |
| 354 | |
| 355 | // PHIValues - Map of blocks to incoming virtual registers. We use this |
| 356 | // so that we only initialize one incoming value for a particular block, |
| 357 | // even if the block has multiple entries in the PHI node. |
| 358 | // |
| 359 | std::map<MachineBasicBlock*, unsigned> PHIValues; |
| 360 | |
| 361 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 362 | MachineBasicBlock *PredMBB = 0; |
| 363 | for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (), |
| 364 | PE = MBB.pred_end (); PI != PE; ++PI) |
| 365 | if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) { |
| 366 | PredMBB = *PI; |
| 367 | break; |
| 368 | } |
| 369 | assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi"); |
| 370 | |
| 371 | unsigned ValReg; |
| 372 | std::map<MachineBasicBlock*, unsigned>::iterator EntryIt = |
| 373 | PHIValues.lower_bound(PredMBB); |
| 374 | |
| 375 | if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { |
| 376 | // We already inserted an initialization of the register for this |
| 377 | // predecessor. Recycle it. |
| 378 | ValReg = EntryIt->second; |
| 379 | |
| 380 | } else { |
| 381 | // Get the incoming value into a virtual register. |
| 382 | // |
| 383 | Value *Val = PN->getIncomingValue(i); |
| 384 | |
| 385 | // If this is a constant or GlobalValue, we may have to insert code |
| 386 | // into the basic block to compute it into a virtual register. |
| 387 | if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) || |
| 388 | isa<GlobalValue>(Val)) { |
| 389 | // Simple constants get emitted at the end of the basic block, |
| 390 | // before any terminator instructions. We "know" that the code to |
| 391 | // move a constant into a register will never clobber any flags. |
| 392 | ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator()); |
| 393 | } else { |
| 394 | // Because we don't want to clobber any values which might be in |
| 395 | // physical registers with the computation of this constant (which |
| 396 | // might be arbitrarily complex if it is a constant expression), |
| 397 | // just insert the computation at the top of the basic block. |
| 398 | MachineBasicBlock::iterator PI = PredMBB->begin(); |
| 399 | |
| 400 | // Skip over any PHI nodes though! |
| 401 | while (PI != PredMBB->end() && PI->getOpcode() == V8::PHI) |
| 402 | ++PI; |
| 403 | |
| 404 | ValReg = getReg(Val, PredMBB, PI); |
| 405 | } |
| 406 | |
| 407 | // Remember that we inserted a value for this PHI for this predecessor |
| 408 | PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); |
| 409 | } |
| 410 | |
| 411 | PhiMI->addRegOperand(ValReg); |
| 412 | PhiMI->addMachineBasicBlockOperand(PredMBB); |
| 413 | if (LongPhiMI) { |
| 414 | LongPhiMI->addRegOperand(ValReg+1); |
| 415 | LongPhiMI->addMachineBasicBlockOperand(PredMBB); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Now that we emitted all of the incoming values for the PHI node, make |
| 420 | // sure to reposition the InsertPoint after the PHI that we just added. |
| 421 | // This is needed because we might have inserted a constant into this |
| 422 | // block, right after the PHI's which is before the old insert point! |
| 423 | PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI; |
| 424 | ++PHIInsertPoint; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 429 | bool V8ISel::runOnFunction(Function &Fn) { |
| 430 | // First pass over the function, lower any unknown intrinsic functions |
| 431 | // with the IntrinsicLowering class. |
| 432 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 433 | |
| 434 | F = &MachineFunction::construct(&Fn, TM); |
| 435 | |
| 436 | // Create all of the machine basic blocks for the function... |
| 437 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 438 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 439 | |
| 440 | BB = &F->front(); |
| 441 | |
| 442 | // Set up a frame object for the return address. This is used by the |
| 443 | // llvm.returnaddress & llvm.frameaddress intrinisics. |
| 444 | //ReturnAddressIndex = F->getFrameInfo()->CreateFixedObject(4, -4); |
| 445 | |
| 446 | // Copy incoming arguments off of the stack and out of fixed registers. |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 447 | LoadArgumentsToVirtualRegs(&Fn); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 448 | |
| 449 | // Instruction select everything except PHI nodes |
| 450 | visit(Fn); |
| 451 | |
| 452 | // Select the PHI nodes |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 453 | SelectPHINodes(); |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 454 | |
| 455 | RegMap.clear(); |
| 456 | MBBMap.clear(); |
| 457 | F = 0; |
| 458 | // We always build a machine code representation for the function |
| 459 | return true; |
| 460 | } |
| 461 | |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 462 | void V8ISel::visitCastInst(CastInst &I) { |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 463 | Value *Op = I.getOperand(0); |
| 464 | unsigned DestReg = getReg(I); |
| 465 | MachineBasicBlock::iterator MI = BB->end(); |
| 466 | emitCastOperation(BB, MI, Op, I.getType(), DestReg); |
| 467 | } |
| 468 | |
| 469 | /// emitCastOperation - Common code shared between visitCastInst and constant |
| 470 | /// expression cast support. |
| 471 | /// |
| 472 | void V8ISel::emitCastOperation(MachineBasicBlock *BB, |
| 473 | MachineBasicBlock::iterator IP, |
| 474 | Value *Src, const Type *DestTy, |
| 475 | unsigned DestReg) { |
| 476 | const Type *SrcTy = Src->getType(); |
| 477 | unsigned SrcClass = getClassB(SrcTy); |
| 478 | unsigned DestClass = getClassB(DestTy); |
| 479 | unsigned SrcReg = getReg(Src, BB, IP); |
| 480 | |
| 481 | const Type *oldTy = SrcTy; |
| 482 | const Type *newTy = DestTy; |
| 483 | unsigned oldTyClass = SrcClass; |
| 484 | unsigned newTyClass = DestClass; |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 485 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 486 | if (oldTyClass < cLong && newTyClass < cLong) { |
| 487 | if (oldTyClass >= newTyClass) { |
| 488 | // Emit a reg->reg copy to do a equal-size or narrowing cast, |
| 489 | // and do sign/zero extension (necessary if we change signedness). |
| 490 | unsigned TmpReg1 = makeAnotherReg (newTy); |
| 491 | unsigned TmpReg2 = makeAnotherReg (newTy); |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 492 | BuildMI (*BB, IP, V8::ORrr, 2, TmpReg1).addReg (V8::G0).addReg (SrcReg); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 493 | unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy)); |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 494 | BuildMI (*BB, IP, V8::SLLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 495 | if (newTy->isSigned ()) { // sign-extend with SRA |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 496 | BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 497 | } else { // zero-extend with SRL |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 498 | BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg2); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 499 | } |
| 500 | } else { |
| 501 | unsigned TmpReg1 = makeAnotherReg (oldTy); |
| 502 | unsigned TmpReg2 = makeAnotherReg (newTy); |
| 503 | unsigned TmpReg3 = makeAnotherReg (newTy); |
| 504 | // Widening integer cast. Make sure it's fully sign/zero-extended |
| 505 | // wrt the input type, then make sure it's fully sign/zero-extended wrt |
| 506 | // the output type. Kind of stupid, but simple... |
| 507 | unsigned shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (oldTy)); |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 508 | BuildMI (*BB, IP, V8::SLLri, 2, TmpReg1).addZImm (shiftWidth).addReg(SrcReg); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 509 | if (oldTy->isSigned ()) { // sign-extend with SRA |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 510 | BuildMI(*BB, IP, V8::SRAri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 511 | } else { // zero-extend with SRL |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 512 | BuildMI(*BB, IP, V8::SRLri, 2, TmpReg2).addZImm (shiftWidth).addReg(TmpReg1); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 513 | } |
| 514 | shiftWidth = 32 - (8 * TM.getTargetData ().getTypeSize (newTy)); |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 515 | BuildMI (*BB, IP, V8::SLLri, 2, TmpReg3).addZImm (shiftWidth).addReg(TmpReg2); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 516 | if (newTy->isSigned ()) { // sign-extend with SRA |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 517 | BuildMI(*BB, IP, V8::SRAri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 518 | } else { // zero-extend with SRL |
Brian Gaeke | 00e514e | 2004-06-24 06:33:00 +0000 | [diff] [blame] | 519 | BuildMI(*BB, IP, V8::SRLri, 2, DestReg).addZImm (shiftWidth).addReg(TmpReg3); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 520 | } |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 521 | } |
| 522 | } else { |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 523 | if (oldTyClass < cLong && newTyClass == cFloat) { |
| 524 | // cast int to float. Store it to a stack slot and then load |
| 525 | // it using ldf into a floating point register. then do fitos. |
| 526 | std::cerr << "Casts to float still unsupported: SrcTy = " |
| 527 | << *SrcTy << ", DestTy = " << *DestTy << "\n"; |
| 528 | abort (); |
| 529 | } else if (oldTyClass < cLong && newTyClass == cDouble) { |
| 530 | std::cerr << "Casts to double still unsupported: SrcTy = " |
| 531 | << *SrcTy << ", DestTy = " << *DestTy << "\n"; |
| 532 | abort (); |
| 533 | } else { |
| 534 | std::cerr << "Cast still unsupported: SrcTy = " |
| 535 | << *SrcTy << ", DestTy = " << *DestTy << "\n"; |
| 536 | abort (); |
| 537 | } |
Brian Gaeke | e302a7e | 2004-05-07 21:39:30 +0000 | [diff] [blame] | 538 | } |
Brian Gaeke | 3d11e8a | 2004-04-13 18:27:46 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 541 | void V8ISel::visitLoadInst(LoadInst &I) { |
| 542 | unsigned DestReg = getReg (I); |
| 543 | unsigned PtrReg = getReg (I.getOperand (0)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 544 | switch (getClassB (I.getType ())) { |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 545 | case cByte: |
| 546 | if (I.getType ()->isSigned ()) |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 547 | BuildMI (BB, V8::LDSB, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 548 | else |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 549 | BuildMI (BB, V8::LDUB, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 550 | return; |
| 551 | case cShort: |
| 552 | if (I.getType ()->isSigned ()) |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 553 | BuildMI (BB, V8::LDSH, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 554 | else |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 555 | BuildMI (BB, V8::LDUH, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 556 | return; |
| 557 | case cInt: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 558 | BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 559 | return; |
| 560 | case cLong: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 561 | BuildMI (BB, V8::LD, 2, DestReg).addReg (PtrReg).addSImm(0); |
| 562 | BuildMI (BB, V8::LD, 2, DestReg+1).addReg (PtrReg).addSImm(4); |
| 563 | return; |
| 564 | case cFloat: |
| 565 | BuildMI (BB, V8::LDFri, 2, DestReg).addReg (PtrReg).addSImm(0); |
| 566 | return; |
| 567 | case cDouble: |
| 568 | BuildMI (BB, V8::LDDFri, 2, DestReg).addReg (PtrReg).addSImm(0); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 569 | return; |
| 570 | default: |
| 571 | std::cerr << "Load instruction not handled: " << I; |
| 572 | abort (); |
| 573 | return; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | void V8ISel::visitStoreInst(StoreInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 578 | Value *SrcVal = I.getOperand (0); |
| 579 | unsigned SrcReg = getReg (SrcVal); |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 580 | unsigned PtrReg = getReg (I.getOperand (1)); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 581 | switch (getClassB (SrcVal->getType ())) { |
| 582 | case cByte: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 583 | BuildMI (BB, V8::STB, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 584 | return; |
| 585 | case cShort: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 586 | BuildMI (BB, V8::STH, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 587 | return; |
| 588 | case cInt: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 589 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 590 | return; |
| 591 | case cLong: |
Brian Gaeke | 4473303 | 2004-06-24 07:36:48 +0000 | [diff] [blame^] | 592 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
| 593 | BuildMI (BB, V8::ST, 3).addReg (PtrReg).addSImm (4).addReg (SrcReg+1); |
| 594 | return; |
| 595 | case cFloat: |
| 596 | BuildMI (BB, V8::STFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
| 597 | return; |
| 598 | case cDouble: |
| 599 | BuildMI (BB, V8::STDFri, 3).addReg (PtrReg).addSImm (0).addReg (SrcReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 600 | return; |
| 601 | default: |
| 602 | std::cerr << "Store instruction not handled: " << I; |
| 603 | abort (); |
| 604 | return; |
| 605 | } |
Brian Gaeke | f3334eb | 2004-04-07 17:29:37 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 608 | void V8ISel::visitCallInst(CallInst &I) { |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 609 | MachineInstr *TheCall; |
| 610 | // Is it an intrinsic function call? |
| 611 | if (Function *F = I.getCalledFunction()) { |
| 612 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) { |
| 613 | visitIntrinsicCall(ID, I); // Special intrinsics are not handled here |
| 614 | return; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | // Deal with args |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 619 | assert (I.getNumOperands () < 8 |
| 620 | && "Can't handle pushing excess call args on the stack yet"); |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 621 | static const unsigned OutgoingArgRegs[] = { V8::O0, V8::O1, V8::O2, V8::O3, |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 622 | V8::O4, V8::O5 }; |
| 623 | for (unsigned i = 1; i < 7; ++i) |
| 624 | if (i < I.getNumOperands ()) { |
| 625 | unsigned ArgReg = getReg (I.getOperand (i)); |
| 626 | // Schlep it over into the incoming arg register |
Brian Gaeke | 562cb16 | 2004-04-07 17:04:09 +0000 | [diff] [blame] | 627 | BuildMI (BB, V8::ORrr, 2, OutgoingArgRegs[i - 1]).addReg (V8::G0) |
Brian Gaeke | d54c38b | 2004-04-07 16:41:22 +0000 | [diff] [blame] | 628 | .addReg (ArgReg); |
| 629 | } |
| 630 | |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 631 | // Emit call instruction |
| 632 | if (Function *F = I.getCalledFunction ()) { |
| 633 | BuildMI (BB, V8::CALL, 1).addGlobalAddress (F, true); |
| 634 | } else { // Emit an indirect call... |
| 635 | unsigned Reg = getReg (I.getCalledValue ()); |
| 636 | BuildMI (BB, V8::JMPLrr, 3, V8::O7).addReg (Reg).addReg (V8::G0); |
| 637 | } |
| 638 | |
| 639 | // Deal w/ return value: schlep it over into the destination register |
Brian Gaeke | e14e338 | 2004-06-15 20:06:32 +0000 | [diff] [blame] | 640 | if (I.getType () == Type::VoidTy) |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 641 | return; |
Brian Gaeke | e14e338 | 2004-06-15 20:06:32 +0000 | [diff] [blame] | 642 | unsigned DestReg = getReg (I); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 643 | switch (getClass (I.getType ())) { |
| 644 | case cByte: |
| 645 | case cShort: |
| 646 | case cInt: |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 647 | BuildMI (BB, V8::ORrr, 2, DestReg).addReg(V8::G0).addReg(V8::O0); |
| 648 | break; |
Brian Gaeke | 9d67ea0 | 2004-06-18 06:27:48 +0000 | [diff] [blame] | 649 | case cFloat: |
| 650 | BuildMI (BB, V8::FMOVS, 2, DestReg).addReg(V8::F0); |
| 651 | break; |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 652 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 653 | std::cerr << "Return type of call instruction not handled: " << I; |
| 654 | abort (); |
Brian Gaeke | ea8494b | 2004-04-06 22:09:23 +0000 | [diff] [blame] | 655 | } |
Brian Gaeke | f7e44ef | 2004-04-02 20:53:33 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 657 | |
| 658 | void V8ISel::visitReturnInst(ReturnInst &I) { |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 659 | if (I.getNumOperands () == 1) { |
| 660 | unsigned RetValReg = getReg (I.getOperand (0)); |
| 661 | switch (getClass (I.getOperand (0)->getType ())) { |
| 662 | case cByte: |
| 663 | case cShort: |
| 664 | case cInt: |
| 665 | // Schlep it over into i0 (where it will become o0 after restore). |
| 666 | BuildMI (BB, V8::ORrr, 2, V8::I0).addReg(V8::G0).addReg(RetValReg); |
| 667 | break; |
| 668 | default: |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 669 | std::cerr << "Return instruction of this type not handled: " << I; |
| 670 | abort (); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 672 | } |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 673 | |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 674 | // Just emit a 'retl' instruction to return. |
| 675 | BuildMI(BB, V8::RETL, 0); |
| 676 | return; |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 679 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 680 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 681 | return I != BB->getParent()->end() ? &*I : 0; |
| 682 | } |
| 683 | |
| 684 | /// visitBranchInst - Handles conditional and unconditional branches. |
| 685 | /// |
| 686 | void V8ISel::visitBranchInst(BranchInst &I) { |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 687 | BasicBlock *takenSucc = I.getSuccessor (0); |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 688 | MachineBasicBlock *takenSuccMBB = MBBMap[takenSucc]; |
| 689 | BB->addSuccessor (takenSuccMBB); |
| 690 | if (I.isConditional()) { // conditional branch |
| 691 | BasicBlock *notTakenSucc = I.getSuccessor (1); |
| 692 | MachineBasicBlock *notTakenSuccMBB = MBBMap[notTakenSucc]; |
| 693 | BB->addSuccessor (notTakenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 694 | |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 695 | // CondReg=(<condition>); |
| 696 | // If (CondReg==0) goto notTakenSuccMBB; |
| 697 | unsigned CondReg = getReg (I.getCondition ()); |
| 698 | BuildMI (BB, V8::CMPri, 2).addSImm (0).addReg (CondReg); |
| 699 | BuildMI (BB, V8::BE, 1).addMBB (notTakenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 700 | } |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 701 | // goto takenSuccMBB; |
| 702 | BuildMI (BB, V8::BA, 1).addMBB (takenSuccMBB); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 706 | /// constant expression GEP support. |
| 707 | /// |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 708 | void V8ISel::emitGEPOperation (MachineBasicBlock *MBB, |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 709 | MachineBasicBlock::iterator IP, |
| 710 | Value *Src, User::op_iterator IdxBegin, |
| 711 | User::op_iterator IdxEnd, unsigned TargetReg) { |
Brian Gaeke | 9f56482 | 2004-05-08 05:27:20 +0000 | [diff] [blame] | 712 | const TargetData &TD = TM.getTargetData (); |
| 713 | const Type *Ty = Src->getType (); |
| 714 | unsigned basePtrReg = getReg (Src); |
| 715 | |
| 716 | // GEPs have zero or more indices; we must perform a struct access |
| 717 | // or array access for each one. |
| 718 | for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe; |
| 719 | ++oi) { |
| 720 | Value *idx = *oi; |
| 721 | unsigned nextBasePtrReg = makeAnotherReg (Type::UIntTy); |
| 722 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 723 | // It's a struct access. idx is the index into the structure, |
| 724 | // which names the field. Use the TargetData structure to |
| 725 | // pick out what the layout of the structure is in memory. |
| 726 | // Use the (constant) structure index's value to find the |
| 727 | // right byte offset from the StructLayout class's list of |
| 728 | // structure member offsets. |
| 729 | unsigned fieldIndex = cast<ConstantUInt> (idx)->getValue (); |
| 730 | unsigned memberOffset = |
| 731 | TD.getStructLayout (StTy)->MemberOffsets[fieldIndex]; |
| 732 | // Emit an ADD to add memberOffset to the basePtr. |
| 733 | BuildMI (*MBB, IP, V8::ADDri, 2, |
| 734 | nextBasePtrReg).addReg (basePtrReg).addZImm (memberOffset); |
| 735 | // The next type is the member of the structure selected by the |
| 736 | // index. |
| 737 | Ty = StTy->getElementType (fieldIndex); |
| 738 | } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) { |
| 739 | // It's an array or pointer access: [ArraySize x ElementType]. |
| 740 | // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we |
| 741 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 742 | // type is the type of the elements in the array). |
| 743 | Ty = SqTy->getElementType (); |
| 744 | unsigned elementSize = TD.getTypeSize (Ty); |
| 745 | unsigned idxReg = getReg (idx, MBB, IP); |
| 746 | unsigned OffsetReg = makeAnotherReg (Type::IntTy); |
| 747 | unsigned elementSizeReg = makeAnotherReg (Type::UIntTy); |
| 748 | BuildMI (*MBB, IP, V8::ORri, 2, |
| 749 | elementSizeReg).addZImm (elementSize).addReg (V8::G0); |
| 750 | // Emit a SMUL to multiply the register holding the index by |
| 751 | // elementSize, putting the result in OffsetReg. |
| 752 | BuildMI (*MBB, IP, V8::SMULrr, 2, |
| 753 | OffsetReg).addReg (elementSizeReg).addReg (idxReg); |
| 754 | // Emit an ADD to add OffsetReg to the basePtr. |
| 755 | BuildMI (*MBB, IP, V8::ADDrr, 2, |
| 756 | nextBasePtrReg).addReg (basePtrReg).addReg (OffsetReg); |
| 757 | } |
| 758 | basePtrReg = nextBasePtrReg; |
| 759 | } |
| 760 | // After we have processed all the indices, the result is left in |
| 761 | // basePtrReg. Move it to the register where we were expected to |
| 762 | // put the answer. |
| 763 | BuildMI (BB, V8::ORrr, 1, TargetReg).addReg (V8::G0).addReg (basePtrReg); |
Brian Gaeke | 532e60c | 2004-05-08 04:21:17 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void V8ISel::visitGetElementPtrInst (GetElementPtrInst &I) { |
| 767 | unsigned outputReg = getReg (I); |
| 768 | emitGEPOperation (BB, BB->end (), I.getOperand (0), |
| 769 | I.op_begin ()+1, I.op_end (), outputReg); |
| 770 | } |
| 771 | |
Brian Gaeke | d6a1053 | 2004-06-15 21:09:46 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 773 | void V8ISel::visitBinaryOperator (Instruction &I) { |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 774 | unsigned DestReg = getReg (I); |
| 775 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 776 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 777 | |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 778 | unsigned ResultReg = DestReg; |
| 779 | if (getClassB(I.getType()) != cInt) |
| 780 | ResultReg = makeAnotherReg (I.getType ()); |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 781 | unsigned OpCase = ~0; |
| 782 | |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 783 | // FIXME: support long, ulong, fp. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 784 | switch (I.getOpcode ()) { |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 785 | case Instruction::Add: OpCase = 0; break; |
| 786 | case Instruction::Sub: OpCase = 1; break; |
| 787 | case Instruction::Mul: OpCase = 2; break; |
| 788 | case Instruction::And: OpCase = 3; break; |
| 789 | case Instruction::Or: OpCase = 4; break; |
| 790 | case Instruction::Xor: OpCase = 5; break; |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 791 | case Instruction::Shl: OpCase = 6; break; |
| 792 | case Instruction::Shr: OpCase = 7+I.getType()->isSigned(); break; |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 793 | |
| 794 | case Instruction::Div: |
| 795 | case Instruction::Rem: { |
| 796 | unsigned Dest = ResultReg; |
| 797 | if (I.getOpcode() == Instruction::Rem) |
| 798 | Dest = makeAnotherReg(I.getType()); |
| 799 | |
| 800 | // FIXME: this is probably only right for 32 bit operands. |
| 801 | if (I.getType ()->isSigned()) { |
| 802 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 803 | // Sign extend into the Y register |
| 804 | BuildMI (BB, V8::SRAri, 2, Tmp).addReg (Op0Reg).addZImm (31); |
| 805 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (Tmp).addReg (V8::G0); |
| 806 | BuildMI (BB, V8::SDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
| 807 | } else { |
| 808 | // Zero extend into the Y register, ie, just set it to zero |
| 809 | BuildMI (BB, V8::WRrr, 2, V8::Y).addReg (V8::G0).addReg (V8::G0); |
| 810 | BuildMI (BB, V8::UDIVrr, 2, Dest).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | 2d4fa8f | 2004-04-07 04:00:49 +0000 | [diff] [blame] | 811 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 812 | |
| 813 | if (I.getOpcode() == Instruction::Rem) { |
| 814 | unsigned Tmp = makeAnotherReg (I.getType ()); |
| 815 | BuildMI (BB, V8::SMULrr, 2, Tmp).addReg(Dest).addReg(Op1Reg); |
| 816 | BuildMI (BB, V8::SUBrr, 2, ResultReg).addReg(Op0Reg).addReg(Tmp); |
Brian Gaeke | f57e364 | 2004-03-16 22:37:11 +0000 | [diff] [blame] | 817 | } |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | default: |
| 821 | visitInstruction (I); |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | if (OpCase != ~0U) { |
| 826 | static const unsigned Opcodes[] = { |
Chris Lattner | 4be7ca5 | 2004-04-07 04:27:16 +0000 | [diff] [blame] | 827 | V8::ADDrr, V8::SUBrr, V8::SMULrr, V8::ANDrr, V8::ORrr, V8::XORrr, |
| 828 | V8::SLLrr, V8::SRLrr, V8::SRArr |
Chris Lattner | 22ede70 | 2004-04-07 04:06:46 +0000 | [diff] [blame] | 829 | }; |
| 830 | BuildMI (BB, Opcodes[OpCase], 2, ResultReg).addReg (Op0Reg).addReg (Op1Reg); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | switch (getClass (I.getType ())) { |
| 834 | case cByte: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 835 | if (I.getType ()->isSigned ()) { // add byte |
| 836 | BuildMI (BB, V8::ANDri, 2, DestReg).addReg (ResultReg).addZImm (0xff); |
| 837 | } else { // add ubyte |
| 838 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 839 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (24); |
| 840 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (24); |
| 841 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 842 | break; |
| 843 | case cShort: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 844 | if (I.getType ()->isSigned ()) { // add short |
| 845 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
| 846 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 847 | BuildMI (BB, V8::SRAri, 2, DestReg).addReg (TmpReg).addZImm (16); |
| 848 | } else { // add ushort |
| 849 | unsigned TmpReg = makeAnotherReg (I.getType ()); |
Brian Gaeke | 6d339f9 | 2004-03-16 22:45:42 +0000 | [diff] [blame] | 850 | BuildMI (BB, V8::SLLri, 2, TmpReg).addReg (ResultReg).addZImm (16); |
| 851 | BuildMI (BB, V8::SRLri, 2, DestReg).addReg (TmpReg).addZImm (16); |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 852 | } |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 853 | break; |
| 854 | case cInt: |
Chris Lattner | 0d538bb | 2004-04-07 04:36:53 +0000 | [diff] [blame] | 855 | // Nothing todo here. |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 856 | break; |
| 857 | default: |
Brian Gaeke | 08f64c3 | 2004-03-06 05:32:28 +0000 | [diff] [blame] | 858 | visitInstruction (I); |
Brian Gaeke | bc1d27a | 2004-03-03 23:03:14 +0000 | [diff] [blame] | 859 | return; |
| 860 | } |
| 861 | } |
| 862 | |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 863 | void V8ISel::visitSetCondInst(Instruction &I) { |
| 864 | unsigned Op0Reg = getReg (I.getOperand (0)); |
| 865 | unsigned Op1Reg = getReg (I.getOperand (1)); |
| 866 | unsigned DestReg = getReg (I); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 867 | const Type *Ty = I.getOperand (0)->getType (); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 868 | |
| 869 | // Compare the two values. |
| 870 | BuildMI(BB, V8::SUBCCrr, 2, V8::G0).addReg(Op0Reg).addReg(Op1Reg); |
| 871 | |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 872 | unsigned BranchIdx; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 873 | switch (I.getOpcode()) { |
| 874 | default: assert(0 && "Unknown setcc instruction!"); |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 875 | case Instruction::SetEQ: BranchIdx = 0; break; |
| 876 | case Instruction::SetNE: BranchIdx = 1; break; |
| 877 | case Instruction::SetLT: BranchIdx = 2; break; |
| 878 | case Instruction::SetGT: BranchIdx = 3; break; |
| 879 | case Instruction::SetLE: BranchIdx = 4; break; |
| 880 | case Instruction::SetGE: BranchIdx = 5; break; |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 881 | } |
Brian Gaeke | 429022b | 2004-05-08 06:36:14 +0000 | [diff] [blame] | 882 | static unsigned OpcodeTab[12] = { |
| 883 | // LLVM SparcV8 |
| 884 | // unsigned signed |
| 885 | V8::BE, V8::BE, // seteq = be be |
| 886 | V8::BNE, V8::BNE, // setne = bne bne |
| 887 | V8::BCS, V8::BL, // setlt = bcs bl |
| 888 | V8::BGU, V8::BG, // setgt = bgu bg |
| 889 | V8::BLEU, V8::BLE, // setle = bleu ble |
| 890 | V8::BCC, V8::BGE // setge = bcc bge |
| 891 | }; |
Brian Gaeke | 6c868a4 | 2004-06-17 22:34:08 +0000 | [diff] [blame] | 892 | unsigned Opcode = OpcodeTab[2*BranchIdx + (Ty->isSigned() ? 1 : 0)]; |
| 893 | |
| 894 | MachineBasicBlock *thisMBB = BB; |
| 895 | const BasicBlock *LLVM_BB = BB->getBasicBlock (); |
| 896 | // thisMBB: |
| 897 | // ... |
| 898 | // subcc %reg0, %reg1, %g0 |
| 899 | // bCC copy1MBB |
| 900 | // ba copy0MBB |
| 901 | |
| 902 | // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB) |
| 903 | // if we could insert other, non-terminator instructions after the |
| 904 | // bCC. But MBB->getFirstTerminator() can't understand this. |
| 905 | MachineBasicBlock *copy1MBB = new MachineBasicBlock (LLVM_BB); |
| 906 | F->getBasicBlockList ().push_back (copy1MBB); |
| 907 | BuildMI (BB, Opcode, 1).addMBB (copy1MBB); |
| 908 | MachineBasicBlock *copy0MBB = new MachineBasicBlock (LLVM_BB); |
| 909 | F->getBasicBlockList ().push_back (copy0MBB); |
| 910 | BuildMI (BB, V8::BA, 1).addMBB (copy0MBB); |
| 911 | // Update machine-CFG edges |
| 912 | BB->addSuccessor (copy1MBB); |
| 913 | BB->addSuccessor (copy0MBB); |
| 914 | |
| 915 | // copy0MBB: |
| 916 | // %FalseValue = or %G0, 0 |
| 917 | // ba sinkMBB |
| 918 | BB = copy0MBB; |
| 919 | unsigned FalseValue = makeAnotherReg (I.getType ()); |
| 920 | BuildMI (BB, V8::ORri, 2, FalseValue).addReg (V8::G0).addZImm (0); |
| 921 | MachineBasicBlock *sinkMBB = new MachineBasicBlock (LLVM_BB); |
| 922 | F->getBasicBlockList ().push_back (sinkMBB); |
| 923 | BuildMI (BB, V8::BA, 1).addMBB (sinkMBB); |
| 924 | // Update machine-CFG edges |
| 925 | BB->addSuccessor (sinkMBB); |
| 926 | |
| 927 | DEBUG (std::cerr << "thisMBB is at " << (void*)thisMBB << "\n"); |
| 928 | DEBUG (std::cerr << "copy1MBB is at " << (void*)copy1MBB << "\n"); |
| 929 | DEBUG (std::cerr << "copy0MBB is at " << (void*)copy0MBB << "\n"); |
| 930 | DEBUG (std::cerr << "sinkMBB is at " << (void*)sinkMBB << "\n"); |
| 931 | |
| 932 | // copy1MBB: |
| 933 | // %TrueValue = or %G0, 1 |
| 934 | // ba sinkMBB |
| 935 | BB = copy1MBB; |
| 936 | unsigned TrueValue = makeAnotherReg (I.getType ()); |
| 937 | BuildMI (BB, V8::ORri, 2, TrueValue).addReg (V8::G0).addZImm (1); |
| 938 | BuildMI (BB, V8::BA, 1).addMBB (sinkMBB); |
| 939 | // Update machine-CFG edges |
| 940 | BB->addSuccessor (sinkMBB); |
| 941 | |
| 942 | // sinkMBB: |
| 943 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ] |
| 944 | // ... |
| 945 | BB = sinkMBB; |
| 946 | BuildMI (BB, V8::PHI, 4, DestReg).addReg (FalseValue) |
| 947 | .addMBB (copy0MBB).addReg (TrueValue).addMBB (copy1MBB); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 950 | void V8ISel::visitAllocaInst(AllocaInst &I) { |
| 951 | // Find the data size of the alloca inst's getAllocatedType. |
| 952 | const Type *Ty = I.getAllocatedType(); |
| 953 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
Chris Lattner | 4d0cda4 | 2004-04-07 05:04:51 +0000 | [diff] [blame] | 954 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 955 | unsigned ArraySizeReg = getReg (I.getArraySize ()); |
| 956 | unsigned TySizeReg = getReg (ConstantUInt::get (Type::UIntTy, TySize)); |
| 957 | unsigned TmpReg1 = makeAnotherReg (Type::UIntTy); |
| 958 | unsigned TmpReg2 = makeAnotherReg (Type::UIntTy); |
| 959 | unsigned StackAdjReg = makeAnotherReg (Type::UIntTy); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 960 | |
| 961 | // StackAdjReg = (ArraySize * TySize) rounded up to nearest doubleword boundary |
| 962 | BuildMI (BB, V8::UMULrr, 2, TmpReg1).addReg (ArraySizeReg).addReg (TySizeReg); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 963 | |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 964 | // Round up TmpReg1 to nearest doubleword boundary: |
| 965 | BuildMI (BB, V8::ADDri, 2, TmpReg2).addReg (TmpReg1).addSImm (7); |
| 966 | BuildMI (BB, V8::ANDri, 2, StackAdjReg).addReg (TmpReg2).addSImm (-8); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 967 | |
| 968 | // Subtract size from stack pointer, thereby allocating some space. |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 969 | BuildMI (BB, V8::SUBrr, 2, V8::SP).addReg (V8::SP).addReg (StackAdjReg); |
Brian Gaeke | cfaf224 | 2004-06-18 08:45:52 +0000 | [diff] [blame] | 970 | |
| 971 | // Put a pointer to the space into the result register, by copying |
| 972 | // the stack pointer. |
| 973 | BuildMI (BB, V8::ADDri, 2, getReg(I)).addReg (V8::SP).addSImm (96); |
| 974 | |
| 975 | // Inform the Frame Information that we have just allocated a variable-sized |
| 976 | // object. |
| 977 | F->getFrameInfo()->CreateVariableSizedObject(); |
Brian Gaeke | c93a752 | 2004-06-18 05:19:16 +0000 | [diff] [blame] | 978 | } |
Chris Lattner | 1c809c5 | 2004-02-29 00:27:00 +0000 | [diff] [blame] | 979 | |
| 980 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 981 | /// function, lowering any calls to unknown intrinsic functions into the |
| 982 | /// equivalent LLVM code. |
| 983 | void V8ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 984 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 985 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 986 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 987 | if (Function *F = CI->getCalledFunction()) |
| 988 | switch (F->getIntrinsicID()) { |
| 989 | case Intrinsic::not_intrinsic: break; |
| 990 | default: |
| 991 | // All other intrinsic calls we must lower. |
| 992 | Instruction *Before = CI->getPrev(); |
| 993 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 994 | if (Before) { // Move iterator to instruction after call |
| 995 | I = Before; ++I; |
| 996 | } else { |
| 997 | I = BB->begin(); |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | void V8ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 1004 | unsigned TmpReg1, TmpReg2; |
| 1005 | switch (ID) { |
| 1006 | default: assert(0 && "Intrinsic not supported!"); |
| 1007 | } |
| 1008 | } |