[llvm-commits] CVS: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp
Brian Gaeke
gaeke at cs.uiuc.edu
Wed Jun 23 15:08:12 PDT 2004
Changes in directory reopt/lib/LightWtProfiling:
UnpackTraceFunction.cpp updated: 1.85 -> 1.86
---
Log message:
Include llvm/Support/CFG.h and llvm/iPHINode.h.
Refactor live-out copying code into addLiveOutCopy.
In rewriteEpilog, add new loop to perform phi elimination at trace exit edges,
using addLiveOutCopy.
---
Diffs of the changes: (+98 -45)
Index: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp
diff -u reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.85 reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.86
--- reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.85 Thu Jun 17 13:13:19 2004
+++ reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp Wed Jun 23 15:06:34 2004
@@ -19,7 +19,9 @@
#include "llvm/CodeGen/InstrSelection.h" // for TmpInstruction
#include "llvm/CodeGen/MachineFunctionInfo.h" // for getStaticStackSize()
#include "llvm/CodeGen/MachineCodeForInstruction.h"
+#include "llvm/Support/CFG.h"
#include "llvm/Module.h"
+#include "llvm/iPHINode.h"
#include "llvm/Assembly/Writer.h"
#include "Support/StringExtras.h" // for utostr()
#include "Support/Debug.h"
@@ -282,6 +284,45 @@
std::cerr << "copyConstantToRegister Output: " << **i << "\n");
}
+void UnpackTraceFunction::addLiveOutCopy (MachineFunction &MF, MachineBasicBlock &MBB,
+ const AllocInfo &Source, const AllocInfo &Target,
+ Value *liveOutValue, Value *liveOutTraceValue) {
+ const SparcV9RegInfo &TRI = *TM->getRegInfo ();
+ std::vector<MachineInstr *> mvec;
+ static const unsigned sp = SparcV9::o6, g1 = SparcV9::g1, g2 = SparcV9::g2;
+
+ assert (Target.AllocState == AllocInfo::Allocated
+ && "Live-out values must be in regs in the matrixFn");
+ mvec.clear ();
+ if (RegsToSave.find (Target.Placement) == RegsToSave.end ()) {
+ DEBUG (std::cerr << "addLiveOutCopy: Adding live-out's matrixFn "
+ << RegStr (Target.Placement) << " to RegsToSave set\n");
+ RegsToSave.insert (Target.Placement);
+ }
+ if (Source.AllocState == AllocInfo::NotAllocated) {
+ assert (liveOutValue);
+ assert (isa<Constant> (liveOutTraceValue)
+ && "Can't handle non-constant, non-allocated live-out value in traceFn");
+ copyConstantToRegister (MF, cast<Constant> (liveOutTraceValue), g1, mvec);
+ unsigned R = g1;
+ unsigned RegType = TRI.getRegType (R);
+ DEBUG (std::cerr << "addLiveOutCopy: " << liveOutValue->getName()
+ << " is a constant in TraceFn\n");
+ TRI.cpReg2MemMI (mvec, R, sp, stackOffsetForReg (Target.Placement),
+ RegType, g2);
+ } else {
+ assert (Source.AllocState == AllocInfo::Allocated
+ && "Can't handle live-out value spilled in traceFn");
+ unsigned R = Source.Placement;
+ unsigned RegType = TRI.getRegType (R);
+ TRI.cpReg2MemMI (mvec, R, sp, stackOffsetForReg (Target.Placement),
+ RegType, g2);
+ }
+ for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
+ ve = mvec.end (); vi != ve; ++vi)
+ MBB.push_back (*vi);
+}
+
void UnpackTraceFunction::rewriteEpilog (MachineFunction &MF,
MachineBasicBlock &MBB) {
const SparcV9RegInfo &TRI = *TM->getRegInfo ();
@@ -292,7 +333,55 @@
// (FIXME: may not work once we start doing optimizations!!! We will probably
// have to use a separate MBB)
MBB.clear ();
-
+
+ // Find the BasicBlock in MatrixFn that this block's return instr.
+ // would have returned to, by consulting our mapping information.
+ // FIXME: Currently we key our mapping info on TraceFn basic blocks.
+ // This is fragile - we should use ReturnInsts instead, I think.
+ const BasicBlock *RBB = TF->ReturnBlockForTraceExit[MBB.getBasicBlock ()];
+ assert (RBB && "Can't find matrix fn BB address to return to from trace");
+ DEBUG (std::cerr << "rewriteEpilog: Return block for trace exit path is:\n"
+ << *RBB << "\n");
+
+ // Perform Phi elimination along trace exit edges.
+ for (BasicBlock::const_iterator Inst = RBB->begin ();
+ const PHINode *PN = dyn_cast<PHINode> (Inst); ++Inst)
+ for (unsigned i = 0, e = PN->getNumIncomingValues (); i != e; ++i) {
+ // If PN has a source S from the source of the trace exit (MBB's predecessor),
+ // then we have to copy S's incoming value into its PhiCp register.
+ const BasicBlock *Src = PN->getIncomingBlock (i);
+ if (TF->T.contains (Src)) {
+ BasicBlock *SrcTTF = cast<BasicBlock> (TF->getCorrespondingValue (Src));
+ DEBUG (std::cerr << "rewriteEpilog: considering Phi node " << PN->getName ()
+ << ", found on-trace source #" << i << " (" << Src->getName ()
+ << ", corresponding to " << SrcTTF->getName () << " on trace)\n"
+ << *PN << "\n");
+ const BasicBlock *TraceExitingBB = *pred_begin(MBB.getBasicBlock ());
+ DEBUG (std::cerr << "rewriteEpilog: trace exiting BB for this block is "
+ << TraceExitingBB->getName () << "\n");
+ if (TraceExitingBB == SrcTTF) {
+ Value *V = PN->getIncomingValue (i);
+ DEBUG (std::cerr << "rewriteEpilog: found match: trace exiting BB is "
+ << "phi source; phi's incoming value from " << Src->getName ()
+ << " is " << V->getName () << "\n");
+ std::pair<AllocInfo, AllocInfo> outgoingValueAI = GetValueAllocState (TF, V, false);
+ std::pair<AllocInfo, AllocInfo> phiCpAI =
+ GetValueAllocState (TF, const_cast<PHINode *> (PN), true); // we want the PhiCp node
+ DEBUG (std::cerr << "rewriteEpilog: Outgoing value is in ";
+ PrintAI (outgoingValueAI.second);
+ std::cerr << " in TraceFn, and " << PN->getName() << "'s PhiCp node is in ";
+ PrintAI (phiCpAI.first);
+ std::cerr << " in MatrixFn\n");
+
+ AllocInfo &Target = phiCpAI.first, &Source = outgoingValueAI.second;
+ DEBUG (std::cerr << "rewriteEpilog: copying live-out phi value: ";
+ PrintValueAIs(V->getName(), Target, Source));
+ addLiveOutCopy (MF, MBB, Source, Target, V, TF->getCorrespondingValue (V, false));
+ }
+ }
+ DEBUG (std::cerr << "\n");
+ }
+
// Insert stores from each live-out variable's reg. in the trace
// to its stack slot in the trace function, from which it will be
// reloaded below into a register.
@@ -304,38 +393,9 @@
std::pair<AllocInfo, AllocInfo> ai = GetValueAllocState (TF, V, false);
// Source is traceFn's register, Target is matrixFn's register
AllocInfo &Target = ai.first, &Source = ai.second;
- assert (Target.AllocState == AllocInfo::Allocated
- && "Live-out values must be in regs in the matrixFn");
- mvec.clear ();
- if (RegsToSave.find (Target.Placement) == RegsToSave.end ()) {
- DEBUG (std::cerr << "rewriteProlog: Adding live-out's matrixFn "
- << RegStr (Target.Placement) << " to RegsToSave set\n");
- RegsToSave.insert (Target.Placement);
- }
DEBUG (std::cerr << "rewriteEpilog: copying live-out value: ";
PrintValueAIs(V->getName(), Target, Source));
- if (Source.AllocState == AllocInfo::NotAllocated) {
- assert (isa<Constant> (TF->getCorrespondingValue (V, false))
- && "Can't handle non-constant, non-allocated live-out value in traceFn");
- copyConstantToRegister (MF, cast<Constant> (TF->getCorrespondingValue (V, false)),
- g1, mvec);
- unsigned R = g1;
- unsigned RegType = TRI.getRegType (R);
- DEBUG (std::cerr << "rewriteEpilog: " << V->getName()
- << " is a constant in TraceFn\n");
- TRI.cpReg2MemMI (mvec, R, sp, stackOffsetForReg (Target.Placement),
- RegType, g2);
- } else {
- assert (Source.AllocState == AllocInfo::Allocated
- && "Can't handle live-out value spilled in traceFn");
- unsigned R = Source.Placement;
- unsigned RegType = TRI.getRegType (R);
- TRI.cpReg2MemMI (mvec, R, sp, stackOffsetForReg (Target.Placement),
- RegType, g2);
- }
- for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
- ve = mvec.end (); vi != ve; ++vi)
- MBB.push_back (*vi);
+ addLiveOutCopy (MF, MBB, Source, Target, V, TF->getCorrespondingValue (V, false));
}
// Restore old FP.
@@ -364,22 +424,15 @@
// Restore stack pointer.
BuildMI (&MBB, V9::ADDi, 3).addMReg (sp).addSImm (TotalStackSize)
.addMReg (sp, MachineOperand::Def);
-
+
// Let ReturnAddress be the address in memory of the compiled
- // code for the MachineBasicBlock in MatrixF that RI would have
- // returned to. Find it by using mapping info.
- const BasicBlock *RBB = MBB.getBasicBlock ();
- assert ((TF->ReturnBlockForTraceExit.find (RBB) !=
- TF->ReturnBlockForTraceExit.end ())
- && "Can't find matrix fn BB address to return to from trace");
- std::pair<uint64_t, uint64_t> BlockAddrs =
- getBasicBlockInfo(TF->ReturnBlockForTraceExit[RBB]);
+ // code for RBB. We find this by consulting our mapping information.
+ std::pair<uint64_t, uint64_t> BlockAddrs = getBasicBlockInfo(const_cast<BasicBlock *> (RBB));
uint64_t ReturnAddress = BlockAddrs.first;
- DEBUG (std::cerr << "rewriteEpilog: Return block for trace exit path is:\n"
- << *TF->ReturnBlockForTraceExit[RBB] << "\n"
- << "rewriteEpilog: Mapping info says addresses are: return address ="
- << " 0x" << std::hex << BlockAddrs.first << ", end of block = 0x"
- << BlockAddrs.second << std::dec << "\n");
+ DEBUG(std::cerr
+ << "rewriteEpilog: Mapping info says addresses are: return address ="
+ << " 0x" << std::hex << BlockAddrs.first << ", end of block = 0x"
+ << BlockAddrs.second << std::dec << "\n");
// Insert a branch back to the return BasicBlock of the matrix fn.
BuildMI (&MBB, V9::BA, 1)
.addPCDisp (ConstantUInt::get (Type::ULongTy, ReturnAddress));
More information about the llvm-commits
mailing list