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