1
- // ===- DXILDataScalarization.cpp - Prepare LLVM Module for DXIL Data
2
- // Legalization----===//
1
+ // ===- DXILDataScalarization.cpp - Perform DXIL Data Legalization----===//
3
2
//
4
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5
4
// See https://llvm.org/LICENSE.txt for license information.
6
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
6
//
8
- // ===-------------------------------------------------------------------------------- ===//
7
+ // ===----------------------------------------------------------------===//
9
8
10
9
#include " DXILDataScalarization.h"
11
10
#include " DirectX.h"
12
11
#include " llvm/ADT/PostOrderIterator.h"
12
+ #include " llvm/ADT/STLExtras.h"
13
13
#include " llvm/IR/GlobalVariable.h"
14
14
#include " llvm/IR/IRBuilder.h"
15
15
#include " llvm/IR/InstVisitor.h"
20
20
#include " llvm/IR/Type.h"
21
21
#include " llvm/Transforms/Utils/Cloning.h"
22
22
#include " llvm/Transforms/Utils/Local.h"
23
- #include < utility>
24
23
25
24
#define DEBUG_TYPE " dxil-data-scalarization"
26
25
#define Max_VEC_SIZE 4
@@ -164,14 +163,16 @@ Constant *transformInitializer(Constant *Init, Type *OrigType, Type *NewType,
164
163
if (isa<VectorType>(OrigType) && isa<ArrayType>(NewType)) {
165
164
// Convert vector initializer to array initializer
166
165
SmallVector<Constant *, Max_VEC_SIZE> ArrayElements;
167
- if ( ConstantVector *ConstVecInit = dyn_cast<ConstantVector>(Init)) {
168
- for (unsigned I = 0 ; I < ConstVecInit->getNumOperands (); ++I)
169
- ArrayElements.push_back (ConstVecInit->getOperand (I));
170
- } else if (ConstantDataVector *ConstDataVecInit = llvm::dyn_cast<llvm::ConstantDataVector>(Init)) {
171
- for (unsigned I = 0 ; I < ConstDataVecInit->getNumElements (); ++I)
172
- ArrayElements.push_back (ConstDataVecInit->getElementAsConstant (I));
173
- } else {
174
- llvm_unreachable (" Expected a ConstantVector or ConstantDataVector for vector initializer!" );
166
+ if (ConstantVector *ConstVecInit = dyn_cast<ConstantVector>(Init)) {
167
+ for (unsigned I = 0 ; I < ConstVecInit->getNumOperands (); ++I)
168
+ ArrayElements.push_back (ConstVecInit->getOperand (I));
169
+ } else if (ConstantDataVector *ConstDataVecInit =
170
+ llvm::dyn_cast<llvm::ConstantDataVector>(Init)) {
171
+ for (unsigned I = 0 ; I < ConstDataVecInit->getNumElements (); ++I)
172
+ ArrayElements.push_back (ConstDataVecInit->getElementAsConstant (I));
173
+ } else {
174
+ llvm_unreachable (" Expected a ConstantVector or ConstantDataVector for "
175
+ " vector initializer!" );
175
176
}
176
177
177
178
return ConstantArray::get (cast<ArrayType>(NewType), ArrayElements);
@@ -213,9 +214,10 @@ static void findAndReplaceVectors(Module &M) {
213
214
// Create a new global variable with the updated type
214
215
GlobalVariable *NewGlobal = new GlobalVariable (
215
216
M, NewType, G.isConstant (), G.getLinkage (),
216
- // This is set via: transformInitializer
217
- nullptr , G.getName () + " .scalarized" , &G, G.getThreadLocalMode (),
218
- G.getAddressSpace (), G.isExternallyInitialized ());
217
+ // Initializer is set via transformInitializer
218
+ /* Initializer=*/ nullptr , G.getName () + " .scalarized" , &G,
219
+ G.getThreadLocalMode (), G.getAddressSpace (),
220
+ G.isExternallyInitialized ());
219
221
220
222
// Copy relevant attributes
221
223
NewGlobal->setUnnamedAddr (G.getUnnamedAddr ());
@@ -230,31 +232,16 @@ static void findAndReplaceVectors(Module &M) {
230
232
}
231
233
232
234
// Note: we want to do G.replaceAllUsesWith(NewGlobal);, but it assumes
233
- // type equality
234
- // So instead we will use the visitor pattern
235
+ // type equality. Instead we will use the visitor pattern.
235
236
Impl.GlobalMap [&G] = NewGlobal;
236
- for (User *U : G.users ()) {
237
- // Note: The GEPS are stored as constExprs
238
- // This step flattens them out to instructions
237
+ for (User *U : make_early_inc_range (G.users ())) {
239
238
if (isa<ConstantExpr>(U) && isa<Operator>(U)) {
240
239
ConstantExpr *CE = cast<ConstantExpr>(U);
241
240
convertUsersOfConstantsToInstructions (CE,
242
241
/* RestrictToFunc=*/ nullptr ,
243
242
/* RemoveDeadConstants=*/ false ,
244
243
/* IncludeSelf=*/ true );
245
244
}
246
- }
247
- // Uses should have grown
248
- std::vector<User *> UsersToProcess;
249
- // Collect all users first
250
- // work around so I can delete
251
- // in a loop body
252
- for (User *U : G.users ()) {
253
- UsersToProcess.push_back (U);
254
- }
255
-
256
- // Now process each user
257
- for (User *U : UsersToProcess) {
258
245
if (isa<Instruction>(U)) {
259
246
Instruction *Inst = cast<Instruction>(U);
260
247
Function *F = Inst->getFunction ();
@@ -294,4 +281,4 @@ INITIALIZE_PASS_END(DXILDataScalarizationLegacy, DEBUG_TYPE,
294
281
295
282
ModulePass *llvm::createDXILDataScalarizationLegacyPass() {
296
283
return new DXILDataScalarizationLegacy ();
297
- }
284
+ }
0 commit comments