@@ -65,7 +65,6 @@ class MCPlusBuilder {
6565private:
6666 // / A struct that represents a single annotation allocator
6767 struct AnnotationAllocator {
68- SpecificBumpPtrAllocator<MCInst> MCInstAllocator;
6968 BumpPtrAllocator ValueAllocator;
7069 std::unordered_set<MCPlus::MCAnnotation *> AnnotationPool;
7170 };
@@ -97,60 +96,62 @@ class MCPlusBuilder {
9796 return SignExtend64<56 >(ImmValue & 0xff'ffff'ffff'ffffULL );
9897 }
9998
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 ;
103103
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." );
107106
108- MCInst *AnnotationInst = const_cast <MCInst *>(LastOp. getInst ()) ;
109- assert (AnnotationInst-> getOpcode () == TargetOpcode::ANNOTATION_LABEL);
107+ return NumPrimeOperands + 1 ;
108+ }
110109
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 ();
112118 }
113119
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 ());
119122 }
120123
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 ;
129132 }
130133
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 ();
134136 if (extractAnnotationIndex (ImmValue) == Index) {
135- AnnotationInst-> getOperand (I).setImm (AnnotationValue);
137+ Inst. getOperand (I).setImm (AnnotationValue);
136138 return ;
137139 }
138140 }
139141
140- AnnotationInst-> addOperand (MCOperand::createImm (AnnotationValue));
142+ Inst. addOperand (MCOperand::createImm (AnnotationValue));
141143 }
142144
143145 std::optional<int64_t > getAnnotationOpValue (const MCInst &Inst,
144146 unsigned Index) const {
145- const MCInst *AnnotationInst = getAnnotationInst (Inst);
146- if (!AnnotationInst )
147+ std::optional< unsigned > FirstAnnotationOp = getFirstAnnotationOpIndex (Inst);
148+ if (!FirstAnnotationOp )
147149 return std::nullopt ;
148150
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)
152154 return extractAnnotationValue (ImmValue);
153- }
154155 }
155156
156157 return std::nullopt ;
@@ -172,21 +173,18 @@ class MCPlusBuilder {
172173 // / AnnotationNameIndexMap and AnnotationsNames.
173174 mutable llvm::sys::RWMutex AnnotationNameMutex;
174175
175- // / Allocate the TailCall annotation value. Clients of the target-specific
176+ // / Set TailCall annotation value to true . Clients of the target-specific
176177 // / MCPlusBuilder classes must use convert/lower/create* interfaces instead.
177- void setTailCall (MCInst &Inst);
178+ void setTailCall (MCInst &Inst) const ;
178179
179180public:
180181 // / Transfer annotations from \p SrcInst to \p DstInst.
181182 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);
187186
188- DstInst.addOperand (MCOperand::createInst (AnnotationInst));
189- removeAnnotationInst (SrcInst);
187+ SrcInst.erase (AnnotationOp, SrcInst.end ());
190188 }
191189
192190 // / Return iterator range covering def operands.
@@ -390,7 +388,6 @@ class MCPlusBuilder {
390388
391389 Allocator.AnnotationPool .clear ();
392390 Allocator.ValueAllocator .Reset ();
393- Allocator.MCInstAllocator .DestroyAll ();
394391 }
395392 }
396393
@@ -1128,20 +1125,19 @@ class MCPlusBuilder {
11281125 std::optional<MCPlus::MCLandingPad> getEHInfo (const MCInst &Inst) const ;
11291126
11301127 // / 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 ;
11321129
11331130 // / Update exception-handling info for the invoke instruction \p Inst.
11341131 // / Return true on success and false otherwise, e.g. if the instruction is
11351132 // / not an invoke.
1136- bool updateEHInfo (MCInst &Inst, const MCPlus::MCLandingPad &LP);
1133+ bool updateEHInfo (MCInst &Inst, const MCPlus::MCLandingPad &LP) const ;
11371134
11381135 // / Return non-negative GNU_args_size associated with the instruction
11391136 // / or -1 if there's no associated info.
11401137 int64_t getGnuArgsSize (const MCInst &Inst) const ;
11411138
11421139 // / 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 ;
11451141
11461142 // / Return jump table addressed by this instruction.
11471143 uint64_t getJumpTable (const MCInst &Inst) const ;
@@ -1154,19 +1150,19 @@ class MCPlusBuilder {
11541150 AllocatorIdTy AllocId = 0 );
11551151
11561152 // / Disassociate instruction with a jump table.
1157- bool unsetJumpTable (MCInst &Inst);
1153+ bool unsetJumpTable (MCInst &Inst) const ;
11581154
11591155 // / Return destination of conditional tail call instruction if \p Inst is one.
11601156 std::optional<uint64_t > getConditionalTailCall (const MCInst &Inst) const ;
11611157
11621158 // / Mark the \p Instruction as a conditional tail call, and set its
11631159 // / destination address if it is known. If \p Instruction was already marked,
11641160 // / 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 ;
11661162
11671163 // / If \p Inst was marked as a conditional tail call convert it to a regular
11681164 // / branch. Return true if the instruction was converted.
1169- bool unsetConditionalTailCall (MCInst &Inst);
1165+ bool unsetConditionalTailCall (MCInst &Inst) const ;
11701166
11711167 // / Return offset of \p Inst in the original function, if available.
11721168 std::optional<uint32_t > getOffset (const MCInst &Inst) const ;
@@ -1175,10 +1171,10 @@ class MCPlusBuilder {
11751171 uint32_t getOffsetWithDefault (const MCInst &Inst, uint32_t Default) const ;
11761172
11771173 // / 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 ;
11791175
11801176 // / Remove offset annotation.
1181- bool clearOffset (MCInst &Inst);
1177+ bool clearOffset (MCInst &Inst) const ;
11821178
11831179 // / Return the label of \p Inst, if available.
11841180 MCSymbol *getLabel (const MCInst &Inst) const ;
@@ -1827,8 +1823,7 @@ class MCPlusBuilder {
18271823
18281824 if (!std::is_trivial<ValueType>::value)
18291825 Allocator.AnnotationPool .insert (A);
1830- setAnnotationOpValue (Inst, Index, reinterpret_cast <int64_t >(A),
1831- AllocatorId);
1826+ setAnnotationOpValue (Inst, Index, reinterpret_cast <int64_t >(A));
18321827 return A->getValue ();
18331828 }
18341829
@@ -1961,21 +1956,21 @@ class MCPlusBuilder {
19611956 // /
19621957 // / Return true if the annotation was removed, false if the annotation
19631958 // / was not present.
1964- bool removeAnnotation (MCInst &Inst, unsigned Index);
1959+ bool removeAnnotation (MCInst &Inst, unsigned Index) const ;
19651960
19661961 // / Remove annotation associated with \p Name.
19671962 // /
19681963 // / Return true if the annotation was removed, false if the annotation
19691964 // / was not present.
1970- bool removeAnnotation (MCInst &Inst, StringRef Name) {
1965+ bool removeAnnotation (MCInst &Inst, StringRef Name) const {
19711966 const auto Index = getAnnotationIndex (Name);
19721967 if (!Index)
19731968 return false ;
19741969 return removeAnnotation (Inst, *Index);
19751970 }
19761971
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 ;
19791974
19801975 virtual InstructionListType
19811976 createInstrumentedIndirectCall (MCInst &&CallInst, MCSymbol *HandlerFuncAddr,
0 commit comments