LLVM 23.0.0git
InlineAsm.h
Go to the documentation of this file.
1//===- llvm/InlineAsm.h - Class to represent inline asm strings -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This class represents the inline asm strings, which are Value*'s that are
10// used as the callee operand of call instructions. InlineAsm's are uniqued
11// like constants, and created via InlineAsm::get(...).
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_INLINEASM_H
16#define LLVM_IR_INLINEASM_H
17
18#include "llvm/ADT/Bitfields.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Value.h"
24#include <cassert>
25#include <string>
26#include <vector>
27
28namespace llvm {
29
30class Error;
31class FunctionType;
32class PointerType;
33template <class ConstantClass> class ConstantUniqueMap;
34
35class InlineAsm final : public Value {
36public:
41
42private:
43 friend struct InlineAsmKeyType;
44 friend class ConstantUniqueMap<InlineAsm>;
45
46 std::string AsmString, Constraints;
47 FunctionType *FTy;
48 bool HasSideEffects;
49 bool IsAlignStack;
50 AsmDialect Dialect;
51 bool CanThrow;
52
53 InlineAsm(FunctionType *Ty, const std::string &AsmString,
54 const std::string &Constraints, bool hasSideEffects,
55 bool isAlignStack, AsmDialect asmDialect, bool canThrow);
56
57 /// When the ConstantUniqueMap merges two types and makes two InlineAsms
58 /// identical, it destroys one of them with this method.
59 void destroyConstant();
60
61public:
62 InlineAsm(const InlineAsm &) = delete;
63 InlineAsm &operator=(const InlineAsm &) = delete;
64
65 /// InlineAsm::get - Return the specified uniqued inline asm string.
66 ///
67 LLVM_ABI static InlineAsm *get(FunctionType *Ty, StringRef AsmString,
68 StringRef Constraints, bool hasSideEffects,
69 bool isAlignStack = false,
70 AsmDialect asmDialect = AD_ATT,
71 bool canThrow = false);
72
73 bool hasSideEffects() const { return HasSideEffects; }
74 bool isAlignStack() const { return IsAlignStack; }
75 AsmDialect getDialect() const { return Dialect; }
76 bool canThrow() const { return CanThrow; }
77
78 /// getType - InlineAsm's are always pointers.
79 ///
81 return reinterpret_cast<PointerType*>(Value::getType());
82 }
83
84 /// getFunctionType - InlineAsm's are always pointers to functions.
85 ///
87
88 StringRef getAsmString() const { return AsmString; }
89 StringRef getConstraintString() const { return Constraints; }
91
92 /// This static method can be used by the parser to check to see if the
93 /// specified constraint string is legal for the type.
94 LLVM_ABI static Error verify(FunctionType *Ty, StringRef Constraints);
95
96 // Constraint String Parsing
98 isInput, // 'x'
99 isOutput, // '=x'
100 isClobber, // '~x'
101 isLabel, // '!x'
102 };
103
104 using ConstraintCodeVector = std::vector<std::string>;
105
107 /// MatchingInput - If this is not -1, this is an output constraint where an
108 /// input constraint is required to match it (e.g. "0"). The value is the
109 /// constraint number that matches this one (for example, if this is
110 /// constraint #0 and constraint #4 has the value "0", this will be 4).
112
113 /// Code - The constraint code, either the register name (in braces) or the
114 /// constraint letter/number.
116
117 /// Default constructor.
118 SubConstraintInfo() = default;
119 };
120
121 using SubConstraintInfoVector = std::vector<SubConstraintInfo>;
122 struct ConstraintInfo;
123 using ConstraintInfoVector = std::vector<ConstraintInfo>;
124
126 /// Type - The basic type of the constraint: input/output/clobber/label
127 ///
129
130 /// isEarlyClobber - "&": output operand writes result before inputs are all
131 /// read. This is only ever set for an output operand.
132 bool isEarlyClobber = false;
133
134 /// MatchingInput - If this is not -1, this is an output constraint where an
135 /// input constraint is required to match it (e.g. "0"). The value is the
136 /// constraint number that matches this one (for example, if this is
137 /// constraint #0 and constraint #4 has the value "0", this will be 4).
139
140 /// hasMatchingInput - Return true if this is an output constraint that has
141 /// a matching input constraint.
142 bool hasMatchingInput() const { return MatchingInput != -1; }
143
144 /// isCommutative - This is set to true for a constraint that is commutative
145 /// with the next operand.
146 bool isCommutative = false;
147
148 /// isIndirect - True if this operand is an indirect operand. This means
149 /// that the address of the source or destination is present in the call
150 /// instruction, instead of it being returned or passed in explicitly. This
151 /// is represented with a '*' in the asm string.
152 bool isIndirect = false;
153
154 /// Code - The constraint code, either the register name (in braces) or the
155 /// constraint letter/number.
157
158 /// isMultipleAlternative - '|': has multiple-alternative constraints.
160
161 /// multipleAlternatives - If there are multiple alternative constraints,
162 /// this array will contain them. Otherwise it will be empty.
164
165 /// The currently selected alternative constraint index.
167
168 /// Default constructor.
169 ConstraintInfo() = default;
170
171 /// Parse - Analyze the specified string (e.g. "=*&{eax}") and fill in the
172 /// fields in this structure. If the constraint string is not understood,
173 /// return true, otherwise return false.
174 LLVM_ABI bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar);
175
176 /// selectAlternative - Point this constraint to the alternative constraint
177 /// indicated by the index.
178 LLVM_ABI void selectAlternative(unsigned index);
179
180 /// Whether this constraint corresponds to an argument.
181 bool hasArg() const {
182 return Type == isInput || (Type == isOutput && isIndirect);
183 }
184 };
185
186 /// ParseConstraints - Split up the constraint string into the specific
187 /// constraints and their prefixes. If this returns an empty vector, and if
188 /// the constraint string itself isn't empty, there was an error parsing.
190 ParseConstraints(StringRef ConstraintString);
191
192 /// ParseConstraints - Parse the constraints of this inlineasm object,
193 /// returning them the same way that ParseConstraints(str) does.
195 return ParseConstraints(Constraints);
196 }
197
198 // Methods for support type inquiry through isa, cast, and dyn_cast:
199 static bool classof(const Value *V) {
200 return V->getValueID() == Value::InlineAsmVal;
201 }
202
203 enum : uint32_t {
204 // Fixed operands on an INLINEASM SDNode.
208 Op_ExtraInfo = 3, // HasSideEffects, IsAlignStack, AsmDialect.
210
211 // Fixed operands on an INLINEASM MachineInstr.
213 MIOp_ExtraInfo = 1, // HasSideEffects, IsAlignStack, AsmDialect.
215
216 // Interpretation of the MIOp_ExtraInfo bit field.
224 };
225
226 // Inline asm operands map to multiple SDNode / MachineInstr operands.
227 // The first operand is an immediate describing the asm operand, the low
228 // bits is the kind:
229 enum class Kind : uint8_t {
230 RegUse = 1, // Input register, "r".
231 RegDef = 2, // Output register, "=r".
232 RegDefEarlyClobber = 3, // Early-clobber output register, "=&r".
233 Clobber = 4, // Clobbered register, "~r".
234 Imm = 5, // Immediate.
235 Mem = 6, // Memory operand, "m", or an address, "p".
236 Func = 7, // Address operand of function call
237 };
238
239 // Memory constraint codes.
240 // Addresses are included here as they need to be treated the same by the
241 // backend, the only difference is that they are not used to actaully
242 // access memory by the instruction.
278
279 // This class is intentionally packed into a 32b value as it is used as a
280 // MVT::i32 ConstantSDNode SDValue for SelectionDAG and as immediate operands
281 // on INLINEASM and INLINEASM_BR MachineInstr's.
282 //
283 // The encoding of Flag is currently:
284 // Bits 2-0 - A Kind::* value indicating the kind of the operand.
285 // (KindField)
286 // Bits 15-3 - The number of SDNode operands associated with this inline
287 // assembly operand. Once lowered to MIR, this represents the
288 // number of MachineOperands necessary to refer to a
289 // MachineOperandType::MO_FrameIndex. (NumOperands)
290 // Bit 31 - Determines if this is a matched operand. (IsMatched)
291 // If bit 31 is set:
292 // Bits 30-16 - The operand number that this operand must match.
293 // (MatchedOperandNo)
294 // Else if bits 2-0 are Kind::Mem:
295 // Bits 30-16 - A ConstraintCode:: value indicating the original
296 // constraint code. (MemConstraintCode)
297 // Else:
298 // Bits 29-16 - The register class ID to use for the operand. (RegClass)
299 // Bit 30 - If the register is permitted to be spilled.
300 // (RegMayBeFolded)
301 // Defaults to false "r", may be set for constraints like
302 // "rm" (or "g").
303 //
304 // As such, MatchedOperandNo, MemConstraintCode, and
305 // (RegClass+RegMayBeFolded) are views of the same slice of bits, but are
306 // mutually exclusive depending on the fields IsMatched then KindField.
307 class Flag {
308 uint32_t Storage;
310 using NumOperands = Bitfield::Element<unsigned, 3, 13>;
311 using MatchedOperandNo = Bitfield::Element<unsigned, 16, 15>;
314 using RegMayBeFolded = Bitfield::Element<bool, 30, 1>;
315 using IsMatched = Bitfield::Element<bool, 31, 1>;
316
317
318 unsigned getMatchedOperandNo() const { return Bitfield::get<MatchedOperandNo>(Storage); }
319 unsigned getRegClass() const { return Bitfield::get<RegClass>(Storage); }
320 bool isMatched() const { return Bitfield::get<IsMatched>(Storage); }
321
322 public:
323 Flag() : Storage(0) {}
324 explicit Flag(uint32_t F) : Storage(F) {}
325 Flag(enum Kind K, unsigned NumOps) : Storage(0) {
326 Bitfield::set<KindField>(Storage, K);
328 }
329 operator uint32_t() { return Storage; }
330 Kind getKind() const { return Bitfield::get<KindField>(Storage); }
331 bool isRegUseKind() const { return getKind() == Kind::RegUse; }
332 bool isRegDefKind() const { return getKind() == Kind::RegDef; }
335 }
336 bool isClobberKind() const { return getKind() == Kind::Clobber; }
337 bool isImmKind() const { return getKind() == Kind::Imm; }
338 bool isMemKind() const { return getKind() == Kind::Mem; }
339 bool isFuncKind() const { return getKind() == Kind::Func; }
341 switch (getKind()) {
342 case Kind::RegUse:
343 return "reguse";
344 case Kind::RegDef:
345 return "regdef";
347 return "regdef-ec";
348 case Kind::Clobber:
349 return "clobber";
350 case Kind::Imm:
351 return "imm";
352 case Kind::Mem:
353 case Kind::Func:
354 return "mem";
355 }
356 llvm_unreachable("impossible kind");
357 }
358
359 /// getNumOperandRegisters - Extract the number of registers field from the
360 /// inline asm operand flag.
361 unsigned getNumOperandRegisters() const {
362 return Bitfield::get<NumOperands>(Storage);
363 }
364
365 /// isUseOperandTiedToDef - Return true if the flag of the inline asm
366 /// operand indicates it is an use operand that's matched to a def operand.
367 bool isUseOperandTiedToDef(unsigned &Idx) const {
368 if (!isMatched())
369 return false;
370 Idx = getMatchedOperandNo();
371 return true;
372 }
373
374 /// hasRegClassConstraint - Returns true if the flag contains a register
375 /// class constraint. Sets RC to the register class ID.
376 bool hasRegClassConstraint(unsigned &RC) const {
377 if (isMatched())
378 return false;
379 // setRegClass() uses 0 to mean no register class, and otherwise stores
380 // RC + 1.
381 if (!getRegClass())
382 return false;
383 RC = getRegClass() - 1;
384 return true;
385 }
386
388 assert((isMemKind() || isFuncKind()) &&
389 "Not expected mem or function flag!");
390 return Bitfield::get<MemConstraintCode>(Storage);
391 }
392
393 /// setMatchingOp - Augment an existing flag with information indicating
394 /// that this input operand is tied to a previous output operand.
395 void setMatchingOp(unsigned OperandNo) {
396 assert(getMatchedOperandNo() == 0 && "Matching operand already set");
397 Bitfield::set<MatchedOperandNo>(Storage, OperandNo);
398 Bitfield::set<IsMatched>(Storage, true);
399 }
400
401 /// setRegClass - Augment an existing flag with the required register class
402 /// for the following register operands. A tied use operand cannot have a
403 /// register class, use the register class from the def operand instead.
404 void setRegClass(unsigned RC) {
405 assert(!isImmKind() && "Immediates cannot have a register class");
406 assert(!isMemKind() && "Memory operand cannot have a register class");
407 assert(getRegClass() == 0 && "Register class already set");
408 // Store RC + 1, reserve the value 0 to mean 'no register class'.
409 Bitfield::set<RegClass>(Storage, RC + 1);
410 }
411
412 /// setMemConstraint - Augment an existing flag with the constraint code for
413 /// a memory constraint.
415 assert(getMemoryConstraintID() == ConstraintCode::Unknown && "Mem constraint already set");
417 }
418 /// clearMemConstraint - Similar to setMemConstraint(0), but without the
419 /// assertion checking that the constraint has not been set previously.
421 assert((isMemKind() || isFuncKind()) &&
422 "Flag is not a memory or function constraint!");
424 }
425
426 /// Set a bit to denote that while this operand is some kind of register
427 /// (use, def, ...), a memory flag did appear in the original constraint
428 /// list. This is set by the instruction selection framework, and consumed
429 /// by the register allocator. While the register allocator is generally
430 /// responsible for spilling registers, we need to be able to distinguish
431 /// between registers that the register allocator has permission to fold
432 /// ("rm") vs ones it does not ("r"). This is because the inline asm may use
433 /// instructions which don't support memory addressing modes for that
434 /// operand.
435 void setRegMayBeFolded(bool B) {
437 "Must be reg");
439 }
440 bool getRegMayBeFolded() const {
442 "Must be reg");
443 return Bitfield::get<RegMayBeFolded>(Storage);
444 }
445 };
446
447 static std::vector<StringRef> getExtraInfoNames(unsigned ExtraInfo) {
448 std::vector<StringRef> Result;
449 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
450 Result.push_back("sideeffect");
451 if (ExtraInfo & InlineAsm::Extra_MayLoad)
452 Result.push_back("mayload");
453 if (ExtraInfo & InlineAsm::Extra_MayStore)
454 Result.push_back("maystore");
455 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
456 Result.push_back("isconvergent");
457 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
458 Result.push_back("alignstack");
459 if (ExtraInfo & InlineAsm::Extra_MayUnwind)
460 Result.push_back("unwind");
461
462 AsmDialect Dialect =
464
465 if (Dialect == InlineAsm::AD_ATT)
466 Result.push_back("attdialect");
467 if (Dialect == InlineAsm::AD_Intel)
468 Result.push_back("inteldialect");
469
470 return Result;
471 }
472
474 switch (C) {
476 return "es";
478 return "i";
480 return "k";
482 return "m";
484 return "o";
486 return "v";
488 return "A";
490 return "Q";
492 return "R";
494 return "S";
496 return "T";
498 return "Um";
500 return "Un";
502 return "Uq";
504 return "Us";
506 return "Ut";
508 return "Uv";
510 return "Uy";
512 return "X";
514 return "Z";
516 return "ZB";
518 return "ZC";
520 return "Zy";
522 return "p";
524 return "ZQ";
526 return "ZR";
528 return "ZS";
530 return "ZT";
531 default:
532 llvm_unreachable("Unknown memory constraint");
533 }
534 }
535};
536
537} // end namespace llvm
538
539#endif // LLVM_IR_INLINEASM_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements methods to test, set and extract typed bits from packed unsigned integers.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
#define F(x, y, z)
Definition MD5.cpp:54
ppc ctr loops verify
This file defines the SmallVector class.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class to represent function types.
void setRegMayBeFolded(bool B)
Set a bit to denote that while this operand is some kind of register (use, def, .....
Definition InlineAsm.h:435
bool isImmKind() const
Definition InlineAsm.h:337
bool isClobberKind() const
Definition InlineAsm.h:336
void setRegClass(unsigned RC)
setRegClass - Augment an existing flag with the required register class for the following register op...
Definition InlineAsm.h:404
StringRef getKindName() const
Definition InlineAsm.h:340
void clearMemConstraint()
clearMemConstraint - Similar to setMemConstraint(0), but without the assertion checking that the cons...
Definition InlineAsm.h:420
Kind getKind() const
Definition InlineAsm.h:330
Flag(enum Kind K, unsigned NumOps)
Definition InlineAsm.h:325
bool hasRegClassConstraint(unsigned &RC) const
hasRegClassConstraint - Returns true if the flag contains a register class constraint.
Definition InlineAsm.h:376
bool isRegUseKind() const
Definition InlineAsm.h:331
bool isMemKind() const
Definition InlineAsm.h:338
void setMatchingOp(unsigned OperandNo)
setMatchingOp - Augment an existing flag with information indicating that this input operand is tied ...
Definition InlineAsm.h:395
void setMemConstraint(ConstraintCode C)
setMemConstraint - Augment an existing flag with the constraint code for a memory constraint.
Definition InlineAsm.h:414
ConstraintCode getMemoryConstraintID() const
Definition InlineAsm.h:387
bool getRegMayBeFolded() const
Definition InlineAsm.h:440
bool isUseOperandTiedToDef(unsigned &Idx) const
isUseOperandTiedToDef - Return true if the flag of the inline asm operand indicates it is an use oper...
Definition InlineAsm.h:367
bool isFuncKind() const
Definition InlineAsm.h:339
unsigned getNumOperandRegisters() const
getNumOperandRegisters - Extract the number of registers field from the inline asm operand flag.
Definition InlineAsm.h:361
bool isRegDefEarlyClobberKind() const
Definition InlineAsm.h:333
bool isRegDefKind() const
Definition InlineAsm.h:332
LLVM_ABI void collectAsmStrs(SmallVectorImpl< StringRef > &AsmStrs) const
Definition InlineAsm.cpp:63
static LLVM_ABI InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT, bool canThrow=false)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition InlineAsm.cpp:43
std::vector< SubConstraintInfo > SubConstraintInfoVector
Definition InlineAsm.h:121
std::vector< ConstraintInfo > ConstraintInfoVector
Definition InlineAsm.h:123
static std::vector< StringRef > getExtraInfoNames(unsigned ExtraInfo)
Definition InlineAsm.h:447
friend struct InlineAsmKeyType
Definition InlineAsm.h:43
bool isAlignStack() const
Definition InlineAsm.h:74
AsmDialect getDialect() const
Definition InlineAsm.h:75
LLVM_ABI FunctionType * getFunctionType() const
getFunctionType - InlineAsm's are always pointers to functions.
Definition InlineAsm.cpp:59
bool hasSideEffects() const
Definition InlineAsm.h:73
StringRef getAsmString() const
Definition InlineAsm.h:88
InlineAsm(const InlineAsm &)=delete
ConstraintInfoVector ParseConstraints() const
ParseConstraints - Parse the constraints of this inlineasm object, returning them the same way that P...
Definition InlineAsm.h:194
bool canThrow() const
Definition InlineAsm.h:76
std::vector< std::string > ConstraintCodeVector
Definition InlineAsm.h:104
static StringRef getMemConstraintName(ConstraintCode C)
Definition InlineAsm.h:473
StringRef getConstraintString() const
Definition InlineAsm.h:89
PointerType * getType() const
getType - InlineAsm's are always pointers.
Definition InlineAsm.h:80
static bool classof(const Value *V)
Definition InlineAsm.h:199
InlineAsm & operator=(const InlineAsm &)=delete
Class to represent pointers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI Value(Type *Ty, unsigned scid)
Definition Value.cpp:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Describes an element of a Bitfield.
Definition Bitfields.h:176
static Bitfield::Type get(StorageType Packed)
Unpacks the field from the Packed value.
Definition Bitfields.h:207
static void set(StorageType &Packed, typename Bitfield::Type Value)
Sets the typed value in the provided Packed value.
Definition Bitfields.h:223
bool isCommutative
isCommutative - This is set to true for a constraint that is commutative with the next operand.
Definition InlineAsm.h:146
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber/label.
Definition InlineAsm.h:128
int MatchingInput
MatchingInput - If this is not -1, this is an output constraint where an input constraint is required...
Definition InlineAsm.h:138
ConstraintCodeVector Codes
Code - The constraint code, either the register name (in braces) or the constraint letter/number.
Definition InlineAsm.h:156
unsigned currentAlternativeIndex
The currently selected alternative constraint index.
Definition InlineAsm.h:166
ConstraintInfo()=default
Default constructor.
LLVM_ABI bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar)
Parse - Analyze the specified string (e.g.
Definition InlineAsm.cpp:79
bool hasArg() const
Whether this constraint corresponds to an argument.
Definition InlineAsm.h:181
SubConstraintInfoVector multipleAlternatives
multipleAlternatives - If there are multiple alternative constraints, this array will contain them.
Definition InlineAsm.h:163
bool isIndirect
isIndirect - True if this operand is an indirect operand.
Definition InlineAsm.h:152
bool isEarlyClobber
isEarlyClobber - "&": output operand writes result before inputs are all read.
Definition InlineAsm.h:132
bool isMultipleAlternative
isMultipleAlternative - '|': has multiple-alternative constraints.
Definition InlineAsm.h:159
LLVM_ABI void selectAlternative(unsigned index)
selectAlternative - Point this constraint to the alternative constraint indicated by the index.
bool hasMatchingInput() const
hasMatchingInput - Return true if this is an output constraint that has a matching input constraint.
Definition InlineAsm.h:142
ConstraintCodeVector Codes
Code - The constraint code, either the register name (in braces) or the constraint letter/number.
Definition InlineAsm.h:115
int MatchingInput
MatchingInput - If this is not -1, this is an output constraint where an input constraint is required...
Definition InlineAsm.h:111
SubConstraintInfo()=default
Default constructor.