@@ -162,27 +162,27 @@ bool ConstantHoistingLegacyPass::runOnFunction(Function &Fn) {
162
162
163
163
void ConstantHoistingPass::collectMatInsertPts (
164
164
const RebasedConstantListType &RebasedConstants,
165
- SmallVectorImpl<Instruction * > &MatInsertPts) const {
165
+ SmallVectorImpl<BasicBlock::iterator > &MatInsertPts) const {
166
166
for (const RebasedConstantInfo &RCI : RebasedConstants)
167
167
for (const ConstantUser &U : RCI.Uses )
168
168
MatInsertPts.emplace_back (findMatInsertPt (U.Inst , U.OpndIdx ));
169
169
}
170
170
171
171
// / Find the constant materialization insertion point.
172
- Instruction * ConstantHoistingPass::findMatInsertPt (Instruction *Inst,
173
- unsigned Idx) const {
172
+ BasicBlock::iterator ConstantHoistingPass::findMatInsertPt (Instruction *Inst,
173
+ unsigned Idx) const {
174
174
// If the operand is a cast instruction, then we have to materialize the
175
175
// constant before the cast instruction.
176
176
if (Idx != ~0U ) {
177
177
Value *Opnd = Inst->getOperand (Idx);
178
178
if (auto CastInst = dyn_cast<Instruction>(Opnd))
179
179
if (CastInst->isCast ())
180
- return CastInst;
180
+ return CastInst-> getIterator () ;
181
181
}
182
182
183
183
// The simple and common case. This also includes constant expressions.
184
184
if (!isa<PHINode>(Inst) && !Inst->isEHPad ())
185
- return Inst;
185
+ return Inst-> getIterator () ;
186
186
187
187
// We can't insert directly before a phi node or an eh pad. Insert before
188
188
// the terminator of the incoming or dominating block.
@@ -191,7 +191,7 @@ Instruction *ConstantHoistingPass::findMatInsertPt(Instruction *Inst,
191
191
if (Idx != ~0U && isa<PHINode>(Inst)) {
192
192
InsertionBlock = cast<PHINode>(Inst)->getIncomingBlock (Idx);
193
193
if (!InsertionBlock->isEHPad ()) {
194
- return InsertionBlock->getTerminator ();
194
+ return InsertionBlock->getTerminator ()-> getIterator () ;
195
195
}
196
196
} else {
197
197
InsertionBlock = Inst->getParent ();
@@ -206,7 +206,7 @@ Instruction *ConstantHoistingPass::findMatInsertPt(Instruction *Inst,
206
206
IDom = IDom->getIDom ();
207
207
}
208
208
209
- return IDom->getBlock ()->getTerminator ();
209
+ return IDom->getBlock ()->getTerminator ()-> getIterator () ;
210
210
}
211
211
212
212
// / Given \p BBs as input, find another set of BBs which collectively
@@ -314,26 +314,27 @@ static void findBestInsertionSet(DominatorTree &DT, BlockFrequencyInfo &BFI,
314
314
}
315
315
316
316
// / Find an insertion point that dominates all uses.
317
- SetVector<Instruction *> ConstantHoistingPass::findConstantInsertionPoint (
317
+ SetVector<BasicBlock::iterator>
318
+ ConstantHoistingPass::findConstantInsertionPoint (
318
319
const ConstantInfo &ConstInfo,
319
- const ArrayRef<Instruction * > MatInsertPts) const {
320
+ const ArrayRef<BasicBlock::iterator > MatInsertPts) const {
320
321
assert (!ConstInfo.RebasedConstants .empty () && " Invalid constant info entry." );
321
322
// Collect all basic blocks.
322
323
SetVector<BasicBlock *> BBs;
323
- SetVector<Instruction * > InsertPts;
324
+ SetVector<BasicBlock::iterator > InsertPts;
324
325
325
- for (Instruction * MatInsertPt : MatInsertPts)
326
+ for (BasicBlock::iterator MatInsertPt : MatInsertPts)
326
327
BBs.insert (MatInsertPt->getParent ());
327
328
328
329
if (BBs.count (Entry)) {
329
- InsertPts.insert (& Entry->front ());
330
+ InsertPts.insert (Entry->begin ());
330
331
return InsertPts;
331
332
}
332
333
333
334
if (BFI) {
334
335
findBestInsertionSet (*DT, *BFI, Entry, BBs);
335
336
for (BasicBlock *BB : BBs)
336
- InsertPts.insert (&* BB->getFirstInsertionPt ());
337
+ InsertPts.insert (BB->getFirstInsertionPt ());
337
338
return InsertPts;
338
339
}
339
340
@@ -343,7 +344,7 @@ SetVector<Instruction *> ConstantHoistingPass::findConstantInsertionPoint(
343
344
BB2 = BBs.pop_back_val ();
344
345
BB = DT->findNearestCommonDominator (BB1, BB2);
345
346
if (BB == Entry) {
346
- InsertPts.insert (& Entry->front ());
347
+ InsertPts.insert (Entry->begin ());
347
348
return InsertPts;
348
349
}
349
350
BBs.insert (BB);
@@ -764,11 +765,13 @@ void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
764
765
Mat = GetElementPtrInst::Create (Type::getInt8Ty (*Ctx), Base, Adj->Offset ,
765
766
" mat_gep" , Adj->MatInsertPt );
766
767
// Hide it behind a bitcast.
767
- Mat = new BitCastInst (Mat, Adj->Ty , " mat_bitcast" , Adj->MatInsertPt );
768
+ Mat = new BitCastInst (Mat, Adj->Ty , " mat_bitcast" ,
769
+ Adj->MatInsertPt ->getIterator ());
768
770
} else
769
771
// Constant being rebased is a ConstantInt.
770
- Mat = BinaryOperator::Create (Instruction::Add, Base, Adj->Offset ,
771
- " const_mat" , Adj->MatInsertPt );
772
+ Mat =
773
+ BinaryOperator::Create (Instruction::Add, Base, Adj->Offset ,
774
+ " const_mat" , Adj->MatInsertPt ->getIterator ());
772
775
773
776
LLVM_DEBUG (dbgs () << " Materialize constant (" << *Base->getOperand (0 )
774
777
<< " + " << *Adj->Offset << " ) in BB "
@@ -819,7 +822,8 @@ void ConstantHoistingPass::emitBaseConstants(Instruction *Base,
819
822
820
823
// Aside from constant GEPs, only constant cast expressions are collected.
821
824
assert (ConstExpr->isCast () && " ConstExpr should be a cast" );
822
- Instruction *ConstExprInst = ConstExpr->getAsInstruction (Adj->MatInsertPt );
825
+ Instruction *ConstExprInst = ConstExpr->getAsInstruction ();
826
+ ConstExprInst->insertBefore (Adj->MatInsertPt );
823
827
ConstExprInst->setOperand (0 , Mat);
824
828
825
829
// Use the same debug location as the instruction we are about to update.
@@ -845,9 +849,9 @@ bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
845
849
SmallVectorImpl<consthoist::ConstantInfo> &ConstInfoVec =
846
850
BaseGV ? ConstGEPInfoMap[BaseGV] : ConstIntInfoVec;
847
851
for (const consthoist::ConstantInfo &ConstInfo : ConstInfoVec) {
848
- SmallVector<Instruction * , 4 > MatInsertPts;
852
+ SmallVector<BasicBlock::iterator , 4 > MatInsertPts;
849
853
collectMatInsertPts (ConstInfo.RebasedConstants , MatInsertPts);
850
- SetVector<Instruction * > IPSet =
854
+ SetVector<BasicBlock::iterator > IPSet =
851
855
findConstantInsertionPoint (ConstInfo, MatInsertPts);
852
856
// We can have an empty set if the function contains unreachable blocks.
853
857
if (IPSet.empty ())
@@ -856,15 +860,15 @@ bool ConstantHoistingPass::emitBaseConstants(GlobalVariable *BaseGV) {
856
860
unsigned UsesNum = 0 ;
857
861
unsigned ReBasesNum = 0 ;
858
862
unsigned NotRebasedNum = 0 ;
859
- for (Instruction * IP : IPSet) {
863
+ for (const BasicBlock::iterator & IP : IPSet) {
860
864
// First, collect constants depending on this IP of the base.
861
865
UsesNum = 0 ;
862
866
SmallVector<UserAdjustment, 4 > ToBeRebased;
863
867
unsigned MatCtr = 0 ;
864
868
for (auto const &RCI : ConstInfo.RebasedConstants ) {
865
869
UsesNum += RCI.Uses .size ();
866
870
for (auto const &U : RCI.Uses ) {
867
- Instruction * MatInsertPt = MatInsertPts[MatCtr++];
871
+ const BasicBlock::iterator & MatInsertPt = MatInsertPts[MatCtr++];
868
872
BasicBlock *OrigMatInsertBB = MatInsertPt->getParent ();
869
873
// If Base constant is to be inserted in multiple places,
870
874
// generate rebase for U using the Base dominating U.
0 commit comments