5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
// ===---------------------------------------------------------------------===//
8
- // ===---------------------------------------------------------------------===//
9
- // /
10
- // / \file This file contains a pass to remove i8 truncations and i64 extract
11
- // / and insert elements.
12
- // /
13
- // ===----------------------------------------------------------------------===//
8
+
14
9
#include " DXILLegalizePass.h"
15
10
#include " DirectX.h"
16
11
#include " llvm/IR/Function.h"
20
15
#include " llvm/Pass.h"
21
16
#include " llvm/Transforms/Utils/BasicBlockUtils.h"
22
17
#include < functional>
23
- #include < map>
24
- #include < stack>
25
- #include < vector>
26
18
27
19
#define DEBUG_TYPE " dxil-legalize"
28
20
29
21
using namespace llvm ;
30
22
namespace {
31
23
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) {
35
26
36
27
auto *Cmp = dyn_cast<CmpInst>(&I);
37
28
38
29
if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
39
30
if (Trunc->getDestTy ()->isIntegerTy (8 )) {
40
31
ReplacedValues[Trunc] = Trunc->getOperand (0 );
41
- ToRemove.push (Trunc);
32
+ ToRemove.push_back (Trunc);
42
33
}
43
34
} else if (I.getType ()->isIntegerTy (8 ) ||
44
35
(Cmp && Cmp->getOperand (0 )->getType ()->isIntegerTy (8 ))) {
45
36
IRBuilder<> Builder (&I);
46
37
47
- std::vector <Value *> NewOperands;
38
+ SmallVector <Value *> NewOperands;
48
39
Type *InstrType = IntegerType::get (I.getContext (), 32 );
49
40
for (unsigned OpIdx = 0 ; OpIdx < I.getNumOperands (); ++OpIdx) {
50
41
Value *Op = I.getOperand (OpIdx);
@@ -88,20 +79,19 @@ static void fixI8TruncUseChain(Instruction &I,
88
79
89
80
if (NewInst) {
90
81
ReplacedValues[&I] = NewInst;
91
- ToRemove.push (&I);
82
+ ToRemove.push_back (&I);
92
83
}
93
84
} else if (auto *Cast = dyn_cast<CastInst>(&I)) {
94
85
if (Cast->getSrcTy ()->isIntegerTy (8 )) {
95
- ToRemove.push (Cast);
86
+ ToRemove.push_back (Cast);
96
87
Cast->replaceAllUsesWith (ReplacedValues[Cast->getOperand (0 )]);
97
88
}
98
89
}
99
90
}
100
91
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 *> &) {
105
95
106
96
if (auto *Extract = dyn_cast<ExtractElementInst>(&I)) {
107
97
Value *Idx = Extract->getIndexOperand ();
@@ -115,7 +105,7 @@ downcastI64toI32InsertExtractElements(Instruction &I,
115
105
Extract->getVectorOperand (), Idx32, Extract->getName ());
116
106
117
107
Extract->replaceAllUsesWith (NewExtract);
118
- ToRemove.push (Extract);
108
+ ToRemove.push_back (Extract);
119
109
}
120
110
}
121
111
@@ -132,7 +122,7 @@ downcastI64toI32InsertExtractElements(Instruction &I,
132
122
Insert->getName ());
133
123
134
124
Insert->replaceAllUsesWith (Insert32Index);
135
- ToRemove.push (Insert);
125
+ ToRemove.push_back (Insert);
136
126
}
137
127
}
138
128
}
@@ -143,27 +133,22 @@ class DXILLegalizationPipeline {
143
133
DXILLegalizationPipeline () { initializeLegalizationPipeline (); }
144
134
145
135
bool runLegalizationPipeline (Function &F) {
146
- std::stack <Instruction *> ToRemove;
147
- std::map <Value *, Value *> ReplacedValues;
136
+ SmallVector <Instruction *> ToRemove;
137
+ DenseMap <Value *, Value *> ReplacedValues;
148
138
for (auto &I : instructions (F)) {
149
- for (auto &LegalizationFn : LegalizationPipeline) {
139
+ for (auto &LegalizationFn : LegalizationPipeline)
150
140
LegalizationFn (I, ToRemove, ReplacedValues);
151
- }
152
141
}
153
- bool MadeChanges = !ToRemove.empty ();
154
142
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 ();
160
145
161
- return MadeChanges ;
146
+ return !ToRemove. empty () ;
162
147
}
163
148
164
149
private:
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 *> &)>>
167
152
LegalizationPipeline;
168
153
169
154
void initializeLegalizationPipeline () {
0 commit comments