Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1 | //===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===// |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the InlineAsm class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/IR/InlineAsm.h" |
Jeffrey Yasskin | bf48a9b | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 15 | #include "ConstantsContext.h" |
| 16 | #include "LLVMContextImpl.h" |
Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | #include "llvm/IR/Value.h" |
| 21 | #include "llvm/Support/Casting.h" |
| 22 | #include "llvm/Support/Compiler.h" |
Jeff Cohen | 09f0bd3 | 2006-02-01 04:37:04 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 24 | #include <cassert> |
Chris Lattner | 3b91778 | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 25 | #include <cctype> |
Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 26 | #include <cstddef> |
| 27 | #include <cstdlib> |
| 28 | |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
David Blaikie | 04180d0 | 2015-07-28 00:06:38 +0000 | [diff] [blame] | 31 | InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString, |
Jeffrey Yasskin | bf48a9b | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 32 | const std::string &constraints, bool hasSideEffects, |
Chad Rosier | 581600b | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 33 | bool isAlignStack, AsmDialect asmDialect) |
David Blaikie | 04180d0 | 2015-07-28 00:06:38 +0000 | [diff] [blame] | 34 | : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal), |
| 35 | AsmString(asmString), Constraints(constraints), FTy(FTy), |
| 36 | HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack), |
| 37 | Dialect(asmDialect) { |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 38 | // Do various checks on the constraint string and type. |
Jeffrey Yasskin | bf48a9b | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 39 | assert(Verify(getFunctionType(), constraints) && |
| 40 | "Function type not legal for constraints!"); |
| 41 | } |
| 42 | |
Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 43 | InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString, |
| 44 | StringRef Constraints, bool hasSideEffects, |
| 45 | bool isAlignStack, AsmDialect asmDialect) { |
| 46 | InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects, |
| 47 | isAlignStack, asmDialect); |
| 48 | LLVMContextImpl *pImpl = FTy->getContext().pImpl; |
| 49 | return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key); |
| 50 | } |
| 51 | |
Jeffrey Yasskin | bf48a9b | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 52 | void InlineAsm::destroyConstant() { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 53 | getType()->getContext().pImpl->InlineAsms.remove(this); |
Jeffrey Yasskin | bf48a9b | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 54 | delete this; |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | c1d414a | 2011-07-15 23:15:45 +0000 | [diff] [blame] | 57 | FunctionType *InlineAsm::getFunctionType() const { |
David Blaikie | 04180d0 | 2015-07-28 00:06:38 +0000 | [diff] [blame] | 58 | return FTy; |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 59 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 60 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 61 | /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the |
| 62 | /// fields in this structure. If the constraint string is not understood, |
| 63 | /// return true, otherwise return false. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 64 | bool InlineAsm::ConstraintInfo::Parse(StringRef Str, |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 65 | InlineAsm::ConstraintInfoVector &ConstraintsSoFar) { |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 66 | StringRef::iterator I = Str.begin(), E = Str.end(); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 67 | unsigned multipleAlternativeCount = Str.count('|') + 1; |
| 68 | unsigned multipleAlternativeIndex = 0; |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 69 | ConstraintCodeVector *pCodes = &Codes; |
Eric Christopher | beded8f | 2015-02-10 21:15:06 +0000 | [diff] [blame] | 70 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 71 | // Initialize |
David Blaikie | da4471d | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 72 | isMultipleAlternative = multipleAlternativeCount > 1; |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 73 | if (isMultipleAlternative) { |
| 74 | multipleAlternatives.resize(multipleAlternativeCount); |
| 75 | pCodes = &multipleAlternatives[0].Codes; |
| 76 | } |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 77 | Type = isInput; |
| 78 | isEarlyClobber = false; |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 79 | MatchingInput = -1; |
Chris Lattner | fe3db46 | 2006-02-23 23:36:53 +0000 | [diff] [blame] | 80 | isCommutative = false; |
Chris Lattner | 73d0d0d | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 81 | isIndirect = false; |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 82 | currentAlternativeIndex = 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 73d0d0d | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 84 | // Parse prefixes. |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 85 | if (*I == '~') { |
| 86 | Type = isClobber; |
| 87 | ++I; |
Akira Hatanaka | cbbae7f | 2014-09-05 22:30:32 +0000 | [diff] [blame] | 88 | |
| 89 | // '{' must immediately follow '~'. |
| 90 | if (I != E && *I != '{') |
| 91 | return true; |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 92 | } else if (*I == '=') { |
| 93 | ++I; |
| 94 | Type = isOutput; |
Chris Lattner | 73d0d0d | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 95 | } |
Eric Christopher | beded8f | 2015-02-10 21:15:06 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 73d0d0d | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 97 | if (*I == '*') { |
| 98 | isIndirect = true; |
| 99 | ++I; |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 100 | } |
Eric Christopher | beded8f | 2015-02-10 21:15:06 +0000 | [diff] [blame] | 101 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 102 | if (I == E) return true; // Just a prefix, like "==" or "~". |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 103 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 104 | // Parse the modifiers. |
| 105 | bool DoneWithModifiers = false; |
| 106 | while (!DoneWithModifiers) { |
| 107 | switch (*I) { |
| 108 | default: |
| 109 | DoneWithModifiers = true; |
| 110 | break; |
Chris Lattner | fe3db46 | 2006-02-23 23:36:53 +0000 | [diff] [blame] | 111 | case '&': // Early clobber. |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 112 | if (Type != isOutput || // Cannot early clobber anything but output. |
| 113 | isEarlyClobber) // Reject &&&&&& |
| 114 | return true; |
| 115 | isEarlyClobber = true; |
| 116 | break; |
Chris Lattner | fe3db46 | 2006-02-23 23:36:53 +0000 | [diff] [blame] | 117 | case '%': // Commutative. |
| 118 | if (Type == isClobber || // Cannot commute clobbers. |
| 119 | isCommutative) // Reject %%%%% |
| 120 | return true; |
| 121 | isCommutative = true; |
| 122 | break; |
| 123 | case '#': // Comment. |
| 124 | case '*': // Register preferencing. |
| 125 | return true; // Not supported. |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 126 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 127 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 128 | if (!DoneWithModifiers) { |
| 129 | ++I; |
| 130 | if (I == E) return true; // Just prefixes and modifiers! |
| 131 | } |
| 132 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 133 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 134 | // Parse the various constraints. |
| 135 | while (I != E) { |
| 136 | if (*I == '{') { // Physical register reference. |
| 137 | // Find the end of the register name. |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 138 | StringRef::iterator ConstraintEnd = std::find(I+1, E, '}'); |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 139 | if (ConstraintEnd == E) return true; // "{foo" |
Alexander Potapenko | 1c9f2a9 | 2016-05-23 13:58:04 +0000 | [diff] [blame] | 140 | pCodes->push_back(StringRef(I, ConstraintEnd+1 - I)); |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 141 | I = ConstraintEnd+1; |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 142 | } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 143 | // Maximal munch numbers. |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 144 | StringRef::iterator NumStart = I; |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 145 | while (I != E && isdigit(static_cast<unsigned char>(*I))) |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 146 | ++I; |
Alexander Potapenko | 1c9f2a9 | 2016-05-23 13:58:04 +0000 | [diff] [blame] | 147 | pCodes->push_back(StringRef(NumStart, I - NumStart)); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 148 | unsigned N = atoi(pCodes->back().c_str()); |
Chris Lattner | 2f0eec6 | 2006-02-02 00:23:53 +0000 | [diff] [blame] | 149 | // Check that this is a valid matching constraint! |
| 150 | if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput|| |
| 151 | Type != isInput) |
| 152 | return true; // Invalid constraint number. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 6bdcda3 | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 154 | // If Operand N already has a matching input, reject this. An output |
| 155 | // can't be constrained to the same value as multiple inputs. |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 156 | if (isMultipleAlternative) { |
Karl Schimpf | 06bd94c | 2015-09-03 15:41:37 +0000 | [diff] [blame] | 157 | if (multipleAlternativeIndex >= |
| 158 | ConstraintsSoFar[N].multipleAlternatives.size()) |
Karl Schimpf | c82ddc2 | 2015-09-03 15:41:34 +0000 | [diff] [blame] | 159 | return true; |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 160 | InlineAsm::SubConstraintInfo &scInfo = |
| 161 | ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex]; |
| 162 | if (scInfo.MatchingInput != -1) |
| 163 | return true; |
| 164 | // Note that operand #n has a matching input. |
| 165 | scInfo.MatchingInput = ConstraintsSoFar.size(); |
Daniil Fukalov | 65f1735 | 2017-10-25 12:51:32 +0000 | [diff] [blame] | 166 | assert(scInfo.MatchingInput >= 0); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 167 | } else { |
Benjamin Kramer | 1553287 | 2015-03-29 20:33:07 +0000 | [diff] [blame] | 168 | if (ConstraintsSoFar[N].hasMatchingInput() && |
Benjamin Kramer | 19d0588 | 2015-03-29 20:49:03 +0000 | [diff] [blame] | 169 | (size_t)ConstraintsSoFar[N].MatchingInput != |
| 170 | ConstraintsSoFar.size()) |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 171 | return true; |
| 172 | // Note that operand #n has a matching input. |
| 173 | ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size(); |
Daniil Fukalov | 65f1735 | 2017-10-25 12:51:32 +0000 | [diff] [blame] | 174 | assert(ConstraintsSoFar[N].MatchingInput >= 0); |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 175 | } |
| 176 | } else if (*I == '|') { |
| 177 | multipleAlternativeIndex++; |
| 178 | pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes; |
| 179 | ++I; |
Eric Christopher | 5fab03d | 2011-06-02 19:26:37 +0000 | [diff] [blame] | 180 | } else if (*I == '^') { |
| 181 | // Multi-letter constraint |
Eric Christopher | ec281c8 | 2011-06-03 22:09:12 +0000 | [diff] [blame] | 182 | // FIXME: For now assuming these are 2-character constraints. |
Alexander Potapenko | 1c9f2a9 | 2016-05-23 13:58:04 +0000 | [diff] [blame] | 183 | pCodes->push_back(StringRef(I+1, 2)); |
Eric Christopher | ec281c8 | 2011-06-03 22:09:12 +0000 | [diff] [blame] | 184 | I += 3; |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 185 | } else { |
| 186 | // Single letter constraint. |
Alexander Potapenko | 1c9f2a9 | 2016-05-23 13:58:04 +0000 | [diff] [blame] | 187 | pCodes->push_back(StringRef(I, 1)); |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 188 | ++I; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return false; |
| 193 | } |
| 194 | |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 195 | /// selectAlternative - Point this constraint to the alternative constraint |
| 196 | /// indicated by the index. |
| 197 | void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) { |
| 198 | if (index < multipleAlternatives.size()) { |
| 199 | currentAlternativeIndex = index; |
| 200 | InlineAsm::SubConstraintInfo &scInfo = |
| 201 | multipleAlternatives[currentAlternativeIndex]; |
| 202 | MatchingInput = scInfo.MatchingInput; |
| 203 | Codes = scInfo.Codes; |
| 204 | } |
| 205 | } |
| 206 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 207 | InlineAsm::ConstraintInfoVector |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 208 | InlineAsm::ParseConstraints(StringRef Constraints) { |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 209 | ConstraintInfoVector Result; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 3b91778 | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 211 | // Scan the constraints string. |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 212 | for (StringRef::iterator I = Constraints.begin(), |
| 213 | E = Constraints.end(); I != E; ) { |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 214 | ConstraintInfo Info; |
| 215 | |
| 216 | // Find the end of this constraint. |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 217 | StringRef::iterator ConstraintEnd = std::find(I, E, ','); |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 218 | |
| 219 | if (ConstraintEnd == I || // Empty constraint like ",," |
Benjamin Kramer | 82d5bae | 2010-07-25 23:18:32 +0000 | [diff] [blame] | 220 | Info.Parse(StringRef(I, ConstraintEnd-I), Result)) { |
Chris Lattner | 2f0eec6 | 2006-02-02 00:23:53 +0000 | [diff] [blame] | 221 | Result.clear(); // Erroneous constraint? |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 222 | break; |
| 223 | } |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 224 | |
| 225 | Result.push_back(Info); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 226 | |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 227 | // ConstraintEnd may be either the next comma or the end of the string. In |
| 228 | // the former case, we skip the comma. |
| 229 | I = ConstraintEnd; |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 230 | if (I != E) { |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 231 | ++I; |
Eric Christopher | beded8f | 2015-02-10 21:15:06 +0000 | [diff] [blame] | 232 | if (I == E) { |
| 233 | Result.clear(); |
| 234 | break; |
| 235 | } // don't allow "xyz," |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 238 | |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 239 | return Result; |
| 240 | } |
| 241 | |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 242 | /// Verify - Verify that the specified constraint string is reasonable for the |
| 243 | /// specified function type, and otherwise validate the constraint string. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 244 | bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) { |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 245 | if (Ty->isVarArg()) return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 246 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 247 | ConstraintInfoVector Constraints = ParseConstraints(ConstStr); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 248 | |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 249 | // Error parsing constraints. |
| 250 | if (Constraints.empty() && !ConstStr.empty()) return false; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 251 | |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 252 | unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0; |
Chris Lattner | ea21ad4 | 2008-05-22 04:46:38 +0000 | [diff] [blame] | 253 | unsigned NumIndirect = 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 254 | |
Chris Lattner | f0b415f | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 255 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 256 | switch (Constraints[i].Type) { |
| 257 | case InlineAsm::isOutput: |
Chris Lattner | ea21ad4 | 2008-05-22 04:46:38 +0000 | [diff] [blame] | 258 | if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0) |
| 259 | return false; // outputs before inputs and clobbers. |
Chris Lattner | 73d0d0d | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 260 | if (!Constraints[i].isIndirect) { |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 261 | ++NumOutputs; |
| 262 | break; |
| 263 | } |
Chris Lattner | ea21ad4 | 2008-05-22 04:46:38 +0000 | [diff] [blame] | 264 | ++NumIndirect; |
Justin Bogner | 6673ea8 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 265 | LLVM_FALLTHROUGH; // We fall through for Indirect Outputs. |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 266 | case InlineAsm::isInput: |
Chris Lattner | 3b91778 | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 267 | if (NumClobbers) return false; // inputs before clobbers. |
| 268 | ++NumInputs; |
| 269 | break; |
Chris Lattner | a55079a | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 270 | case InlineAsm::isClobber: |
Chris Lattner | 3b91778 | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 271 | ++NumClobbers; |
| 272 | break; |
| 273 | } |
Chris Lattner | 3b91778 | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 274 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 96bb622 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 276 | switch (NumOutputs) { |
| 277 | case 0: |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 278 | if (!Ty->getReturnType()->isVoidTy()) return false; |
Chris Lattner | 96bb622 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 279 | break; |
| 280 | case 1: |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 281 | if (Ty->getReturnType()->isStructTy()) return false; |
Chris Lattner | 96bb622 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 282 | break; |
| 283 | default: |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 284 | StructType *STy = dyn_cast<StructType>(Ty->getReturnType()); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 285 | if (!STy || STy->getNumElements() != NumOutputs) |
Chris Lattner | 96bb622 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 286 | return false; |
| 287 | break; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 3b91778 | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 290 | if (Ty->getNumParams() != NumInputs) return false; |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 291 | return true; |
| 292 | } |