| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1 | //===- FastISel.cpp - Implementation of the FastISel class ----------------===// |
| Dan Gohman | b2226e2 | 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 | b486350 | 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 | c52af45 | 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 | b486350 | 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 | c52af45 | 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 | b486350 | 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 | c52af45 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 27 | // weighed against the speed at which the code can be generated. Also, |
| Dan Gohman | b486350 | 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 | c52af45 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 30 | // time. Despite its limitations, "fast" instruction selection is able to |
| Dan Gohman | b486350 | 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 | c52af45 | 2008-10-13 01:59:13 +0000 | [diff] [blame] | 37 | // from simple operators. More complicated operations currently require |
| Dan Gohman | b486350 | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 38 | // target-specific code. |
| 39 | // |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/FastISel.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/APFloat.h" |
| Simon Pilgrim | 8424df7 | 2017-03-19 16:50:25 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/APSInt.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/DenseMap.h" |
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/Optional.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/SmallPtrSet.h" |
| 48 | #include "llvm/ADT/SmallString.h" |
| 49 | #include "llvm/ADT/SmallVector.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/Statistic.h" |
| Juergen Ributzka | 454d374 | 2014-06-13 00:45:11 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/Analysis.h" |
| 54 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/ISDOpcodes.h" |
| 56 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 57 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 58 | #include "llvm/CodeGen/MachineInstr.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 59 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 60 | #include "llvm/CodeGen/MachineMemOperand.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 61 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 62 | #include "llvm/CodeGen/MachineOperand.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 63 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 64 | #include "llvm/CodeGen/MachineValueType.h" |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 65 | #include "llvm/CodeGen/StackMaps.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 66 | #include "llvm/CodeGen/ValueTypes.h" |
| 67 | #include "llvm/IR/Argument.h" |
| 68 | #include "llvm/IR/Attributes.h" |
| 69 | #include "llvm/IR/BasicBlock.h" |
| 70 | #include "llvm/IR/CallSite.h" |
| 71 | #include "llvm/IR/CallingConv.h" |
| 72 | #include "llvm/IR/Constant.h" |
| 73 | #include "llvm/IR/Constants.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 74 | #include "llvm/IR/DataLayout.h" |
| Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 75 | #include "llvm/IR/DebugInfo.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 76 | #include "llvm/IR/DebugLoc.h" |
| 77 | #include "llvm/IR/DerivedTypes.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 78 | #include "llvm/IR/Function.h" |
| Eduard Burtescu | 23c4d83 | 2016-01-20 00:26:52 +0000 | [diff] [blame] | 79 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 80 | #include "llvm/IR/GlobalValue.h" |
| 81 | #include "llvm/IR/InlineAsm.h" |
| 82 | #include "llvm/IR/InstrTypes.h" |
| 83 | #include "llvm/IR/Instruction.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 84 | #include "llvm/IR/Instructions.h" |
| 85 | #include "llvm/IR/IntrinsicInst.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 86 | #include "llvm/IR/LLVMContext.h" |
| Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 87 | #include "llvm/IR/Mangler.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 88 | #include "llvm/IR/Metadata.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 89 | #include "llvm/IR/Operator.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 90 | #include "llvm/IR/Type.h" |
| 91 | #include "llvm/IR/User.h" |
| 92 | #include "llvm/IR/Value.h" |
| 93 | #include "llvm/MC/MCContext.h" |
| 94 | #include "llvm/MC/MCInstrDesc.h" |
| 95 | #include "llvm/MC/MCRegisterInfo.h" |
| 96 | #include "llvm/Support/Casting.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 97 | #include "llvm/Support/Debug.h" |
| 98 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 99 | #include "llvm/Support/MathExtras.h" |
| Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 100 | #include "llvm/Support/raw_ostream.h" |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 101 | #include "llvm/Target/TargetInstrInfo.h" |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 102 | #include "llvm/Target/TargetLowering.h" |
| Dan Gohman | 02c84b8 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 103 | #include "llvm/Target/TargetMachine.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 104 | #include "llvm/Target/TargetOptions.h" |
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 105 | #include "llvm/Target/TargetSubtargetInfo.h" |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 106 | #include <algorithm> |
| 107 | #include <cassert> |
| 108 | #include <cstdint> |
| 109 | #include <iterator> |
| 110 | #include <utility> |
| 111 | |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 112 | using namespace llvm; |
| 113 | |
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 114 | #define DEBUG_TYPE "isel" |
| 115 | |
| Chad Rosier | 61e8d10 | 2011-11-28 19:59:09 +0000 | [diff] [blame] | 116 | STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by " |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 117 | "target-independent selector"); |
| Chad Rosier | 61e8d10 | 2011-11-28 19:59:09 +0000 | [diff] [blame] | 118 | STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by " |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 119 | "target-specific selector"); |
| Chad Rosier | 46addb9 | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 120 | STATISTIC(NumFastIselDead, "Number of dead insts removed on failure"); |
| Chad Rosier | ff40b1e | 2011-11-16 21:05:28 +0000 | [diff] [blame] | 121 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 122 | /// Set the current block to which generated machine instructions will be |
| 123 | /// appended, and clear the local CSE map. |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 124 | void FastISel::startNewBlock() { |
| 125 | LocalValueMap.clear(); |
| 126 | |
| Jakob Stoklund Olesen | 6a7d683 | 2013-07-04 04:53:49 +0000 | [diff] [blame] | 127 | // Instructions are appended to FuncInfo.MBB. If the basic block already |
| Jakob Stoklund Olesen | 3d8560c | 2013-07-04 04:32:39 +0000 | [diff] [blame] | 128 | // contains labels or copies, use the last instruction as the last local |
| 129 | // value. |
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 130 | EmitStartPt = nullptr; |
| Jakob Stoklund Olesen | 3d8560c | 2013-07-04 04:32:39 +0000 | [diff] [blame] | 131 | if (!FuncInfo.MBB->empty()) |
| 132 | EmitStartPt = &FuncInfo.MBB->back(); |
| Ivan Krasin | d7cbd4c | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 133 | LastLocalValue = EmitStartPt; |
| 134 | } |
| 135 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 136 | bool FastISel::lowerArguments() { |
| Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 137 | if (!FuncInfo.CanLowerReturn) |
| 138 | // Fallback to SDISel argument lowering code to deal with sret pointer |
| 139 | // parameter. |
| 140 | return false; |
| Stephen Lin | cfe7f35 | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 141 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 142 | if (!fastLowerArguments()) |
| Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 143 | return false; |
| 144 | |
| David Blaikie | 97c6c5b | 2013-06-21 22:56:30 +0000 | [diff] [blame] | 145 | // Enter arguments into ValueMap for uses in non-entry BBs. |
| Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 146 | for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(), |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 147 | E = FuncInfo.Fn->arg_end(); |
| 148 | I != E; ++I) { |
| Duncan P. N. Exon Smith | e400a7d | 2015-10-13 19:47:46 +0000 | [diff] [blame] | 149 | DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(&*I); |
| David Blaikie | 97c6c5b | 2013-06-21 22:56:30 +0000 | [diff] [blame] | 150 | assert(VI != LocalValueMap.end() && "Missed an argument?"); |
| Duncan P. N. Exon Smith | e400a7d | 2015-10-13 19:47:46 +0000 | [diff] [blame] | 151 | FuncInfo.ValueMap[&*I] = VI->second; |
| Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
| Ivan Krasin | d7cbd4c | 2011-08-18 22:06:10 +0000 | [diff] [blame] | 156 | void FastISel::flushLocalValueMap() { |
| 157 | LocalValueMap.clear(); |
| 158 | LastLocalValue = EmitStartPt; |
| 159 | recomputeInsertPt(); |
| Hans Wennborg | 18f0a98 | 2014-09-08 20:24:10 +0000 | [diff] [blame] | 160 | SavedInsertPt = FuncInfo.InsertPt; |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| Juergen Ributzka | 4f1a54a | 2014-08-28 00:09:46 +0000 | [diff] [blame] | 163 | bool FastISel::hasTrivialKill(const Value *V) { |
| Dan Gohman | 88fb253 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 164 | // Don't consider constants or arguments to have trivial kills. |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 165 | const Instruction *I = dyn_cast<Instruction>(V); |
| Dan Gohman | 88fb253 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 166 | if (!I) |
| 167 | return false; |
| 168 | |
| 169 | // No-op casts are trivially coalesced by fast-isel. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 170 | if (const auto *Cast = dyn_cast<CastInst>(I)) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 171 | if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) && |
| Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 172 | !hasTrivialKill(Cast->getOperand(0))) |
| Dan Gohman | 88fb253 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 173 | return false; |
| 174 | |
| Juergen Ributzka | 4f1a54a | 2014-08-28 00:09:46 +0000 | [diff] [blame] | 175 | // Even the value might have only one use in the LLVM IR, it is possible that |
| 176 | // FastISel might fold the use into another instruction and now there is more |
| 177 | // than one use at the Machine Instruction level. |
| 178 | unsigned Reg = lookUpRegForValue(V); |
| 179 | if (Reg && !MRI.use_empty(Reg)) |
| 180 | return false; |
| 181 | |
| Chad Rosier | 291ce47 | 2011-11-15 23:34:05 +0000 | [diff] [blame] | 182 | // GEPs with all zero indices are trivially coalesced by fast-isel. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 183 | if (const auto *GEP = dyn_cast<GetElementPtrInst>(I)) |
| Chad Rosier | 291ce47 | 2011-11-15 23:34:05 +0000 | [diff] [blame] | 184 | if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0))) |
| 185 | return false; |
| 186 | |
| Dan Gohman | 88fb253 | 2010-05-14 22:53:18 +0000 | [diff] [blame] | 187 | // Only instructions with a single use in the same basic block are considered |
| 188 | // to have trivial kills. |
| 189 | return I->hasOneUse() && |
| 190 | !(I->getOpcode() == Instruction::BitCast || |
| 191 | I->getOpcode() == Instruction::PtrToInt || |
| 192 | I->getOpcode() == Instruction::IntToPtr) && |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 193 | cast<Instruction>(*I->user_begin())->getParent() == I->getParent(); |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 196 | unsigned FastISel::getRegForValue(const Value *V) { |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 197 | EVT RealVT = TLI.getValueType(DL, V->getType(), /*AllowUnknown=*/true); |
| Dan Gohman | ca93aab | 2009-04-07 20:40:11 +0000 | [diff] [blame] | 198 | // Don't handle non-simple values in FastISel. |
| 199 | if (!RealVT.isSimple()) |
| 200 | return 0; |
| Dan Gohman | 4c31524 | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 201 | |
| 202 | // Ignore illegal types. We must do this before looking up the value |
| 203 | // in ValueMap because Arguments are given virtual registers regardless |
| 204 | // of whether FastISel can handle them. |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 205 | MVT VT = RealVT.getSimpleVT(); |
| Dan Gohman | 4c31524 | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 206 | if (!TLI.isTypeLegal(VT)) { |
| Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 207 | // Handle integer promotions, though, because they're common and easy. |
| 208 | if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16) |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 209 | VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT(); |
| Dan Gohman | 4c31524 | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 210 | else |
| 211 | return 0; |
| 212 | } |
| 213 | |
| Eric Christopher | 1a06cc9 | 2012-03-20 01:07:47 +0000 | [diff] [blame] | 214 | // Look up the value to see if we already have a register for it. |
| 215 | unsigned Reg = lookUpRegForValue(V); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 216 | if (Reg) |
| Dan Gohman | e039d55 | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 217 | return Reg; |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 218 | |
| Dan Gohman | a7c717d8 | 2010-05-06 00:02:14 +0000 | [diff] [blame] | 219 | // In bottom-up mode, just create the virtual register which will be used |
| 220 | // to hold the value. It will be materialized later. |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 221 | if (isa<Instruction>(V) && |
| 222 | (!isa<AllocaInst>(V) || |
| 223 | !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V)))) |
| 224 | return FuncInfo.InitializeRegForValue(V); |
| Dan Gohman | a7c717d8 | 2010-05-06 00:02:14 +0000 | [diff] [blame] | 225 | |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 226 | SavePoint SaveInsertPt = enterLocalValueArea(); |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 227 | |
| 228 | // Materialize the value in a register. Emit any instructions in the |
| 229 | // local value area. |
| 230 | Reg = materializeRegForValue(V, VT); |
| 231 | |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 232 | leaveLocalValueArea(SaveInsertPt); |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 233 | |
| 234 | return Reg; |
| Dan Gohman | 626b5d8 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 237 | unsigned FastISel::materializeConstant(const Value *V, MVT VT) { |
| Dan Gohman | 626b5d8 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 238 | unsigned Reg = 0; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 239 | if (const auto *CI = dyn_cast<ConstantInt>(V)) { |
| Dan Gohman | 9801ba4 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 240 | if (CI->getValue().getActiveBits() <= 64) |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 241 | Reg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue()); |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 242 | } else if (isa<AllocaInst>(V)) |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 243 | Reg = fastMaterializeAlloca(cast<AllocaInst>(V)); |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 244 | else if (isa<ConstantPointerNull>(V)) |
| Dan Gohman | c1d47c5 | 2008-10-07 22:03:27 +0000 | [diff] [blame] | 245 | // Translate this as an integer zero so that it can be |
| 246 | // local-CSE'd with actual integer zeros. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 247 | Reg = getRegForValue( |
| 248 | Constant::getNullValue(DL.getIntPtrType(V->getContext()))); |
| 249 | else if (const auto *CF = dyn_cast<ConstantFP>(V)) { |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 250 | if (CF->isNullValue()) |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 251 | Reg = fastMaterializeFloatZero(CF); |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 252 | else |
| Eli Friedman | 406c471 | 2011-04-27 22:41:55 +0000 | [diff] [blame] | 253 | // Try to emit the constant directly. |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 254 | Reg = fastEmit_f(VT, VT, ISD::ConstantFP, CF); |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 255 | |
| 256 | if (!Reg) { |
| Dan Gohman | 8a2dae5 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 257 | // Try to emit the constant by using an integer constant with a cast. |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 258 | const APFloat &Flt = CF->getValueAPF(); |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 259 | EVT IntVT = TLI.getPointerTy(DL); |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 260 | uint32_t IntBitWidth = IntVT.getSizeInBits(); |
| Simon Pilgrim | 8424df7 | 2017-03-19 16:50:25 +0000 | [diff] [blame] | 261 | APSInt SIntVal(IntBitWidth, /*isUnsigned=*/false); |
| Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 262 | bool isExact; |
| Simon Pilgrim | 8424df7 | 2017-03-19 16:50:25 +0000 | [diff] [blame] | 263 | (void)Flt.convertToInteger(SIntVal, APFloat::rmTowardZero, &isExact); |
| Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 264 | if (isExact) { |
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 265 | unsigned IntegerReg = |
| Simon Pilgrim | 8424df7 | 2017-03-19 16:50:25 +0000 | [diff] [blame] | 266 | getRegForValue(ConstantInt::get(V->getContext(), SIntVal)); |
| Dan Gohman | 9801ba4 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 267 | if (IntegerReg != 0) |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 268 | Reg = fastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 269 | /*Kill=*/false); |
| Dan Gohman | 9801ba4 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 270 | } |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 271 | } |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 272 | } else if (const auto *Op = dyn_cast<Operator>(V)) { |
| 273 | if (!selectOperator(Op, Op->getOpcode())) |
| Dan Gohman | 722f5fc | 2010-07-01 02:58:57 +0000 | [diff] [blame] | 274 | if (!isa<Instruction>(Op) || |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 275 | !fastSelectInstruction(cast<Instruction>(Op))) |
| Dan Gohman | 722f5fc | 2010-07-01 02:58:57 +0000 | [diff] [blame] | 276 | return 0; |
| Dan Gohman | 7c58cf7 | 2010-06-21 14:17:46 +0000 | [diff] [blame] | 277 | Reg = lookUpRegForValue(Op); |
| Dan Gohman | c45733f | 2008-08-28 21:19:07 +0000 | [diff] [blame] | 278 | } else if (isa<UndefValue>(V)) { |
| Dan Gohman | e039d55 | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 279 | Reg = createResultReg(TLI.getRegClassFor(VT)); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 280 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 281 | TII.get(TargetOpcode::IMPLICIT_DEF), Reg); |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 282 | } |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 283 | return Reg; |
| 284 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 285 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 286 | /// Helper for getRegForValue. This function is called when the value isn't |
| 287 | /// already available in a register and must be materialized with new |
| 288 | /// instructions. |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 289 | unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) { |
| 290 | unsigned Reg = 0; |
| 291 | // Give the target-specific code a try first. |
| 292 | if (isa<Constant>(V)) |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 293 | Reg = fastMaterializeConstant(cast<Constant>(V)); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 294 | |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 295 | // If target-specific code couldn't or didn't want to handle the value, then |
| 296 | // give target-independent code a try. |
| 297 | if (!Reg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 298 | Reg = materializeConstant(V, VT); |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 299 | |
| Dan Gohman | 9801ba4 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 300 | // Don't cache constant materializations in the general ValueMap. |
| 301 | // To do so would require tracking what uses they dominate. |
| Juergen Ributzka | 4bf6c01 | 2014-08-19 19:05:24 +0000 | [diff] [blame] | 302 | if (Reg) { |
| Dan Gohman | 3663f15 | 2008-09-25 01:28:51 +0000 | [diff] [blame] | 303 | LocalValueMap[V] = Reg; |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 304 | LastLocalValue = MRI.getVRegDef(Reg); |
| 305 | } |
| Dan Gohman | e039d55 | 2008-09-03 23:32:19 +0000 | [diff] [blame] | 306 | return Reg; |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 309 | unsigned FastISel::lookUpRegForValue(const Value *V) { |
| Evan Cheng | 1e97901 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 310 | // Look up the value to see if we already have a register for it. We |
| 311 | // cache values defined by Instructions across blocks, and other values |
| 312 | // only locally. This is because Instructions already have the SSA |
| Dan Gohman | 626b5d8 | 2010-05-03 23:36:34 +0000 | [diff] [blame] | 313 | // def-dominates-use requirement enforced. |
| Dan Gohman | 87fb4e8 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 314 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V); |
| 315 | if (I != FuncInfo.ValueMap.end()) |
| Dan Gohman | f91aff5 | 2010-06-21 14:21:47 +0000 | [diff] [blame] | 316 | return I->second; |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 317 | return LocalValueMap[V]; |
| Evan Cheng | 1e97901 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 320 | void FastISel::updateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) { |
| Dan Gohman | fcf5456 | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 321 | if (!isa<Instruction>(I)) { |
| 322 | LocalValueMap[I] = Reg; |
| Eli Friedman | a4d4a01 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 323 | return; |
| Dan Gohman | fcf5456 | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 324 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 325 | |
| Dan Gohman | 87fb4e8 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 326 | unsigned &AssignedReg = FuncInfo.ValueMap[I]; |
| Chris Lattner | ada5d6c | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 327 | if (AssignedReg == 0) |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 328 | // Use the new register. |
| Chris Lattner | ada5d6c | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 329 | AssignedReg = Reg; |
| Chris Lattner | a101f6f | 2009-04-12 07:46:30 +0000 | [diff] [blame] | 330 | else if (Reg != AssignedReg) { |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 331 | // Arrange for uses of AssignedReg to be replaced by uses of Reg. |
| Eli Friedman | a4d4a01 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 332 | for (unsigned i = 0; i < NumRegs; i++) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 333 | FuncInfo.RegFixups[AssignedReg + i] = Reg + i; |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 334 | |
| 335 | AssignedReg = Reg; |
| Chris Lattner | ada5d6c | 2009-04-12 07:45:01 +0000 | [diff] [blame] | 336 | } |
| Owen Anderson | 6f0c51d | 2008-08-30 00:38:46 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 339 | std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) { |
| Dan Gohman | 4c31524 | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 340 | unsigned IdxN = getRegForValue(Idx); |
| 341 | if (IdxN == 0) |
| 342 | // Unhandled operand. Halt "fast" selection and bail. |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 343 | return std::pair<unsigned, bool>(0, false); |
| 344 | |
| 345 | bool IdxNIsKill = hasTrivialKill(Idx); |
| Dan Gohman | 4c31524 | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 346 | |
| 347 | // If the index is smaller or larger than intptr_t, truncate or extend it. |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 348 | MVT PtrVT = TLI.getPointerTy(DL); |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 349 | EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false); |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 350 | if (IdxVT.bitsLT(PtrVT)) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 351 | IdxN = fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 352 | IdxNIsKill); |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 353 | IdxNIsKill = true; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 354 | } else if (IdxVT.bitsGT(PtrVT)) { |
| 355 | IdxN = |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 356 | fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN, IdxNIsKill); |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 357 | IdxNIsKill = true; |
| 358 | } |
| 359 | return std::pair<unsigned, bool>(IdxN, IdxNIsKill); |
| Dan Gohman | 4c31524 | 2008-12-08 07:57:47 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 362 | void FastISel::recomputeInsertPt() { |
| 363 | if (getLastLocalValue()) { |
| 364 | FuncInfo.InsertPt = getLastLocalValue(); |
| Dan Gohman | b5e918d | 2010-07-19 22:48:56 +0000 | [diff] [blame] | 365 | FuncInfo.MBB = FuncInfo.InsertPt->getParent(); |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 366 | ++FuncInfo.InsertPt; |
| 367 | } else |
| 368 | FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI(); |
| 369 | |
| 370 | // Now skip past any EH_LABELs, which must remain at the beginning. |
| 371 | while (FuncInfo.InsertPt != FuncInfo.MBB->end() && |
| 372 | FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL) |
| 373 | ++FuncInfo.InsertPt; |
| 374 | } |
| 375 | |
| Chad Rosier | 46addb9 | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 376 | void FastISel::removeDeadCode(MachineBasicBlock::iterator I, |
| 377 | MachineBasicBlock::iterator E) { |
| Duncan P. N. Exon Smith | f197b1f | 2016-08-12 05:05:36 +0000 | [diff] [blame] | 378 | assert(I.isValid() && E.isValid() && std::distance(I, E) > 0 && |
| 379 | "Invalid iterator!"); |
| Chad Rosier | 46addb9 | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 380 | while (I != E) { |
| 381 | MachineInstr *Dead = &*I; |
| 382 | ++I; |
| 383 | Dead->eraseFromParent(); |
| Jan Wen Voung | 7857a64 | 2013-03-08 22:56:31 +0000 | [diff] [blame] | 384 | ++NumFastIselDead; |
| Chad Rosier | 46addb9 | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 385 | } |
| 386 | recomputeInsertPt(); |
| 387 | } |
| 388 | |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 389 | FastISel::SavePoint FastISel::enterLocalValueArea() { |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 390 | MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt; |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 391 | DebugLoc OldDL = DbgLoc; |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 392 | recomputeInsertPt(); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 393 | DbgLoc = DebugLoc(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 394 | SavePoint SP = {OldInsertPt, OldDL}; |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 395 | return SP; |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 398 | void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) { |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 399 | if (FuncInfo.InsertPt != FuncInfo.MBB->begin()) |
| Duncan P. N. Exon Smith | 10383ecd | 2016-07-08 18:36:41 +0000 | [diff] [blame] | 400 | LastLocalValue = &*std::prev(FuncInfo.InsertPt); |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 401 | |
| 402 | // Restore the previous insert position. |
| Eric Christopher | f4fba5c | 2012-10-03 08:10:01 +0000 | [diff] [blame] | 403 | FuncInfo.InsertPt = OldInsertPt.InsertPt; |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 404 | DbgLoc = OldInsertPt.DL; |
| Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 407 | bool FastISel::selectBinaryOp(const User *I, unsigned ISDOpcode) { |
| Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 408 | EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true); |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 409 | if (VT == MVT::Other || !VT.isSimple()) |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 410 | // Unhandled type. Halt "fast" selection and bail. |
| 411 | return false; |
| Dan Gohman | fd63459 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 412 | |
| Dan Gohman | 3bcbbec | 2008-08-26 20:52:40 +0000 | [diff] [blame] | 413 | // We only handle legal types. For example, on x86-32 the instruction |
| 414 | // selector contains all of the 64-bit instructions from x86-64, |
| 415 | // under the assumption that i64 won't be used if the target doesn't |
| 416 | // support it. |
| Dan Gohman | fd63459 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 417 | if (!TLI.isTypeLegal(VT)) { |
| Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 418 | // MVT::i1 is special. Allow AND, OR, or XOR because they |
| Dan Gohman | fd63459 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 419 | // don't require additional zeroing, which makes them easy. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 420 | if (VT == MVT::i1 && (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR || |
| 421 | ISDOpcode == ISD::XOR)) |
| Owen Anderson | 117c9e8 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 422 | VT = TLI.getTypeToTransformTo(I->getContext(), VT); |
| Dan Gohman | fd63459 | 2008-09-05 18:44:22 +0000 | [diff] [blame] | 423 | else |
| 424 | return false; |
| 425 | } |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 426 | |
| Chris Lattner | fba7ca6 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 427 | // Check if the first operand is a constant, and handle it as "ri". At -O0, |
| 428 | // we don't have anything that canonicalizes operand order. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 429 | if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(0))) |
| Chris Lattner | fba7ca6 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 430 | if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) { |
| 431 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 432 | if (!Op1) |
| 433 | return false; |
| Chris Lattner | fba7ca6 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 434 | bool Op1IsKill = hasTrivialKill(I->getOperand(1)); |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 435 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 436 | unsigned ResultReg = |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 437 | fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, Op1IsKill, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 438 | CI->getZExtValue(), VT.getSimpleVT()); |
| 439 | if (!ResultReg) |
| 440 | return false; |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 441 | |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 442 | // We successfully emitted code for the given LLVM Instruction. |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 443 | updateValueMap(I, ResultReg); |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 444 | return true; |
| Chris Lattner | fba7ca6 | 2011-04-17 01:16:47 +0000 | [diff] [blame] | 445 | } |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 446 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 447 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 448 | if (!Op0) // Unhandled operand. Halt "fast" selection and bail. |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 449 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 450 | bool Op0IsKill = hasTrivialKill(I->getOperand(0)); |
| 451 | |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 452 | // Check if the second operand is a constant and handle it appropriately. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 453 | if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| Rafael Espindola | d58de06 | 2015-04-06 22:29:07 +0000 | [diff] [blame] | 454 | uint64_t Imm = CI->getSExtValue(); |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 455 | |
| Chris Lattner | 48f75ad | 2011-04-18 07:00:40 +0000 | [diff] [blame] | 456 | // Transform "sdiv exact X, 8" -> "sra X, 3". |
| 457 | if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) && |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 458 | cast<BinaryOperator>(I)->isExact() && isPowerOf2_64(Imm)) { |
| Chris Lattner | 48f75ad | 2011-04-18 07:00:40 +0000 | [diff] [blame] | 459 | Imm = Log2_64(Imm); |
| 460 | ISDOpcode = ISD::SRA; |
| 461 | } |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 462 | |
| Chad Rosier | 6a63a74 | 2012-03-22 00:21:17 +0000 | [diff] [blame] | 463 | // Transform "urem x, pow2" -> "and x, pow2-1". |
| 464 | if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) && |
| 465 | isPowerOf2_64(Imm)) { |
| 466 | --Imm; |
| 467 | ISDOpcode = ISD::AND; |
| 468 | } |
| 469 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 470 | unsigned ResultReg = fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 471 | Op0IsKill, Imm, VT.getSimpleVT()); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 472 | if (!ResultReg) |
| 473 | return false; |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 474 | |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 475 | // We successfully emitted code for the given LLVM Instruction. |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 476 | updateValueMap(I, ResultReg); |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 477 | return true; |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 480 | unsigned Op1 = getRegForValue(I->getOperand(1)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 481 | if (!Op1) // Unhandled operand. Halt "fast" selection and bail. |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 482 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 483 | bool Op1IsKill = hasTrivialKill(I->getOperand(1)); |
| 484 | |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 485 | // Now we have both operands in registers. Emit the instruction. |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 486 | unsigned ResultReg = fastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(), |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 487 | ISDOpcode, Op0, Op0IsKill, Op1, Op1IsKill); |
| 488 | if (!ResultReg) |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 489 | // Target-specific code wasn't able to find a machine opcode for |
| 490 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 491 | return false; |
| 492 | |
| Dan Gohman | b16a778 | 2008-08-20 00:23:20 +0000 | [diff] [blame] | 493 | // We successfully emitted code for the given LLVM Instruction. |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 494 | updateValueMap(I, ResultReg); |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 495 | return true; |
| 496 | } |
| 497 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 498 | bool FastISel::selectGetElementPtr(const User *I) { |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 499 | unsigned N = getRegForValue(I->getOperand(0)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 500 | if (!N) // Unhandled operand. Halt "fast" selection and bail. |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 501 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 502 | bool NIsKill = hasTrivialKill(I->getOperand(0)); |
| 503 | |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 504 | // Keep a running tab of the total offset to coalesce multiple N = N + Offset |
| 505 | // into a single N = N + TotalOffset. |
| 506 | uint64_t TotalOffs = 0; |
| 507 | // FIXME: What's a good SWAG number for MaxOffs? |
| 508 | uint64_t MaxOffs = 2048; |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 509 | MVT VT = TLI.getPointerTy(DL); |
| Eduard Burtescu | 23c4d83 | 2016-01-20 00:26:52 +0000 | [diff] [blame] | 510 | for (gep_type_iterator GTI = gep_type_begin(I), E = gep_type_end(I); |
| 511 | GTI != E; ++GTI) { |
| 512 | const Value *Idx = GTI.getOperand(); |
| Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 513 | if (StructType *StTy = GTI.getStructTypeOrNull()) { |
| Reid Kleckner | 016c6b2 | 2015-03-11 23:36:10 +0000 | [diff] [blame] | 514 | uint64_t Field = cast<ConstantInt>(Idx)->getZExtValue(); |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 515 | if (Field) { |
| 516 | // N = N + Offset |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 517 | TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field); |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 518 | if (TotalOffs >= MaxOffs) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 519 | N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 520 | if (!N) // Unhandled operand. Halt "fast" selection and bail. |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 521 | return false; |
| 522 | NIsKill = true; |
| 523 | TotalOffs = 0; |
| 524 | } |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 525 | } |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 526 | } else { |
| Eduard Burtescu | 23c4d83 | 2016-01-20 00:26:52 +0000 | [diff] [blame] | 527 | Type *Ty = GTI.getIndexedType(); |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 528 | |
| 529 | // If this is a constant subscript, handle it quickly. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 530 | if (const auto *CI = dyn_cast<ConstantInt>(Idx)) { |
| 531 | if (CI->isZero()) |
| 532 | continue; |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 533 | // N = N + Offset |
| Reid Kleckner | 016c6b2 | 2015-03-11 23:36:10 +0000 | [diff] [blame] | 534 | uint64_t IdxN = CI->getValue().sextOrTrunc(64).getSExtValue(); |
| 535 | TotalOffs += DL.getTypeAllocSize(Ty) * IdxN; |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 536 | if (TotalOffs >= MaxOffs) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 537 | N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 538 | if (!N) // Unhandled operand. Halt "fast" selection and bail. |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 539 | return false; |
| 540 | NIsKill = true; |
| 541 | TotalOffs = 0; |
| 542 | } |
| 543 | continue; |
| 544 | } |
| 545 | if (TotalOffs) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 546 | N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 547 | if (!N) // Unhandled operand. Halt "fast" selection and bail. |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 548 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 549 | NIsKill = true; |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 550 | TotalOffs = 0; |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 551 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 552 | |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 553 | // N = N + Idx * ElementSize; |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 554 | uint64_t ElementSize = DL.getTypeAllocSize(Ty); |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 555 | std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx); |
| 556 | unsigned IdxN = Pair.first; |
| 557 | bool IdxNIsKill = Pair.second; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 558 | if (!IdxN) // Unhandled operand. Halt "fast" selection and bail. |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 559 | return false; |
| 560 | |
| Dan Gohman | b5e04bf | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 561 | if (ElementSize != 1) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 562 | IdxN = fastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 563 | if (!IdxN) // Unhandled operand. Halt "fast" selection and bail. |
| Dan Gohman | b5e04bf | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 564 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 565 | IdxNIsKill = true; |
| Dan Gohman | b5e04bf | 2008-08-26 20:57:08 +0000 | [diff] [blame] | 566 | } |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 567 | N = fastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 568 | if (!N) // Unhandled operand. Halt "fast" selection and bail. |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | } |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 572 | if (TotalOffs) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 573 | N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 574 | if (!N) // Unhandled operand. Halt "fast" selection and bail. |
| Chad Rosier | f83ab70 | 2011-11-17 07:15:58 +0000 | [diff] [blame] | 575 | return false; |
| 576 | } |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 577 | |
| 578 | // We successfully emitted code for the given LLVM Instruction. |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 579 | updateValueMap(I, N); |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 580 | return true; |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 583 | bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops, |
| 584 | const CallInst *CI, unsigned StartIdx) { |
| 585 | for (unsigned i = StartIdx, e = CI->getNumArgOperands(); i != e; ++i) { |
| 586 | Value *Val = CI->getArgOperand(i); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 587 | // Check for constants and encode them with a StackMaps::ConstantOp prefix. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 588 | if (const auto *C = dyn_cast<ConstantInt>(Val)) { |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 589 | Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp)); |
| 590 | Ops.push_back(MachineOperand::CreateImm(C->getSExtValue())); |
| 591 | } else if (isa<ConstantPointerNull>(Val)) { |
| 592 | Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp)); |
| 593 | Ops.push_back(MachineOperand::CreateImm(0)); |
| 594 | } else if (auto *AI = dyn_cast<AllocaInst>(Val)) { |
| Simon Pilgrim | 7a6b6d5 | 2016-11-20 13:14:57 +0000 | [diff] [blame] | 595 | // Values coming from a stack location also require a special encoding, |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 596 | // but that is added later on by the target specific frame index |
| 597 | // elimination implementation. |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 598 | auto SI = FuncInfo.StaticAllocaMap.find(AI); |
| 599 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 600 | Ops.push_back(MachineOperand::CreateFI(SI->second)); |
| 601 | else |
| 602 | return false; |
| 603 | } else { |
| 604 | unsigned Reg = getRegForValue(Val); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 605 | if (!Reg) |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 606 | return false; |
| 607 | Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false)); |
| 608 | } |
| 609 | } |
| Juergen Ributzka | 04558dc | 2014-06-12 03:29:26 +0000 | [diff] [blame] | 610 | return true; |
| 611 | } |
| 612 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 613 | bool FastISel::selectStackmap(const CallInst *I) { |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 614 | // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>, |
| 615 | // [live variables...]) |
| 616 | assert(I->getCalledFunction()->getReturnType()->isVoidTy() && |
| 617 | "Stackmap cannot return a value."); |
| 618 | |
| 619 | // The stackmap intrinsic only records the live variables (the arguments |
| 620 | // passed to it) and emits NOPS (if requested). Unlike the patchpoint |
| 621 | // intrinsic, this won't be lowered to a function call. This means we don't |
| 622 | // have to worry about calling conventions and target-specific lowering code. |
| 623 | // Instead we perform the call lowering right here. |
| 624 | // |
| Serge Pavlov | d526b13 | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 625 | // CALLSEQ_START(0, 0...) |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 626 | // STACKMAP(id, nbytes, ...) |
| 627 | // CALLSEQ_END(0, 0) |
| 628 | // |
| 629 | SmallVector<MachineOperand, 32> Ops; |
| 630 | |
| 631 | // Add the <id> and <numBytes> constants. |
| 632 | assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) && |
| 633 | "Expected a constant integer."); |
| 634 | const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)); |
| 635 | Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue())); |
| 636 | |
| 637 | assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) && |
| 638 | "Expected a constant integer."); |
| 639 | const auto *NumBytes = |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 640 | cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 641 | Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue())); |
| 642 | |
| 643 | // Push live variables for the stack map (skipping the first two arguments |
| 644 | // <id> and <numBytes>). |
| 645 | if (!addStackMapLiveVars(Ops, I, 2)) |
| 646 | return false; |
| 647 | |
| 648 | // We are not adding any register mask info here, because the stackmap doesn't |
| 649 | // clobber anything. |
| 650 | |
| 651 | // Add scratch registers as implicit def and early clobber. |
| 652 | CallingConv::ID CC = I->getCallingConv(); |
| 653 | const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC); |
| 654 | for (unsigned i = 0; ScratchRegs[i]; ++i) |
| 655 | Ops.push_back(MachineOperand::CreateReg( |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 656 | ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false, |
| 657 | /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true)); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 658 | |
| 659 | // Issue CALLSEQ_START |
| 660 | unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); |
| Alex Lorenz | 2f43dd5 | 2015-08-10 21:27:03 +0000 | [diff] [blame] | 661 | auto Builder = |
| 662 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown)); |
| 663 | const MCInstrDesc &MCID = Builder.getInstr()->getDesc(); |
| 664 | for (unsigned I = 0, E = MCID.getNumOperands(); I < E; ++I) |
| 665 | Builder.addImm(0); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 666 | |
| 667 | // Issue STACKMAP. |
| 668 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 669 | TII.get(TargetOpcode::STACKMAP)); |
| 670 | for (auto const &MO : Ops) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 671 | MIB.add(MO); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 672 | |
| 673 | // Issue CALLSEQ_END |
| 674 | unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); |
| 675 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp)) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 676 | .addImm(0) |
| 677 | .addImm(0); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 678 | |
| 679 | // Inform the Frame Information that we have a stackmap in this function. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 680 | FuncInfo.MF->getFrameInfo().setHasStackMap(); |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 681 | |
| 682 | return true; |
| 683 | } |
| 684 | |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 685 | /// \brief Lower an argument list according to the target calling convention. |
| 686 | /// |
| 687 | /// This is a helper for lowering intrinsics that follow a target calling |
| 688 | /// convention or require stack pointer adjustment. Only a subset of the |
| 689 | /// intrinsic's operands need to participate in the calling convention. |
| 690 | bool FastISel::lowerCallOperands(const CallInst *CI, unsigned ArgIdx, |
| 691 | unsigned NumArgs, const Value *Callee, |
| 692 | bool ForceRetVoidTy, CallLoweringInfo &CLI) { |
| 693 | ArgListTy Args; |
| 694 | Args.reserve(NumArgs); |
| 695 | |
| 696 | // Populate the argument list. |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 697 | ImmutableCallSite CS(CI); |
| Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 698 | for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs; ArgI != ArgE; ++ArgI) { |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 699 | Value *V = CI->getOperand(ArgI); |
| 700 | |
| 701 | assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic."); |
| 702 | |
| 703 | ArgListEntry Entry; |
| 704 | Entry.Val = V; |
| 705 | Entry.Ty = V->getType(); |
| Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 706 | Entry.setAttributes(&CS, ArgIdx); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 707 | Args.push_back(Entry); |
| 708 | } |
| 709 | |
| 710 | Type *RetTy = ForceRetVoidTy ? Type::getVoidTy(CI->getType()->getContext()) |
| 711 | : CI->getType(); |
| 712 | CLI.setCallee(CI->getCallingConv(), RetTy, Callee, std::move(Args), NumArgs); |
| 713 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 714 | return lowerCallTo(CLI); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 717 | FastISel::CallLoweringInfo &FastISel::CallLoweringInfo::setCallee( |
| 718 | const DataLayout &DL, MCContext &Ctx, CallingConv::ID CC, Type *ResultTy, |
| Mehdi Amini | 3e021be | 2016-10-05 01:37:29 +0000 | [diff] [blame] | 719 | StringRef Target, ArgListTy &&ArgsList, unsigned FixedArgs) { |
| Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 720 | SmallString<32> MangledName; |
| 721 | Mangler::getNameWithPrefix(MangledName, Target, DL); |
| 722 | MCSymbol *Sym = Ctx.getOrCreateSymbol(MangledName); |
| 723 | return setCallee(CC, ResultTy, Sym, std::move(ArgsList), FixedArgs); |
| 724 | } |
| 725 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 726 | bool FastISel::selectPatchpoint(const CallInst *I) { |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 727 | // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>, |
| 728 | // i32 <numBytes>, |
| 729 | // i8* <target>, |
| 730 | // i32 <numArgs>, |
| 731 | // [Args...], |
| 732 | // [live variables...]) |
| 733 | CallingConv::ID CC = I->getCallingConv(); |
| 734 | bool IsAnyRegCC = CC == CallingConv::AnyReg; |
| 735 | bool HasDef = !I->getType()->isVoidTy(); |
| Lang Hames | 65613a6 | 2015-04-22 06:02:31 +0000 | [diff] [blame] | 736 | Value *Callee = I->getOperand(PatchPointOpers::TargetPos)->stripPointerCasts(); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 737 | |
| 738 | // Get the real number of arguments participating in the call <numArgs> |
| 739 | assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)) && |
| 740 | "Expected a constant integer."); |
| 741 | const auto *NumArgsVal = |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 742 | cast<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 743 | unsigned NumArgs = NumArgsVal->getZExtValue(); |
| 744 | |
| 745 | // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs> |
| 746 | // This includes all meta-operands up to but not including CC. |
| 747 | unsigned NumMetaOpers = PatchPointOpers::CCPos; |
| 748 | assert(I->getNumArgOperands() >= NumMetaOpers + NumArgs && |
| 749 | "Not enough arguments provided to the patchpoint intrinsic"); |
| 750 | |
| 751 | // For AnyRegCC the arguments are lowered later on manually. |
| 752 | unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs; |
| 753 | CallLoweringInfo CLI; |
| Hal Finkel | 0ad96c8 | 2015-01-13 17:48:04 +0000 | [diff] [blame] | 754 | CLI.setIsPatchPoint(); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 755 | if (!lowerCallOperands(I, NumMetaOpers, NumCallArgs, Callee, IsAnyRegCC, CLI)) |
| 756 | return false; |
| 757 | |
| 758 | assert(CLI.Call && "No call instruction specified."); |
| 759 | |
| 760 | SmallVector<MachineOperand, 32> Ops; |
| 761 | |
| 762 | // Add an explicit result reg if we use the anyreg calling convention. |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 763 | if (IsAnyRegCC && HasDef) { |
| Juergen Ributzka | a415943 | 2014-07-15 02:22:43 +0000 | [diff] [blame] | 764 | assert(CLI.NumResultRegs == 0 && "Unexpected result register."); |
| 765 | CLI.ResultReg = createResultReg(TLI.getRegClassFor(MVT::i64)); |
| 766 | CLI.NumResultRegs = 1; |
| 767 | Ops.push_back(MachineOperand::CreateReg(CLI.ResultReg, /*IsDef=*/true)); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // Add the <id> and <numBytes> constants. |
| 771 | assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) && |
| 772 | "Expected a constant integer."); |
| 773 | const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)); |
| 774 | Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue())); |
| 775 | |
| 776 | assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) && |
| 777 | "Expected a constant integer."); |
| 778 | const auto *NumBytes = |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 779 | cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 780 | Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue())); |
| 781 | |
| Lang Hames | 65613a6 | 2015-04-22 06:02:31 +0000 | [diff] [blame] | 782 | // Add the call target. |
| 783 | if (const auto *C = dyn_cast<IntToPtrInst>(Callee)) { |
| 784 | uint64_t CalleeConstAddr = |
| 785 | cast<ConstantInt>(C->getOperand(0))->getZExtValue(); |
| 786 | Ops.push_back(MachineOperand::CreateImm(CalleeConstAddr)); |
| 787 | } else if (const auto *C = dyn_cast<ConstantExpr>(Callee)) { |
| 788 | if (C->getOpcode() == Instruction::IntToPtr) { |
| 789 | uint64_t CalleeConstAddr = |
| 790 | cast<ConstantInt>(C->getOperand(0))->getZExtValue(); |
| 791 | Ops.push_back(MachineOperand::CreateImm(CalleeConstAddr)); |
| 792 | } else |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 793 | llvm_unreachable("Unsupported ConstantExpr."); |
| Lang Hames | 65613a6 | 2015-04-22 06:02:31 +0000 | [diff] [blame] | 794 | } else if (const auto *GV = dyn_cast<GlobalValue>(Callee)) { |
| 795 | Ops.push_back(MachineOperand::CreateGA(GV, 0)); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 796 | } else if (isa<ConstantPointerNull>(Callee)) |
| Lang Hames | 65613a6 | 2015-04-22 06:02:31 +0000 | [diff] [blame] | 797 | Ops.push_back(MachineOperand::CreateImm(0)); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 798 | else |
| 799 | llvm_unreachable("Unsupported callee address."); |
| 800 | |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 801 | // Adjust <numArgs> to account for any arguments that have been passed on |
| 802 | // the stack instead. |
| 803 | unsigned NumCallRegArgs = IsAnyRegCC ? NumArgs : CLI.OutRegs.size(); |
| 804 | Ops.push_back(MachineOperand::CreateImm(NumCallRegArgs)); |
| 805 | |
| 806 | // Add the calling convention |
| 807 | Ops.push_back(MachineOperand::CreateImm((unsigned)CC)); |
| 808 | |
| 809 | // Add the arguments we omitted previously. The register allocator should |
| 810 | // place these in any free register. |
| 811 | if (IsAnyRegCC) { |
| 812 | for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i) { |
| 813 | unsigned Reg = getRegForValue(I->getArgOperand(i)); |
| 814 | if (!Reg) |
| 815 | return false; |
| 816 | Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false)); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // Push the arguments from the call instruction. |
| 821 | for (auto Reg : CLI.OutRegs) |
| 822 | Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false)); |
| 823 | |
| 824 | // Push live variables for the stack map. |
| 825 | if (!addStackMapLiveVars(Ops, I, NumMetaOpers + NumArgs)) |
| 826 | return false; |
| 827 | |
| 828 | // Push the register mask info. |
| Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 829 | Ops.push_back(MachineOperand::CreateRegMask( |
| 830 | TRI.getCallPreservedMask(*FuncInfo.MF, CC))); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 831 | |
| 832 | // Add scratch registers as implicit def and early clobber. |
| 833 | const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC); |
| 834 | for (unsigned i = 0; ScratchRegs[i]; ++i) |
| 835 | Ops.push_back(MachineOperand::CreateReg( |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 836 | ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false, |
| 837 | /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true)); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 838 | |
| 839 | // Add implicit defs (return values). |
| 840 | for (auto Reg : CLI.InRegs) |
| 841 | Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/true, |
| 842 | /*IsImpl=*/true)); |
| 843 | |
| Juergen Ributzka | 718bb71 | 2014-07-15 02:22:46 +0000 | [diff] [blame] | 844 | // Insert the patchpoint instruction before the call generated by the target. |
| 845 | MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, CLI.Call, DbgLoc, |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 846 | TII.get(TargetOpcode::PATCHPOINT)); |
| 847 | |
| 848 | for (auto &MO : Ops) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 849 | MIB.add(MO); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 850 | |
| 851 | MIB->setPhysRegsDeadExcept(CLI.InRegs, TRI); |
| 852 | |
| 853 | // Delete the original call instruction. |
| 854 | CLI.Call->eraseFromParent(); |
| 855 | |
| 856 | // Inform the Frame Information that we have a patchpoint in this function. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 857 | FuncInfo.MF->getFrameInfo().setHasPatchPoint(); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 858 | |
| Juergen Ributzka | a415943 | 2014-07-15 02:22:43 +0000 | [diff] [blame] | 859 | if (CLI.NumResultRegs) |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 860 | updateValueMap(I, CLI.ResultReg, CLI.NumResultRegs); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 861 | return true; |
| 862 | } |
| 863 | |
| Dean Michael Berris | 9bcaed8 | 2017-05-08 05:45:21 +0000 | [diff] [blame] | 864 | bool FastISel::selectXRayCustomEvent(const CallInst *I) { |
| 865 | const auto &Triple = TM.getTargetTriple(); |
| 866 | if (Triple.getArch() != Triple::x86_64 || !Triple.isOSLinux()) |
| 867 | return true; // don't do anything to this instruction. |
| 868 | SmallVector<MachineOperand, 8> Ops; |
| 869 | Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(0)), |
| 870 | /*IsDef=*/false)); |
| 871 | Ops.push_back(MachineOperand::CreateReg(getRegForValue(I->getArgOperand(1)), |
| 872 | /*IsDef=*/false)); |
| 873 | MachineInstrBuilder MIB = |
| 874 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 875 | TII.get(TargetOpcode::PATCHABLE_EVENT_CALL)); |
| 876 | for (auto &MO : Ops) |
| 877 | MIB.add(MO); |
| 878 | // Insert the Patchable Event Call instruction, that gets lowered properly. |
| 879 | return true; |
| 880 | } |
| 881 | |
| 882 | |
| Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 883 | /// Returns an AttributeList representing the attributes applied to the return |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 884 | /// value of the given call. |
| Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 885 | static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI) { |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 886 | SmallVector<Attribute::AttrKind, 2> Attrs; |
| 887 | if (CLI.RetSExt) |
| 888 | Attrs.push_back(Attribute::SExt); |
| 889 | if (CLI.RetZExt) |
| 890 | Attrs.push_back(Attribute::ZExt); |
| 891 | if (CLI.IsInReg) |
| 892 | Attrs.push_back(Attribute::InReg); |
| 893 | |
| Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 894 | return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex, |
| 895 | Attrs); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 898 | bool FastISel::lowerCallTo(const CallInst *CI, const char *SymName, |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 899 | unsigned NumArgs) { |
| Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 900 | MCContext &Ctx = MF->getContext(); |
| 901 | SmallString<32> MangledName; |
| 902 | Mangler::getNameWithPrefix(MangledName, SymName, DL); |
| 903 | MCSymbol *Sym = Ctx.getOrCreateSymbol(MangledName); |
| 904 | return lowerCallTo(CI, Sym, NumArgs); |
| 905 | } |
| 906 | |
| 907 | bool FastISel::lowerCallTo(const CallInst *CI, MCSymbol *Symbol, |
| 908 | unsigned NumArgs) { |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 909 | ImmutableCallSite CS(CI); |
| 910 | |
| Manuel Jacob | 190577a | 2016-01-17 22:37:39 +0000 | [diff] [blame] | 911 | FunctionType *FTy = CS.getFunctionType(); |
| 912 | Type *RetTy = CS.getType(); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 913 | |
| 914 | ArgListTy Args; |
| 915 | Args.reserve(NumArgs); |
| 916 | |
| 917 | // Populate the argument list. |
| 918 | // Attributes for args start at offset 1, after the return attribute. |
| 919 | for (unsigned ArgI = 0; ArgI != NumArgs; ++ArgI) { |
| 920 | Value *V = CI->getOperand(ArgI); |
| 921 | |
| 922 | assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic."); |
| 923 | |
| 924 | ArgListEntry Entry; |
| 925 | Entry.Val = V; |
| 926 | Entry.Ty = V->getType(); |
| Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 927 | Entry.setAttributes(&CS, ArgI); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 928 | Args.push_back(Entry); |
| 929 | } |
| Nirav Dave | ac6081c | 2017-03-18 00:44:07 +0000 | [diff] [blame] | 930 | TLI.markLibCallAttributes(MF, CS.getCallingConv(), Args); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 931 | |
| 932 | CallLoweringInfo CLI; |
| Rafael Espindola | ce4c2bc | 2015-06-23 12:21:54 +0000 | [diff] [blame] | 933 | CLI.setCallee(RetTy, FTy, Symbol, std::move(Args), CS, NumArgs); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 934 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 935 | return lowerCallTo(CLI); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 938 | bool FastISel::lowerCallTo(CallLoweringInfo &CLI) { |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 939 | // Handle the incoming return values from the call. |
| 940 | CLI.clearIns(); |
| 941 | SmallVector<EVT, 4> RetTys; |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 942 | ComputeValueVTs(TLI, DL, CLI.RetTy, RetTys); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 943 | |
| 944 | SmallVector<ISD::OutputArg, 4> Outs; |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 945 | GetReturnInfo(CLI.RetTy, getReturnAttrs(CLI), Outs, TLI, DL); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 946 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 947 | bool CanLowerReturn = TLI.CanLowerReturn( |
| 948 | CLI.CallConv, *FuncInfo.MF, CLI.IsVarArg, Outs, CLI.RetTy->getContext()); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 949 | |
| 950 | // FIXME: sret demotion isn't supported yet - bail out. |
| 951 | if (!CanLowerReturn) |
| 952 | return false; |
| 953 | |
| 954 | for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { |
| 955 | EVT VT = RetTys[I]; |
| 956 | MVT RegisterVT = TLI.getRegisterType(CLI.RetTy->getContext(), VT); |
| 957 | unsigned NumRegs = TLI.getNumRegisters(CLI.RetTy->getContext(), VT); |
| 958 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 959 | ISD::InputArg MyFlags; |
| 960 | MyFlags.VT = RegisterVT; |
| 961 | MyFlags.ArgVT = VT; |
| 962 | MyFlags.Used = CLI.IsReturnValueUsed; |
| 963 | if (CLI.RetSExt) |
| 964 | MyFlags.Flags.setSExt(); |
| 965 | if (CLI.RetZExt) |
| 966 | MyFlags.Flags.setZExt(); |
| 967 | if (CLI.IsInReg) |
| 968 | MyFlags.Flags.setInReg(); |
| 969 | CLI.Ins.push_back(MyFlags); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // Handle all of the outgoing arguments. |
| 974 | CLI.clearOuts(); |
| 975 | for (auto &Arg : CLI.getArgs()) { |
| 976 | Type *FinalType = Arg.Ty; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 977 | if (Arg.IsByVal) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 978 | FinalType = cast<PointerType>(Arg.Ty)->getElementType(); |
| 979 | bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters( |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 980 | FinalType, CLI.CallConv, CLI.IsVarArg); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 981 | |
| 982 | ISD::ArgFlagsTy Flags; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 983 | if (Arg.IsZExt) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 984 | Flags.setZExt(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 985 | if (Arg.IsSExt) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 986 | Flags.setSExt(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 987 | if (Arg.IsInReg) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 988 | Flags.setInReg(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 989 | if (Arg.IsSRet) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 990 | Flags.setSRet(); |
| Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 991 | if (Arg.IsSwiftSelf) |
| 992 | Flags.setSwiftSelf(); |
| Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 993 | if (Arg.IsSwiftError) |
| 994 | Flags.setSwiftError(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 995 | if (Arg.IsByVal) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 996 | Flags.setByVal(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 997 | if (Arg.IsInAlloca) { |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 998 | Flags.setInAlloca(); |
| 999 | // Set the byval flag for CCAssignFn callbacks that don't know about |
| 1000 | // inalloca. This way we can know how many bytes we should've allocated |
| 1001 | // and how many bytes a callee cleanup function will pop. If we port |
| 1002 | // inalloca to more targets, we'll have to add custom inalloca handling in |
| 1003 | // the various CC lowering callbacks. |
| 1004 | Flags.setByVal(); |
| 1005 | } |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1006 | if (Arg.IsByVal || Arg.IsInAlloca) { |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1007 | PointerType *Ty = cast<PointerType>(Arg.Ty); |
| 1008 | Type *ElementTy = Ty->getElementType(); |
| 1009 | unsigned FrameSize = DL.getTypeAllocSize(ElementTy); |
| 1010 | // For ByVal, alignment should come from FE. BE will guess if this info is |
| 1011 | // not there, but there are cases it cannot get right. |
| 1012 | unsigned FrameAlign = Arg.Alignment; |
| 1013 | if (!FrameAlign) |
| Mehdi Amini | 5c183d5 | 2015-07-09 02:09:28 +0000 | [diff] [blame] | 1014 | FrameAlign = TLI.getByValTypeAlignment(ElementTy, DL); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1015 | Flags.setByValSize(FrameSize); |
| 1016 | Flags.setByValAlign(FrameAlign); |
| 1017 | } |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1018 | if (Arg.IsNest) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1019 | Flags.setNest(); |
| 1020 | if (NeedsRegBlock) |
| 1021 | Flags.setInConsecutiveRegs(); |
| 1022 | unsigned OriginalAlignment = DL.getABITypeAlignment(Arg.Ty); |
| 1023 | Flags.setOrigAlign(OriginalAlignment); |
| 1024 | |
| 1025 | CLI.OutVals.push_back(Arg.Val); |
| 1026 | CLI.OutFlags.push_back(Flags); |
| 1027 | } |
| 1028 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1029 | if (!fastLowerCall(CLI)) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1030 | return false; |
| 1031 | |
| 1032 | // Set all unused physreg defs as dead. |
| 1033 | assert(CLI.Call && "No call instruction specified."); |
| 1034 | CLI.Call->setPhysRegsDeadExcept(CLI.InRegs, TRI); |
| 1035 | |
| 1036 | if (CLI.NumResultRegs && CLI.CS) |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1037 | updateValueMap(CLI.CS->getInstruction(), CLI.ResultReg, CLI.NumResultRegs); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1038 | |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1042 | bool FastISel::lowerCall(const CallInst *CI) { |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1043 | ImmutableCallSite CS(CI); |
| 1044 | |
| Manuel Jacob | 190577a | 2016-01-17 22:37:39 +0000 | [diff] [blame] | 1045 | FunctionType *FuncTy = CS.getFunctionType(); |
| 1046 | Type *RetTy = CS.getType(); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1047 | |
| 1048 | ArgListTy Args; |
| 1049 | ArgListEntry Entry; |
| 1050 | Args.reserve(CS.arg_size()); |
| 1051 | |
| 1052 | for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 1053 | i != e; ++i) { |
| 1054 | Value *V = *i; |
| 1055 | |
| 1056 | // Skip empty types |
| 1057 | if (V->getType()->isEmptyTy()) |
| 1058 | continue; |
| 1059 | |
| 1060 | Entry.Val = V; |
| 1061 | Entry.Ty = V->getType(); |
| 1062 | |
| 1063 | // Skip the first return-type Attribute to get to params. |
| Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 1064 | Entry.setAttributes(&CS, i - CS.arg_begin()); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1065 | Args.push_back(Entry); |
| 1066 | } |
| 1067 | |
| 1068 | // Check if target-independent constraints permit a tail call here. |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1069 | // Target-dependent constraints are checked within fastLowerCall. |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1070 | bool IsTailCall = CI->isTailCall(); |
| Juergen Ributzka | 480872b | 2014-07-16 00:01:22 +0000 | [diff] [blame] | 1071 | if (IsTailCall && !isInTailCallPosition(CS, TM)) |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1072 | IsTailCall = false; |
| 1073 | |
| 1074 | CallLoweringInfo CLI; |
| 1075 | CLI.setCallee(RetTy, FuncTy, CI->getCalledValue(), std::move(Args), CS) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1076 | .setTailCall(IsTailCall); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1077 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1078 | return lowerCallTo(CLI); |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1081 | bool FastISel::selectCall(const User *I) { |
| Dan Gohman | 7da91ae | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 1082 | const CallInst *Call = cast<CallInst>(I); |
| 1083 | |
| 1084 | // Handle simple inline asms. |
| Dan Gohman | de239d2 | 2011-10-12 15:56:56 +0000 | [diff] [blame] | 1085 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) { |
| Juergen Ributzka | 618ce3e | 2014-07-16 22:20:51 +0000 | [diff] [blame] | 1086 | // If the inline asm has side effects, then make sure that no local value |
| 1087 | // lives across by flushing the local value map. |
| 1088 | if (IA->hasSideEffects()) |
| 1089 | flushLocalValueMap(); |
| 1090 | |
| Dan Gohman | 7da91ae | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 1091 | // Don't attempt to handle constraints. |
| 1092 | if (!IA->getConstraintString().empty()) |
| 1093 | return false; |
| 1094 | |
| 1095 | unsigned ExtraInfo = 0; |
| 1096 | if (IA->hasSideEffects()) |
| 1097 | ExtraInfo |= InlineAsm::Extra_HasSideEffects; |
| 1098 | if (IA->isAlignStack()) |
| 1099 | ExtraInfo |= InlineAsm::Extra_IsAlignStack; |
| 1100 | |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1101 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| Dan Gohman | 7da91ae | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 1102 | TII.get(TargetOpcode::INLINEASM)) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1103 | .addExternalSymbol(IA->getAsmString().c_str()) |
| 1104 | .addImm(ExtraInfo); |
| Dan Gohman | 7da91ae | 2011-04-26 17:18:34 +0000 | [diff] [blame] | 1105 | return true; |
| 1106 | } |
| 1107 | |
| Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 1108 | MachineModuleInfo &MMI = FuncInfo.MF->getMMI(); |
| Ahmed Bougacha | bd6ce9a | 2016-11-16 22:25:03 +0000 | [diff] [blame] | 1109 | computeUsesVAFloatArgument(*Call, MMI); |
| Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 1110 | |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1111 | // Handle intrinsic function calls. |
| 1112 | if (const auto *II = dyn_cast<IntrinsicInst>(Call)) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1113 | return selectIntrinsicCall(II); |
| Dan Gohman | 32a733e | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 1114 | |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1115 | // Usually, it does not make sense to initialize a value, |
| 1116 | // make an unrelated function call and use the value, because |
| 1117 | // it tends to be spilled on the stack. So, we move the pointer |
| 1118 | // to the last local value to the beginning of the block, so that |
| 1119 | // all the values which have already been materialized, |
| 1120 | // appear after the call. It also makes sense to skip intrinsics |
| 1121 | // since they tend to be inlined. |
| 1122 | flushLocalValueMap(); |
| 1123 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1124 | return lowerCall(Call); |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1127 | bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) { |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1128 | switch (II->getIntrinsicID()) { |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1129 | default: |
| 1130 | break; |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1131 | // At -O0 we don't care about the lifetime intrinsics. |
| Eric Christopher | 81e2bf2 | 2012-02-17 23:03:39 +0000 | [diff] [blame] | 1132 | case Intrinsic::lifetime_start: |
| 1133 | case Intrinsic::lifetime_end: |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1134 | // The donothing intrinsic does, well, nothing. |
| Chad Rosier | 88d53ea | 2012-07-06 17:33:39 +0000 | [diff] [blame] | 1135 | case Intrinsic::donothing: |
| Ahmed Bougacha | 29333c9 | 2016-07-22 12:54:53 +0000 | [diff] [blame] | 1136 | // Neither does the assume intrinsic; it's also OK not to codegen its operand. |
| 1137 | case Intrinsic::assume: |
| Eric Christopher | 81e2bf2 | 2012-02-17 23:03:39 +0000 | [diff] [blame] | 1138 | return true; |
| Bill Wendling | 65c0fd4 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 1139 | case Intrinsic::dbg_declare: { |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1140 | const DbgDeclareInst *DI = cast<DbgDeclareInst>(II); |
| Duncan P. N. Exon Smith | d4a19a3 | 2015-04-21 18:24:23 +0000 | [diff] [blame] | 1141 | assert(DI->getVariable() && "Missing variable"); |
| 1142 | if (!FuncInfo.MF->getMMI().hasDebugInfo()) { |
| Eric Christopher | 142820b | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 1143 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
| Devang Patel | 8712771 | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 1144 | return true; |
| Eric Christopher | 142820b | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 1145 | } |
| Devang Patel | 8712771 | 2009-07-02 22:43:26 +0000 | [diff] [blame] | 1146 | |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1147 | const Value *Address = DI->getAddress(); |
| Eric Christopher | 3390a6e | 2012-03-15 21:33:47 +0000 | [diff] [blame] | 1148 | if (!Address || isa<UndefValue>(Address)) { |
| Eric Christopher | 142820b | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 1149 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
| Dale Johannesen | db2eb47 | 2010-02-06 02:26:02 +0000 | [diff] [blame] | 1150 | return true; |
| Eric Christopher | 142820b | 2012-03-15 21:33:44 +0000 | [diff] [blame] | 1151 | } |
| Devang Patel | e4682fa | 2010-09-14 20:29:31 +0000 | [diff] [blame] | 1152 | |
| Reid Kleckner | 3a363ff | 2017-05-09 16:02:20 +0000 | [diff] [blame] | 1153 | // Byval arguments with frame indices were already handled after argument |
| 1154 | // lowering and before isel. |
| 1155 | const auto *Arg = |
| 1156 | dyn_cast<Argument>(Address->stripInBoundsConstantOffsets()); |
| 1157 | if (Arg && FuncInfo.getArgumentFrameIndex(Arg) != INT_MAX) |
| 1158 | return true; |
| 1159 | |
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1160 | Optional<MachineOperand> Op; |
| Reid Kleckner | 3a363ff | 2017-05-09 16:02:20 +0000 | [diff] [blame] | 1161 | if (unsigned Reg = lookUpRegForValue(Address)) |
| 1162 | Op = MachineOperand::CreateReg(Reg, false); |
| Eric Christopher | 60e01c5 | 2012-03-20 01:07:58 +0000 | [diff] [blame] | 1163 | |
| Bill Wendling | 9f829f1 | 2012-03-30 00:02:55 +0000 | [diff] [blame] | 1164 | // If we have a VLA that has a "use" in a metadata node that's then used |
| 1165 | // here but it has no other uses, then we have a problem. E.g., |
| 1166 | // |
| 1167 | // int foo (const int *x) { |
| 1168 | // char a[*x]; |
| 1169 | // return 0; |
| 1170 | // } |
| 1171 | // |
| 1172 | // If we assign 'a' a vreg and fast isel later on has to use the selection |
| 1173 | // DAG isel, it will want to copy the value to the vreg. However, there are |
| 1174 | // no uses, which goes counter to what selection DAG isel expects. |
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1175 | if (!Op && !Address->use_empty() && isa<Instruction>(Address) && |
| Eric Christopher | 60e01c5 | 2012-03-20 01:07:58 +0000 | [diff] [blame] | 1176 | (!isa<AllocaInst>(Address) || |
| 1177 | !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address)))) |
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1178 | Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address), |
| Adrian Prantl | 262bcf4 | 2013-09-18 22:08:59 +0000 | [diff] [blame] | 1179 | false); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1180 | |
| Adrian Prantl | 262bcf4 | 2013-09-18 22:08:59 +0000 | [diff] [blame] | 1181 | if (Op) { |
| Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 1182 | assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) && |
| 1183 | "Expected inlined-at fields to agree"); |
| Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 1184 | if (Op->isReg()) { |
| 1185 | Op->setIsDebug(true); |
| Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 1186 | // A dbg.declare describes the address of a source variable, so lower it |
| 1187 | // into an indirect DBG_VALUE. |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1188 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 1189 | TII.get(TargetOpcode::DBG_VALUE), /*IsIndirect*/ true, |
| 1190 | Op->getReg(), 0, DI->getVariable(), DI->getExpression()); |
| David Blaikie | 6004dbc | 2013-10-14 20:15:04 +0000 | [diff] [blame] | 1191 | } else |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1192 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| David Blaikie | 6004dbc | 2013-10-14 20:15:04 +0000 | [diff] [blame] | 1193 | TII.get(TargetOpcode::DBG_VALUE)) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1194 | .add(*Op) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1195 | .addImm(0) |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1196 | .addMetadata(DI->getVariable()) |
| 1197 | .addMetadata(DI->getExpression()); |
| Adrian Prantl | 262bcf4 | 2013-09-18 22:08:59 +0000 | [diff] [blame] | 1198 | } else { |
| Eric Christopher | e5e54c8 | 2012-03-20 01:07:53 +0000 | [diff] [blame] | 1199 | // We can't yet handle anything else here because it would require |
| 1200 | // generating code, thus altering codegen because of debug info. |
| Adrian Prantl | 0d1e559 | 2013-05-22 18:02:19 +0000 | [diff] [blame] | 1201 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
| Adrian Prantl | 262bcf4 | 2013-09-18 22:08:59 +0000 | [diff] [blame] | 1202 | } |
| Dan Gohman | 32a733e | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 1203 | return true; |
| Bill Wendling | 65c0fd4 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 1204 | } |
| Dale Johannesen | dd33104 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 1205 | case Intrinsic::dbg_value: { |
| Dale Johannesen | 5d7f0a0 | 2010-04-07 01:15:14 +0000 | [diff] [blame] | 1206 | // This form of DBG_VALUE is target-independent. |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1207 | const DbgValueInst *DI = cast<DbgValueInst>(II); |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1208 | const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE); |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1209 | const Value *V = DI->getValue(); |
| Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 1210 | assert(DI->getVariable()->isValidLocationForIntrinsic(DbgLoc) && |
| 1211 | "Expected inlined-at fields to agree"); |
| Dale Johannesen | dd33104 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 1212 | if (!V) { |
| 1213 | // Currently the optimizer can produce this; insert an undef to |
| 1214 | // help debugging. Probably the optimizer should not do this. |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1215 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1216 | .addReg(0U) |
| 1217 | .addImm(DI->getOffset()) |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1218 | .addMetadata(DI->getVariable()) |
| 1219 | .addMetadata(DI->getExpression()); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1220 | } else if (const auto *CI = dyn_cast<ConstantInt>(V)) { |
| Devang Patel | f071d72 | 2011-06-24 20:46:11 +0000 | [diff] [blame] | 1221 | if (CI->getBitWidth() > 64) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1222 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1223 | .addCImm(CI) |
| 1224 | .addImm(DI->getOffset()) |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1225 | .addMetadata(DI->getVariable()) |
| 1226 | .addMetadata(DI->getExpression()); |
| Chad Rosier | 879c34f | 2012-07-06 17:44:22 +0000 | [diff] [blame] | 1227 | else |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1228 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1229 | .addImm(CI->getZExtValue()) |
| 1230 | .addImm(DI->getOffset()) |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1231 | .addMetadata(DI->getVariable()) |
| 1232 | .addMetadata(DI->getExpression()); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1233 | } else if (const auto *CF = dyn_cast<ConstantFP>(V)) { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1234 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1235 | .addFPImm(CF) |
| 1236 | .addImm(DI->getOffset()) |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1237 | .addMetadata(DI->getVariable()) |
| 1238 | .addMetadata(DI->getExpression()); |
| Dale Johannesen | dd33104 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 1239 | } else if (unsigned Reg = lookUpRegForValue(V)) { |
| Adrian Prantl | db3e26d | 2013-09-16 23:29:03 +0000 | [diff] [blame] | 1240 | // FIXME: This does not handle register-indirect values at offset 0. |
| Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 1241 | bool IsIndirect = DI->getOffset() != 0; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1242 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect, Reg, |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1243 | DI->getOffset(), DI->getVariable(), DI->getExpression()); |
| Dale Johannesen | dd33104 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 1244 | } else { |
| 1245 | // We can't yet handle anything else here because it would require |
| 1246 | // generating code, thus altering codegen because of debug info. |
| Adrian Prantl | 0d1e559 | 2013-05-22 18:02:19 +0000 | [diff] [blame] | 1247 | DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n"); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1248 | } |
| Dale Johannesen | dd33104 | 2010-02-26 20:01:55 +0000 | [diff] [blame] | 1249 | return true; |
| 1250 | } |
| Eli Friedman | 8f1e11c | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 1251 | case Intrinsic::objectsize: { |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1252 | ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1)); |
| Eli Friedman | 8f1e11c | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 1253 | unsigned long long Res = CI->isZero() ? -1ULL : 0; |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1254 | Constant *ResCI = ConstantInt::get(II->getType(), Res); |
| Eli Friedman | 8f1e11c | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 1255 | unsigned ResultReg = getRegForValue(ResCI); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1256 | if (!ResultReg) |
| Eli Friedman | 8f1e11c | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 1257 | return false; |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1258 | updateValueMap(II, ResultReg); |
| Eli Friedman | 8f1e11c | 2011-05-14 00:47:51 +0000 | [diff] [blame] | 1259 | return true; |
| 1260 | } |
| Richard Smith | 857efb0 | 2016-11-07 16:47:20 +0000 | [diff] [blame] | 1261 | case Intrinsic::invariant_group_barrier: |
| Chad Rosier | 9c1796f | 2013-03-07 20:42:17 +0000 | [diff] [blame] | 1262 | case Intrinsic::expect: { |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1263 | unsigned ResultReg = getRegForValue(II->getArgOperand(0)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1264 | if (!ResultReg) |
| Nick Lewycky | 48beb21 | 2013-03-11 21:44:37 +0000 | [diff] [blame] | 1265 | return false; |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1266 | updateValueMap(II, ResultReg); |
| Chad Rosier | 3a200e1 | 2013-03-07 21:38:33 +0000 | [diff] [blame] | 1267 | return true; |
| Chad Rosier | 9c1796f | 2013-03-07 20:42:17 +0000 | [diff] [blame] | 1268 | } |
| Juergen Ributzka | 190305b | 2014-07-01 22:25:49 +0000 | [diff] [blame] | 1269 | case Intrinsic::experimental_stackmap: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1270 | return selectStackmap(II); |
| Juergen Ributzka | 3d9e675 | 2014-07-11 22:19:02 +0000 | [diff] [blame] | 1271 | case Intrinsic::experimental_patchpoint_void: |
| 1272 | case Intrinsic::experimental_patchpoint_i64: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1273 | return selectPatchpoint(II); |
| Dean Michael Berris | 9bcaed8 | 2017-05-08 05:45:21 +0000 | [diff] [blame] | 1274 | |
| 1275 | case Intrinsic::xray_customevent: |
| 1276 | return selectXRayCustomEvent(II); |
| Dan Gohman | 32a733e | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 1277 | } |
| Dan Gohman | 8a2dae5 | 2010-04-13 17:07:06 +0000 | [diff] [blame] | 1278 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1279 | return fastLowerIntrinsicCall(II); |
| Dan Gohman | 32a733e | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1282 | bool FastISel::selectCast(const User *I, unsigned Opcode) { |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1283 | EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); |
| 1284 | EVT DstVT = TLI.getValueType(DL, I->getType()); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1285 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1286 | if (SrcVT == MVT::Other || !SrcVT.isSimple() || DstVT == MVT::Other || |
| 1287 | !DstVT.isSimple()) |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1288 | // Unhandled type. Halt "fast" selection and bail. |
| 1289 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1290 | |
| Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 1291 | // Check if the destination type is legal. |
| Dan Gohman | a62e4ab | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 1292 | if (!TLI.isTypeLegal(DstVT)) |
| Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 1293 | return false; |
| Dan Gohman | a62e4ab | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 1294 | |
| Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 1295 | // Check if the source operand is legal. |
| Dan Gohman | a62e4ab | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 1296 | if (!TLI.isTypeLegal(SrcVT)) |
| Eli Friedman | c703551 | 2011-05-25 23:49:02 +0000 | [diff] [blame] | 1297 | return false; |
| Dan Gohman | a62e4ab | 2009-03-13 23:53:06 +0000 | [diff] [blame] | 1298 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1299 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1300 | if (!InputReg) |
| 1301 | // Unhandled operand. Halt "fast" selection and bail. |
| 1302 | return false; |
| Dan Gohman | c0bb959 | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 1303 | |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1304 | bool InputRegIsKill = hasTrivialKill(I->getOperand(0)); |
| 1305 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1306 | unsigned ResultReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1307 | Opcode, InputReg, InputRegIsKill); |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1308 | if (!ResultReg) |
| 1309 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1310 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1311 | updateValueMap(I, ResultReg); |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1312 | return true; |
| 1313 | } |
| 1314 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1315 | bool FastISel::selectBitCast(const User *I) { |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 1316 | // If the bitcast doesn't change the type, just use the operand value. |
| 1317 | if (I->getType() == I->getOperand(0)->getType()) { |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1318 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1319 | if (!Reg) |
| Dan Gohman | 61cfa30 | 2008-08-27 20:41:38 +0000 | [diff] [blame] | 1320 | return false; |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1321 | updateValueMap(I, Reg); |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 1322 | return true; |
| 1323 | } |
| 1324 | |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1325 | // Bitcasts of other values become reg-reg copies or BITCAST operators. |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1326 | EVT SrcEVT = TLI.getValueType(DL, I->getOperand(0)->getType()); |
| 1327 | EVT DstEVT = TLI.getValueType(DL, I->getType()); |
| Patrik Hagglund | c494d24 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 1328 | if (SrcEVT == MVT::Other || DstEVT == MVT::Other || |
| 1329 | !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT)) |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1330 | // Unhandled type. Halt "fast" selection and bail. |
| 1331 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1332 | |
| Patrik Hagglund | c494d24 | 2012-12-17 14:30:06 +0000 | [diff] [blame] | 1333 | MVT SrcVT = SrcEVT.getSimpleVT(); |
| 1334 | MVT DstVT = DstEVT.getSimpleVT(); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1335 | unsigned Op0 = getRegForValue(I->getOperand(0)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1336 | if (!Op0) // Unhandled operand. Halt "fast" selection and bail. |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1337 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1338 | bool Op0IsKill = hasTrivialKill(I->getOperand(0)); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1339 | |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 1340 | // First, try to perform the bitcast by inserting a reg-reg copy. |
| 1341 | unsigned ResultReg = 0; |
| Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 1342 | if (SrcVT == DstVT) { |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1343 | const TargetRegisterClass *SrcClass = TLI.getRegClassFor(SrcVT); |
| 1344 | const TargetRegisterClass *DstClass = TLI.getRegClassFor(DstVT); |
| Jakob Stoklund Olesen | 51642ae | 2010-07-11 05:16:54 +0000 | [diff] [blame] | 1345 | // Don't attempt a cross-class copy. It will likely fail. |
| 1346 | if (SrcClass == DstClass) { |
| 1347 | ResultReg = createResultReg(DstClass); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1348 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1349 | TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0); |
| Jakob Stoklund Olesen | 51642ae | 2010-07-11 05:16:54 +0000 | [diff] [blame] | 1350 | } |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 1351 | } |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1352 | |
| 1353 | // If the reg-reg copy failed, select a BITCAST opcode. |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 1354 | if (!ResultReg) |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1355 | ResultReg = fastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1356 | |
| Dan Gohman | b0b5a27 | 2008-08-27 18:10:19 +0000 | [diff] [blame] | 1357 | if (!ResultReg) |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1358 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1359 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1360 | updateValueMap(I, ResultReg); |
| Owen Anderson | ca1711a | 2008-08-26 23:46:32 +0000 | [diff] [blame] | 1361 | return true; |
| 1362 | } |
| 1363 | |
| Paul Robinson | accc3e0 | 2015-12-14 18:33:18 +0000 | [diff] [blame] | 1364 | // Remove local value instructions starting from the instruction after |
| 1365 | // SavedLastLocalValue to the current function insert point. |
| 1366 | void FastISel::removeDeadLocalValueCode(MachineInstr *SavedLastLocalValue) |
| 1367 | { |
| 1368 | MachineInstr *CurLastLocalValue = getLastLocalValue(); |
| 1369 | if (CurLastLocalValue != SavedLastLocalValue) { |
| 1370 | // Find the first local value instruction to be deleted. |
| 1371 | // This is the instruction after SavedLastLocalValue if it is non-NULL. |
| 1372 | // Otherwise it's the first instruction in the block. |
| 1373 | MachineBasicBlock::iterator FirstDeadInst(SavedLastLocalValue); |
| 1374 | if (SavedLastLocalValue) |
| 1375 | ++FirstDeadInst; |
| 1376 | else |
| 1377 | FirstDeadInst = FuncInfo.MBB->getFirstNonPHI(); |
| 1378 | setLastLocalValue(SavedLastLocalValue); |
| 1379 | removeDeadCode(FirstDeadInst, FuncInfo.InsertPt); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1383 | bool FastISel::selectInstruction(const Instruction *I) { |
| Paul Robinson | accc3e0 | 2015-12-14 18:33:18 +0000 | [diff] [blame] | 1384 | MachineInstr *SavedLastLocalValue = getLastLocalValue(); |
| Dan Gohman | 6e9a8fc | 2010-04-23 15:29:50 +0000 | [diff] [blame] | 1385 | // Just before the terminator instruction, insert instructions to |
| 1386 | // feed PHI nodes in successor blocks. |
| Manman Ren | e221a87 | 2016-04-05 18:13:16 +0000 | [diff] [blame] | 1387 | if (isa<TerminatorInst>(I)) { |
| Paul Robinson | accc3e0 | 2015-12-14 18:33:18 +0000 | [diff] [blame] | 1388 | if (!handlePHINodesInSuccessorBlocks(I->getParent())) { |
| 1389 | // PHI node handling may have generated local value instructions, |
| 1390 | // even though it failed to handle all PHI nodes. |
| 1391 | // We remove these instructions because SelectionDAGISel will generate |
| 1392 | // them again. |
| 1393 | removeDeadLocalValueCode(SavedLastLocalValue); |
| Dan Gohman | 6e9a8fc | 2010-04-23 15:29:50 +0000 | [diff] [blame] | 1394 | return false; |
| Paul Robinson | accc3e0 | 2015-12-14 18:33:18 +0000 | [diff] [blame] | 1395 | } |
| Manman Ren | e221a87 | 2016-04-05 18:13:16 +0000 | [diff] [blame] | 1396 | } |
| Dan Gohman | 6e9a8fc | 2010-04-23 15:29:50 +0000 | [diff] [blame] | 1397 | |
| Sanjoy Das | 38bfc22 | 2016-03-22 00:59:13 +0000 | [diff] [blame] | 1398 | // FastISel does not handle any operand bundles except OB_funclet. |
| 1399 | if (ImmutableCallSite CS = ImmutableCallSite(I)) |
| 1400 | for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) |
| 1401 | if (CS.getOperandBundleAt(i).getTagID() != LLVMContext::OB_funclet) |
| 1402 | return false; |
| 1403 | |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1404 | DbgLoc = I->getDebugLoc(); |
| Dan Gohman | e450d74 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 1405 | |
| Hans Wennborg | 18f0a98 | 2014-09-08 20:24:10 +0000 | [diff] [blame] | 1406 | SavedInsertPt = FuncInfo.InsertPt; |
| Chad Rosier | 46addb9 | 2011-11-29 19:40:47 +0000 | [diff] [blame] | 1407 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1408 | if (const auto *Call = dyn_cast<CallInst>(I)) { |
| Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 1409 | const Function *F = Call->getCalledFunction(); |
| David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 1410 | LibFunc Func; |
| Akira Hatanaka | 3d90f99 | 2014-04-15 21:30:06 +0000 | [diff] [blame] | 1411 | |
| 1412 | // As a special case, don't handle calls to builtin library functions that |
| 1413 | // may be translated directly to target instructions. |
| Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 1414 | if (F && !F->hasLocalLinkage() && F->hasName() && |
| 1415 | LibInfo->getLibFunc(F->getName(), Func) && |
| Bob Wilson | 871701c | 2012-08-03 21:26:24 +0000 | [diff] [blame] | 1416 | LibInfo->hasOptimizedCodeGen(Func)) |
| Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 1417 | return false; |
| Akira Hatanaka | 3d90f99 | 2014-04-15 21:30:06 +0000 | [diff] [blame] | 1418 | |
| Wolfgang Pieb | 60b7ca6 | 2015-12-16 00:08:18 +0000 | [diff] [blame] | 1419 | // Don't handle Intrinsic::trap if a trap function is specified. |
| Akira Hatanaka | 3d90f99 | 2014-04-15 21:30:06 +0000 | [diff] [blame] | 1420 | if (F && F->getIntrinsicID() == Intrinsic::trap && |
| Akira Hatanaka | 56c7044 | 2015-07-02 22:13:27 +0000 | [diff] [blame] | 1421 | Call->hasFnAttr("trap-func-name")) |
| Akira Hatanaka | 3d90f99 | 2014-04-15 21:30:06 +0000 | [diff] [blame] | 1422 | return false; |
| Bob Wilson | 3e6fa46 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| Dan Gohman | 18f9446 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 1425 | // First, try doing target-independent selection. |
| Juergen Ributzka | 7e998fb | 2014-09-02 21:07:44 +0000 | [diff] [blame] | 1426 | if (!SkipTargetIndependentISel) { |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1427 | if (selectOperator(I, I->getOpcode())) { |
| Juergen Ributzka | 7e998fb | 2014-09-02 21:07:44 +0000 | [diff] [blame] | 1428 | ++NumFastIselSuccessIndependent; |
| 1429 | DbgLoc = DebugLoc(); |
| 1430 | return true; |
| 1431 | } |
| Hans Wennborg | 18f0a98 | 2014-09-08 20:24:10 +0000 | [diff] [blame] | 1432 | // Remove dead code. |
| 1433 | recomputeInsertPt(); |
| 1434 | if (SavedInsertPt != FuncInfo.InsertPt) |
| 1435 | removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); |
| Juergen Ributzka | 7e998fb | 2014-09-02 21:07:44 +0000 | [diff] [blame] | 1436 | SavedInsertPt = FuncInfo.InsertPt; |
| 1437 | } |
| 1438 | // Next, try calling the target to attempt to handle the instruction. |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1439 | if (fastSelectInstruction(I)) { |
| Juergen Ributzka | 7e998fb | 2014-09-02 21:07:44 +0000 | [diff] [blame] | 1440 | ++NumFastIselSuccessTarget; |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1441 | DbgLoc = DebugLoc(); |
| Dan Gohman | 18f9446 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 1442 | return true; |
| Dan Gohman | e450d74 | 2010-04-20 00:48:35 +0000 | [diff] [blame] | 1443 | } |
| Hans Wennborg | 18f0a98 | 2014-09-08 20:24:10 +0000 | [diff] [blame] | 1444 | // Remove dead code. |
| 1445 | recomputeInsertPt(); |
| 1446 | if (SavedInsertPt != FuncInfo.InsertPt) |
| 1447 | removeDeadCode(FuncInfo.InsertPt, SavedInsertPt); |
| Dan Gohman | 18f9446 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 1448 | |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1449 | DbgLoc = DebugLoc(); |
| Juergen Ributzka | 3132816 | 2014-08-28 02:06:55 +0000 | [diff] [blame] | 1450 | // Undo phi node updates, because they will be added again by SelectionDAG. |
| Paul Robinson | accc3e0 | 2015-12-14 18:33:18 +0000 | [diff] [blame] | 1451 | if (isa<TerminatorInst>(I)) { |
| 1452 | // PHI node handling may have generated local value instructions. |
| 1453 | // We remove them because SelectionDAGISel will generate them again. |
| 1454 | removeDeadLocalValueCode(SavedLastLocalValue); |
| Juergen Ributzka | 3132816 | 2014-08-28 02:06:55 +0000 | [diff] [blame] | 1455 | FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate); |
| Paul Robinson | accc3e0 | 2015-12-14 18:33:18 +0000 | [diff] [blame] | 1456 | } |
| Dan Gohman | 18f9446 | 2009-12-05 01:27:58 +0000 | [diff] [blame] | 1457 | return false; |
| Dan Gohman | fcf5456 | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1460 | /// Emit an unconditional branch to the given block, unless it is the immediate |
| 1461 | /// (fall-through) successor, and update the CFG. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 1462 | void FastISel::fastEmitBranch(MachineBasicBlock *MSucc, |
| 1463 | const DebugLoc &DbgLoc) { |
| Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 1464 | if (FuncInfo.MBB->getBasicBlock()->size() > 1 && |
| 1465 | FuncInfo.MBB->isLayoutSuccessor(MSucc)) { |
| Eric Christopher | e9abba7 | 2012-04-10 18:18:10 +0000 | [diff] [blame] | 1466 | // For more accurate line information if this is the only instruction |
| 1467 | // in the block then emit it, otherwise we have the unconditional |
| 1468 | // fall-through case, which needs no instructions. |
| Dan Gohman | 1ab1d31 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 1469 | } else { |
| 1470 | // The unconditional branch case. |
| Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1471 | TII.insertBranch(*FuncInfo.MBB, MSucc, nullptr, |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1472 | SmallVector<MachineOperand, 0>(), DbgLoc); |
| Dan Gohman | 1ab1d31 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 1473 | } |
| Cong Hou | 07eeb80 | 2015-10-27 17:59:36 +0000 | [diff] [blame] | 1474 | if (FuncInfo.BPI) { |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 1475 | auto BranchProbability = FuncInfo.BPI->getEdgeProbability( |
| Cong Hou | 07eeb80 | 2015-10-27 17:59:36 +0000 | [diff] [blame] | 1476 | FuncInfo.MBB->getBasicBlock(), MSucc->getBasicBlock()); |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 1477 | FuncInfo.MBB->addSuccessor(MSucc, BranchProbability); |
| Cong Hou | 07eeb80 | 2015-10-27 17:59:36 +0000 | [diff] [blame] | 1478 | } else |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 1479 | FuncInfo.MBB->addSuccessorWithoutProb(MSucc); |
| Dan Gohman | 1ab1d31 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| Matthias Braun | 17af607 | 2015-08-26 01:38:00 +0000 | [diff] [blame] | 1482 | void FastISel::finishCondBranch(const BasicBlock *BranchBB, |
| 1483 | MachineBasicBlock *TrueMBB, |
| 1484 | MachineBasicBlock *FalseMBB) { |
| Matthias Braun | 4816b18 | 2015-08-26 20:46:49 +0000 | [diff] [blame] | 1485 | // Add TrueMBB as successor unless it is equal to the FalseMBB: This can |
| 1486 | // happen in degenerate IR and MachineIR forbids to have a block twice in the |
| 1487 | // successor/predecessor lists. |
| Cong Hou | 07eeb80 | 2015-10-27 17:59:36 +0000 | [diff] [blame] | 1488 | if (TrueMBB != FalseMBB) { |
| 1489 | if (FuncInfo.BPI) { |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 1490 | auto BranchProbability = |
| 1491 | FuncInfo.BPI->getEdgeProbability(BranchBB, TrueMBB->getBasicBlock()); |
| 1492 | FuncInfo.MBB->addSuccessor(TrueMBB, BranchProbability); |
| Cong Hou | 07eeb80 | 2015-10-27 17:59:36 +0000 | [diff] [blame] | 1493 | } else |
| Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 1494 | FuncInfo.MBB->addSuccessorWithoutProb(TrueMBB); |
| Cong Hou | 07eeb80 | 2015-10-27 17:59:36 +0000 | [diff] [blame] | 1495 | } |
| Matthias Braun | 17af607 | 2015-08-26 01:38:00 +0000 | [diff] [blame] | 1496 | |
| 1497 | fastEmitBranch(FalseMBB, DbgLoc); |
| 1498 | } |
| 1499 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1500 | /// Emit an FNeg operation. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1501 | bool FastISel::selectFNeg(const User *I) { |
| Dan Gohman | aa92dc1 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 1502 | unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1503 | if (!OpReg) |
| 1504 | return false; |
| Dan Gohman | 1a1b51f | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 1505 | bool OpRegIsKill = hasTrivialKill(I); |
| 1506 | |
| Dan Gohman | 9cbef32 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 1507 | // If the target has ISD::FNEG, use it. |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1508 | EVT VT = TLI.getValueType(DL, I->getType()); |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1509 | unsigned ResultReg = fastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), ISD::FNEG, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1510 | OpReg, OpRegIsKill); |
| 1511 | if (ResultReg) { |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1512 | updateValueMap(I, ResultReg); |
| Dan Gohman | 9cbef32 | 2009-09-11 00:36:43 +0000 | [diff] [blame] | 1513 | return true; |
| 1514 | } |
| 1515 | |
| Dan Gohman | 89b090e | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 1516 | // Bitcast the value to integer, twiddle the sign bit with xor, |
| 1517 | // and then bitcast it back to floating-point. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1518 | if (VT.getSizeInBits() > 64) |
| 1519 | return false; |
| Dan Gohman | 89b090e | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 1520 | EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits()); |
| 1521 | if (!TLI.isTypeLegal(IntVT)) |
| 1522 | return false; |
| 1523 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1524 | unsigned IntReg = fastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(), |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1525 | ISD::BITCAST, OpReg, OpRegIsKill); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1526 | if (!IntReg) |
| Dan Gohman | 89b090e | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 1527 | return false; |
| 1528 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1529 | unsigned IntResultReg = fastEmit_ri_( |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1530 | IntVT.getSimpleVT(), ISD::XOR, IntReg, /*IsKill=*/true, |
| 1531 | UINT64_C(1) << (VT.getSizeInBits() - 1), IntVT.getSimpleVT()); |
| 1532 | if (!IntResultReg) |
| Dan Gohman | 89b090e | 2009-09-11 00:34:46 +0000 | [diff] [blame] | 1533 | return false; |
| 1534 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1535 | ResultReg = fastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), ISD::BITCAST, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1536 | IntResultReg, /*IsKill=*/true); |
| 1537 | if (!ResultReg) |
| Dan Gohman | aa92dc1 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 1538 | return false; |
| 1539 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1540 | updateValueMap(I, ResultReg); |
| Dan Gohman | aa92dc1 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 1541 | return true; |
| 1542 | } |
| 1543 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1544 | bool FastISel::selectExtractValue(const User *U) { |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1545 | const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U); |
| Eli Friedman | 4c08bb4 | 2011-05-16 20:34:53 +0000 | [diff] [blame] | 1546 | if (!EVI) |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1547 | return false; |
| 1548 | |
| Eli Friedman | a4d4a01 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 1549 | // Make sure we only try to handle extracts with a legal result. But also |
| 1550 | // allow i1 because it's easy. |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1551 | EVT RealVT = TLI.getValueType(DL, EVI->getType(), /*AllowUnknown=*/true); |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1552 | if (!RealVT.isSimple()) |
| 1553 | return false; |
| 1554 | MVT VT = RealVT.getSimpleVT(); |
| Eli Friedman | a4d4a01 | 2011-05-16 21:06:17 +0000 | [diff] [blame] | 1555 | if (!TLI.isTypeLegal(VT) && VT != MVT::i1) |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1556 | return false; |
| 1557 | |
| 1558 | const Value *Op0 = EVI->getOperand(0); |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1559 | Type *AggTy = Op0->getType(); |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Get the base result register. |
| 1562 | unsigned ResultReg; |
| 1563 | DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0); |
| 1564 | if (I != FuncInfo.ValueMap.end()) |
| 1565 | ResultReg = I->second; |
| Eli Friedman | bd375f1 | 2011-06-06 05:46:34 +0000 | [diff] [blame] | 1566 | else if (isa<Instruction>(Op0)) |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1567 | ResultReg = FuncInfo.InitializeRegForValue(Op0); |
| Eli Friedman | bd375f1 | 2011-06-06 05:46:34 +0000 | [diff] [blame] | 1568 | else |
| 1569 | return false; // fast-isel can't handle aggregate constants at the moment |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1570 | |
| 1571 | // Get the actual result register, which is an offset from the base register. |
| Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1572 | unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices()); |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1573 | |
| 1574 | SmallVector<EVT, 4> AggValueVTs; |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 1575 | ComputeValueVTs(TLI, DL, AggTy, AggValueVTs); |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1576 | |
| 1577 | for (unsigned i = 0; i < VTIndex; i++) |
| 1578 | ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]); |
| 1579 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1580 | updateValueMap(EVI, ResultReg); |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1581 | return true; |
| 1582 | } |
| 1583 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1584 | bool FastISel::selectOperator(const User *I, unsigned Opcode) { |
| Dan Gohman | fcf5456 | 2008-09-05 18:18:20 +0000 | [diff] [blame] | 1585 | switch (Opcode) { |
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1586 | case Instruction::Add: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1587 | return selectBinaryOp(I, ISD::ADD); |
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1588 | case Instruction::FAdd: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1589 | return selectBinaryOp(I, ISD::FADD); |
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1590 | case Instruction::Sub: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1591 | return selectBinaryOp(I, ISD::SUB); |
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1592 | case Instruction::FSub: |
| Dan Gohman | aa92dc1 | 2009-09-03 22:53:57 +0000 | [diff] [blame] | 1593 | // FNeg is currently represented in LLVM IR as a special case of FSub. |
| 1594 | if (BinaryOperator::isFNeg(I)) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1595 | return selectFNeg(I); |
| 1596 | return selectBinaryOp(I, ISD::FSUB); |
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1597 | case Instruction::Mul: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1598 | return selectBinaryOp(I, ISD::MUL); |
| Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1599 | case Instruction::FMul: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1600 | return selectBinaryOp(I, ISD::FMUL); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1601 | case Instruction::SDiv: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1602 | return selectBinaryOp(I, ISD::SDIV); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1603 | case Instruction::UDiv: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1604 | return selectBinaryOp(I, ISD::UDIV); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1605 | case Instruction::FDiv: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1606 | return selectBinaryOp(I, ISD::FDIV); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1607 | case Instruction::SRem: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1608 | return selectBinaryOp(I, ISD::SREM); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1609 | case Instruction::URem: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1610 | return selectBinaryOp(I, ISD::UREM); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1611 | case Instruction::FRem: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1612 | return selectBinaryOp(I, ISD::FREM); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1613 | case Instruction::Shl: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1614 | return selectBinaryOp(I, ISD::SHL); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1615 | case Instruction::LShr: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1616 | return selectBinaryOp(I, ISD::SRL); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1617 | case Instruction::AShr: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1618 | return selectBinaryOp(I, ISD::SRA); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1619 | case Instruction::And: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1620 | return selectBinaryOp(I, ISD::AND); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1621 | case Instruction::Or: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1622 | return selectBinaryOp(I, ISD::OR); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1623 | case Instruction::Xor: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1624 | return selectBinaryOp(I, ISD::XOR); |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1625 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1626 | case Instruction::GetElementPtr: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1627 | return selectGetElementPtr(I); |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 1628 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1629 | case Instruction::Br: { |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1630 | const BranchInst *BI = cast<BranchInst>(I); |
| Dan Gohman | a3e4d5a | 2008-08-20 00:11:48 +0000 | [diff] [blame] | 1631 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1632 | if (BI->isUnconditional()) { |
| Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1633 | const BasicBlock *LLVMSucc = BI->getSuccessor(0); |
| Dan Gohman | 87fb4e8 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1634 | MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc]; |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1635 | fastEmitBranch(MSucc, BI->getDebugLoc()); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1636 | return true; |
| Owen Anderson | 1405492 | 2008-08-27 00:31:01 +0000 | [diff] [blame] | 1637 | } |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1638 | |
| 1639 | // Conditional branches are not handed yet. |
| 1640 | // Halt "fast" selection and bail. |
| 1641 | return false; |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| Dan Gohman | ea56bdd | 2008-09-05 01:08:41 +0000 | [diff] [blame] | 1644 | case Instruction::Unreachable: |
| Reid Kleckner | ae44e87 | 2015-10-09 01:13:17 +0000 | [diff] [blame] | 1645 | if (TM.Options.TrapUnreachable) |
| 1646 | return fastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0; |
| 1647 | else |
| 1648 | return true; |
| Dan Gohman | ea56bdd | 2008-09-05 01:08:41 +0000 | [diff] [blame] | 1649 | |
| Dan Gohman | 39d82f9 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1650 | case Instruction::Alloca: |
| 1651 | // FunctionLowering has the static-sized case covered. |
| Dan Gohman | 87fb4e8 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 1652 | if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I))) |
| Dan Gohman | 39d82f9 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1653 | return true; |
| 1654 | |
| 1655 | // Dynamic-sized alloca is not handled yet. |
| 1656 | return false; |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1657 | |
| Dan Gohman | 32a733e | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 1658 | case Instruction::Call: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1659 | return selectCall(I); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1660 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1661 | case Instruction::BitCast: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1662 | return selectBitCast(I); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1663 | |
| 1664 | case Instruction::FPToSI: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1665 | return selectCast(I, ISD::FP_TO_SINT); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1666 | case Instruction::ZExt: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1667 | return selectCast(I, ISD::ZERO_EXTEND); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1668 | case Instruction::SExt: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1669 | return selectCast(I, ISD::SIGN_EXTEND); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1670 | case Instruction::Trunc: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1671 | return selectCast(I, ISD::TRUNCATE); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1672 | case Instruction::SIToFP: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1673 | return selectCast(I, ISD::SINT_TO_FP); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1674 | |
| 1675 | case Instruction::IntToPtr: // Deliberate fall-through. |
| 1676 | case Instruction::PtrToInt: { |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1677 | EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType()); |
| 1678 | EVT DstVT = TLI.getValueType(DL, I->getType()); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1679 | if (DstVT.bitsGT(SrcVT)) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1680 | return selectCast(I, ISD::ZERO_EXTEND); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1681 | if (DstVT.bitsLT(SrcVT)) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1682 | return selectCast(I, ISD::TRUNCATE); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1683 | unsigned Reg = getRegForValue(I->getOperand(0)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1684 | if (!Reg) |
| 1685 | return false; |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1686 | updateValueMap(I, Reg); |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1687 | return true; |
| 1688 | } |
| Dan Gohman | 918fe08 | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 1689 | |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1690 | case Instruction::ExtractValue: |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1691 | return selectExtractValue(I); |
| Eli Friedman | 9ac9447 | 2011-05-16 20:27:46 +0000 | [diff] [blame] | 1692 | |
| Dan Gohman | f41ad47 | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 1693 | case Instruction::PHI: |
| 1694 | llvm_unreachable("FastISel shouldn't visit PHI nodes!"); |
| 1695 | |
| Dan Gohman | 7bda51f | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1696 | default: |
| 1697 | // Unhandled instruction. Halt "fast" selection and bail. |
| 1698 | return false; |
| 1699 | } |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| Juergen Ributzka | 7e998fb | 2014-09-02 21:07:44 +0000 | [diff] [blame] | 1702 | FastISel::FastISel(FunctionLoweringInfo &FuncInfo, |
| 1703 | const TargetLibraryInfo *LibInfo, |
| 1704 | bool SkipTargetIndependentISel) |
| 1705 | : FuncInfo(FuncInfo), MF(FuncInfo.MF), MRI(FuncInfo.MF->getRegInfo()), |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1706 | MFI(FuncInfo.MF->getFrameInfo()), MCP(*FuncInfo.MF->getConstantPool()), |
| Mehdi Amini | 7da8b53 | 2015-07-07 18:39:02 +0000 | [diff] [blame] | 1707 | TM(FuncInfo.MF->getTarget()), DL(MF->getDataLayout()), |
| Eric Christopher | 4e3d6de | 2014-10-08 23:38:33 +0000 | [diff] [blame] | 1708 | TII(*MF->getSubtarget().getInstrInfo()), |
| 1709 | TLI(*MF->getSubtarget().getTargetLowering()), |
| 1710 | TRI(*MF->getSubtarget().getRegisterInfo()), LibInfo(LibInfo), |
| Juergen Ributzka | 7e998fb | 2014-09-02 21:07:44 +0000 | [diff] [blame] | 1711 | SkipTargetIndependentISel(SkipTargetIndependentISel) {} |
| Dan Gohman | 02c84b8 | 2008-08-20 21:05:57 +0000 | [diff] [blame] | 1712 | |
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1713 | FastISel::~FastISel() = default; |
| Dan Gohman | c444238 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 1714 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1715 | bool FastISel::fastLowerArguments() { return false; } |
| Evan Cheng | 615620c | 2013-02-11 01:27:15 +0000 | [diff] [blame] | 1716 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1717 | bool FastISel::fastLowerCall(CallLoweringInfo & /*CLI*/) { return false; } |
| Juergen Ributzka | 8179e9e | 2014-07-11 22:01:42 +0000 | [diff] [blame] | 1718 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 1719 | bool FastISel::fastLowerIntrinsicCall(const IntrinsicInst * /*II*/) { |
| Juergen Ributzka | 5dd3213 | 2014-07-11 20:42:12 +0000 | [diff] [blame] | 1720 | return false; |
| 1721 | } |
| 1722 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1723 | unsigned FastISel::fastEmit_(MVT, MVT, unsigned) { return 0; } |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1724 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1725 | unsigned FastISel::fastEmit_r(MVT, MVT, unsigned, unsigned /*Op0*/, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1726 | bool /*Op0IsKill*/) { |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1727 | return 0; |
| 1728 | } |
| 1729 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1730 | unsigned FastISel::fastEmit_rr(MVT, MVT, unsigned, unsigned /*Op0*/, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1731 | bool /*Op0IsKill*/, unsigned /*Op1*/, |
| 1732 | bool /*Op1IsKill*/) { |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1733 | return 0; |
| 1734 | } |
| 1735 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1736 | unsigned FastISel::fastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) { |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1740 | unsigned FastISel::fastEmit_f(MVT, MVT, unsigned, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1741 | const ConstantFP * /*FPImm*/) { |
| Dan Gohman | 5ca269e | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 1742 | return 0; |
| 1743 | } |
| 1744 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1745 | unsigned FastISel::fastEmit_ri(MVT, MVT, unsigned, unsigned /*Op0*/, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1746 | bool /*Op0IsKill*/, uint64_t /*Imm*/) { |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1747 | return 0; |
| 1748 | } |
| 1749 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1750 | /// This method is a wrapper of fastEmit_ri. It first tries to emit an |
| 1751 | /// instruction with an immediate operand using fastEmit_ri. |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1752 | /// If that fails, it materializes the immediate into a register and try |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1753 | /// fastEmit_rr instead. |
| 1754 | unsigned FastISel::fastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1755 | bool Op0IsKill, uint64_t Imm, MVT ImmType) { |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1756 | // If this is a multiply by a power of two, emit this as a shift left. |
| 1757 | if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) { |
| 1758 | Opcode = ISD::SHL; |
| 1759 | Imm = Log2_64(Imm); |
| Chris Lattner | 562d6e8 | 2011-04-18 06:55:51 +0000 | [diff] [blame] | 1760 | } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) { |
| 1761 | // div x, 8 -> srl x, 3 |
| 1762 | Opcode = ISD::SRL; |
| 1763 | Imm = Log2_64(Imm); |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1764 | } |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1765 | |
| Chris Lattner | b53ccb8 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 1766 | // Horrible hack (to be removed), check to make sure shift amounts are |
| 1767 | // in-range. |
| 1768 | if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) && |
| 1769 | Imm >= VT.getSizeInBits()) |
| 1770 | return 0; |
| Owen Anderson | dd450b8 | 2011-04-22 23:38:06 +0000 | [diff] [blame] | 1771 | |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1772 | // First check if immediate type is legal. If not, we can't use the ri form. |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1773 | unsigned ResultReg = fastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1774 | if (ResultReg) |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1775 | return ResultReg; |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1776 | unsigned MaterialReg = fastEmit_i(ImmType, ImmType, ISD::Constant, Imm); |
| Pete Cooper | 54085cd | 2015-05-06 22:09:29 +0000 | [diff] [blame] | 1777 | bool IsImmKill = true; |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1778 | if (!MaterialReg) { |
| Eli Friedman | 4105ed1 | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1779 | // This is a bit ugly/slow, but failing here means falling out of |
| 1780 | // fast-isel, which would be very slow. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1781 | IntegerType *ITy = |
| 1782 | IntegerType::get(FuncInfo.Fn->getContext(), VT.getSizeInBits()); |
| Eli Friedman | 4105ed1 | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1783 | MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1784 | if (!MaterialReg) |
| 1785 | return 0; |
| Pete Cooper | d54fb89 | 2015-05-09 00:51:03 +0000 | [diff] [blame] | 1786 | // FIXME: If the materialized register here has no uses yet then this |
| 1787 | // will be the first use and we should be able to mark it as killed. |
| 1788 | // However, the local value area for materialising constant expressions |
| 1789 | // grows down, not up, which means that any constant expressions we generate |
| 1790 | // later which also use 'Imm' could be after this instruction and therefore |
| 1791 | // after this kill. |
| 1792 | IsImmKill = false; |
| Eli Friedman | 4105ed1 | 2011-04-29 23:34:52 +0000 | [diff] [blame] | 1793 | } |
| Pete Cooper | 54085cd | 2015-05-06 22:09:29 +0000 | [diff] [blame] | 1794 | return fastEmit_rr(VT, VT, Opcode, Op0, Op0IsKill, MaterialReg, IsImmKill); |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1797 | unsigned FastISel::createResultReg(const TargetRegisterClass *RC) { |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1798 | return MRI.createVirtualRegister(RC); |
| Evan Cheng | 864fcc1 | 2008-08-20 22:45:34 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1801 | unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II, unsigned Op, |
| 1802 | unsigned OpNum) { |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1803 | if (TargetRegisterInfo::isVirtualRegister(Op)) { |
| 1804 | const TargetRegisterClass *RegClass = |
| 1805 | TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF); |
| 1806 | if (!MRI.constrainRegClass(Op, RegClass)) { |
| 1807 | // If it's not legal to COPY between the register classes, something |
| 1808 | // has gone very wrong before we got here. |
| 1809 | unsigned NewOp = createResultReg(RegClass); |
| 1810 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1811 | TII.get(TargetOpcode::COPY), NewOp).addReg(Op); |
| 1812 | return NewOp; |
| 1813 | } |
| 1814 | } |
| 1815 | return Op; |
| 1816 | } |
| 1817 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1818 | unsigned FastISel::fastEmitInst_(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1819 | const TargetRegisterClass *RC) { |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1820 | unsigned ResultReg = createResultReg(RC); |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1821 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1822 | |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1823 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg); |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1824 | return ResultReg; |
| 1825 | } |
| 1826 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1827 | unsigned FastISel::fastEmitInst_r(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1828 | const TargetRegisterClass *RC, unsigned Op0, |
| 1829 | bool Op0IsKill) { |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1830 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1831 | |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1832 | unsigned ResultReg = createResultReg(RC); |
| 1833 | Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); |
| 1834 | |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1835 | if (II.getNumDefs() >= 1) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1836 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1837 | .addReg(Op0, getKillRegState(Op0IsKill)); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1838 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1839 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1840 | .addReg(Op0, getKillRegState(Op0IsKill)); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1841 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1842 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1845 | return ResultReg; |
| 1846 | } |
| 1847 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1848 | unsigned FastISel::fastEmitInst_rr(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1849 | const TargetRegisterClass *RC, unsigned Op0, |
| 1850 | bool Op0IsKill, unsigned Op1, |
| 1851 | bool Op1IsKill) { |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1852 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1853 | |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1854 | unsigned ResultReg = createResultReg(RC); |
| 1855 | Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); |
| 1856 | Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); |
| 1857 | |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1858 | if (II.getNumDefs() >= 1) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1859 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1860 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1861 | .addReg(Op1, getKillRegState(Op1IsKill)); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1862 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1863 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1864 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1865 | .addReg(Op1, getKillRegState(Op1IsKill)); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1866 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1867 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1868 | } |
| Dan Gohman | b2226e2 | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1869 | return ResultReg; |
| 1870 | } |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1871 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1872 | unsigned FastISel::fastEmitInst_rrr(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1873 | const TargetRegisterClass *RC, unsigned Op0, |
| 1874 | bool Op0IsKill, unsigned Op1, |
| 1875 | bool Op1IsKill, unsigned Op2, |
| 1876 | bool Op2IsKill) { |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1877 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Owen Anderson | 68b6b0e | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1878 | |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1879 | unsigned ResultReg = createResultReg(RC); |
| 1880 | Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); |
| 1881 | Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); |
| 1882 | Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2); |
| 1883 | |
| Owen Anderson | 68b6b0e | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1884 | if (II.getNumDefs() >= 1) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1885 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1886 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1887 | .addReg(Op1, getKillRegState(Op1IsKill)) |
| 1888 | .addReg(Op2, getKillRegState(Op2IsKill)); |
| Owen Anderson | 68b6b0e | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1889 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1890 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1891 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1892 | .addReg(Op1, getKillRegState(Op1IsKill)) |
| 1893 | .addReg(Op2, getKillRegState(Op2IsKill)); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1894 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1895 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Owen Anderson | 68b6b0e | 2011-05-05 17:59:04 +0000 | [diff] [blame] | 1896 | } |
| 1897 | return ResultReg; |
| 1898 | } |
| 1899 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1900 | unsigned FastISel::fastEmitInst_ri(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1901 | const TargetRegisterClass *RC, unsigned Op0, |
| 1902 | bool Op0IsKill, uint64_t Imm) { |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1903 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1904 | |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1905 | unsigned ResultReg = createResultReg(RC); |
| Juergen Ributzka | 833bc68 | 2014-08-27 20:47:33 +0000 | [diff] [blame] | 1906 | Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1907 | |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1908 | if (II.getNumDefs() >= 1) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1909 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1910 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1911 | .addImm(Imm); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1912 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1913 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1914 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1915 | .addImm(Imm); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1916 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1917 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1918 | } |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1919 | return ResultReg; |
| 1920 | } |
| 1921 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1922 | unsigned FastISel::fastEmitInst_rii(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1923 | const TargetRegisterClass *RC, unsigned Op0, |
| 1924 | bool Op0IsKill, uint64_t Imm1, |
| 1925 | uint64_t Imm2) { |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1926 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Owen Anderson | 66443c0 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1927 | |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1928 | unsigned ResultReg = createResultReg(RC); |
| 1929 | Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); |
| 1930 | |
| Owen Anderson | 66443c0 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1931 | if (II.getNumDefs() >= 1) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1932 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1933 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1934 | .addImm(Imm1) |
| 1935 | .addImm(Imm2); |
| Owen Anderson | 66443c0 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1936 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1937 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1938 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1939 | .addImm(Imm1) |
| 1940 | .addImm(Imm2); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1941 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1942 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Owen Anderson | 66443c0 | 2011-03-11 21:33:55 +0000 | [diff] [blame] | 1943 | } |
| 1944 | return ResultReg; |
| 1945 | } |
| 1946 | |
| Dan Gohman | 7b63484 | 2015-08-24 18:44:37 +0000 | [diff] [blame] | 1947 | unsigned FastISel::fastEmitInst_f(unsigned MachineInstOpcode, |
| 1948 | const TargetRegisterClass *RC, |
| 1949 | const ConstantFP *FPImm) { |
| 1950 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| 1951 | |
| 1952 | unsigned ResultReg = createResultReg(RC); |
| 1953 | |
| 1954 | if (II.getNumDefs() >= 1) |
| 1955 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| 1956 | .addFPImm(FPImm); |
| 1957 | else { |
| 1958 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| 1959 | .addFPImm(FPImm); |
| 1960 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1961 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| 1962 | } |
| 1963 | return ResultReg; |
| 1964 | } |
| 1965 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1966 | unsigned FastISel::fastEmitInst_rri(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1967 | const TargetRegisterClass *RC, unsigned Op0, |
| 1968 | bool Op0IsKill, unsigned Op1, |
| 1969 | bool Op1IsKill, uint64_t Imm) { |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1970 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1971 | |
| Tim Northover | 2f553f3 | 2014-04-15 13:59:49 +0000 | [diff] [blame] | 1972 | unsigned ResultReg = createResultReg(RC); |
| 1973 | Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs()); |
| 1974 | Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1); |
| 1975 | |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1976 | if (II.getNumDefs() >= 1) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1977 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1978 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1979 | .addReg(Op1, getKillRegState(Op1IsKill)) |
| 1980 | .addImm(Imm); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1981 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1982 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1983 | .addReg(Op0, getKillRegState(Op0IsKill)) |
| 1984 | .addReg(Op1, getKillRegState(Op1IsKill)) |
| 1985 | .addImm(Imm); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 1986 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 1987 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1988 | } |
| Dan Gohman | fe90565 | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 1989 | return ResultReg; |
| 1990 | } |
| Owen Anderson | 32635db | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1991 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 1992 | unsigned FastISel::fastEmitInst_i(unsigned MachineInstOpcode, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1993 | const TargetRegisterClass *RC, uint64_t Imm) { |
| Owen Anderson | 32635db | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 1994 | unsigned ResultReg = createResultReg(RC); |
| Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1995 | const MCInstrDesc &II = TII.get(MachineInstOpcode); |
| Wesley Peck | 527da1b | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1996 | |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 1997 | if (II.getNumDefs() >= 1) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 1998 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg) |
| 1999 | .addImm(Imm); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 2000 | else { |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2001 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm); |
| 2002 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, |
| 2003 | TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]); |
| Evan Cheng | e775d35 | 2008-09-08 08:38:20 +0000 | [diff] [blame] | 2004 | } |
| Owen Anderson | 32635db | 2008-08-25 20:20:32 +0000 | [diff] [blame] | 2005 | return ResultReg; |
| Evan Cheng | 2c06732 | 2008-08-25 22:20:39 +0000 | [diff] [blame] | 2006 | } |
| Owen Anderson | 5f57bc2 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 2007 | |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 2008 | unsigned FastISel::fastEmitInst_extractsubreg(MVT RetVT, unsigned Op0, |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2009 | bool Op0IsKill, uint32_t Idx) { |
| Evan Cheng | 4a0bf66 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 2010 | unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT)); |
| Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 2011 | assert(TargetRegisterInfo::isVirtualRegister(Op0) && |
| 2012 | "Cannot yet extract from physregs"); |
| Jakob Stoklund Olesen | 1f1c6ad | 2012-05-20 06:38:37 +0000 | [diff] [blame] | 2013 | const TargetRegisterClass *RC = MRI.getRegClass(Op0); |
| 2014 | MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx)); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2015 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY), |
| 2016 | ResultReg).addReg(Op0, getKillRegState(Op0IsKill), Idx); |
| Owen Anderson | 5f57bc2 | 2008-08-27 22:30:02 +0000 | [diff] [blame] | 2017 | return ResultReg; |
| 2018 | } |
| Dan Gohman | c0bb959 | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 2019 | |
| Juergen Ributzka | 5b8bb4d | 2014-09-03 20:56:52 +0000 | [diff] [blame] | 2020 | /// Emit MachineInstrs to compute the value of Op with all but the least |
| 2021 | /// significant bit set to zero. |
| 2022 | unsigned FastISel::fastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) { |
| Juergen Ributzka | 88e3251 | 2014-09-03 20:56:59 +0000 | [diff] [blame] | 2023 | return fastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1); |
| Dan Gohman | c0bb959 | 2009-03-13 20:42:20 +0000 | [diff] [blame] | 2024 | } |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2025 | |
| 2026 | /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks. |
| 2027 | /// Emit code to ensure constants are copied into registers when needed. |
| 2028 | /// Remember the virtual registers that need to be added to the Machine PHI |
| 2029 | /// nodes as input. We cannot just directly add them, because expansion |
| 2030 | /// might result in multiple MBB's for one BB. As such, the start of the |
| 2031 | /// BB might correspond to a different MBB than the end. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2032 | bool FastISel::handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2033 | const TerminatorInst *TI = LLVMBB->getTerminator(); |
| 2034 | |
| 2035 | SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; |
| Juergen Ributzka | 3132816 | 2014-08-28 02:06:55 +0000 | [diff] [blame] | 2036 | FuncInfo.OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size(); |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Check successor nodes' PHI nodes that expect a constant to be available |
| 2039 | // from this block. |
| 2040 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 2041 | const BasicBlock *SuccBB = TI->getSuccessor(succ); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2042 | if (!isa<PHINode>(SuccBB->begin())) |
| 2043 | continue; |
| Dan Gohman | 87fb4e8 | 2010-07-07 16:29:44 +0000 | [diff] [blame] | 2044 | MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2045 | |
| 2046 | // If this terminator has multiple identical successors (common for |
| 2047 | // switches), only handle each succ once. |
| David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2048 | if (!SuccsHandled.insert(SuccMBB).second) |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2049 | continue; |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2050 | |
| 2051 | MachineBasicBlock::iterator MBBI = SuccMBB->begin(); |
| 2052 | |
| 2053 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 2054 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 2055 | // emitted yet. |
| 2056 | for (BasicBlock::const_iterator I = SuccBB->begin(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2057 | const auto *PN = dyn_cast<PHINode>(I); ++I) { |
| Dan Gohman | e6d4016 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 2058 | |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2059 | // Ignore dead phi's. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2060 | if (PN->use_empty()) |
| 2061 | continue; |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Only handle legal types. Two interesting things to note here. First, |
| 2064 | // by bailing out early, we may leave behind some dead instructions, |
| 2065 | // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its |
| Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 2066 | // own moves. Second, this check is necessary because FastISel doesn't |
| Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 2067 | // use CreateRegs to create registers, so it always creates |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2068 | // exactly one register for each non-void instruction. |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2069 | EVT VT = TLI.getValueType(DL, PN->getType(), /*AllowUnknown=*/true); |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2070 | if (VT == MVT::Other || !TLI.isTypeLegal(VT)) { |
| Chad Rosier | 6d68c7c | 2012-02-04 00:39:19 +0000 | [diff] [blame] | 2071 | // Handle integer promotions, though, because they're common and easy. |
| Eric Christopher | ffcbe9b | 2014-10-08 22:25:45 +0000 | [diff] [blame] | 2072 | if (!(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)) { |
| Juergen Ributzka | 3132816 | 2014-08-28 02:06:55 +0000 | [diff] [blame] | 2073 | FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate); |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2074 | return false; |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 2079 | |
| Dan Gohman | e6d4016 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 2080 | // Set the DebugLoc for the copy. Prefer the location of the operand |
| 2081 | // if there is one; use the location of the PHI otherwise. |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2082 | DbgLoc = PN->getDebugLoc(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2083 | if (const auto *Inst = dyn_cast<Instruction>(PHIOp)) |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2084 | DbgLoc = Inst->getDebugLoc(); |
| Dan Gohman | e6d4016 | 2010-05-07 01:10:20 +0000 | [diff] [blame] | 2085 | |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2086 | unsigned Reg = getRegForValue(PHIOp); |
| Juergen Ributzka | 3132816 | 2014-08-28 02:06:55 +0000 | [diff] [blame] | 2087 | if (!Reg) { |
| 2088 | FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate); |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2089 | return false; |
| 2090 | } |
| Duncan P. N. Exon Smith | 10383ecd | 2016-07-08 18:36:41 +0000 | [diff] [blame] | 2091 | FuncInfo.PHINodesToUpdate.push_back(std::make_pair(&*MBBI++, Reg)); |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2092 | DbgLoc = DebugLoc(); |
| Dan Gohman | c594eab | 2010-04-22 20:46:50 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | return true; |
| 2097 | } |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2098 | |
| 2099 | bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) { |
| Eli Bendersky | e80691d | 2013-04-19 23:26:18 +0000 | [diff] [blame] | 2100 | assert(LI->hasOneUse() && |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2101 | "tryToFoldLoad expected a LoadInst with a single use"); |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2102 | // We know that the load has a single use, but don't know what it is. If it |
| 2103 | // isn't one of the folded instructions, then we can't succeed here. Handle |
| 2104 | // this by scanning the single-use users of the load until we get to FoldInst. |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2105 | unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs. |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2106 | |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2107 | const Instruction *TheUser = LI->user_back(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2108 | while (TheUser != FoldInst && // Scan up until we find FoldInst. |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2109 | // Stay in the right block. |
| 2110 | TheUser->getParent() == FoldInst->getParent() && |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2111 | --MaxUsers) { // Don't scan too far. |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2112 | // If there are multiple or no uses of this instruction, then bail out. |
| 2113 | if (!TheUser->hasOneUse()) |
| 2114 | return false; |
| 2115 | |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2116 | TheUser = TheUser->user_back(); |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | // If we didn't find the fold instruction, then we failed to collapse the |
| 2120 | // sequence. |
| 2121 | if (TheUser != FoldInst) |
| 2122 | return false; |
| 2123 | |
| 2124 | // Don't try to fold volatile loads. Target has to deal with alignment |
| 2125 | // constraints. |
| Eli Bendersky | e80691d | 2013-04-19 23:26:18 +0000 | [diff] [blame] | 2126 | if (LI->isVolatile()) |
| 2127 | return false; |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Figure out which vreg this is going into. If there is no assigned vreg yet |
| 2130 | // then there actually was no reference to it. Perhaps the load is referenced |
| 2131 | // by a dead instruction. |
| 2132 | unsigned LoadReg = getRegForValue(LI); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2133 | if (!LoadReg) |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2134 | return false; |
| 2135 | |
| Eli Bendersky | e80691d | 2013-04-19 23:26:18 +0000 | [diff] [blame] | 2136 | // We can't fold if this vreg has no uses or more than one use. Multiple uses |
| 2137 | // may mean that the instruction got lowered to multiple MIs, or the use of |
| 2138 | // the loaded value ended up being multiple operands of the result. |
| 2139 | if (!MRI.hasOneUse(LoadReg)) |
| 2140 | return false; |
| 2141 | |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2142 | MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg); |
| Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 2143 | MachineInstr *User = RI->getParent(); |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2144 | |
| 2145 | // Set the insertion point properly. Folding the load can cause generation of |
| Eli Bendersky | e80691d | 2013-04-19 23:26:18 +0000 | [diff] [blame] | 2146 | // other random instructions (like sign extends) for addressing modes; make |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2147 | // sure they get inserted in a logical place before the new instruction. |
| 2148 | FuncInfo.InsertPt = User; |
| 2149 | FuncInfo.MBB = User->getParent(); |
| 2150 | |
| 2151 | // Ask the target to try folding the load. |
| 2152 | return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI); |
| 2153 | } |
| 2154 | |
| Bob Wilson | 9f3e6b2 | 2013-11-15 19:09:27 +0000 | [diff] [blame] | 2155 | bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) { |
| 2156 | // Must be an add. |
| 2157 | if (!isa<AddOperator>(Add)) |
| 2158 | return false; |
| 2159 | // Type size needs to match. |
| Rafael Espindola | ea09c59 | 2014-02-18 22:05:46 +0000 | [diff] [blame] | 2160 | if (DL.getTypeSizeInBits(GEP->getType()) != |
| 2161 | DL.getTypeSizeInBits(Add->getType())) |
| Bob Wilson | 9f3e6b2 | 2013-11-15 19:09:27 +0000 | [diff] [blame] | 2162 | return false; |
| 2163 | // Must be in the same basic block. |
| 2164 | if (isa<Instruction>(Add) && |
| 2165 | FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB) |
| 2166 | return false; |
| 2167 | // Must have a constant operand. |
| 2168 | return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1)); |
| 2169 | } |
| Eli Bendersky | 90dd3e7 | 2013-04-19 22:29:18 +0000 | [diff] [blame] | 2170 | |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2171 | MachineMemOperand * |
| 2172 | FastISel::createMachineMemOperandFor(const Instruction *I) const { |
| 2173 | const Value *Ptr; |
| 2174 | Type *ValTy; |
| 2175 | unsigned Alignment; |
| Justin Lebar | 0af80cd | 2016-07-15 18:26:59 +0000 | [diff] [blame] | 2176 | MachineMemOperand::Flags Flags; |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2177 | bool IsVolatile; |
| 2178 | |
| 2179 | if (const auto *LI = dyn_cast<LoadInst>(I)) { |
| 2180 | Alignment = LI->getAlignment(); |
| 2181 | IsVolatile = LI->isVolatile(); |
| 2182 | Flags = MachineMemOperand::MOLoad; |
| 2183 | Ptr = LI->getPointerOperand(); |
| 2184 | ValTy = LI->getType(); |
| 2185 | } else if (const auto *SI = dyn_cast<StoreInst>(I)) { |
| 2186 | Alignment = SI->getAlignment(); |
| 2187 | IsVolatile = SI->isVolatile(); |
| 2188 | Flags = MachineMemOperand::MOStore; |
| 2189 | Ptr = SI->getPointerOperand(); |
| 2190 | ValTy = SI->getValueOperand()->getType(); |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2191 | } else |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2192 | return nullptr; |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2193 | |
| Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2194 | bool IsNonTemporal = I->getMetadata(LLVMContext::MD_nontemporal) != nullptr; |
| 2195 | bool IsInvariant = I->getMetadata(LLVMContext::MD_invariant_load) != nullptr; |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 2196 | bool IsDereferenceable = |
| 2197 | I->getMetadata(LLVMContext::MD_dereferenceable) != nullptr; |
| Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2198 | const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range); |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2199 | |
| Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 2200 | AAMDNodes AAInfo; |
| 2201 | I->getAAMetadata(AAInfo); |
| 2202 | |
| Juergen Ributzka | 7a76c24 | 2014-09-03 18:46:45 +0000 | [diff] [blame] | 2203 | if (Alignment == 0) // Ensure that codegen never sees alignment 0. |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2204 | Alignment = DL.getABITypeAlignment(ValTy); |
| 2205 | |
| Eric Christopher | 4e3d6de | 2014-10-08 23:38:33 +0000 | [diff] [blame] | 2206 | unsigned Size = DL.getTypeStoreSize(ValTy); |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2207 | |
| 2208 | if (IsVolatile) |
| 2209 | Flags |= MachineMemOperand::MOVolatile; |
| 2210 | if (IsNonTemporal) |
| 2211 | Flags |= MachineMemOperand::MONonTemporal; |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 2212 | if (IsDereferenceable) |
| 2213 | Flags |= MachineMemOperand::MODereferenceable; |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2214 | if (IsInvariant) |
| 2215 | Flags |= MachineMemOperand::MOInvariant; |
| 2216 | |
| 2217 | return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size, |
| Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 2218 | Alignment, AAInfo, Ranges); |
| Juergen Ributzka | 349777d | 2014-06-12 23:27:57 +0000 | [diff] [blame] | 2219 | } |
| Juergen Ributzka | d111d29 | 2014-09-15 20:47:13 +0000 | [diff] [blame] | 2220 | |
| 2221 | CmpInst::Predicate FastISel::optimizeCmpPredicate(const CmpInst *CI) const { |
| 2222 | // If both operands are the same, then try to optimize or fold the cmp. |
| 2223 | CmpInst::Predicate Predicate = CI->getPredicate(); |
| 2224 | if (CI->getOperand(0) != CI->getOperand(1)) |
| 2225 | return Predicate; |
| 2226 | |
| 2227 | switch (Predicate) { |
| 2228 | default: llvm_unreachable("Invalid predicate!"); |
| 2229 | case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break; |
| 2230 | case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break; |
| 2231 | case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break; |
| 2232 | case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break; |
| 2233 | case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break; |
| 2234 | case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break; |
| 2235 | case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break; |
| 2236 | case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break; |
| 2237 | case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break; |
| 2238 | case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break; |
| 2239 | case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break; |
| 2240 | case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2241 | case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break; |
| 2242 | case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2243 | case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break; |
| 2244 | case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2245 | |
| 2246 | case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break; |
| 2247 | case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break; |
| 2248 | case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break; |
| 2249 | case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2250 | case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break; |
| 2251 | case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2252 | case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break; |
| 2253 | case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2254 | case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break; |
| 2255 | case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break; |
| 2256 | } |
| 2257 | |
| 2258 | return Predicate; |
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 2259 | } |