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