Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1 | ///===-- FastISel.cpp - Implementation of the FastISel class --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the FastISel class. |
| 11 | // |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 12 | // "Fast" instruction selection is designed to emit very poor code quickly. |
| 13 | // Also, it is not designed to be able to do much lowering, so most illegal |
Chris Lattner | 44d2a98 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 14 | // types (e.g. i64 on 32-bit targets) and operations are not supported. It is |
| 15 | // also not intended to be able to do much optimization, except in a few cases |
| 16 | // where doing optimizations reduces overall compile time. For example, folding |
| 17 | // constants into immediate fields is often done, because it's cheap and it |
| 18 | // reduces the number of instructions later phases have to examine. |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 19 | // |
| 20 | // "Fast" instruction selection is able to fail gracefully and transfer |
| 21 | // control to the SelectionDAG selector for operations that it doesn't |
Chris Lattner | 44d2a98 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 22 | // support. In many cases, this allows us to avoid duplicating a lot of |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 23 | // the complicated lowering logic that SelectionDAG currently has. |
| 24 | // |
| 25 | // The intended use for "fast" instruction selection is "-O0" mode |
| 26 | // compilation, where the quality of the generated code is irrelevant when |
Chris Lattner | 44d2a98 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 27 | // weighed against the speed at which the code can be generated. Also, |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 28 | // at -O0, the LLVM optimizers are not running, and this makes the |
| 29 | // compile time of codegen a much higher portion of the overall compile |
Chris Lattner | 44d2a98 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 30 | // time. Despite its limitations, "fast" instruction selection is able to |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 31 | // handle enough code on its own to provide noticeable overall speedups |
| 32 | // in -O0 compiles. |
| 33 | // |
| 34 | // Basic operations are supported in a target-independent way, by reading |
| 35 | // the same instruction descriptions that the SelectionDAG selector reads, |
| 36 | // and identifying simple arithmetic operations that can be directly selected |
Chris Lattner | 44d2a98 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 37 | // from simple operators. More complicated operations currently require |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 38 | // target-specific code. |
| 39 | // |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 42 | #include "llvm/Function.h" |
| 43 | #include "llvm/GlobalVariable.h" |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 44 | #include "llvm/Instructions.h" |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 45 | #include "llvm/IntrinsicInst.h" |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 46 | #include "llvm/LLVMContext.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/FastISel.h" |
| 48 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/DwarfWriter.h" |
| 52 | #include "llvm/Analysis/DebugInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetData.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 57 | #include "SelectionDAGBuild.h" |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame^] | 58 | #include "FunctionLoweringInfo.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 59 | using namespace llvm; |
| 60 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 61 | unsigned FastISel::getRegForValue(Value *V) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 62 | EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true); |
Dan Gohman | 4fd5528 | 2009-04-07 20:40:11 +0000 | [diff] [blame] | 63 | // Don't handle non-simple values in FastISel. |
| 64 | if (!RealVT.isSimple()) |
| 65 | return 0; |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 66 | |
| 67 | // Ignore illegal types. We must do this before looking up the value |
| 68 | // in ValueMap because Arguments are given virtual registers regardless |
| 69 | // of whether FastISel can handle them. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 70 | MVT VT = RealVT.getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 71 | if (!TLI.isTypeLegal(VT)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 72 | // Promote MVT::i1 to a legal type though, because it's common and easy. |
| 73 | if (VT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 74 | VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 75 | else |
| 76 | return 0; |
| 77 | } |
| 78 | |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 79 | // Look up the value to see if we already have a register for it. We |
| 80 | // cache values defined by Instructions across blocks, and other values |
| 81 | // only locally. This is because Instructions already have the SSA |
| 82 | // def-dominatess-use requirement enforced. |
Owen Anderson | 99aaf10 | 2008-09-03 17:37:03 +0000 | [diff] [blame] | 83 | if (ValueMap.count(V)) |
| 84 | return ValueMap[V]; |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 85 | unsigned Reg = LocalValueMap[V]; |
| 86 | if (Reg != 0) |
| 87 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 88 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 89 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 90 | if (CI->getValue().getActiveBits() <= 64) |
| 91 | Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 92 | } else if (isa<AllocaInst>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 93 | Reg = TargetMaterializeAlloca(cast<AllocaInst>(V)); |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 94 | } else if (isa<ConstantPointerNull>(V)) { |
Dan Gohman | 1e9e8c3 | 2008-10-07 22:03:27 +0000 | [diff] [blame] | 95 | // Translate this as an integer zero so that it can be |
| 96 | // local-CSE'd with actual integer zeros. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 97 | Reg = |
| 98 | getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext()))); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 99 | } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 100 | Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 101 | |
| 102 | if (!Reg) { |
| 103 | const APFloat &Flt = CF->getValueAPF(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 104 | EVT IntVT = TLI.getPointerTy(); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 105 | |
| 106 | uint64_t x[2]; |
| 107 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 108 | bool isExact; |
| 109 | (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
| 110 | APFloat::rmTowardZero, &isExact); |
| 111 | if (isExact) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 112 | APInt IntVal(IntBitWidth, 2, x); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 113 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 114 | unsigned IntegerReg = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 115 | getRegForValue(ConstantInt::get(V->getContext(), IntVal)); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 116 | if (IntegerReg != 0) |
| 117 | Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg); |
| 118 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 119 | } |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 120 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 121 | if (!SelectOperator(CE, CE->getOpcode())) return 0; |
| 122 | Reg = LocalValueMap[CE]; |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 123 | } else if (isa<UndefValue>(V)) { |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 124 | Reg = createResultReg(TLI.getRegClassFor(VT)); |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 125 | BuildMI(MBB, DL, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 126 | } |
Owen Anderson | d5d81a4 | 2008-09-03 17:51:57 +0000 | [diff] [blame] | 127 | |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 128 | // If target-independent code couldn't handle the value, give target-specific |
| 129 | // code a try. |
Owen Anderson | 6e60745 | 2008-09-05 23:36:01 +0000 | [diff] [blame] | 130 | if (!Reg && isa<Constant>(V)) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 131 | Reg = TargetMaterializeConstant(cast<Constant>(V)); |
Owen Anderson | 6e60745 | 2008-09-05 23:36:01 +0000 | [diff] [blame] | 132 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 133 | // Don't cache constant materializations in the general ValueMap. |
| 134 | // To do so would require tracking what uses they dominate. |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 135 | if (Reg != 0) |
| 136 | LocalValueMap[V] = Reg; |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 137 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 140 | unsigned FastISel::lookUpRegForValue(Value *V) { |
| 141 | // Look up the value to see if we already have a register for it. We |
| 142 | // cache values defined by Instructions across blocks, and other values |
| 143 | // only locally. This is because Instructions already have the SSA |
| 144 | // def-dominatess-use requirement enforced. |
| 145 | if (ValueMap.count(V)) |
| 146 | return ValueMap[V]; |
| 147 | return LocalValueMap[V]; |
| 148 | } |
| 149 | |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 150 | /// UpdateValueMap - Update the value map to include the new mapping for this |
| 151 | /// instruction, or insert an extra copy to get the result in a previous |
| 152 | /// determined register. |
| 153 | /// NOTE: This is only necessary because we might select a block that uses |
| 154 | /// a value before we select the block that defines the value. It might be |
| 155 | /// possible to fix this by selecting blocks in reverse postorder. |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 156 | unsigned FastISel::UpdateValueMap(Value* I, unsigned Reg) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 157 | if (!isa<Instruction>(I)) { |
| 158 | LocalValueMap[I] = Reg; |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 159 | return Reg; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 161 | |
| 162 | unsigned &AssignedReg = ValueMap[I]; |
| 163 | if (AssignedReg == 0) |
| 164 | AssignedReg = Reg; |
Chris Lattner | 36e3946 | 2009-04-12 07:46:30 +0000 | [diff] [blame] | 165 | else if (Reg != AssignedReg) { |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 166 | const TargetRegisterClass *RegClass = MRI.getRegClass(Reg); |
| 167 | TII.copyRegToReg(*MBB, MBB->end(), AssignedReg, |
| 168 | Reg, RegClass, RegClass); |
| 169 | } |
| 170 | return AssignedReg; |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 173 | unsigned FastISel::getRegForGEPIndex(Value *Idx) { |
| 174 | unsigned IdxN = getRegForValue(Idx); |
| 175 | if (IdxN == 0) |
| 176 | // Unhandled operand. Halt "fast" selection and bail. |
| 177 | return 0; |
| 178 | |
| 179 | // If the index is smaller or larger than intptr_t, truncate or extend it. |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 180 | MVT PtrVT = TLI.getPointerTy(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 181 | EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 182 | if (IdxVT.bitsLT(PtrVT)) |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 183 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 184 | else if (IdxVT.bitsGT(PtrVT)) |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 185 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 186 | return IdxN; |
| 187 | } |
| 188 | |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 189 | /// SelectBinaryOp - Select and emit code for a binary operator instruction, |
| 190 | /// which has an opcode which directly corresponds to the given ISD opcode. |
| 191 | /// |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 192 | bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 193 | EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 194 | if (VT == MVT::Other || !VT.isSimple()) |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 195 | // Unhandled type. Halt "fast" selection and bail. |
| 196 | return false; |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 197 | |
Dan Gohman | b71fea2 | 2008-08-26 20:52:40 +0000 | [diff] [blame] | 198 | // We only handle legal types. For example, on x86-32 the instruction |
| 199 | // selector contains all of the 64-bit instructions from x86-64, |
| 200 | // under the assumption that i64 won't be used if the target doesn't |
| 201 | // support it. |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 202 | if (!TLI.isTypeLegal(VT)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 203 | // MVT::i1 is special. Allow AND, OR, or XOR because they |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 204 | // don't require additional zeroing, which makes them easy. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 205 | if (VT == MVT::i1 && |
Dan Gohman | 5dd9c2e | 2008-09-25 17:22:52 +0000 | [diff] [blame] | 206 | (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || |
| 207 | ISDOpcode == ISD::XOR)) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 208 | VT = TLI.getTypeToTransformTo(I->getContext(), VT); |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 209 | else |
| 210 | return false; |
| 211 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 212 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 213 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 214 | if (Op0 == 0) |
| 215 | // Unhandled operand. Halt "fast" selection and bail. |
| 216 | return false; |
| 217 | |
| 218 | // Check if the second operand is a constant and handle it appropriately. |
| 219 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 220 | unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(), |
| 221 | ISDOpcode, Op0, CI->getZExtValue()); |
| 222 | if (ResultReg != 0) { |
| 223 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 224 | UpdateValueMap(I, ResultReg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 225 | return true; |
| 226 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 229 | // Check if the second operand is a constant float. |
| 230 | if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 231 | unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(), |
| 232 | ISDOpcode, Op0, CF); |
| 233 | if (ResultReg != 0) { |
| 234 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 235 | UpdateValueMap(I, ResultReg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 236 | return true; |
| 237 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 240 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 241 | if (Op1 == 0) |
| 242 | // Unhandled operand. Halt "fast" selection and bail. |
| 243 | return false; |
| 244 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 245 | // Now we have both operands in registers. Emit the instruction. |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 246 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
| 247 | ISDOpcode, Op0, Op1); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 248 | if (ResultReg == 0) |
| 249 | // Target-specific code wasn't able to find a machine opcode for |
| 250 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 251 | return false; |
| 252 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 253 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 254 | UpdateValueMap(I, ResultReg); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 258 | bool FastISel::SelectGetElementPtr(User *I) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 259 | unsigned N = getRegForValue(I->getOperand(0)); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 260 | if (N == 0) |
| 261 | // Unhandled operand. Halt "fast" selection and bail. |
| 262 | return false; |
| 263 | |
| 264 | const Type *Ty = I->getOperand(0)->getType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 265 | MVT VT = TLI.getPointerTy(); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 266 | for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end(); |
| 267 | OI != E; ++OI) { |
| 268 | Value *Idx = *OI; |
| 269 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 270 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 271 | if (Field) { |
| 272 | // N = N + Offset |
| 273 | uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); |
| 274 | // FIXME: This can be optimized by combining the add with a |
| 275 | // subsequent one. |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 276 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 277 | if (N == 0) |
| 278 | // Unhandled operand. Halt "fast" selection and bail. |
| 279 | return false; |
| 280 | } |
| 281 | Ty = StTy->getElementType(Field); |
| 282 | } else { |
| 283 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 284 | |
| 285 | // If this is a constant subscript, handle it quickly. |
| 286 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 287 | if (CI->getZExtValue() == 0) continue; |
| 288 | uint64_t Offs = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 289 | TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Dan Gohman | 7a0e659 | 2008-08-21 17:25:26 +0000 | [diff] [blame] | 290 | N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 291 | if (N == 0) |
| 292 | // Unhandled operand. Halt "fast" selection and bail. |
| 293 | return false; |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | // N = N + Idx * ElementSize; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 298 | uint64_t ElementSize = TD.getTypeAllocSize(Ty); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 299 | unsigned IdxN = getRegForGEPIndex(Idx); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 300 | if (IdxN == 0) |
| 301 | // Unhandled operand. Halt "fast" selection and bail. |
| 302 | return false; |
| 303 | |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 304 | if (ElementSize != 1) { |
Dan Gohman | f93cf79 | 2008-08-21 17:37:05 +0000 | [diff] [blame] | 305 | IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 306 | if (IdxN == 0) |
| 307 | // Unhandled operand. Halt "fast" selection and bail. |
| 308 | return false; |
| 309 | } |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 310 | N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 311 | if (N == 0) |
| 312 | // Unhandled operand. Halt "fast" selection and bail. |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 318 | UpdateValueMap(I, N); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 319 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 322 | bool FastISel::SelectCall(User *I) { |
| 323 | Function *F = cast<CallInst>(I)->getCalledFunction(); |
| 324 | if (!F) return false; |
| 325 | |
| 326 | unsigned IID = F->getIntrinsicID(); |
| 327 | switch (IID) { |
| 328 | default: break; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 329 | case Intrinsic::dbg_stoppoint: |
| 330 | case Intrinsic::dbg_region_start: |
| 331 | case Intrinsic::dbg_region_end: |
| 332 | case Intrinsic::dbg_func_start: |
| 333 | // FIXME - Remove this instructions once the dust settles. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 334 | return true; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 335 | case Intrinsic::dbg_declare: { |
| 336 | DbgDeclareInst *DI = cast<DbgDeclareInst>(I); |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 337 | if (!isValidDebugInfoIntrinsic(*DI, CodeGenOpt::None) || !DW |
| 338 | || !DW->ShouldEmitDwarfDebug()) |
| 339 | return true; |
| 340 | |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 341 | Value *Address = DI->getAddress(); |
| 342 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 343 | Address = BCI->getOperand(0); |
| 344 | AllocaInst *AI = dyn_cast<AllocaInst>(Address); |
| 345 | // Don't handle byval struct arguments or VLAs, for example. |
| 346 | if (!AI) break; |
| 347 | DenseMap<const AllocaInst*, int>::iterator SI = |
| 348 | StaticAllocaMap.find(AI); |
| 349 | if (SI == StaticAllocaMap.end()) break; // VLAs. |
| 350 | int FI = SI->second; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 351 | if (MMI) { |
| 352 | MetadataContext &TheMetadata = |
| 353 | DI->getParent()->getContext().getMetadata(); |
| 354 | unsigned MDDbgKind = TheMetadata.getMDKind("dbg"); |
| 355 | MDNode *Dbg = TheMetadata.getMD(MDDbgKind, DI); |
| 356 | MMI->setVariableDbgInfo(DI->getVariable(), FI, Dbg); |
| 357 | } |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 358 | return true; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 359 | } |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 360 | case Intrinsic::eh_exception: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 361 | EVT VT = TLI.getValueType(I->getType()); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 362 | switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) { |
| 363 | default: break; |
| 364 | case TargetLowering::Expand: { |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 365 | assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!"); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 366 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 367 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 368 | unsigned ResultReg = createResultReg(RC); |
| 369 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 370 | Reg, RC, RC); |
| 371 | assert(InsertedCopy && "Can't copy address registers!"); |
Evan Cheng | 24ac408 | 2008-11-24 07:09:49 +0000 | [diff] [blame] | 372 | InsertedCopy = InsertedCopy; |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 373 | UpdateValueMap(I, ResultReg); |
| 374 | return true; |
| 375 | } |
| 376 | } |
| 377 | break; |
| 378 | } |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 379 | case Intrinsic::eh_selector: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 380 | EVT VT = TLI.getValueType(I->getType()); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 381 | switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) { |
| 382 | default: break; |
| 383 | case TargetLowering::Expand: { |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 384 | if (MMI) { |
| 385 | if (MBB->isLandingPad()) |
| 386 | AddCatchInfo(*cast<CallInst>(I), MMI, MBB); |
| 387 | else { |
| 388 | #ifndef NDEBUG |
| 389 | CatchInfoLost.insert(cast<CallInst>(I)); |
| 390 | #endif |
| 391 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
| 392 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 393 | if (Reg) MBB->addLiveIn(Reg); |
| 394 | } |
| 395 | |
| 396 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 397 | EVT SrcVT = TLI.getPointerTy(); |
| 398 | const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 399 | unsigned ResultReg = createResultReg(RC); |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 400 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg, |
| 401 | RC, RC); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 402 | assert(InsertedCopy && "Can't copy address registers!"); |
Evan Cheng | 24ac408 | 2008-11-24 07:09:49 +0000 | [diff] [blame] | 403 | InsertedCopy = InsertedCopy; |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 404 | |
| 405 | // Cast the register to the type of the selector. |
| 406 | if (SrcVT.bitsGT(MVT::i32)) |
| 407 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE, |
| 408 | ResultReg); |
| 409 | else if (SrcVT.bitsLT(MVT::i32)) |
| 410 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, |
| 411 | ISD::SIGN_EXTEND, ResultReg); |
| 412 | if (ResultReg == 0) |
| 413 | // Unhandled operand. Halt "fast" selection and bail. |
| 414 | return false; |
| 415 | |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 416 | UpdateValueMap(I, ResultReg); |
| 417 | } else { |
| 418 | unsigned ResultReg = |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 419 | getRegForValue(Constant::getNullValue(I->getType())); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 420 | UpdateValueMap(I, ResultReg); |
| 421 | } |
| 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | break; |
| 426 | } |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 431 | bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 432 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 433 | EVT DstVT = TLI.getValueType(I->getType()); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 434 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 435 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 436 | DstVT == MVT::Other || !DstVT.isSimple()) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 437 | // Unhandled type. Halt "fast" selection and bail. |
| 438 | return false; |
| 439 | |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 440 | // Check if the destination type is legal. Or as a special case, |
| 441 | // it may be i1 if we're doing a truncate because that's |
| 442 | // easy and somewhat common. |
| 443 | if (!TLI.isTypeLegal(DstVT)) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 444 | if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE) |
Dan Gohman | 91b6f97 | 2008-10-03 01:28:47 +0000 | [diff] [blame] | 445 | // Unhandled type. Halt "fast" selection and bail. |
| 446 | return false; |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 447 | |
| 448 | // Check if the source operand is legal. Or as a special case, |
| 449 | // it may be i1 if we're doing zero-extension because that's |
| 450 | // easy and somewhat common. |
| 451 | if (!TLI.isTypeLegal(SrcVT)) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 452 | if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND) |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 453 | // Unhandled type. Halt "fast" selection and bail. |
| 454 | return false; |
| 455 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 456 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 457 | if (!InputReg) |
| 458 | // Unhandled operand. Halt "fast" selection and bail. |
| 459 | return false; |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 460 | |
| 461 | // If the operand is i1, arrange for the high bits in the register to be zero. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 462 | if (SrcVT == MVT::i1) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 463 | SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT); |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 464 | InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg); |
| 465 | if (!InputReg) |
| 466 | return false; |
| 467 | } |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 468 | // If the result is i1, truncate to the target's type for i1 first. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 469 | if (DstVT == MVT::i1) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 470 | DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT); |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 471 | |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 472 | unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), |
| 473 | DstVT.getSimpleVT(), |
| 474 | Opcode, |
| 475 | InputReg); |
| 476 | if (!ResultReg) |
| 477 | return false; |
| 478 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 479 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 483 | bool FastISel::SelectBitCast(User *I) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 484 | // If the bitcast doesn't change the type, just use the operand value. |
| 485 | if (I->getType() == I->getOperand(0)->getType()) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 486 | unsigned Reg = getRegForValue(I->getOperand(0)); |
Dan Gohman | a318dab | 2008-08-27 20:41:38 +0000 | [diff] [blame] | 487 | if (Reg == 0) |
| 488 | return false; |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 489 | UpdateValueMap(I, Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 490 | return true; |
| 491 | } |
| 492 | |
| 493 | // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 494 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 495 | EVT DstVT = TLI.getValueType(I->getType()); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 496 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 497 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 498 | DstVT == MVT::Other || !DstVT.isSimple() || |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 499 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 500 | // Unhandled type. Halt "fast" selection and bail. |
| 501 | return false; |
| 502 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 503 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 504 | if (Op0 == 0) |
| 505 | // Unhandled operand. Halt "fast" selection and bail. |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 506 | return false; |
| 507 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 508 | // First, try to perform the bitcast by inserting a reg-reg copy. |
| 509 | unsigned ResultReg = 0; |
| 510 | if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) { |
| 511 | TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); |
| 512 | TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); |
| 513 | ResultReg = createResultReg(DstClass); |
| 514 | |
| 515 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 516 | Op0, DstClass, SrcClass); |
| 517 | if (!InsertedCopy) |
| 518 | ResultReg = 0; |
| 519 | } |
| 520 | |
| 521 | // If the reg-reg copy failed, select a BIT_CONVERT opcode. |
| 522 | if (!ResultReg) |
| 523 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), |
| 524 | ISD::BIT_CONVERT, Op0); |
| 525 | |
| 526 | if (!ResultReg) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 527 | return false; |
| 528 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 529 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 530 | return true; |
| 531 | } |
| 532 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 533 | bool |
| 534 | FastISel::SelectInstruction(Instruction *I) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 535 | return SelectOperator(I, I->getOpcode()); |
| 536 | } |
| 537 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 538 | /// FastEmitBranch - Emit an unconditional branch to the given block, |
| 539 | /// unless it is the immediate (fall-through) successor, and update |
| 540 | /// the CFG. |
| 541 | void |
| 542 | FastISel::FastEmitBranch(MachineBasicBlock *MSucc) { |
| 543 | MachineFunction::iterator NextMBB = |
| 544 | next(MachineFunction::iterator(MBB)); |
| 545 | |
| 546 | if (MBB->isLayoutSuccessor(MSucc)) { |
| 547 | // The unconditional fall-through case, which needs no instructions. |
| 548 | } else { |
| 549 | // The unconditional branch case. |
| 550 | TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>()); |
| 551 | } |
| 552 | MBB->addSuccessor(MSucc); |
| 553 | } |
| 554 | |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 555 | /// SelectFNeg - Emit an FNeg operation. |
| 556 | /// |
| 557 | bool |
| 558 | FastISel::SelectFNeg(User *I) { |
| 559 | unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I)); |
| 560 | if (OpReg == 0) return false; |
| 561 | |
Dan Gohman | 4a215a1 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 562 | // If the target has ISD::FNEG, use it. |
| 563 | EVT VT = TLI.getValueType(I->getType()); |
| 564 | unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), |
| 565 | ISD::FNEG, OpReg); |
| 566 | if (ResultReg != 0) { |
| 567 | UpdateValueMap(I, ResultReg); |
| 568 | return true; |
| 569 | } |
| 570 | |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 571 | // Bitcast the value to integer, twiddle the sign bit with xor, |
| 572 | // and then bitcast it back to floating-point. |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 573 | if (VT.getSizeInBits() > 64) return false; |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 574 | EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); |
| 575 | if (!TLI.isTypeLegal(IntVT)) |
| 576 | return false; |
| 577 | |
| 578 | unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), |
| 579 | ISD::BIT_CONVERT, OpReg); |
| 580 | if (IntReg == 0) |
| 581 | return false; |
| 582 | |
| 583 | unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, IntReg, |
| 584 | UINT64_C(1) << (VT.getSizeInBits()-1), |
| 585 | IntVT.getSimpleVT()); |
| 586 | if (IntResultReg == 0) |
| 587 | return false; |
| 588 | |
| 589 | ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), |
| 590 | ISD::BIT_CONVERT, IntResultReg); |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 591 | if (ResultReg == 0) |
| 592 | return false; |
| 593 | |
| 594 | UpdateValueMap(I, ResultReg); |
| 595 | return true; |
| 596 | } |
| 597 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 598 | bool |
| 599 | FastISel::SelectOperator(User *I, unsigned Opcode) { |
| 600 | switch (Opcode) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 601 | case Instruction::Add: |
| 602 | return SelectBinaryOp(I, ISD::ADD); |
| 603 | case Instruction::FAdd: |
| 604 | return SelectBinaryOp(I, ISD::FADD); |
| 605 | case Instruction::Sub: |
| 606 | return SelectBinaryOp(I, ISD::SUB); |
| 607 | case Instruction::FSub: |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 608 | // FNeg is currently represented in LLVM IR as a special case of FSub. |
| 609 | if (BinaryOperator::isFNeg(I)) |
| 610 | return SelectFNeg(I); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 611 | return SelectBinaryOp(I, ISD::FSUB); |
| 612 | case Instruction::Mul: |
| 613 | return SelectBinaryOp(I, ISD::MUL); |
| 614 | case Instruction::FMul: |
| 615 | return SelectBinaryOp(I, ISD::FMUL); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 616 | case Instruction::SDiv: |
| 617 | return SelectBinaryOp(I, ISD::SDIV); |
| 618 | case Instruction::UDiv: |
| 619 | return SelectBinaryOp(I, ISD::UDIV); |
| 620 | case Instruction::FDiv: |
| 621 | return SelectBinaryOp(I, ISD::FDIV); |
| 622 | case Instruction::SRem: |
| 623 | return SelectBinaryOp(I, ISD::SREM); |
| 624 | case Instruction::URem: |
| 625 | return SelectBinaryOp(I, ISD::UREM); |
| 626 | case Instruction::FRem: |
| 627 | return SelectBinaryOp(I, ISD::FREM); |
| 628 | case Instruction::Shl: |
| 629 | return SelectBinaryOp(I, ISD::SHL); |
| 630 | case Instruction::LShr: |
| 631 | return SelectBinaryOp(I, ISD::SRL); |
| 632 | case Instruction::AShr: |
| 633 | return SelectBinaryOp(I, ISD::SRA); |
| 634 | case Instruction::And: |
| 635 | return SelectBinaryOp(I, ISD::AND); |
| 636 | case Instruction::Or: |
| 637 | return SelectBinaryOp(I, ISD::OR); |
| 638 | case Instruction::Xor: |
| 639 | return SelectBinaryOp(I, ISD::XOR); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 640 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 641 | case Instruction::GetElementPtr: |
| 642 | return SelectGetElementPtr(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 643 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 644 | case Instruction::Br: { |
| 645 | BranchInst *BI = cast<BranchInst>(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 646 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 647 | if (BI->isUnconditional()) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 648 | BasicBlock *LLVMSucc = BI->getSuccessor(0); |
| 649 | MachineBasicBlock *MSucc = MBBMap[LLVMSucc]; |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 650 | FastEmitBranch(MSucc); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 651 | return true; |
Owen Anderson | 9d5b416 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 652 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 653 | |
| 654 | // Conditional branches are not handed yet. |
| 655 | // Halt "fast" selection and bail. |
| 656 | return false; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Dan Gohman | 087c850 | 2008-09-05 01:08:41 +0000 | [diff] [blame] | 659 | case Instruction::Unreachable: |
| 660 | // Nothing to emit. |
| 661 | return true; |
| 662 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 663 | case Instruction::PHI: |
| 664 | // PHI nodes are already emitted. |
| 665 | return true; |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 666 | |
| 667 | case Instruction::Alloca: |
| 668 | // FunctionLowering has the static-sized case covered. |
| 669 | if (StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 670 | return true; |
| 671 | |
| 672 | // Dynamic-sized alloca is not handled yet. |
| 673 | return false; |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 674 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 675 | case Instruction::Call: |
| 676 | return SelectCall(I); |
| 677 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 678 | case Instruction::BitCast: |
| 679 | return SelectBitCast(I); |
| 680 | |
| 681 | case Instruction::FPToSI: |
| 682 | return SelectCast(I, ISD::FP_TO_SINT); |
| 683 | case Instruction::ZExt: |
| 684 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 685 | case Instruction::SExt: |
| 686 | return SelectCast(I, ISD::SIGN_EXTEND); |
| 687 | case Instruction::Trunc: |
| 688 | return SelectCast(I, ISD::TRUNCATE); |
| 689 | case Instruction::SIToFP: |
| 690 | return SelectCast(I, ISD::SINT_TO_FP); |
| 691 | |
| 692 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 693 | case Instruction::PtrToInt: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 694 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 695 | EVT DstVT = TLI.getValueType(I->getType()); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 696 | if (DstVT.bitsGT(SrcVT)) |
| 697 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 698 | if (DstVT.bitsLT(SrcVT)) |
| 699 | return SelectCast(I, ISD::TRUNCATE); |
| 700 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 701 | if (Reg == 0) return false; |
| 702 | UpdateValueMap(I, Reg); |
| 703 | return true; |
| 704 | } |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 705 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 706 | default: |
| 707 | // Unhandled instruction. Halt "fast" selection and bail. |
| 708 | return false; |
| 709 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 712 | FastISel::FastISel(MachineFunction &mf, |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 713 | MachineModuleInfo *mmi, |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 714 | DwarfWriter *dw, |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 715 | DenseMap<const Value *, unsigned> &vm, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 716 | DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 717 | DenseMap<const AllocaInst *, int> &am |
| 718 | #ifndef NDEBUG |
| 719 | , SmallSet<Instruction*, 8> &cil |
| 720 | #endif |
| 721 | ) |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 722 | : MBB(0), |
| 723 | ValueMap(vm), |
| 724 | MBBMap(bm), |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 725 | StaticAllocaMap(am), |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 726 | #ifndef NDEBUG |
| 727 | CatchInfoLost(cil), |
| 728 | #endif |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 729 | MF(mf), |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 730 | MMI(mmi), |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 731 | DW(dw), |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 732 | MRI(MF.getRegInfo()), |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 733 | MFI(*MF.getFrameInfo()), |
| 734 | MCP(*MF.getConstantPool()), |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 735 | TM(MF.getTarget()), |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 736 | TD(*TM.getTargetData()), |
| 737 | TII(*TM.getInstrInfo()), |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 738 | TLI(*TM.getTargetLowering()) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 741 | FastISel::~FastISel() {} |
| 742 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 743 | unsigned FastISel::FastEmit_(MVT, MVT, |
Evan Cheng | 36fd941 | 2008-09-02 21:59:13 +0000 | [diff] [blame] | 744 | ISD::NodeType) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 748 | unsigned FastISel::FastEmit_r(MVT, MVT, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 749 | ISD::NodeType, unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 753 | unsigned FastISel::FastEmit_rr(MVT, MVT, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 754 | ISD::NodeType, unsigned /*Op0*/, |
| 755 | unsigned /*Op0*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 759 | unsigned FastISel::FastEmit_i(MVT, MVT, ISD::NodeType, uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 763 | unsigned FastISel::FastEmit_f(MVT, MVT, |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 764 | ISD::NodeType, ConstantFP * /*FPImm*/) { |
| 765 | return 0; |
| 766 | } |
| 767 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 768 | unsigned FastISel::FastEmit_ri(MVT, MVT, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 769 | ISD::NodeType, unsigned /*Op0*/, |
| 770 | uint64_t /*Imm*/) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 774 | unsigned FastISel::FastEmit_rf(MVT, MVT, |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 775 | ISD::NodeType, unsigned /*Op0*/, |
| 776 | ConstantFP * /*FPImm*/) { |
| 777 | return 0; |
| 778 | } |
| 779 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 780 | unsigned FastISel::FastEmit_rri(MVT, MVT, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 781 | ISD::NodeType, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 782 | unsigned /*Op0*/, unsigned /*Op1*/, |
| 783 | uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 788 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 789 | /// If that fails, it materializes the immediate into a register and try |
| 790 | /// FastEmit_rr instead. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 791 | unsigned FastISel::FastEmit_ri_(MVT VT, ISD::NodeType Opcode, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 792 | unsigned Op0, uint64_t Imm, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 793 | MVT ImmType) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 794 | // First check if immediate type is legal. If not, we can't use the ri form. |
Dan Gohman | 151ed61 | 2008-08-27 18:15:05 +0000 | [diff] [blame] | 795 | unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 796 | if (ResultReg != 0) |
| 797 | return ResultReg; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 798 | unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 799 | if (MaterialReg == 0) |
| 800 | return 0; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 801 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 804 | /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries |
| 805 | /// to emit an instruction with a floating-point immediate operand using |
| 806 | /// FastEmit_rf. If that fails, it materializes the immediate into a register |
| 807 | /// and try FastEmit_rr instead. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 808 | unsigned FastISel::FastEmit_rf_(MVT VT, ISD::NodeType Opcode, |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 809 | unsigned Op0, ConstantFP *FPImm, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 810 | MVT ImmType) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 811 | // First check if immediate type is legal. If not, we can't use the rf form. |
Dan Gohman | 151ed61 | 2008-08-27 18:15:05 +0000 | [diff] [blame] | 812 | unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm); |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 813 | if (ResultReg != 0) |
| 814 | return ResultReg; |
| 815 | |
| 816 | // Materialize the constant in a register. |
| 817 | unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm); |
| 818 | if (MaterialReg == 0) { |
Dan Gohman | 96a9999 | 2008-08-27 18:01:42 +0000 | [diff] [blame] | 819 | // If the target doesn't have a way to directly enter a floating-point |
| 820 | // value into a register, use an alternate approach. |
| 821 | // TODO: The current approach only supports floating-point constants |
| 822 | // that can be constructed by conversion from integer values. This should |
| 823 | // be replaced by code that creates a load from a constant-pool entry, |
| 824 | // which will require some target-specific work. |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 825 | const APFloat &Flt = FPImm->getValueAPF(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 826 | EVT IntVT = TLI.getPointerTy(); |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 827 | |
| 828 | uint64_t x[2]; |
| 829 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 830 | bool isExact; |
| 831 | (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
| 832 | APFloat::rmTowardZero, &isExact); |
| 833 | if (!isExact) |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 834 | return 0; |
| 835 | APInt IntVal(IntBitWidth, 2, x); |
| 836 | |
| 837 | unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(), |
| 838 | ISD::Constant, IntVal.getZExtValue()); |
| 839 | if (IntegerReg == 0) |
| 840 | return 0; |
| 841 | MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT, |
| 842 | ISD::SINT_TO_FP, IntegerReg); |
| 843 | if (MaterialReg == 0) |
| 844 | return 0; |
| 845 | } |
| 846 | return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg); |
| 847 | } |
| 848 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 849 | unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { |
| 850 | return MRI.createVirtualRegister(RC); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 853 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 854 | const TargetRegisterClass* RC) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 855 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 856 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 857 | |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 858 | BuildMI(MBB, DL, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 859 | return ResultReg; |
| 860 | } |
| 861 | |
| 862 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 863 | const TargetRegisterClass *RC, |
| 864 | unsigned Op0) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 865 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 866 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 867 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 868 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 869 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 870 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 871 | BuildMI(MBB, DL, II).addReg(Op0); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 872 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 873 | II.ImplicitDefs[0], RC, RC); |
| 874 | if (!InsertedCopy) |
| 875 | ResultReg = 0; |
| 876 | } |
| 877 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 878 | return ResultReg; |
| 879 | } |
| 880 | |
| 881 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 882 | const TargetRegisterClass *RC, |
| 883 | unsigned Op0, unsigned Op1) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 884 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 885 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 886 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 887 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 888 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 889 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 890 | BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 891 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 892 | II.ImplicitDefs[0], RC, RC); |
| 893 | if (!InsertedCopy) |
| 894 | ResultReg = 0; |
| 895 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 896 | return ResultReg; |
| 897 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 898 | |
| 899 | unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 900 | const TargetRegisterClass *RC, |
| 901 | unsigned Op0, uint64_t Imm) { |
| 902 | unsigned ResultReg = createResultReg(RC); |
| 903 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 904 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 905 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 906 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 907 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 908 | BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 909 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 910 | II.ImplicitDefs[0], RC, RC); |
| 911 | if (!InsertedCopy) |
| 912 | ResultReg = 0; |
| 913 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 914 | return ResultReg; |
| 915 | } |
| 916 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 917 | unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 918 | const TargetRegisterClass *RC, |
| 919 | unsigned Op0, ConstantFP *FPImm) { |
| 920 | unsigned ResultReg = createResultReg(RC); |
| 921 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 922 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 923 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 924 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 925 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 926 | BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 927 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 928 | II.ImplicitDefs[0], RC, RC); |
| 929 | if (!InsertedCopy) |
| 930 | ResultReg = 0; |
| 931 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 932 | return ResultReg; |
| 933 | } |
| 934 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 935 | unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 936 | const TargetRegisterClass *RC, |
| 937 | unsigned Op0, unsigned Op1, uint64_t Imm) { |
| 938 | unsigned ResultReg = createResultReg(RC); |
| 939 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 940 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 941 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 942 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 943 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 944 | BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 945 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 946 | II.ImplicitDefs[0], RC, RC); |
| 947 | if (!InsertedCopy) |
| 948 | ResultReg = 0; |
| 949 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 950 | return ResultReg; |
| 951 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 952 | |
| 953 | unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 954 | const TargetRegisterClass *RC, |
| 955 | uint64_t Imm) { |
| 956 | unsigned ResultReg = createResultReg(RC); |
| 957 | const TargetInstrDesc &II = TII.get(MachineInstOpcode); |
| 958 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 959 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 960 | BuildMI(MBB, DL, II, ResultReg).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 961 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 962 | BuildMI(MBB, DL, II).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 963 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 964 | II.ImplicitDefs[0], RC, RC); |
| 965 | if (!InsertedCopy) |
| 966 | ResultReg = 0; |
| 967 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 968 | return ResultReg; |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 969 | } |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 970 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 971 | unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT, |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 972 | unsigned Op0, uint32_t Idx) { |
Owen Anderson | 40a468f | 2008-08-28 17:47:37 +0000 | [diff] [blame] | 973 | const TargetRegisterClass* RC = MRI.getRegClass(Op0); |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 974 | |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 975 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 976 | const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG); |
| 977 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 978 | if (II.getNumDefs() >= 1) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 979 | BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 980 | else { |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 981 | BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 982 | bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 983 | II.ImplicitDefs[0], RC, RC); |
| 984 | if (!InsertedCopy) |
| 985 | ResultReg = 0; |
| 986 | } |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 987 | return ResultReg; |
| 988 | } |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 989 | |
| 990 | /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op |
| 991 | /// with all but the least significant bit set to zero. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 992 | unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) { |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 993 | return FastEmit_ri(VT, VT, ISD::AND, Op, 1); |
| 994 | } |