Dan Gohman | 3b172f1 | 2010-04-22 20:06:42 +0000 | [diff] [blame] | 1 | //===-- FastISel.cpp - Implementation of the FastISel class ---------------===// |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 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 | |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "isel" |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 43 | #include "llvm/Function.h" |
| 44 | #include "llvm/GlobalVariable.h" |
Dan Gohman | 6f2766d | 2008-08-19 22:31:46 +0000 | [diff] [blame] | 45 | #include "llvm/Instructions.h" |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 46 | #include "llvm/IntrinsicInst.h" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 47 | #include "llvm/Operator.h" |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/Analysis.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/FastISel.h" |
Dan Gohman | 4c3fd9f | 2010-07-07 16:01:37 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 54 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | 7fbcc98 | 2010-07-01 03:49:38 +0000 | [diff] [blame] | 55 | #include "llvm/Analysis/Loads.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetData.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 60 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 61 | #include "llvm/Support/Debug.h" |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 63 | using namespace llvm; |
| 64 | |
Chad Rosier | aa5656c | 2011-11-28 19:59:09 +0000 | [diff] [blame^] | 65 | STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by " |
| 66 | "target-independent selector"); |
| 67 | STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by " |
| 68 | "target-specific selector"); |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 69 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 70 | /// startNewBlock - Set the current block to which generated machine |
| 71 | /// instructions will be appended, and clear the local CSE map. |
| 72 | /// |
| 73 | void FastISel::startNewBlock() { |
| 74 | LocalValueMap.clear(); |
| 75 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 76 | EmitStartPt = 0; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 77 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 78 | // Advance the emit start point past any EH_LABEL instructions. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 79 | MachineBasicBlock::iterator |
| 80 | I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end(); |
| 81 | while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) { |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 82 | EmitStartPt = I; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 83 | ++I; |
| 84 | } |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 85 | LastLocalValue = EmitStartPt; |
| 86 | } |
| 87 | |
| 88 | void FastISel::flushLocalValueMap() { |
| 89 | LocalValueMap.clear(); |
| 90 | LastLocalValue = EmitStartPt; |
| 91 | recomputeInsertPt(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 94 | bool FastISel::hasTrivialKill(const Value *V) const { |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 95 | // Don't consider constants or arguments to have trivial kills. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 96 | const Instruction *I = dyn_cast<Instruction>(V); |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 97 | if (!I) |
| 98 | return false; |
| 99 | |
| 100 | // No-op casts are trivially coalesced by fast-isel. |
| 101 | if (const CastInst *Cast = dyn_cast<CastInst>(I)) |
| 102 | if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) && |
| 103 | !hasTrivialKill(Cast->getOperand(0))) |
| 104 | return false; |
| 105 | |
Chad Rosier | 22b34cc | 2011-11-15 23:34:05 +0000 | [diff] [blame] | 106 | // GEPs with all zero indices are trivially coalesced by fast-isel. |
| 107 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) |
| 108 | if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0))) |
| 109 | return false; |
| 110 | |
Dan Gohman | 7f0d695 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 111 | // Only instructions with a single use in the same basic block are considered |
| 112 | // to have trivial kills. |
| 113 | return I->hasOneUse() && |
| 114 | !(I->getOpcode() == Instruction::BitCast || |
| 115 | I->getOpcode() == Instruction::PtrToInt || |
| 116 | I->getOpcode() == Instruction::IntToPtr) && |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 117 | cast<Instruction>(*I->use_begin())->getParent() == I->getParent(); |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 120 | unsigned FastISel::getRegForValue(const Value *V) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 121 | EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true); |
Dan Gohman | 4fd5528 | 2009-04-07 20:40:11 +0000 | [diff] [blame] | 122 | // Don't handle non-simple values in FastISel. |
| 123 | if (!RealVT.isSimple()) |
| 124 | return 0; |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 125 | |
| 126 | // Ignore illegal types. We must do this before looking up the value |
| 127 | // in ValueMap because Arguments are given virtual registers regardless |
| 128 | // of whether FastISel can handle them. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 129 | MVT VT = RealVT.getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 130 | if (!TLI.isTypeLegal(VT)) { |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 131 | // Handle integer promotions, though, because they're common and easy. |
| 132 | if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 133 | VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 134 | else |
| 135 | return 0; |
| 136 | } |
| 137 | |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 138 | // Look up the value to see if we already have a register for it. We |
| 139 | // cache values defined by Instructions across blocks, and other values |
| 140 | // only locally. This is because Instructions already have the SSA |
Dan Gohman | 5c9cf19 | 2010-01-12 04:30:26 +0000 | [diff] [blame] | 141 | // def-dominates-use requirement enforced. |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 142 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V); |
Chris Lattner | fff65b3 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 143 | if (I != FuncInfo.ValueMap.end()) |
| 144 | return I->second; |
| 145 | |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 146 | unsigned Reg = LocalValueMap[V]; |
| 147 | if (Reg != 0) |
| 148 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 149 | |
Dan Gohman | 97c94b8 | 2010-05-06 00:02:14 +0000 | [diff] [blame] | 150 | // In bottom-up mode, just create the virtual register which will be used |
| 151 | // to hold the value. It will be materialized later. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 152 | if (isa<Instruction>(V) && |
| 153 | (!isa<AllocaInst>(V) || |
| 154 | !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V)))) |
| 155 | return FuncInfo.InitializeRegForValue(V); |
Dan Gohman | 97c94b8 | 2010-05-06 00:02:14 +0000 | [diff] [blame] | 156 | |
Dan Gohman | a10b849 | 2010-07-14 01:07:44 +0000 | [diff] [blame] | 157 | SavePoint SaveInsertPt = enterLocalValueArea(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 158 | |
| 159 | // Materialize the value in a register. Emit any instructions in the |
| 160 | // local value area. |
| 161 | Reg = materializeRegForValue(V, VT); |
| 162 | |
| 163 | leaveLocalValueArea(SaveInsertPt); |
| 164 | |
| 165 | return Reg; |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Eric Christopher | 44a2c34 | 2010-08-17 01:30:33 +0000 | [diff] [blame] | 168 | /// materializeRegForValue - Helper for getRegForValue. This function is |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 169 | /// called when the value isn't already available in a register and must |
| 170 | /// be materialized with new instructions. |
| 171 | unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) { |
| 172 | unsigned Reg = 0; |
| 173 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 174 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 175 | if (CI->getValue().getActiveBits() <= 64) |
| 176 | Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 177 | } else if (isa<AllocaInst>(V)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 178 | Reg = TargetMaterializeAlloca(cast<AllocaInst>(V)); |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 179 | } else if (isa<ConstantPointerNull>(V)) { |
Dan Gohman | 1e9e8c3 | 2008-10-07 22:03:27 +0000 | [diff] [blame] | 180 | // Translate this as an integer zero so that it can be |
| 181 | // local-CSE'd with actual integer zeros. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 182 | Reg = |
| 183 | getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext()))); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 184 | } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Eli Friedman | bd12538 | 2011-04-28 00:42:03 +0000 | [diff] [blame] | 185 | if (CF->isNullValue()) { |
Eli Friedman | 2790ba8 | 2011-04-27 22:41:55 +0000 | [diff] [blame] | 186 | Reg = TargetMaterializeFloatZero(CF); |
| 187 | } else { |
| 188 | // Try to emit the constant directly. |
| 189 | Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF); |
| 190 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 191 | |
| 192 | if (!Reg) { |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 193 | // Try to emit the constant by using an integer constant with a cast. |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 194 | const APFloat &Flt = CF->getValueAPF(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 195 | EVT IntVT = TLI.getPointerTy(); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 196 | |
| 197 | uint64_t x[2]; |
| 198 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 199 | bool isExact; |
| 200 | (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true, |
| 201 | APFloat::rmTowardZero, &isExact); |
| 202 | if (isExact) { |
Jeffrey Yasskin | 3ba292d | 2011-07-18 21:45:40 +0000 | [diff] [blame] | 203 | APInt IntVal(IntBitWidth, x); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 204 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 205 | unsigned IntegerReg = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 206 | getRegForValue(ConstantInt::get(V->getContext(), IntVal)); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 207 | if (IntegerReg != 0) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 208 | Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, |
| 209 | IntegerReg, /*Kill=*/false); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 210 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 211 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 212 | } else if (const Operator *Op = dyn_cast<Operator>(V)) { |
Dan Gohman | 20d4be1 | 2010-07-01 02:58:57 +0000 | [diff] [blame] | 213 | if (!SelectOperator(Op, Op->getOpcode())) |
| 214 | if (!isa<Instruction>(Op) || |
| 215 | !TargetSelectInstruction(cast<Instruction>(Op))) |
| 216 | return 0; |
Dan Gohman | 37db6cd | 2010-06-21 14:17:46 +0000 | [diff] [blame] | 217 | Reg = lookUpRegForValue(Op); |
Dan Gohman | 205d925 | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 218 | } else if (isa<UndefValue>(V)) { |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 219 | Reg = createResultReg(TLI.getRegClassFor(VT)); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 220 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 221 | TII.get(TargetOpcode::IMPLICIT_DEF), Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 222 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 223 | |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 224 | // If target-independent code couldn't handle the value, give target-specific |
| 225 | // code a try. |
Owen Anderson | 6e60745 | 2008-09-05 23:36:01 +0000 | [diff] [blame] | 226 | if (!Reg && isa<Constant>(V)) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 227 | Reg = TargetMaterializeConstant(cast<Constant>(V)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 228 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 229 | // Don't cache constant materializations in the general ValueMap. |
| 230 | // To do so would require tracking what uses they dominate. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 231 | if (Reg != 0) { |
Dan Gohman | dceffe6 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 232 | LocalValueMap[V] = Reg; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 233 | LastLocalValue = MRI.getVRegDef(Reg); |
| 234 | } |
Dan Gohman | 104e4ce | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 235 | return Reg; |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 238 | unsigned FastISel::lookUpRegForValue(const Value *V) { |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 239 | // Look up the value to see if we already have a register for it. We |
| 240 | // cache values defined by Instructions across blocks, and other values |
| 241 | // only locally. This is because Instructions already have the SSA |
Dan Gohman | 1fdc614 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 242 | // def-dominates-use requirement enforced. |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 243 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V); |
| 244 | if (I != FuncInfo.ValueMap.end()) |
Dan Gohman | 3193a68 | 2010-06-21 14:21:47 +0000 | [diff] [blame] | 245 | return I->second; |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 246 | return LocalValueMap[V]; |
| 247 | } |
| 248 | |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 249 | /// UpdateValueMap - Update the value map to include the new mapping for this |
| 250 | /// instruction, or insert an extra copy to get the result in a previous |
| 251 | /// determined register. |
| 252 | /// NOTE: This is only necessary because we might select a block that uses |
| 253 | /// a value before we select the block that defines the value. It might be |
| 254 | /// possible to fix this by selecting blocks in reverse postorder. |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 255 | void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 256 | if (!isa<Instruction>(I)) { |
| 257 | LocalValueMap[I] = Reg; |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 258 | return; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 259 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 260 | |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 261 | unsigned &AssignedReg = FuncInfo.ValueMap[I]; |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 262 | if (AssignedReg == 0) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 263 | // Use the new register. |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 264 | AssignedReg = Reg; |
Chris Lattner | 36e3946 | 2009-04-12 07:46:30 +0000 | [diff] [blame] | 265 | else if (Reg != AssignedReg) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 266 | // Arrange for uses of AssignedReg to be replaced by uses of Reg. |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 267 | for (unsigned i = 0; i < NumRegs; i++) |
| 268 | FuncInfo.RegFixups[AssignedReg+i] = Reg+i; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 269 | |
| 270 | AssignedReg = Reg; |
Chris Lattner | c5040ab | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 271 | } |
Owen Anderson | cc54e76 | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 274 | std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) { |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 275 | unsigned IdxN = getRegForValue(Idx); |
| 276 | if (IdxN == 0) |
| 277 | // Unhandled operand. Halt "fast" selection and bail. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 278 | return std::pair<unsigned, bool>(0, false); |
| 279 | |
| 280 | bool IdxNIsKill = hasTrivialKill(Idx); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 281 | |
| 282 | // 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] | 283 | MVT PtrVT = TLI.getPointerTy(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 284 | EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 285 | if (IdxVT.bitsLT(PtrVT)) { |
| 286 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, |
| 287 | IdxN, IdxNIsKill); |
| 288 | IdxNIsKill = true; |
| 289 | } |
| 290 | else if (IdxVT.bitsGT(PtrVT)) { |
| 291 | IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, |
| 292 | IdxN, IdxNIsKill); |
| 293 | IdxNIsKill = true; |
| 294 | } |
| 295 | return std::pair<unsigned, bool>(IdxN, IdxNIsKill); |
Dan Gohman | c8a1a3c | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 298 | void FastISel::recomputeInsertPt() { |
| 299 | if (getLastLocalValue()) { |
| 300 | FuncInfo.InsertPt = getLastLocalValue(); |
Dan Gohman | c6e59b7 | 2010-07-19 22:48:56 +0000 | [diff] [blame] | 301 | FuncInfo.MBB = FuncInfo.InsertPt->getParent(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 302 | ++FuncInfo.InsertPt; |
| 303 | } else |
| 304 | FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI(); |
| 305 | |
| 306 | // Now skip past any EH_LABELs, which must remain at the beginning. |
| 307 | while (FuncInfo.InsertPt != FuncInfo.MBB->end() && |
| 308 | FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL) |
| 309 | ++FuncInfo.InsertPt; |
| 310 | } |
| 311 | |
Dan Gohman | a10b849 | 2010-07-14 01:07:44 +0000 | [diff] [blame] | 312 | FastISel::SavePoint FastISel::enterLocalValueArea() { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 313 | MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt; |
Dan Gohman | 163f78e | 2010-07-14 22:01:31 +0000 | [diff] [blame] | 314 | DebugLoc OldDL = DL; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 315 | recomputeInsertPt(); |
Dan Gohman | a10b849 | 2010-07-14 01:07:44 +0000 | [diff] [blame] | 316 | DL = DebugLoc(); |
Dan Gohman | 163f78e | 2010-07-14 22:01:31 +0000 | [diff] [blame] | 317 | SavePoint SP = { OldInsertPt, OldDL }; |
Dan Gohman | a10b849 | 2010-07-14 01:07:44 +0000 | [diff] [blame] | 318 | return SP; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Dan Gohman | a10b849 | 2010-07-14 01:07:44 +0000 | [diff] [blame] | 321 | void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 322 | if (FuncInfo.InsertPt != FuncInfo.MBB->begin()) |
| 323 | LastLocalValue = llvm::prior(FuncInfo.InsertPt); |
| 324 | |
| 325 | // Restore the previous insert position. |
Dan Gohman | a10b849 | 2010-07-14 01:07:44 +0000 | [diff] [blame] | 326 | FuncInfo.InsertPt = OldInsertPt.InsertPt; |
| 327 | DL = OldInsertPt.DL; |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 330 | /// SelectBinaryOp - Select and emit code for a binary operator instruction, |
| 331 | /// which has an opcode which directly corresponds to the given ISD opcode. |
| 332 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 333 | bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 334 | EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 335 | if (VT == MVT::Other || !VT.isSimple()) |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 336 | // Unhandled type. Halt "fast" selection and bail. |
| 337 | return false; |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 338 | |
Dan Gohman | b71fea2 | 2008-08-26 20:52:40 +0000 | [diff] [blame] | 339 | // We only handle legal types. For example, on x86-32 the instruction |
| 340 | // selector contains all of the 64-bit instructions from x86-64, |
| 341 | // under the assumption that i64 won't be used if the target doesn't |
| 342 | // support it. |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 343 | if (!TLI.isTypeLegal(VT)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 344 | // MVT::i1 is special. Allow AND, OR, or XOR because they |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 345 | // don't require additional zeroing, which makes them easy. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 346 | if (VT == MVT::i1 && |
Dan Gohman | 5dd9c2e | 2008-09-25 17:22:52 +0000 | [diff] [blame] | 347 | (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || |
| 348 | ISDOpcode == ISD::XOR)) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 349 | VT = TLI.getTypeToTransformTo(I->getContext(), VT); |
Dan Gohman | 638c683 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 350 | else |
| 351 | return false; |
| 352 | } |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 353 | |
Chris Lattner | fff65b3 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 354 | // Check if the first operand is a constant, and handle it as "ri". At -O0, |
| 355 | // we don't have anything that canonicalizes operand order. |
| 356 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0))) |
| 357 | if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) { |
| 358 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
| 359 | if (Op1 == 0) return false; |
| 360 | |
| 361 | bool Op1IsKill = hasTrivialKill(I->getOperand(1)); |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 363 | unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, |
| 364 | Op1IsKill, CI->getZExtValue(), |
| 365 | VT.getSimpleVT()); |
| 366 | if (ResultReg == 0) return false; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 368 | // We successfully emitted code for the given LLVM Instruction. |
| 369 | UpdateValueMap(I, ResultReg); |
| 370 | return true; |
Chris Lattner | fff65b3 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 371 | } |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 372 | |
| 373 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 374 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 375 | if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail. |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 376 | return false; |
| 377 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 378 | bool Op0IsKill = hasTrivialKill(I->getOperand(0)); |
| 379 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 380 | // Check if the second operand is a constant and handle it appropriately. |
| 381 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 382 | uint64_t Imm = CI->getZExtValue(); |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 383 | |
Chris Lattner | f051c1a | 2011-04-18 07:00:40 +0000 | [diff] [blame] | 384 | // Transform "sdiv exact X, 8" -> "sra X, 3". |
| 385 | if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) && |
| 386 | cast<BinaryOperator>(I)->isExact() && |
| 387 | isPowerOf2_64(Imm)) { |
| 388 | Imm = Log2_64(Imm); |
| 389 | ISDOpcode = ISD::SRA; |
| 390 | } |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 392 | unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, |
| 393 | Op0IsKill, Imm, VT.getSimpleVT()); |
| 394 | if (ResultReg == 0) return false; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 396 | // We successfully emitted code for the given LLVM Instruction. |
| 397 | UpdateValueMap(I, ResultReg); |
| 398 | return true; |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 401 | // Check if the second operand is a constant float. |
| 402 | if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 403 | unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(), |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 404 | ISDOpcode, Op0, Op0IsKill, CF); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 405 | if (ResultReg != 0) { |
| 406 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 407 | UpdateValueMap(I, ResultReg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 408 | return true; |
| 409 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 412 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 413 | if (Op1 == 0) |
| 414 | // Unhandled operand. Halt "fast" selection and bail. |
| 415 | return false; |
| 416 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 417 | bool Op1IsKill = hasTrivialKill(I->getOperand(1)); |
| 418 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 419 | // Now we have both operands in registers. Emit the instruction. |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 420 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 421 | ISDOpcode, |
| 422 | Op0, Op0IsKill, |
| 423 | Op1, Op1IsKill); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 424 | if (ResultReg == 0) |
| 425 | // Target-specific code wasn't able to find a machine opcode for |
| 426 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 427 | return false; |
| 428 | |
Dan Gohman | 8014e86 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 429 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 430 | UpdateValueMap(I, ResultReg); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
| 433 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 434 | bool FastISel::SelectGetElementPtr(const User *I) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 435 | unsigned N = getRegForValue(I->getOperand(0)); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 436 | if (N == 0) |
| 437 | // Unhandled operand. Halt "fast" selection and bail. |
| 438 | return false; |
| 439 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 440 | bool NIsKill = hasTrivialKill(I->getOperand(0)); |
| 441 | |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 442 | // Keep a running tab of the total offset to coalesce multiple N = N + Offset |
| 443 | // into a single N = N + TotalOffset. |
| 444 | uint64_t TotalOffs = 0; |
| 445 | // FIXME: What's a good SWAG number for MaxOffs? |
| 446 | uint64_t MaxOffs = 2048; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 447 | Type *Ty = I->getOperand(0)->getType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 448 | MVT VT = TLI.getPointerTy(); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 449 | for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1, |
| 450 | E = I->op_end(); OI != E; ++OI) { |
| 451 | const Value *Idx = *OI; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 452 | if (StructType *StTy = dyn_cast<StructType>(Ty)) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 453 | unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| 454 | if (Field) { |
| 455 | // N = N + Offset |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 456 | TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field); |
| 457 | if (TotalOffs >= MaxOffs) { |
| 458 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| 459 | if (N == 0) |
| 460 | // Unhandled operand. Halt "fast" selection and bail. |
| 461 | return false; |
| 462 | NIsKill = true; |
| 463 | TotalOffs = 0; |
| 464 | } |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 465 | } |
| 466 | Ty = StTy->getElementType(Field); |
| 467 | } else { |
| 468 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 469 | |
| 470 | // If this is a constant subscript, handle it quickly. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 471 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 472 | if (CI->isZero()) continue; |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 473 | // N = N + Offset |
| 474 | TotalOffs += |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 475 | TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 476 | if (TotalOffs >= MaxOffs) { |
| 477 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| 478 | if (N == 0) |
| 479 | // Unhandled operand. Halt "fast" selection and bail. |
| 480 | return false; |
| 481 | NIsKill = true; |
| 482 | TotalOffs = 0; |
| 483 | } |
| 484 | continue; |
| 485 | } |
| 486 | if (TotalOffs) { |
| 487 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 488 | if (N == 0) |
| 489 | // Unhandled operand. Halt "fast" selection and bail. |
| 490 | return false; |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 491 | NIsKill = true; |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 492 | TotalOffs = 0; |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 493 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 494 | |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 495 | // N = N + Idx * ElementSize; |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 496 | uint64_t ElementSize = TD.getTypeAllocSize(Ty); |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 497 | std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx); |
| 498 | unsigned IdxN = Pair.first; |
| 499 | bool IdxNIsKill = Pair.second; |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 500 | if (IdxN == 0) |
| 501 | // Unhandled operand. Halt "fast" selection and bail. |
| 502 | return false; |
| 503 | |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 504 | if (ElementSize != 1) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 505 | IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT); |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 506 | if (IdxN == 0) |
| 507 | // Unhandled operand. Halt "fast" selection and bail. |
| 508 | return false; |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 509 | IdxNIsKill = true; |
Dan Gohman | 80bc6e2 | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 510 | } |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 511 | N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 512 | if (N == 0) |
| 513 | // Unhandled operand. Halt "fast" selection and bail. |
| 514 | return false; |
| 515 | } |
| 516 | } |
Chad Rosier | 478b06c | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 517 | if (TotalOffs) { |
| 518 | N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| 519 | if (N == 0) |
| 520 | // Unhandled operand. Halt "fast" selection and bail. |
| 521 | return false; |
| 522 | } |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 523 | |
| 524 | // We successfully emitted code for the given LLVM Instruction. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 525 | UpdateValueMap(I, N); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 526 | return true; |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 529 | bool FastISel::SelectCall(const User *I) { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 530 | const CallInst *Call = cast<CallInst>(I); |
| 531 | |
| 532 | // Handle simple inline asms. |
Dan Gohman | 9e15d65 | 2011-10-12 15:56:56 +0000 | [diff] [blame] | 533 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 534 | // Don't attempt to handle constraints. |
| 535 | if (!IA->getConstraintString().empty()) |
| 536 | return false; |
| 537 | |
| 538 | unsigned ExtraInfo = 0; |
| 539 | if (IA->hasSideEffects()) |
| 540 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 541 | if (IA->isAlignStack()) |
| 542 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
| 543 | |
| 544 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
| 545 | TII.get(TargetOpcode::INLINEASM)) |
| 546 | .addExternalSymbol(IA->getAsmString().c_str()) |
| 547 | .addImm(ExtraInfo); |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | const Function *F = Call->getCalledFunction(); |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 552 | if (!F) return false; |
| 553 | |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 554 | // Handle selected intrinsic function calls. |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 555 | switch (F->getIntrinsicID()) { |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 556 | default: break; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 557 | case Intrinsic::dbg_declare: { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 558 | const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call); |
Devang Patel | 02f0dbd | 2010-05-07 22:04:20 +0000 | [diff] [blame] | 559 | if (!DIVariable(DI->getVariable()).Verify() || |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 560 | !FuncInfo.MF->getMMI().hasDebugInfo()) |
Devang Patel | 7e1e31f | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 561 | return true; |
| 562 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 563 | const Value *Address = DI->getAddress(); |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 564 | if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address)) |
Dale Johannesen | dc91856 | 2010-02-06 02:26:02 +0000 | [diff] [blame] | 565 | return true; |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 566 | |
| 567 | unsigned Reg = 0; |
| 568 | unsigned Offset = 0; |
| 569 | if (const Argument *Arg = dyn_cast<Argument>(Address)) { |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 570 | // Some arguments' frame index is recorded during argument lowering. |
| 571 | Offset = FuncInfo.getArgumentFrameIndex(Arg); |
| 572 | if (Offset) |
| 573 | Reg = TRI.getFrameRegister(*FuncInfo.MF); |
Devang Patel | 4bafda9 | 2010-09-10 20:32:09 +0000 | [diff] [blame] | 574 | } |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 575 | if (!Reg) |
| 576 | Reg = getRegForValue(Address); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 577 | |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 578 | if (Reg) |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 579 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, |
Devang Patel | 6fe75aa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 580 | TII.get(TargetOpcode::DBG_VALUE)) |
| 581 | .addReg(Reg, RegState::Debug).addImm(Offset) |
| 582 | .addMetadata(DI->getVariable()); |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 583 | return true; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 584 | } |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 585 | case Intrinsic::dbg_value: { |
Dale Johannesen | 343b42e | 2010-04-07 01:15:14 +0000 | [diff] [blame] | 586 | // This form of DBG_VALUE is target-independent. |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 587 | const DbgValueInst *DI = cast<DbgValueInst>(Call); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 588 | const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 589 | const Value *V = DI->getValue(); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 590 | if (!V) { |
| 591 | // Currently the optimizer can produce this; insert an undef to |
| 592 | // help debugging. Probably the optimizer should not do this. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 593 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 594 | .addReg(0U).addImm(DI->getOffset()) |
| 595 | .addMetadata(DI->getVariable()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 596 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Devang Patel | 8594d42 | 2011-06-24 20:46:11 +0000 | [diff] [blame] | 597 | if (CI->getBitWidth() > 64) |
| 598 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 599 | .addCImm(CI).addImm(DI->getOffset()) |
| 600 | .addMetadata(DI->getVariable()); |
| 601 | else |
| 602 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 603 | .addImm(CI->getZExtValue()).addImm(DI->getOffset()) |
| 604 | .addMetadata(DI->getVariable()); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 605 | } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 606 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 607 | .addFPImm(CF).addImm(DI->getOffset()) |
| 608 | .addMetadata(DI->getVariable()); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 609 | } else if (unsigned Reg = lookUpRegForValue(V)) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 610 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 611 | .addReg(Reg, RegState::Debug).addImm(DI->getOffset()) |
| 612 | .addMetadata(DI->getVariable()); |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 613 | } else { |
| 614 | // We can't yet handle anything else here because it would require |
| 615 | // generating code, thus altering codegen because of debug info. |
Devang Patel | afeaae7 | 2010-12-06 22:39:26 +0000 | [diff] [blame] | 616 | DEBUG(dbgs() << "Dropping debug info for " << DI); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 617 | } |
Dale Johannesen | 45df761 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 618 | return true; |
| 619 | } |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 620 | case Intrinsic::eh_exception: { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 621 | EVT VT = TLI.getValueType(Call->getType()); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 622 | if (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)!=TargetLowering::Expand) |
| 623 | break; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 625 | assert(FuncInfo.MBB->isLandingPad() && |
| 626 | "Call to eh.exception not in landing pad!"); |
| 627 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 628 | const TargetRegisterClass *RC = TLI.getRegClassFor(VT); |
| 629 | unsigned ResultReg = createResultReg(RC); |
| 630 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 631 | ResultReg).addReg(Reg); |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 632 | UpdateValueMap(Call, ResultReg); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 633 | return true; |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 634 | } |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 635 | case Intrinsic::eh_selector: { |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 636 | EVT VT = TLI.getValueType(Call->getType()); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 637 | if (TLI.getOperationAction(ISD::EHSELECTION, VT) != TargetLowering::Expand) |
| 638 | break; |
| 639 | if (FuncInfo.MBB->isLandingPad()) |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 640 | AddCatchInfo(*Call, &FuncInfo.MF->getMMI(), FuncInfo.MBB); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 641 | else { |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 642 | #ifndef NDEBUG |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 643 | FuncInfo.CatchInfoLost.insert(Call); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 644 | #endif |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 645 | // FIXME: Mark exception selector register as live in. Hack for PR1508. |
Chris Lattner | ed3a806 | 2010-04-05 06:05:26 +0000 | [diff] [blame] | 646 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 647 | if (Reg) FuncInfo.MBB->addLiveIn(Reg); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 649 | |
| 650 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 651 | EVT SrcVT = TLI.getPointerTy(); |
| 652 | const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT); |
| 653 | unsigned ResultReg = createResultReg(RC); |
| 654 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 655 | ResultReg).addReg(Reg); |
| 656 | |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 657 | bool ResultRegIsKill = hasTrivialKill(Call); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 658 | |
| 659 | // Cast the register to the type of the selector. |
| 660 | if (SrcVT.bitsGT(MVT::i32)) |
| 661 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE, |
| 662 | ResultReg, ResultRegIsKill); |
| 663 | else if (SrcVT.bitsLT(MVT::i32)) |
| 664 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, |
| 665 | ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill); |
| 666 | if (ResultReg == 0) |
| 667 | // Unhandled operand. Halt "fast" selection and bail. |
| 668 | return false; |
| 669 | |
Dan Gohman | a61e73b | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 670 | UpdateValueMap(Call, ResultReg); |
Chris Lattner | 832e494 | 2011-04-19 05:52:03 +0000 | [diff] [blame] | 671 | |
| 672 | return true; |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 673 | } |
Eli Friedman | d0118a2 | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 674 | case Intrinsic::objectsize: { |
| 675 | ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1)); |
| 676 | unsigned long long Res = CI->isZero() ? -1ULL : 0; |
| 677 | Constant *ResCI = ConstantInt::get(Call->getType(), Res); |
| 678 | unsigned ResultReg = getRegForValue(ResCI); |
| 679 | if (ResultReg == 0) |
| 680 | return false; |
| 681 | UpdateValueMap(Call, ResultReg); |
| 682 | return true; |
| 683 | } |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 684 | } |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 685 | |
Ivan Krasin | 74af88a | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 686 | // Usually, it does not make sense to initialize a value, |
| 687 | // make an unrelated function call and use the value, because |
| 688 | // it tends to be spilled on the stack. So, we move the pointer |
| 689 | // to the last local value to the beginning of the block, so that |
| 690 | // all the values which have already been materialized, |
| 691 | // appear after the call. It also makes sense to skip intrinsics |
| 692 | // since they tend to be inlined. |
| 693 | if (!isa<IntrinsicInst>(F)) |
| 694 | flushLocalValueMap(); |
| 695 | |
Dan Gohman | 4183e31 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 696 | // An arbitrary call. Bail. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 697 | return false; |
| 698 | } |
| 699 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 700 | bool FastISel::SelectCast(const User *I, unsigned Opcode) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 701 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 702 | EVT DstVT = TLI.getValueType(I->getType()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 703 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 704 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 705 | DstVT == MVT::Other || !DstVT.isSimple()) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 706 | // Unhandled type. Halt "fast" selection and bail. |
| 707 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 708 | |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 709 | // Check if the destination type is legal. |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 710 | if (!TLI.isTypeLegal(DstVT)) |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 711 | return false; |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 712 | |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 713 | // Check if the source operand is legal. |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 714 | if (!TLI.isTypeLegal(SrcVT)) |
Eli Friedman | 76927d73 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 715 | return false; |
Dan Gohman | 474d3b3 | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 716 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 717 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 718 | if (!InputReg) |
| 719 | // Unhandled operand. Halt "fast" selection and bail. |
| 720 | return false; |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 721 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 722 | bool InputRegIsKill = hasTrivialKill(I->getOperand(0)); |
| 723 | |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 724 | unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), |
| 725 | DstVT.getSimpleVT(), |
| 726 | Opcode, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 727 | InputReg, InputRegIsKill); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 728 | if (!ResultReg) |
| 729 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 730 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 731 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 735 | bool FastISel::SelectBitCast(const User *I) { |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 736 | // If the bitcast doesn't change the type, just use the operand value. |
| 737 | if (I->getType() == I->getOperand(0)->getType()) { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 738 | unsigned Reg = getRegForValue(I->getOperand(0)); |
Dan Gohman | a318dab | 2008-08-27 20:41:38 +0000 | [diff] [blame] | 739 | if (Reg == 0) |
| 740 | return false; |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 741 | UpdateValueMap(I, Reg); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 742 | return true; |
| 743 | } |
| 744 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 745 | // Bitcasts of other values become reg-reg copies or BITCAST operators. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 746 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 747 | EVT DstVT = TLI.getValueType(I->getType()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 748 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 749 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || |
| 750 | DstVT == MVT::Other || !DstVT.isSimple() || |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 751 | !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) |
| 752 | // Unhandled type. Halt "fast" selection and bail. |
| 753 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 754 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 755 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 756 | if (Op0 == 0) |
| 757 | // Unhandled operand. Halt "fast" selection and bail. |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 758 | return false; |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 759 | |
| 760 | bool Op0IsKill = hasTrivialKill(I->getOperand(0)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 761 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 762 | // First, try to perform the bitcast by inserting a reg-reg copy. |
| 763 | unsigned ResultReg = 0; |
| 764 | if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) { |
| 765 | TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); |
| 766 | TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); |
Jakob Stoklund Olesen | e7917bb | 2010-07-11 05:16:54 +0000 | [diff] [blame] | 767 | // Don't attempt a cross-class copy. It will likely fail. |
| 768 | if (SrcClass == DstClass) { |
| 769 | ResultReg = createResultReg(DstClass); |
| 770 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 771 | ResultReg).addReg(Op0); |
| 772 | } |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 773 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 774 | |
| 775 | // If the reg-reg copy failed, select a BITCAST opcode. |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 776 | if (!ResultReg) |
| 777 | ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 778 | ISD::BITCAST, Op0, Op0IsKill); |
| 779 | |
Dan Gohman | ad368ac | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 780 | if (!ResultReg) |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 781 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 782 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 783 | UpdateValueMap(I, ResultReg); |
Owen Anderson | d0533c9 | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 784 | return true; |
| 785 | } |
| 786 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 787 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 788 | FastISel::SelectInstruction(const Instruction *I) { |
Dan Gohman | e8c92dd | 2010-04-23 15:29:50 +0000 | [diff] [blame] | 789 | // Just before the terminator instruction, insert instructions to |
| 790 | // feed PHI nodes in successor blocks. |
| 791 | if (isa<TerminatorInst>(I)) |
| 792 | if (!HandlePHINodesInSuccessorBlocks(I->getParent())) |
| 793 | return false; |
| 794 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 795 | DL = I->getDebugLoc(); |
| 796 | |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 797 | // First, try doing target-independent selection. |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 798 | if (SelectOperator(I, I->getOpcode())) { |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 799 | ++NumFastIselSuccessIndependent; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 800 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 801 | return true; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 802 | } |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 803 | |
| 804 | // Next, try calling the target to attempt to handle the instruction. |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 805 | if (TargetSelectInstruction(I)) { |
Chad Rosier | 053e69a | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 806 | ++NumFastIselSuccessTarget; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 807 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 808 | return true; |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 809 | } |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 810 | |
Dan Gohman | 8ba3aa7 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 811 | DL = DebugLoc(); |
Dan Gohman | 6e3ff37 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 812 | return false; |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 815 | /// FastEmitBranch - Emit an unconditional branch to the given block, |
| 816 | /// unless it is the immediate (fall-through) successor, and update |
| 817 | /// the CFG. |
| 818 | void |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 819 | FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 820 | if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) { |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 821 | // The unconditional fall-through case, which needs no instructions. |
| 822 | } else { |
| 823 | // The unconditional branch case. |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 824 | TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL, |
| 825 | SmallVector<MachineOperand, 0>(), DL); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 826 | } |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 827 | FuncInfo.MBB->addSuccessor(MSucc); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 830 | /// SelectFNeg - Emit an FNeg operation. |
| 831 | /// |
| 832 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 833 | FastISel::SelectFNeg(const User *I) { |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 834 | unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I)); |
| 835 | if (OpReg == 0) return false; |
| 836 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 837 | bool OpRegIsKill = hasTrivialKill(I); |
| 838 | |
Dan Gohman | 4a215a1 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 839 | // If the target has ISD::FNEG, use it. |
| 840 | EVT VT = TLI.getValueType(I->getType()); |
| 841 | unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 842 | ISD::FNEG, OpReg, OpRegIsKill); |
Dan Gohman | 4a215a1 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 843 | if (ResultReg != 0) { |
| 844 | UpdateValueMap(I, ResultReg); |
| 845 | return true; |
| 846 | } |
| 847 | |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 848 | // Bitcast the value to integer, twiddle the sign bit with xor, |
| 849 | // and then bitcast it back to floating-point. |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 850 | if (VT.getSizeInBits() > 64) return false; |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 851 | EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); |
| 852 | if (!TLI.isTypeLegal(IntVT)) |
| 853 | return false; |
| 854 | |
| 855 | unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 856 | ISD::BITCAST, OpReg, OpRegIsKill); |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 857 | if (IntReg == 0) |
| 858 | return false; |
| 859 | |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 860 | unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, |
| 861 | IntReg, /*Kill=*/true, |
Dan Gohman | 5e5abb7 | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 862 | UINT64_C(1) << (VT.getSizeInBits()-1), |
| 863 | IntVT.getSimpleVT()); |
| 864 | if (IntResultReg == 0) |
| 865 | return false; |
| 866 | |
| 867 | ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 868 | ISD::BITCAST, IntResultReg, /*Kill=*/true); |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 869 | if (ResultReg == 0) |
| 870 | return false; |
| 871 | |
| 872 | UpdateValueMap(I, ResultReg); |
| 873 | return true; |
| 874 | } |
| 875 | |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 876 | bool |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 877 | FastISel::SelectExtractValue(const User *U) { |
| 878 | const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U); |
Eli Friedman | a4c920d | 2011-05-16 20:34:53 +0000 | [diff] [blame] | 879 | if (!EVI) |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 880 | return false; |
| 881 | |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 882 | // Make sure we only try to handle extracts with a legal result. But also |
| 883 | // allow i1 because it's easy. |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 884 | EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true); |
| 885 | if (!RealVT.isSimple()) |
| 886 | return false; |
| 887 | MVT VT = RealVT.getSimpleVT(); |
Eli Friedman | 482feb3 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 888 | if (!TLI.isTypeLegal(VT) && VT != MVT::i1) |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 889 | return false; |
| 890 | |
| 891 | const Value *Op0 = EVI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 892 | Type *AggTy = Op0->getType(); |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 893 | |
| 894 | // Get the base result register. |
| 895 | unsigned ResultReg; |
| 896 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0); |
| 897 | if (I != FuncInfo.ValueMap.end()) |
| 898 | ResultReg = I->second; |
Eli Friedman | 0b4d96b | 2011-06-06 05:46:34 +0000 | [diff] [blame] | 899 | else if (isa<Instruction>(Op0)) |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 900 | ResultReg = FuncInfo.InitializeRegForValue(Op0); |
Eli Friedman | 0b4d96b | 2011-06-06 05:46:34 +0000 | [diff] [blame] | 901 | else |
| 902 | return false; // fast-isel can't handle aggregate constants at the moment |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 903 | |
| 904 | // Get the actual result register, which is an offset from the base register. |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 905 | unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices()); |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 906 | |
| 907 | SmallVector<EVT, 4> AggValueVTs; |
| 908 | ComputeValueVTs(TLI, AggTy, AggValueVTs); |
| 909 | |
| 910 | for (unsigned i = 0; i < VTIndex; i++) |
| 911 | ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]); |
| 912 | |
| 913 | UpdateValueMap(EVI, ResultReg); |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | bool |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 918 | FastISel::SelectOperator(const User *I, unsigned Opcode) { |
Dan Gohman | 40b189e | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 919 | switch (Opcode) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 920 | case Instruction::Add: |
| 921 | return SelectBinaryOp(I, ISD::ADD); |
| 922 | case Instruction::FAdd: |
| 923 | return SelectBinaryOp(I, ISD::FADD); |
| 924 | case Instruction::Sub: |
| 925 | return SelectBinaryOp(I, ISD::SUB); |
| 926 | case Instruction::FSub: |
Dan Gohman | 3d45a85 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 927 | // FNeg is currently represented in LLVM IR as a special case of FSub. |
| 928 | if (BinaryOperator::isFNeg(I)) |
| 929 | return SelectFNeg(I); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 930 | return SelectBinaryOp(I, ISD::FSUB); |
| 931 | case Instruction::Mul: |
| 932 | return SelectBinaryOp(I, ISD::MUL); |
| 933 | case Instruction::FMul: |
| 934 | return SelectBinaryOp(I, ISD::FMUL); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 935 | case Instruction::SDiv: |
| 936 | return SelectBinaryOp(I, ISD::SDIV); |
| 937 | case Instruction::UDiv: |
| 938 | return SelectBinaryOp(I, ISD::UDIV); |
| 939 | case Instruction::FDiv: |
| 940 | return SelectBinaryOp(I, ISD::FDIV); |
| 941 | case Instruction::SRem: |
| 942 | return SelectBinaryOp(I, ISD::SREM); |
| 943 | case Instruction::URem: |
| 944 | return SelectBinaryOp(I, ISD::UREM); |
| 945 | case Instruction::FRem: |
| 946 | return SelectBinaryOp(I, ISD::FREM); |
| 947 | case Instruction::Shl: |
| 948 | return SelectBinaryOp(I, ISD::SHL); |
| 949 | case Instruction::LShr: |
| 950 | return SelectBinaryOp(I, ISD::SRL); |
| 951 | case Instruction::AShr: |
| 952 | return SelectBinaryOp(I, ISD::SRA); |
| 953 | case Instruction::And: |
| 954 | return SelectBinaryOp(I, ISD::AND); |
| 955 | case Instruction::Or: |
| 956 | return SelectBinaryOp(I, ISD::OR); |
| 957 | case Instruction::Xor: |
| 958 | return SelectBinaryOp(I, ISD::XOR); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 959 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 960 | case Instruction::GetElementPtr: |
| 961 | return SelectGetElementPtr(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 962 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 963 | case Instruction::Br: { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 964 | const BranchInst *BI = cast<BranchInst>(I); |
Dan Gohman | bdedd44 | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 965 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 966 | if (BI->isUnconditional()) { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 967 | const BasicBlock *LLVMSucc = BI->getSuccessor(0); |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 968 | MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc]; |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 969 | FastEmitBranch(MSucc, BI->getDebugLoc()); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 970 | return true; |
Owen Anderson | 9d5b416 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 971 | } |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 972 | |
| 973 | // Conditional branches are not handed yet. |
| 974 | // Halt "fast" selection and bail. |
| 975 | return false; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Dan Gohman | 087c850 | 2008-09-05 01:08:41 +0000 | [diff] [blame] | 978 | case Instruction::Unreachable: |
| 979 | // Nothing to emit. |
| 980 | return true; |
| 981 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 982 | case Instruction::Alloca: |
| 983 | // FunctionLowering has the static-sized case covered. |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 984 | if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I))) |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 985 | return true; |
| 986 | |
| 987 | // Dynamic-sized alloca is not handled yet. |
| 988 | return false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 989 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 990 | case Instruction::Call: |
| 991 | return SelectCall(I); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 992 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 993 | case Instruction::BitCast: |
| 994 | return SelectBitCast(I); |
| 995 | |
| 996 | case Instruction::FPToSI: |
| 997 | return SelectCast(I, ISD::FP_TO_SINT); |
| 998 | case Instruction::ZExt: |
| 999 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 1000 | case Instruction::SExt: |
| 1001 | return SelectCast(I, ISD::SIGN_EXTEND); |
| 1002 | case Instruction::Trunc: |
| 1003 | return SelectCast(I, ISD::TRUNCATE); |
| 1004 | case Instruction::SIToFP: |
| 1005 | return SelectCast(I, ISD::SINT_TO_FP); |
| 1006 | |
| 1007 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 1008 | case Instruction::PtrToInt: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1009 | EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 1010 | EVT DstVT = TLI.getValueType(I->getType()); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1011 | if (DstVT.bitsGT(SrcVT)) |
| 1012 | return SelectCast(I, ISD::ZERO_EXTEND); |
| 1013 | if (DstVT.bitsLT(SrcVT)) |
| 1014 | return SelectCast(I, ISD::TRUNCATE); |
| 1015 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| 1016 | if (Reg == 0) return false; |
| 1017 | UpdateValueMap(I, Reg); |
| 1018 | return true; |
| 1019 | } |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 1020 | |
Eli Friedman | 2586b8f | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1021 | case Instruction::ExtractValue: |
| 1022 | return SelectExtractValue(I); |
| 1023 | |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 1024 | case Instruction::PHI: |
| 1025 | llvm_unreachable("FastISel shouldn't visit PHI nodes!"); |
| 1026 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1027 | default: |
| 1028 | // Unhandled instruction. Halt "fast" selection and bail. |
| 1029 | return false; |
| 1030 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1033 | FastISel::FastISel(FunctionLoweringInfo &funcInfo) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1034 | : FuncInfo(funcInfo), |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1035 | MRI(FuncInfo.MF->getRegInfo()), |
| 1036 | MFI(*FuncInfo.MF->getFrameInfo()), |
| 1037 | MCP(*FuncInfo.MF->getConstantPool()), |
| 1038 | TM(FuncInfo.MF->getTarget()), |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 1039 | TD(*TM.getTargetData()), |
| 1040 | TII(*TM.getInstrInfo()), |
Dan Gohman | a7a0ed7 | 2010-05-05 23:58:35 +0000 | [diff] [blame] | 1041 | TLI(*TM.getTargetLowering()), |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1042 | TRI(*TM.getRegisterInfo()) { |
Dan Gohman | bb46633 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 1045 | FastISel::~FastISel() {} |
| 1046 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1047 | unsigned FastISel::FastEmit_(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1048 | unsigned) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1052 | unsigned FastISel::FastEmit_r(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1053 | unsigned, |
| 1054 | unsigned /*Op0*/, bool /*Op0IsKill*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1055 | return 0; |
| 1056 | } |
| 1057 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1058 | unsigned FastISel::FastEmit_rr(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1059 | unsigned, |
| 1060 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
| 1061 | unsigned /*Op1*/, bool /*Op1IsKill*/) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
| 1064 | |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1065 | unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1069 | unsigned FastISel::FastEmit_f(MVT, MVT, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1070 | unsigned, const ConstantFP * /*FPImm*/) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
| 1073 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1074 | unsigned FastISel::FastEmit_ri(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1075 | unsigned, |
| 1076 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 1077 | uint64_t /*Imm*/) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1078 | return 0; |
| 1079 | } |
| 1080 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1081 | unsigned FastISel::FastEmit_rf(MVT, MVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1082 | unsigned, |
| 1083 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1084 | const ConstantFP * /*FPImm*/) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1088 | unsigned FastISel::FastEmit_rri(MVT, MVT, |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1089 | unsigned, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1090 | unsigned /*Op0*/, bool /*Op0IsKill*/, |
| 1091 | unsigned /*Op1*/, bool /*Op1IsKill*/, |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1092 | uint64_t /*Imm*/) { |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries |
| 1097 | /// to emit an instruction with an immediate operand using FastEmit_ri. |
| 1098 | /// If that fails, it materializes the immediate into a register and try |
| 1099 | /// FastEmit_rr instead. |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 1100 | unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1101 | unsigned Op0, bool Op0IsKill, |
| 1102 | uint64_t Imm, MVT ImmType) { |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1103 | // If this is a multiply by a power of two, emit this as a shift left. |
| 1104 | if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) { |
| 1105 | Opcode = ISD::SHL; |
| 1106 | Imm = Log2_64(Imm); |
Chris Lattner | 090ca91 | 2011-04-18 06:55:51 +0000 | [diff] [blame] | 1107 | } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) { |
| 1108 | // div x, 8 -> srl x, 3 |
| 1109 | Opcode = ISD::SRL; |
| 1110 | Imm = Log2_64(Imm); |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1111 | } |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1113 | // Horrible hack (to be removed), check to make sure shift amounts are |
| 1114 | // in-range. |
| 1115 | if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) && |
| 1116 | Imm >= VT.getSizeInBits()) |
| 1117 | return 0; |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1118 | |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1119 | // First check if immediate type is legal. If not, we can't use the ri form. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1120 | unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1121 | if (ResultReg != 0) |
| 1122 | return ResultReg; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 1123 | unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
Eli Friedman | b2b03fc | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1124 | if (MaterialReg == 0) { |
| 1125 | // This is a bit ugly/slow, but failing here means falling out of |
| 1126 | // fast-isel, which would be very slow. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1127 | IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(), |
Eli Friedman | b2b03fc | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1128 | VT.getSizeInBits()); |
| 1129 | MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm)); |
| 1130 | } |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1131 | return FastEmit_rr(VT, VT, Opcode, |
| 1132 | Op0, Op0IsKill, |
| 1133 | MaterialReg, /*Kill=*/true); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | unsigned FastISel::createResultReg(const TargetRegisterClass* RC) { |
| 1137 | return MRI.createVirtualRegister(RC); |
Evan Cheng | 83785c8 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1140 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
Dan Gohman | 77ad796 | 2008-08-20 18:09:38 +0000 | [diff] [blame] | 1141 | const TargetRegisterClass* RC) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1142 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1143 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1144 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1145 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1146 | return ResultReg; |
| 1147 | } |
| 1148 | |
| 1149 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 1150 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1151 | unsigned Op0, bool Op0IsKill) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1152 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1153 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1154 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1155 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1156 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1157 | .addReg(Op0, Op0IsKill * RegState::Kill); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1158 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1159 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1160 | .addReg(Op0, Op0IsKill * RegState::Kill); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1161 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1162 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1165 | return ResultReg; |
| 1166 | } |
| 1167 | |
| 1168 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 1169 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1170 | unsigned Op0, bool Op0IsKill, |
| 1171 | unsigned Op1, bool Op1IsKill) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1172 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1173 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1174 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1175 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1176 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1177 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1178 | .addReg(Op1, Op1IsKill * RegState::Kill); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1179 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1180 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1181 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1182 | .addReg(Op1, Op1IsKill * RegState::Kill); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1183 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1184 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1185 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1186 | return ResultReg; |
| 1187 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1188 | |
Owen Anderson | d71867a | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1189 | unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode, |
| 1190 | const TargetRegisterClass *RC, |
| 1191 | unsigned Op0, bool Op0IsKill, |
| 1192 | unsigned Op1, bool Op1IsKill, |
| 1193 | unsigned Op2, bool Op2IsKill) { |
| 1194 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1195 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Owen Anderson | d71867a | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (II.getNumDefs() >= 1) |
| 1198 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1199 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1200 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1201 | .addReg(Op2, Op2IsKill * RegState::Kill); |
| 1202 | else { |
| 1203 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1204 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1205 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1206 | .addReg(Op2, Op2IsKill * RegState::Kill); |
| 1207 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1208 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1209 | } |
| 1210 | return ResultReg; |
| 1211 | } |
| 1212 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1213 | unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, |
| 1214 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1215 | unsigned Op0, bool Op0IsKill, |
| 1216 | uint64_t Imm) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1217 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1218 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1219 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1220 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1221 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1222 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1223 | .addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1224 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1225 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1226 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1227 | .addImm(Imm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1228 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1229 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1230 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1231 | return ResultReg; |
| 1232 | } |
| 1233 | |
Owen Anderson | 2ce5bf1 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1234 | unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode, |
| 1235 | const TargetRegisterClass *RC, |
| 1236 | unsigned Op0, bool Op0IsKill, |
| 1237 | uint64_t Imm1, uint64_t Imm2) { |
| 1238 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1239 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Owen Anderson | 2ce5bf1 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (II.getNumDefs() >= 1) |
| 1242 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1243 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1244 | .addImm(Imm1) |
| 1245 | .addImm(Imm2); |
| 1246 | else { |
| 1247 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
| 1248 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1249 | .addImm(Imm1) |
| 1250 | .addImm(Imm2); |
| 1251 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1252 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1253 | } |
| 1254 | return ResultReg; |
| 1255 | } |
| 1256 | |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1257 | unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode, |
| 1258 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1259 | unsigned Op0, bool Op0IsKill, |
| 1260 | const ConstantFP *FPImm) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1261 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1262 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1263 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1264 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1265 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1266 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1267 | .addFPImm(FPImm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1268 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1269 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1270 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1271 | .addFPImm(FPImm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1272 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1273 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1274 | } |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1275 | return ResultReg; |
| 1276 | } |
| 1277 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1278 | unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, |
| 1279 | const TargetRegisterClass *RC, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1280 | unsigned Op0, bool Op0IsKill, |
| 1281 | unsigned Op1, bool Op1IsKill, |
| 1282 | uint64_t Imm) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1283 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1284 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1285 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1286 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1287 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1288 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1289 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1290 | .addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1291 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1292 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II) |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1293 | .addReg(Op0, Op0IsKill * RegState::Kill) |
| 1294 | .addReg(Op1, Op1IsKill * RegState::Kill) |
| 1295 | .addImm(Imm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1296 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1297 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1298 | } |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1299 | return ResultReg; |
| 1300 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1301 | |
| 1302 | unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, |
| 1303 | const TargetRegisterClass *RC, |
| 1304 | uint64_t Imm) { |
| 1305 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1306 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1307 | |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1308 | if (II.getNumDefs() >= 1) |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1309 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1310 | else { |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1311 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm); |
Jakob Stoklund Olesen | e797e0c | 2010-07-11 03:31:05 +0000 | [diff] [blame] | 1312 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1313 | ResultReg).addReg(II.ImplicitDefs[0]); |
Evan Cheng | 5960e4e | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1314 | } |
Owen Anderson | 6d0c25e | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1315 | return ResultReg; |
Evan Cheng | b41aec5 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 1316 | } |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1317 | |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1318 | unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode, |
| 1319 | const TargetRegisterClass *RC, |
| 1320 | uint64_t Imm1, uint64_t Imm2) { |
| 1321 | unsigned ResultReg = createResultReg(RC); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1322 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
Owen Anderson | d74ea77 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (II.getNumDefs() >= 1) |
| 1325 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg) |
| 1326 | .addImm(Imm1).addImm(Imm2); |
| 1327 | else { |
| 1328 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2); |
| 1329 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY), |
| 1330 | ResultReg).addReg(II.ImplicitDefs[0]); |
| 1331 | } |
| 1332 | return ResultReg; |
| 1333 | } |
| 1334 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1335 | unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT, |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1336 | unsigned Op0, bool Op0IsKill, |
| 1337 | uint32_t Idx) { |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 1338 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1339 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 1340 | "Cannot yet extract from physregs"); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1341 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, |
| 1342 | DL, TII.get(TargetOpcode::COPY), ResultReg) |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1343 | .addReg(Op0, getKillRegState(Op0IsKill), Idx); |
Owen Anderson | 8970f00 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 1344 | return ResultReg; |
| 1345 | } |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1346 | |
| 1347 | /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op |
| 1348 | /// with all but the least significant bit set to zero. |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1349 | unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) { |
| 1350 | return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1); |
Dan Gohman | 14ea1ec | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1351 | } |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1352 | |
| 1353 | /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks. |
| 1354 | /// Emit code to ensure constants are copied into registers when needed. |
| 1355 | /// Remember the virtual registers that need to be added to the Machine PHI |
| 1356 | /// nodes as input. We cannot just directly add them, because expansion |
| 1357 | /// might result in multiple MBB's for one BB. As such, the start of the |
| 1358 | /// BB might correspond to a different MBB than the end. |
| 1359 | bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
| 1360 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1361 | |
| 1362 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1363 | unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size(); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 1366 | // from this block. |
| 1367 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1368 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1369 | if (!isa<PHINode>(SuccBB->begin())) continue; |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1370 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1371 | |
| 1372 | // If this terminator has multiple identical successors (common for |
| 1373 | // switches), only handle each succ once. |
| 1374 | if (!SuccsHandled.insert(SuccMBB)) continue; |
| 1375 | |
| 1376 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 1377 | |
| 1378 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1379 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1380 | // emitted yet. |
| 1381 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| 1382 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | fb95f89 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 1383 | |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1384 | // Ignore dead phi's. |
| 1385 | if (PN->use_empty()) continue; |
| 1386 | |
| 1387 | // Only handle legal types. Two interesting things to note here. First, |
| 1388 | // by bailing out early, we may leave behind some dead instructions, |
| 1389 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1390 | // own moves. Second, this check is necessary because FastISel doesn't |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 1391 | // use CreateRegs to create registers, so it always creates |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1392 | // exactly one register for each non-void instruction. |
| 1393 | EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true); |
| 1394 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| 1395 | // Promote MVT::i1. |
| 1396 | if (VT == MVT::i1) |
| 1397 | VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT); |
| 1398 | else { |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1399 | FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1405 | |
Dan Gohman | fb95f89 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 1406 | // Set the DebugLoc for the copy. Prefer the location of the operand |
| 1407 | // if there is one; use the location of the PHI otherwise. |
| 1408 | DL = PN->getDebugLoc(); |
| 1409 | if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp)) |
| 1410 | DL = Inst->getDebugLoc(); |
| 1411 | |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1412 | unsigned Reg = getRegForValue(PHIOp); |
| 1413 | if (Reg == 0) { |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1414 | FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1415 | return false; |
| 1416 | } |
Dan Gohman | a4160c3 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1417 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg)); |
Dan Gohman | fb95f89 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 1418 | DL = DebugLoc(); |
Dan Gohman | f81eca0 | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | return true; |
| 1423 | } |