summaryrefslogtreecommitdiff
path: root/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Target/DirectX/DXILDataScalarization.cpp')
-rw-r--r--llvm/lib/Target/DirectX/DXILDataScalarization.cpp108
1 files changed, 55 insertions, 53 deletions
diff --git a/llvm/lib/Target/DirectX/DXILDataScalarization.cpp b/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
index 0e6cf59e2575..2ab2daaff5b5 100644
--- a/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
+++ b/llvm/lib/Target/DirectX/DXILDataScalarization.cpp
@@ -10,7 +10,6 @@
#include "DirectX.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/Analysis/DXILResource.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstVisitor.h"
@@ -33,7 +32,6 @@ public:
bool runOnModule(Module &M) override;
DXILDataScalarizationLegacy() : ModulePass(ID) {}
- void getAnalysisUsage(AnalysisUsage &AU) const override;
static char ID; // Pass identification.
};
@@ -42,7 +40,7 @@ static bool findAndReplaceVectors(Module &M);
class DataScalarizerVisitor : public InstVisitor<DataScalarizerVisitor, bool> {
public:
DataScalarizerVisitor() : GlobalMap() {}
- bool visit(Function &F);
+ bool visit(Instruction &I);
// InstVisitor methods. They return true if the instruction was scalarized,
// false if nothing changed.
bool visitInstruction(Instruction &I) { return false; }
@@ -67,28 +65,11 @@ public:
private:
GlobalVariable *lookupReplacementGlobal(Value *CurrOperand);
DenseMap<GlobalVariable *, GlobalVariable *> GlobalMap;
- SmallVector<WeakTrackingVH, 32> PotentiallyDeadInstrs;
- bool finish();
};
-bool DataScalarizerVisitor::visit(Function &F) {
+bool DataScalarizerVisitor::visit(Instruction &I) {
assert(!GlobalMap.empty());
- ReversePostOrderTraversal<BasicBlock *> RPOT(&F.getEntryBlock());
- for (BasicBlock *BB : RPOT) {
- for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
- Instruction *I = &*II;
- bool Done = InstVisitor::visit(I);
- ++II;
- if (Done && I->getType()->isVoidTy())
- I->eraseFromParent();
- }
- }
- return finish();
-}
-
-bool DataScalarizerVisitor::finish() {
- RecursivelyDeleteTriviallyDeadInstructionsPermissive(PotentiallyDeadInstrs);
- return true;
+ return InstVisitor::visit(I);
}
GlobalVariable *
@@ -106,6 +87,20 @@ bool DataScalarizerVisitor::visitLoadInst(LoadInst &LI) {
unsigned NumOperands = LI.getNumOperands();
for (unsigned I = 0; I < NumOperands; ++I) {
Value *CurrOpperand = LI.getOperand(I);
+ ConstantExpr *CE = dyn_cast<ConstantExpr>(CurrOpperand);
+ if (CE && CE->getOpcode() == Instruction::GetElementPtr) {
+ GetElementPtrInst *OldGEP =
+ cast<GetElementPtrInst>(CE->getAsInstruction());
+ OldGEP->insertBefore(&LI);
+ IRBuilder<> Builder(&LI);
+ LoadInst *NewLoad =
+ Builder.CreateLoad(LI.getType(), OldGEP, LI.getName());
+ NewLoad->setAlignment(LI.getAlign());
+ LI.replaceAllUsesWith(NewLoad);
+ LI.eraseFromParent();
+ visitGetElementPtrInst(*OldGEP);
+ return true;
+ }
if (GlobalVariable *NewGlobal = lookupReplacementGlobal(CurrOpperand))
LI.setOperand(I, NewGlobal);
}
@@ -116,32 +111,48 @@ bool DataScalarizerVisitor::visitStoreInst(StoreInst &SI) {
unsigned NumOperands = SI.getNumOperands();
for (unsigned I = 0; I < NumOperands; ++I) {
Value *CurrOpperand = SI.getOperand(I);
- if (GlobalVariable *NewGlobal = lookupReplacementGlobal(CurrOpperand)) {
- SI.setOperand(I, NewGlobal);
+ ConstantExpr *CE = dyn_cast<ConstantExpr>(CurrOpperand);
+ if (CE && CE->getOpcode() == Instruction::GetElementPtr) {
+ GetElementPtrInst *OldGEP =
+ cast<GetElementPtrInst>(CE->getAsInstruction());
+ OldGEP->insertBefore(&SI);
+ IRBuilder<> Builder(&SI);
+ StoreInst *NewStore = Builder.CreateStore(SI.getValueOperand(), OldGEP);
+ NewStore->setAlignment(SI.getAlign());
+ SI.replaceAllUsesWith(NewStore);
+ SI.eraseFromParent();
+ visitGetElementPtrInst(*OldGEP);
+ return true;
}
+ if (GlobalVariable *NewGlobal = lookupReplacementGlobal(CurrOpperand))
+ SI.setOperand(I, NewGlobal);
}
return false;
}
bool DataScalarizerVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
+
unsigned NumOperands = GEPI.getNumOperands();
+ GlobalVariable *NewGlobal = nullptr;
for (unsigned I = 0; I < NumOperands; ++I) {
Value *CurrOpperand = GEPI.getOperand(I);
- GlobalVariable *NewGlobal = lookupReplacementGlobal(CurrOpperand);
- if (!NewGlobal)
- continue;
- IRBuilder<> Builder(&GEPI);
-
- SmallVector<Value *, MaxVecSize> Indices;
- for (auto &Index : GEPI.indices())
- Indices.push_back(Index);
-
- Value *NewGEP =
- Builder.CreateGEP(NewGlobal->getValueType(), NewGlobal, Indices);
-
- GEPI.replaceAllUsesWith(NewGEP);
- PotentiallyDeadInstrs.emplace_back(&GEPI);
+ NewGlobal = lookupReplacementGlobal(CurrOpperand);
+ if (NewGlobal)
+ break;
}
+ if (!NewGlobal)
+ return false;
+
+ IRBuilder<> Builder(&GEPI);
+ SmallVector<Value *, MaxVecSize> Indices;
+ for (auto &Index : GEPI.indices())
+ Indices.push_back(Index);
+
+ Value *NewGEP =
+ Builder.CreateGEP(NewGlobal->getValueType(), NewGlobal, Indices,
+ GEPI.getName(), GEPI.getNoWrapFlags());
+ GEPI.replaceAllUsesWith(NewGEP);
+ GEPI.eraseFromParent();
return true;
}
@@ -247,17 +258,13 @@ static bool findAndReplaceVectors(Module &M) {
for (User *U : make_early_inc_range(G.users())) {
if (isa<ConstantExpr>(U) && isa<Operator>(U)) {
ConstantExpr *CE = cast<ConstantExpr>(U);
- convertUsersOfConstantsToInstructions(CE,
- /*RestrictToFunc=*/nullptr,
- /*RemoveDeadConstants=*/false,
- /*IncludeSelf=*/true);
- }
- if (isa<Instruction>(U)) {
- Instruction *Inst = cast<Instruction>(U);
- Function *F = Inst->getFunction();
- if (F)
- Impl.visit(*F);
+ for (User *UCE : make_early_inc_range(CE->users())) {
+ if (Instruction *Inst = dyn_cast<Instruction>(UCE))
+ Impl.visit(*Inst);
+ }
}
+ if (Instruction *Inst = dyn_cast<Instruction>(U))
+ Impl.visit(*Inst);
}
}
}
@@ -276,7 +283,6 @@ PreservedAnalyses DXILDataScalarization::run(Module &M,
if (!MadeChanges)
return PreservedAnalyses::all();
PreservedAnalyses PA;
- PA.preserve<DXILResourceAnalysis>();
return PA;
}
@@ -284,10 +290,6 @@ bool DXILDataScalarizationLegacy::runOnModule(Module &M) {
return findAndReplaceVectors(M);
}
-void DXILDataScalarizationLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addPreserved<DXILResourceWrapperPass>();
-}
-
char DXILDataScalarizationLegacy::ID = 0;
INITIALIZE_PASS_BEGIN(DXILDataScalarizationLegacy, DEBUG_TYPE,