summaryrefslogtreecommitdiff
path: root/source/slang/slang-emit-c-like.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-02-07 18:36:35 -0800
committerGitHub <noreply@github.com>2023-02-07 18:36:35 -0800
commit4be623c52a6518eb86756a0369706c1d6670f6bb (patch)
treec24f54e34db9f1f02c2d51808b15121eba9195a9 /source/slang/slang-emit-c-like.cpp
parent101f164b036d0c1c012243df69179559b6f40fb8 (diff)
Arithmetic simplifications and more IR clean up logic. (#2632)
Diffstat (limited to 'source/slang/slang-emit-c-like.cpp')
-rw-r--r--source/slang/slang-emit-c-like.cpp37
1 files changed, 26 insertions, 11 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index 160585e26..c664449e5 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -1082,6 +1082,7 @@ bool CLikeSourceEmitter::shouldFoldInstIntoUseSites(IRInst* inst)
case kIROp_Param:
case kIROp_Func:
case kIROp_Alloca:
+ case kIROp_Store:
return false;
// Never fold these, because their result cannot be computed
@@ -1997,17 +1998,6 @@ void CLikeSourceEmitter::defaultEmitInstExpr(IRInst* inst, const EmitOpInfo& inO
}
break;
- case kIROp_Store:
- {
- auto prec = getInfo(EmitOp::Assign);
- needClose = maybeEmitParens(outerPrec, prec);
-
- emitDereferenceOperand(inst->getOperand(0), leftSide(outerPrec, prec));
- m_writer->emit(" = ");
- emitOperand(inst->getOperand(1), rightSide(prec, outerPrec));
- }
- break;
-
case kIROp_Call:
{
emitCallExpr((IRCall*)inst, outerPrec);
@@ -2393,6 +2383,22 @@ void CLikeSourceEmitter::_emitInst(IRInst* inst)
}
break;
+ case kIROp_Store:
+ {
+ if (inst->getPrevInst() == inst->getOperand(0) && inst->getOperand(0)->getOp() == kIROp_Var)
+ {
+ // If we are storing into a var that is defined right before the store, we have
+ // already folded the store in the initialization of the var, so we can skip here.
+ break;
+ }
+ auto prec = getInfo(EmitOp::Assign);
+ emitDereferenceOperand(inst->getOperand(0), leftSide(getInfo(EmitOp::General), prec));
+ m_writer->emit(" = ");
+ emitOperand(inst->getOperand(1), rightSide(prec, getInfo(EmitOp::General)));
+ m_writer->emit(";\n");
+ }
+ break;
+
case kIROp_Param:
// Don't emit parameters, since they are declared as part of the function.
break;
@@ -3321,6 +3327,15 @@ void CLikeSourceEmitter::emitVar(IRVar* varDecl)
emitLayoutSemantics(varDecl);
+ if (auto store = as<IRStore>(varDecl->getNextInst()))
+ {
+ if (store->getPtr() == varDecl)
+ {
+ m_writer->emit(" = ");
+ emitOperand(store->getVal(), getInfo(EmitOp::General));
+ }
+ }
+
m_writer->emit(";\n");
}