@@ -65,7 +65,6 @@ class MCPlusBuilder {
65
65
private:
66
66
// / A struct that represents a single annotation allocator
67
67
struct AnnotationAllocator {
68
- SpecificBumpPtrAllocator<MCInst> MCInstAllocator;
69
68
BumpPtrAllocator ValueAllocator;
70
69
std::unordered_set<MCPlus::MCAnnotation *> AnnotationPool;
71
70
};
@@ -97,60 +96,62 @@ class MCPlusBuilder {
97
96
return SignExtend64<56 >(ImmValue & 0xff'ffff'ffff'ffffULL );
98
97
}
99
98
100
- MCInst *getAnnotationInst (const MCInst &Inst) const {
101
- if (Inst.getNumOperands () == 0 )
102
- return nullptr ;
99
+ std::optional<unsigned > getFirstAnnotationOpIndex (const MCInst &Inst) const {
100
+ const unsigned NumPrimeOperands = MCPlus::getNumPrimeOperands (Inst);
101
+ if (Inst.getNumOperands () == NumPrimeOperands)
102
+ return std::nullopt;
103
103
104
- const MCOperand &LastOp = Inst.getOperand (Inst.getNumOperands () - 1 );
105
- if (!LastOp.isInst ())
106
- return nullptr ;
104
+ assert (Inst.getOperand (NumPrimeOperands).getInst () == nullptr &&
105
+ " Empty instruction expected." );
107
106
108
- MCInst *AnnotationInst = const_cast <MCInst *>(LastOp. getInst ()) ;
109
- assert (AnnotationInst-> getOpcode () == TargetOpcode::ANNOTATION_LABEL);
107
+ return NumPrimeOperands + 1 ;
108
+ }
110
109
111
- return AnnotationInst;
110
+ MCInst::iterator getAnnotationInstOp (MCInst &Inst) const {
111
+ for (MCInst::iterator Iter = Inst.begin (); Iter != Inst.end (); ++Iter) {
112
+ if (Iter->isInst ()) {
113
+ assert (Iter->getInst () == nullptr && " Empty instruction expected." );
114
+ return Iter;
115
+ }
116
+ }
117
+ return Inst.end ();
112
118
}
113
119
114
- void removeAnnotationInst (MCInst &Inst) const {
115
- assert (getAnnotationInst (Inst) && " Expected annotation instruction." );
116
- Inst.erase (std::prev (Inst.end ()));
117
- assert (!getAnnotationInst (Inst) &&
118
- " More than one annotation instruction detected." );
120
+ void removeAnnotations (MCInst &Inst) const {
121
+ Inst.erase (getAnnotationInstOp (Inst), Inst.end ());
119
122
}
120
123
121
- void setAnnotationOpValue (MCInst &Inst, unsigned Index, int64_t Value,
122
- AllocatorIdTy AllocatorId = 0 ) {
123
- MCInst *AnnotationInst = getAnnotationInst (Inst);
124
- if (!AnnotationInst) {
125
- AnnotationAllocator &Allocator = getAnnotationAllocator (AllocatorId);
126
- AnnotationInst = new (Allocator. MCInstAllocator . Allocate ()) MCInst ( );
127
- AnnotationInst-> setOpcode (TargetOpcode::ANNOTATION_LABEL );
128
- Inst. addOperand ( MCOperand::createInst (AnnotationInst)) ;
124
+ void setAnnotationOpValue (MCInst &Inst, unsigned Index, int64_t Value) const {
125
+ const int64_t AnnotationValue = encodeAnnotationImm (Index, Value);
126
+ const std::optional< unsigned > FirstAnnotationOp =
127
+ getFirstAnnotationOpIndex (Inst);
128
+ if (!FirstAnnotationOp) {
129
+ Inst. addOperand ( MCOperand::createInst ( nullptr ) );
130
+ Inst. addOperand ( MCOperand::createImm (AnnotationValue) );
131
+ return ;
129
132
}
130
133
131
- const int64_t AnnotationValue = encodeAnnotationImm (Index, Value);
132
- for (int I = AnnotationInst->getNumOperands () - 1 ; I >= 0 ; --I) {
133
- int64_t ImmValue = AnnotationInst->getOperand (I).getImm ();
134
+ for (unsigned I = *FirstAnnotationOp; I < Inst.getNumOperands (); ++I) {
135
+ const int64_t ImmValue = Inst.getOperand (I).getImm ();
134
136
if (extractAnnotationIndex (ImmValue) == Index) {
135
- AnnotationInst-> getOperand (I).setImm (AnnotationValue);
137
+ Inst. getOperand (I).setImm (AnnotationValue);
136
138
return ;
137
139
}
138
140
}
139
141
140
- AnnotationInst-> addOperand (MCOperand::createImm (AnnotationValue));
142
+ Inst. addOperand (MCOperand::createImm (AnnotationValue));
141
143
}
142
144
143
145
std::optional<int64_t > getAnnotationOpValue (const MCInst &Inst,
144
146
unsigned Index) const {
145
- const MCInst *AnnotationInst = getAnnotationInst (Inst);
146
- if (!AnnotationInst )
147
+ std::optional< unsigned > FirstAnnotationOp = getFirstAnnotationOpIndex (Inst);
148
+ if (!FirstAnnotationOp )
147
149
return std::nullopt;
148
150
149
- for (int I = AnnotationInst-> getNumOperands () - 1 ; I >= 0 ; -- I) {
150
- int64_t ImmValue = AnnotationInst-> getOperand (I).getImm ();
151
- if (extractAnnotationIndex (ImmValue) == Index) {
151
+ for (unsigned I = *FirstAnnotationOp ; I < Inst. getNumOperands (); ++ I) {
152
+ const int64_t ImmValue = Inst. getOperand (I).getImm ();
153
+ if (extractAnnotationIndex (ImmValue) == Index)
152
154
return extractAnnotationValue (ImmValue);
153
- }
154
155
}
155
156
156
157
return std::nullopt;
@@ -172,21 +173,18 @@ class MCPlusBuilder {
172
173
// / AnnotationNameIndexMap and AnnotationsNames.
173
174
mutable llvm::sys::RWMutex AnnotationNameMutex;
174
175
175
- // / Allocate the TailCall annotation value. Clients of the target-specific
176
+ // / Set TailCall annotation value to true . Clients of the target-specific
176
177
// / MCPlusBuilder classes must use convert/lower/create* interfaces instead.
177
- void setTailCall (MCInst &Inst);
178
+ void setTailCall (MCInst &Inst) const ;
178
179
179
180
public:
180
181
// / Transfer annotations from \p SrcInst to \p DstInst.
181
182
void moveAnnotations (MCInst &&SrcInst, MCInst &DstInst) const {
182
- assert (!getAnnotationInst (DstInst) &&
183
- " Destination instruction should not have annotations." );
184
- const MCInst *AnnotationInst = getAnnotationInst (SrcInst);
185
- if (!AnnotationInst)
186
- return ;
183
+ MCInst::iterator AnnotationOp = getAnnotationInstOp (SrcInst);
184
+ for (MCInst::iterator Iter = AnnotationOp; Iter != SrcInst.end (); ++Iter)
185
+ DstInst.addOperand (*Iter);
187
186
188
- DstInst.addOperand (MCOperand::createInst (AnnotationInst));
189
- removeAnnotationInst (SrcInst);
187
+ SrcInst.erase (AnnotationOp, SrcInst.end ());
190
188
}
191
189
192
190
// / Return iterator range covering def operands.
@@ -390,7 +388,6 @@ class MCPlusBuilder {
390
388
391
389
Allocator.AnnotationPool .clear ();
392
390
Allocator.ValueAllocator .Reset ();
393
- Allocator.MCInstAllocator .DestroyAll ();
394
391
}
395
392
}
396
393
@@ -1128,20 +1125,19 @@ class MCPlusBuilder {
1128
1125
std::optional<MCPlus::MCLandingPad> getEHInfo (const MCInst &Inst) const ;
1129
1126
1130
1127
// / Add handler and action info for call instruction.
1131
- void addEHInfo (MCInst &Inst, const MCPlus::MCLandingPad &LP);
1128
+ void addEHInfo (MCInst &Inst, const MCPlus::MCLandingPad &LP) const ;
1132
1129
1133
1130
// / Update exception-handling info for the invoke instruction \p Inst.
1134
1131
// / Return true on success and false otherwise, e.g. if the instruction is
1135
1132
// / not an invoke.
1136
- bool updateEHInfo (MCInst &Inst, const MCPlus::MCLandingPad &LP);
1133
+ bool updateEHInfo (MCInst &Inst, const MCPlus::MCLandingPad &LP) const ;
1137
1134
1138
1135
// / Return non-negative GNU_args_size associated with the instruction
1139
1136
// / or -1 if there's no associated info.
1140
1137
int64_t getGnuArgsSize (const MCInst &Inst) const ;
1141
1138
1142
1139
// / Add the value of GNU_args_size to Inst if it already has EH info.
1143
- void addGnuArgsSize (MCInst &Inst, int64_t GnuArgsSize,
1144
- AllocatorIdTy AllocId = 0 );
1140
+ void addGnuArgsSize (MCInst &Inst, int64_t GnuArgsSize) const ;
1145
1141
1146
1142
// / Return jump table addressed by this instruction.
1147
1143
uint64_t getJumpTable (const MCInst &Inst) const ;
@@ -1154,19 +1150,19 @@ class MCPlusBuilder {
1154
1150
AllocatorIdTy AllocId = 0 );
1155
1151
1156
1152
// / Disassociate instruction with a jump table.
1157
- bool unsetJumpTable (MCInst &Inst);
1153
+ bool unsetJumpTable (MCInst &Inst) const ;
1158
1154
1159
1155
// / Return destination of conditional tail call instruction if \p Inst is one.
1160
1156
std::optional<uint64_t > getConditionalTailCall (const MCInst &Inst) const ;
1161
1157
1162
1158
// / Mark the \p Instruction as a conditional tail call, and set its
1163
1159
// / destination address if it is known. If \p Instruction was already marked,
1164
1160
// / update its destination with \p Dest.
1165
- bool setConditionalTailCall (MCInst &Inst, uint64_t Dest = 0 );
1161
+ bool setConditionalTailCall (MCInst &Inst, uint64_t Dest = 0 ) const ;
1166
1162
1167
1163
// / If \p Inst was marked as a conditional tail call convert it to a regular
1168
1164
// / branch. Return true if the instruction was converted.
1169
- bool unsetConditionalTailCall (MCInst &Inst);
1165
+ bool unsetConditionalTailCall (MCInst &Inst) const ;
1170
1166
1171
1167
// / Return offset of \p Inst in the original function, if available.
1172
1168
std::optional<uint32_t > getOffset (const MCInst &Inst) const ;
@@ -1175,10 +1171,10 @@ class MCPlusBuilder {
1175
1171
uint32_t getOffsetWithDefault (const MCInst &Inst, uint32_t Default) const ;
1176
1172
1177
1173
// / Set offset of \p Inst in the original function.
1178
- bool setOffset (MCInst &Inst, uint32_t Offset, AllocatorIdTy AllocatorId = 0 ) ;
1174
+ bool setOffset (MCInst &Inst, uint32_t Offset) const ;
1179
1175
1180
1176
// / Remove offset annotation.
1181
- bool clearOffset (MCInst &Inst);
1177
+ bool clearOffset (MCInst &Inst) const ;
1182
1178
1183
1179
// / Return the label of \p Inst, if available.
1184
1180
MCSymbol *getLabel (const MCInst &Inst) const ;
@@ -1827,8 +1823,7 @@ class MCPlusBuilder {
1827
1823
1828
1824
if (!std::is_trivial<ValueType>::value)
1829
1825
Allocator.AnnotationPool .insert (A);
1830
- setAnnotationOpValue (Inst, Index, reinterpret_cast <int64_t >(A),
1831
- AllocatorId);
1826
+ setAnnotationOpValue (Inst, Index, reinterpret_cast <int64_t >(A));
1832
1827
return A->getValue ();
1833
1828
}
1834
1829
@@ -1961,21 +1956,21 @@ class MCPlusBuilder {
1961
1956
// /
1962
1957
// / Return true if the annotation was removed, false if the annotation
1963
1958
// / was not present.
1964
- bool removeAnnotation (MCInst &Inst, unsigned Index);
1959
+ bool removeAnnotation (MCInst &Inst, unsigned Index) const ;
1965
1960
1966
1961
// / Remove annotation associated with \p Name.
1967
1962
// /
1968
1963
// / Return true if the annotation was removed, false if the annotation
1969
1964
// / was not present.
1970
- bool removeAnnotation (MCInst &Inst, StringRef Name) {
1965
+ bool removeAnnotation (MCInst &Inst, StringRef Name) const {
1971
1966
const auto Index = getAnnotationIndex (Name);
1972
1967
if (!Index)
1973
1968
return false ;
1974
1969
return removeAnnotation (Inst, *Index);
1975
1970
}
1976
1971
1977
- // / Remove meta-data, but don't destroy it.
1978
- void stripAnnotations (MCInst &Inst, bool KeepTC = false );
1972
+ // / Remove meta-data from the instruction , but don't destroy it.
1973
+ void stripAnnotations (MCInst &Inst, bool KeepTC = false ) const ;
1979
1974
1980
1975
virtual InstructionListType
1981
1976
createInstrumentedIndirectCall (MCInst &&CallInst, MCSymbol *HandlerFuncAddr,
0 commit comments