55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
77// ===---------------------------------------------------------------------===//
8- // ===---------------------------------------------------------------------===//
9- // /
10- // / \file This file contains a pass to remove i8 truncations and i64 extract
11- // / and insert elements.
12- // /
13- // ===----------------------------------------------------------------------===//
8+
149#include " DXILLegalizePass.h"
1510#include " DirectX.h"
1611#include " llvm/IR/Function.h"
2015#include " llvm/Pass.h"
2116#include " llvm/Transforms/Utils/BasicBlockUtils.h"
2217#include < functional>
23- #include < map>
24- #include < stack>
25- #include < vector>
2618
2719#define DEBUG_TYPE " dxil-legalize"
2820
2921using namespace llvm ;
3022namespace {
3123
32- static void fixI8TruncUseChain (Instruction &I,
33- std::stack<Instruction *> &ToRemove,
34- std::map<Value *, Value *> &ReplacedValues) {
24+ void fixI8TruncUseChain (Instruction &I, SmallVector<Instruction *> &ToRemove,
25+ DenseMap<Value *, Value *> &ReplacedValues) {
3526
3627 auto *Cmp = dyn_cast<CmpInst>(&I);
3728
3829 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3930 if (Trunc->getDestTy ()->isIntegerTy (8 )) {
4031 ReplacedValues[Trunc] = Trunc->getOperand (0 );
41- ToRemove.push (Trunc);
32+ ToRemove.push_back (Trunc);
4233 }
4334 } else if (I.getType ()->isIntegerTy (8 ) ||
4435 (Cmp && Cmp->getOperand (0 )->getType ()->isIntegerTy (8 ))) {
4536 IRBuilder<> Builder (&I);
4637
47- std::vector <Value *> NewOperands;
38+ SmallVector <Value *> NewOperands;
4839 Type *InstrType = IntegerType::get (I.getContext (), 32 );
4940 for (unsigned OpIdx = 0 ; OpIdx < I.getNumOperands (); ++OpIdx) {
5041 Value *Op = I.getOperand (OpIdx);
@@ -88,20 +79,19 @@ static void fixI8TruncUseChain(Instruction &I,
8879
8980 if (NewInst) {
9081 ReplacedValues[&I] = NewInst;
91- ToRemove.push (&I);
82+ ToRemove.push_back (&I);
9283 }
9384 } else if (auto *Cast = dyn_cast<CastInst>(&I)) {
9485 if (Cast->getSrcTy ()->isIntegerTy (8 )) {
95- ToRemove.push (Cast);
86+ ToRemove.push_back (Cast);
9687 Cast->replaceAllUsesWith (ReplacedValues[Cast->getOperand (0 )]);
9788 }
9889 }
9990}
10091
101- static void
102- downcastI64toI32InsertExtractElements (Instruction &I,
103- std::stack<Instruction *> &ToRemove,
104- std::map<Value *, Value *> &) {
92+ void downcastI64toI32InsertExtractElements (Instruction &I,
93+ SmallVector<Instruction *> &ToRemove,
94+ DenseMap<Value *, Value *> &) {
10595
10696 if (auto *Extract = dyn_cast<ExtractElementInst>(&I)) {
10797 Value *Idx = Extract->getIndexOperand ();
@@ -115,7 +105,7 @@ downcastI64toI32InsertExtractElements(Instruction &I,
115105 Extract->getVectorOperand (), Idx32, Extract->getName ());
116106
117107 Extract->replaceAllUsesWith (NewExtract);
118- ToRemove.push (Extract);
108+ ToRemove.push_back (Extract);
119109 }
120110 }
121111
@@ -132,7 +122,7 @@ downcastI64toI32InsertExtractElements(Instruction &I,
132122 Insert->getName ());
133123
134124 Insert->replaceAllUsesWith (Insert32Index);
135- ToRemove.push (Insert);
125+ ToRemove.push_back (Insert);
136126 }
137127 }
138128}
@@ -143,27 +133,22 @@ class DXILLegalizationPipeline {
143133 DXILLegalizationPipeline () { initializeLegalizationPipeline (); }
144134
145135 bool runLegalizationPipeline (Function &F) {
146- std::stack <Instruction *> ToRemove;
147- std::map <Value *, Value *> ReplacedValues;
136+ SmallVector <Instruction *> ToRemove;
137+ DenseMap <Value *, Value *> ReplacedValues;
148138 for (auto &I : instructions (F)) {
149- for (auto &LegalizationFn : LegalizationPipeline) {
139+ for (auto &LegalizationFn : LegalizationPipeline)
150140 LegalizationFn (I, ToRemove, ReplacedValues);
151- }
152141 }
153- bool MadeChanges = !ToRemove.empty ();
154142
155- while (!ToRemove.empty ()) {
156- Instruction *I = ToRemove.top ();
157- I->eraseFromParent ();
158- ToRemove.pop ();
159- }
143+ for (auto *Inst : reverse (ToRemove))
144+ Inst->eraseFromParent ();
160145
161- return MadeChanges ;
146+ return !ToRemove. empty () ;
162147 }
163148
164149private:
165- std::vector <std::function<void (Instruction &, std::stack <Instruction *> &,
166- std::map <Value *, Value *> &)>>
150+ SmallVector <std::function<void (Instruction &, SmallVector <Instruction *> &,
151+ DenseMap <Value *, Value *> &)>>
167152 LegalizationPipeline;
168153
169154 void initializeLegalizationPipeline () {
0 commit comments