Make code layout more consistent.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9426 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Misha Brukman
2003-10-23 17:39:37 +00:00
parent eea7cca366
commit 5e152593e0
6 changed files with 534 additions and 652 deletions

View File

@ -19,13 +19,13 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/CodeGen/InstrForest.h" #include "llvm/Constant.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/iTerminators.h" #include "llvm/iTerminators.h"
#include "llvm/iMemory.h" #include "llvm/iMemory.h"
#include "llvm/Constant.h"
#include "llvm/Type.h" #include "llvm/Type.h"
#include "llvm/CodeGen/InstrForest.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/MachineInstr.h"
#include "Support/STLExtras.h" #include "Support/STLExtras.h"
#include "Config/alloca.h" #include "Config/alloca.h"
@ -35,12 +35,10 @@
//------------------------------------------------------------------------ //------------------------------------------------------------------------
void void
InstrTreeNode::dump(int dumpChildren, int indent) const InstrTreeNode::dump(int dumpChildren, int indent) const {
{
dumpNode(indent); dumpNode(indent);
if (dumpChildren) if (dumpChildren) {
{
if (LeftChild) if (LeftChild)
LeftChild->dump(dumpChildren, indent+1); LeftChild->dump(dumpChildren, indent+1);
if (RightChild) if (RightChild)
@ -50,50 +48,35 @@ InstrTreeNode::dump(int dumpChildren, int indent) const
InstructionNode::InstructionNode(Instruction* I) InstructionNode::InstructionNode(Instruction* I)
: InstrTreeNode(NTInstructionNode, I), : InstrTreeNode(NTInstructionNode, I), codeIsFoldedIntoParent(false)
codeIsFoldedIntoParent(false)
{ {
opLabel = I->getOpcode(); opLabel = I->getOpcode();
// Distinguish special cases of some instructions such as Ret and Br // Distinguish special cases of some instructions such as Ret and Br
// //
if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue()) if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue()) {
{
opLabel = RetValueOp; // ret(value) operation opLabel = RetValueOp; // ret(value) operation
} }
else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional()) else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
{ {
opLabel = BrCondOp; // br(cond) operation opLabel = BrCondOp; // br(cond) operation
} } else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) {
else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
{
opLabel = SetCCOp; // common label for all SetCC ops opLabel = SetCCOp; // common label for all SetCC ops
} } else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0) {
else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
{
opLabel = AllocaN; // Alloca(ptr, N) operation opLabel = AllocaN; // Alloca(ptr, N) operation
} } else if (opLabel == Instruction::GetElementPtr &&
else if (opLabel == Instruction::GetElementPtr && cast<GetElementPtrInst>(I)->hasIndices()) {
cast<GetElementPtrInst>(I)->hasIndices())
{
opLabel = opLabel + 100; // getElem with index vector opLabel = opLabel + 100; // getElem with index vector
} } else if (opLabel == Instruction::Xor &&
else if (opLabel == Instruction::Xor && BinaryOperator::isNot(I)) {
BinaryOperator::isNot(I))
{
opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
: BNotOp; // bitwise Not operator : BNotOp; // bitwise Not operator
} } else if (opLabel == Instruction::And || opLabel == Instruction::Or ||
else if (opLabel == Instruction::And || opLabel == Instruction::Xor) {
opLabel == Instruction::Or ||
opLabel == Instruction::Xor)
{
// Distinguish bitwise operators from logical operators! // Distinguish bitwise operators from logical operators!
if (I->getType() != Type::BoolTy) if (I->getType() != Type::BoolTy)
opLabel = opLabel + 100; // bitwise operator opLabel = opLabel + 100; // bitwise operator
} } else if (opLabel == Instruction::Cast) {
else if (opLabel == Instruction::Cast)
{
const Type *ITy = I->getType(); const Type *ITy = I->getType();
switch(ITy->getPrimitiveID()) switch(ITy->getPrimitiveID())
{ {
@ -119,18 +102,15 @@ InstructionNode::InstructionNode(Instruction* I)
void void
InstructionNode::dumpNode(int indent) const InstructionNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
std::cerr << getInstruction()->getOpcodeName() std::cerr << getInstruction()->getOpcodeName()
<< " [label " << getOpLabel() << "]" << "\n"; << " [label " << getOpLabel() << "]" << "\n";
} }
void void
VRegListNode::dumpNode(int indent) const VRegListNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -139,8 +119,7 @@ VRegListNode::dumpNode(int indent) const
void void
VRegNode::dumpNode(int indent) const VRegNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -149,8 +128,7 @@ VRegNode::dumpNode(int indent) const
} }
void void
ConstantNode::dumpNode(int indent) const ConstantNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -158,9 +136,7 @@ ConstantNode::dumpNode(int indent) const
<< (int) getValue()->getValueType() << ")" << "\n"; << (int) getValue()->getValueType() << ")" << "\n";
} }
void void LabelNode::dumpNode(int indent) const {
LabelNode::dumpNode(int indent) const
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -173,56 +149,46 @@ LabelNode::dumpNode(int indent) const
// A forest of instruction trees, usually for a single method. // A forest of instruction trees, usually for a single method.
//------------------------------------------------------------------------ //------------------------------------------------------------------------
InstrForest::InstrForest(Function *F) InstrForest::InstrForest(Function *F) {
{
for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) { for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
buildTreeForInstruction(I); buildTreeForInstruction(I);
} }
} }
InstrForest::~InstrForest() InstrForest::~InstrForest() {
{
for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>); for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
} }
void void InstrForest::dump() const {
InstrForest::dump() const
{
for (const_root_iterator I = roots_begin(); I != roots_end(); ++I) for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
(*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0); (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
} }
inline void inline void InstrForest::eraseRoot(InstructionNode* node) {
InstrForest::eraseRoot(InstructionNode* node)
{
for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend(); for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
RI != RE; ++RI) RI != RE; ++RI)
if (*RI == node) if (*RI == node)
treeRoots.erase(RI.base()-1); treeRoots.erase(RI.base()-1);
} }
inline void inline void InstrForest::noteTreeNodeForInstr(Instruction *instr,
InstrForest::noteTreeNodeForInstr(Instruction *instr, InstructionNode *treeNode) {
InstructionNode *treeNode)
{
(*this)[instr] = treeNode; (*this)[instr] = treeNode;
treeRoots.push_back(treeNode); // mark node as root of a new tree treeRoots.push_back(treeNode); // mark node as root of a new tree
} }
inline void inline void InstrForest::setLeftChild(InstrTreeNode *parent,
InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child) InstrTreeNode *child) {
{
parent->LeftChild = child; parent->LeftChild = child;
child->Parent = parent; child->Parent = parent;
if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child)) if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
eraseRoot(instrNode); // no longer a tree root eraseRoot(instrNode); // no longer a tree root
} }
inline void inline void InstrForest::setRightChild(InstrTreeNode *parent,
InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child) InstrTreeNode *child) {
{
parent->RightChild = child; parent->RightChild = child;
child->Parent = parent; child->Parent = parent;
if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child)) if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
@ -230,12 +196,9 @@ InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
} }
InstructionNode* InstructionNode* InstrForest::buildTreeForInstruction(Instruction *instr) {
InstrForest::buildTreeForInstruction(Instruction *instr)
{
InstructionNode *treeNode = getTreeNodeForInstr(instr); InstructionNode *treeNode = getTreeNodeForInstr(instr);
if (treeNode) if (treeNode) {
{
// treeNode has already been constructed for this instruction // treeNode has already been constructed for this instruction
assert(treeNode->getInstruction() == instr); assert(treeNode->getInstruction() == instr);
return treeNode; return treeNode;
@ -246,8 +209,8 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
treeNode = new InstructionNode(instr); treeNode = new InstructionNode(instr);
noteTreeNodeForInstr(instr, treeNode); noteTreeNodeForInstr(instr, treeNode);
if (instr->getOpcode() == Instruction::Call) if (instr->getOpcode() == Instruction::Call) {
{ // Operands of call instruction // Operands of call instruction
return treeNode; return treeNode;
} }
@ -311,14 +274,10 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
{ {
// Recursively create a treeNode for it. // Recursively create a treeNode for it.
opTreeNode = buildTreeForInstruction((Instruction*)operand); opTreeNode = buildTreeForInstruction((Instruction*)operand);
} } else if (Constant *CPV = dyn_cast<Constant>(operand)) {
else if (Constant *CPV = dyn_cast<Constant>(operand))
{
// Create a leaf node for a constant // Create a leaf node for a constant
opTreeNode = new ConstantNode(CPV); opTreeNode = new ConstantNode(CPV);
} } else {
else
{
// Create a leaf node for the virtual register // Create a leaf node for the virtual register
opTreeNode = new VRegNode(operand); opTreeNode = new VRegNode(operand);
} }
@ -338,8 +297,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
InstrTreeNode *parent = treeNode; InstrTreeNode *parent = treeNode;
if (numChildren > 2) if (numChildren > 2) {
{
unsigned instrOpcode = treeNode->getInstruction()->getOpcode(); unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
assert(instrOpcode == Instruction::PHI || assert(instrOpcode == Instruction::PHI ||
instrOpcode == Instruction::Call || instrOpcode == Instruction::Call ||
@ -355,8 +313,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
int n; int n;
// Create a list node for children 2 .. N-1, if any // Create a list node for children 2 .. N-1, if any
for (n = numChildren-1; n >= 2; n--) for (n = numChildren-1; n >= 2; n--) {
{
// We have more than two children // We have more than two children
InstrTreeNode *listNode = new VRegListNode(); InstrTreeNode *listNode = new VRegListNode();
setRightChild(parent, listNode); setRightChild(parent, listNode);
@ -365,8 +322,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
} }
// Now insert the last remaining child (if any). // Now insert the last remaining child (if any).
if (numChildren >= 2) if (numChildren >= 2) {
{
assert(n == 1); assert(n == 1);
setRightChild(parent, childArray[numChildren - 1]); setRightChild(parent, childArray[numChildren - 1]);
} }

View File

@ -14,19 +14,19 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/CodeGen/InstrSelection.h"
#include "llvm/CodeGen/InstrSelectionSupport.h"
#include "llvm/CodeGen/InstrForest.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Target/TargetRegInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/iPHINode.h" #include "llvm/iPHINode.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/CodeGen/InstrForest.h"
#include "llvm/CodeGen/InstrSelection.h"
#include "llvm/CodeGen/InstrSelectionSupport.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegInfo.h"
#include "Support/CommandLine.h" #include "Support/CommandLine.h"
#include "Support/LeakDetector.h" #include "Support/LeakDetector.h"
using std::vector; #include <vector>
std::vector<MachineInstr*> std::vector<MachineInstr*>
FixConstantOperandsForInstr(Instruction* vmInstr, MachineInstr* minstr, FixConstantOperandsForInstr(Instruction* vmInstr, MachineInstr* minstr,
@ -66,7 +66,7 @@ namespace {
TargetMachine &Target; TargetMachine &Target;
void InsertCodeForPhis(Function &F); void InsertCodeForPhis(Function &F);
void InsertPhiElimInstructions(BasicBlock *BB, void InsertPhiElimInstructions(BasicBlock *BB,
const vector<MachineInstr*>& CpVec); const std::vector<MachineInstr*>& CpVec);
void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt); void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt);
void PostprocessMachineCodeForTree(InstructionNode* instrNode, void PostprocessMachineCodeForTree(InstructionNode* instrNode,
int ruleForNode, short* nts); int ruleForNode, short* nts);
@ -89,9 +89,8 @@ TmpInstruction::TmpInstruction(MachineCodeForInstruction& mcfi,
mcfi.addTemp(this); mcfi.addTemp(this);
Operands.push_back(Use(s1, this)); // s1 must be non-null Operands.push_back(Use(s1, this)); // s1 must be non-null
if (s2) { if (s2)
Operands.push_back(Use(s2, this)); Operands.push_back(Use(s2, this));
}
// TmpInstructions should not be garbage checked. // TmpInstructions should not be garbage checked.
LeakDetector::removeGarbageObject(this); LeakDetector::removeGarbageObject(this);
@ -106,8 +105,10 @@ TmpInstruction::TmpInstruction(MachineCodeForInstruction& mcfi,
{ {
mcfi.addTemp(this); mcfi.addTemp(this);
if (s1) { Operands.push_back(Use(s1, this)); } if (s1)
if (s2) { Operands.push_back(Use(s2, this)); } Operands.push_back(Use(s1, this));
if (s2)
Operands.push_back(Use(s2, this));
// TmpInstructions should not be garbage checked. // TmpInstructions should not be garbage checked.
LeakDetector::removeGarbageObject(this); LeakDetector::removeGarbageObject(this);
@ -121,8 +122,7 @@ bool InstructionSelection::runOnFunction(Function &F)
// //
InstrForest instrForest(&F); InstrForest instrForest(&F);
if (SelectDebugLevel >= Select_DebugInstTrees) if (SelectDebugLevel >= Select_DebugInstTrees) {
{
std::cerr << "\n\n*** Input to instruction selection for function " std::cerr << "\n\n*** Input to instruction selection for function "
<< F.getName() << "\n\n" << F << F.getName() << "\n\n" << F
<< "\n\n*** Instruction trees for function " << "\n\n*** Instruction trees for function "
@ -134,16 +134,14 @@ bool InstructionSelection::runOnFunction(Function &F)
// Invoke BURG instruction selection for each tree // Invoke BURG instruction selection for each tree
// //
for (InstrForest::const_root_iterator RI = instrForest.roots_begin(); for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
RI != instrForest.roots_end(); ++RI) RI != instrForest.roots_end(); ++RI) {
{
InstructionNode* basicNode = *RI; InstructionNode* basicNode = *RI;
assert(basicNode->parent() == NULL && "A `root' node has a parent?"); assert(basicNode->parent() == NULL && "A `root' node has a parent?");
// Invoke BURM to label each tree node with a state // Invoke BURM to label each tree node with a state
burm_label(basicNode); burm_label(basicNode);
if (SelectDebugLevel >= Select_DebugBurgTrees) if (SelectDebugLevel >= Select_DebugBurgTrees) {
{
printcover(basicNode, 1, 0); printcover(basicNode, 1, 0);
std::cerr << "\nCover cost == " << treecost(basicNode, 1, 0) <<"\n\n"; std::cerr << "\nCover cost == " << treecost(basicNode, 1, 0) <<"\n\n";
printMatches(basicNode); printMatches(basicNode);
@ -172,8 +170,7 @@ bool InstructionSelection::runOnFunction(Function &F)
// Insert phi elimination code // Insert phi elimination code
InsertCodeForPhis(F); InsertCodeForPhis(F);
if (SelectDebugLevel >= Select_PrintMachineCode) if (SelectDebugLevel >= Select_PrintMachineCode) {
{
std::cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n"; std::cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
MachineFunction::get(&F).dump(); MachineFunction::get(&F).dump();
} }
@ -187,8 +184,7 @@ bool InstructionSelection::runOnFunction(Function &F)
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void void
InstructionSelection::InsertCodeForPhis(Function &F) InstructionSelection::InsertCodeForPhis(Function &F) {
{
// for all basic blocks in function // for all basic blocks in function
// //
MachineFunction &MF = MachineFunction::get(&F); MachineFunction &MF = MachineFunction::get(&F);
@ -207,12 +203,12 @@ InstructionSelection::InsertCodeForPhis(Function &F)
// //
for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
// insert the copy instruction to the predecessor BB // insert the copy instruction to the predecessor BB
vector<MachineInstr*> mvec, CpVec; std::vector<MachineInstr*> mvec, CpVec;
Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes, Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
mvec); mvec);
for (vector<MachineInstr*>::iterator MI=mvec.begin(); for (std::vector<MachineInstr*>::iterator MI=mvec.begin();
MI != mvec.end(); ++MI) { MI != mvec.end(); ++MI) {
vector<MachineInstr*> CpVec2 = std::vector<MachineInstr*> CpVec2 =
FixConstantOperandsForInstr(const_cast<PHINode*>(PN), *MI, Target); FixConstantOperandsForInstr(const_cast<PHINode*>(PN), *MI, Target);
CpVec2.push_back(*MI); CpVec2.push_back(*MI);
CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end()); CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
@ -221,7 +217,7 @@ InstructionSelection::InsertCodeForPhis(Function &F)
InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec); InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
} }
vector<MachineInstr*> mvec; std::vector<MachineInstr*> mvec;
Target.getRegInfo().cpValue2Value(PhiCpRes, const_cast<PHINode*>(PN), Target.getRegInfo().cpValue2Value(PhiCpRes, const_cast<PHINode*>(PN),
mvec); mvec);
BB->insert(BB->begin(), mvec.begin(), mvec.end()); BB->insert(BB->begin(), mvec.begin(), mvec.end());
@ -236,7 +232,7 @@ InstructionSelection::InsertCodeForPhis(Function &F)
void void
InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB, InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
const vector<MachineInstr*>& CpVec) const std::vector<MachineInstr*>& CpVec)
{ {
Instruction *TermInst = (Instruction*)BB->getTerminator(); Instruction *TermInst = (Instruction*)BB->getTerminator();
MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst); MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
@ -304,9 +300,8 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// (If this is a list node, not an instruction, then skip this step). // (If this is a list node, not an instruction, then skip this step).
// This function is specific to the target architecture. // This function is specific to the target architecture.
// //
if (treeRoot->opLabel != VRegListOp) if (treeRoot->opLabel != VRegListOp) {
{ std::vector<MachineInstr*> minstrVec;
vector<MachineInstr*> minstrVec;
InstructionNode* instrNode = (InstructionNode*)treeRoot; InstructionNode* instrNode = (InstructionNode*)treeRoot;
assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode); assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
@ -320,8 +315,8 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// Then, recursively compile the child nodes, if any. // Then, recursively compile the child nodes, if any.
// //
if (nts[0]) if (nts[0]) {
{ // i.e., there is at least one kid // i.e., there is at least one kid
InstrTreeNode* kids[2]; InstrTreeNode* kids[2];
int currentRule = ruleForNode; int currentRule = ruleForNode;
burm_kids(treeRoot, currentRule, kids); burm_kids(treeRoot, currentRule, kids);
@ -329,8 +324,7 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// First skip over any chain rules so that we don't visit // First skip over any chain rules so that we don't visit
// the current node again. // the current node again.
// //
while (ThisIsAChainRule(currentRule)) while (ThisIsAChainRule(currentRule)) {
{
currentRule = burm_rule(treeRoot->state, nts[0]); currentRule = burm_rule(treeRoot->state, nts[0]);
nts = burm_nts[currentRule]; nts = burm_nts[currentRule];
burm_kids(treeRoot, currentRule, kids); burm_kids(treeRoot, currentRule, kids);
@ -339,8 +333,7 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// Now we have the first non-chain rule so we have found // Now we have the first non-chain rule so we have found
// the actual child nodes. Recursively compile them. // the actual child nodes. Recursively compile them.
// //
for (unsigned i = 0; nts[i]; i++) for (unsigned i = 0; nts[i]; i++) {
{
assert(i < 2); assert(i < 2);
InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType(); InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
if (nodeType == InstrTreeNode::NTVRegListNode || if (nodeType == InstrTreeNode::NTVRegListNode ||
@ -373,9 +366,8 @@ InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode,
// //
Instruction* vmInstr = instrNode->getInstruction(); Instruction* vmInstr = instrNode->getInstruction();
MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr); MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
for (unsigned i = mvec.size(); i != 0; --i) for (unsigned i = mvec.size(); i != 0; --i) {
{ std::vector<MachineInstr*> loadConstVec =
vector<MachineInstr*> loadConstVec =
FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target); FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target);
mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end()); mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end());

View File

@ -66,14 +66,11 @@ ChooseRegOrImmed(int64_t intValue,
getImmedValue = 0; getImmedValue = 0;
if (canUseImmed && if (canUseImmed &&
target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) {
{
opType = isSigned? MachineOperand::MO_SignExtendedImmed opType = isSigned? MachineOperand::MO_SignExtendedImmed
: MachineOperand::MO_UnextendedImmed; : MachineOperand::MO_UnextendedImmed;
getImmedValue = intValue; getImmedValue = intValue;
} } else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) {
else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
{
opType = MachineOperand::MO_MachineRegister; opType = MachineOperand::MO_MachineRegister;
getMachineRegNum = target.getRegInfo().getZeroRegNum(); getMachineRegNum = target.getRegInfo().getZeroRegNum();
} }
@ -158,8 +155,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
MachineOperand::MO_VirtualRegister; MachineOperand::MO_VirtualRegister;
// Operand may be a virtual register or a compile-time constant // Operand may be a virtual register or a compile-time constant
if (mop.getType() == MachineOperand::MO_VirtualRegister) if (mop.getType() == MachineOperand::MO_VirtualRegister) {
{
assert(mop.getVRegValue() != NULL); assert(mop.getVRegValue() != NULL);
opValue = mop.getVRegValue(); opValue = mop.getVRegValue();
if (Constant *opConst = dyn_cast<Constant>(opValue)) { if (Constant *opConst = dyn_cast<Constant>(opValue)) {
@ -169,9 +165,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
if (opType == MachineOperand::MO_VirtualRegister) if (opType == MachineOperand::MO_VirtualRegister)
constantThatMustBeLoaded = true; constantThatMustBeLoaded = true;
} }
} } else {
else
{
assert(mop.isImmediate()); assert(mop.isImmediate());
bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed; bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
@ -196,8 +190,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
if (opType == mop.getType()) if (opType == mop.getType())
continue; // no change: this is the most common case continue; // no change: this is the most common case
if (opType == MachineOperand::MO_VirtualRegister) if (opType == MachineOperand::MO_VirtualRegister) {
{
constantThatMustBeLoaded = true; constantThatMustBeLoaded = true;
opValue = isSigned opValue = isSigned
? (Value*)ConstantSInt::get(Type::LongTy, immedValue) ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
@ -250,8 +243,8 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target); InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
minstr->setImplicitRef(i, tmpReg); minstr->setImplicitRef(i, tmpReg);
if (isCall) if (isCall) {
{ // find and replace the argument in the CallArgsDescriptor // find and replace the argument in the CallArgsDescriptor
unsigned i=lastCallArgNum; unsigned i=lastCallArgNum;
while (argDesc->getArgInfo(i).getArgVal() != oldVal) while (argDesc->getArgInfo(i).getArgVal() != oldVal)
++i; ++i;

View File

@ -19,13 +19,13 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/CodeGen/InstrForest.h" #include "llvm/Constant.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/iTerminators.h" #include "llvm/iTerminators.h"
#include "llvm/iMemory.h" #include "llvm/iMemory.h"
#include "llvm/Constant.h"
#include "llvm/Type.h" #include "llvm/Type.h"
#include "llvm/CodeGen/InstrForest.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/MachineInstr.h"
#include "Support/STLExtras.h" #include "Support/STLExtras.h"
#include "Config/alloca.h" #include "Config/alloca.h"
@ -35,12 +35,10 @@
//------------------------------------------------------------------------ //------------------------------------------------------------------------
void void
InstrTreeNode::dump(int dumpChildren, int indent) const InstrTreeNode::dump(int dumpChildren, int indent) const {
{
dumpNode(indent); dumpNode(indent);
if (dumpChildren) if (dumpChildren) {
{
if (LeftChild) if (LeftChild)
LeftChild->dump(dumpChildren, indent+1); LeftChild->dump(dumpChildren, indent+1);
if (RightChild) if (RightChild)
@ -50,50 +48,35 @@ InstrTreeNode::dump(int dumpChildren, int indent) const
InstructionNode::InstructionNode(Instruction* I) InstructionNode::InstructionNode(Instruction* I)
: InstrTreeNode(NTInstructionNode, I), : InstrTreeNode(NTInstructionNode, I), codeIsFoldedIntoParent(false)
codeIsFoldedIntoParent(false)
{ {
opLabel = I->getOpcode(); opLabel = I->getOpcode();
// Distinguish special cases of some instructions such as Ret and Br // Distinguish special cases of some instructions such as Ret and Br
// //
if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue()) if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue()) {
{
opLabel = RetValueOp; // ret(value) operation opLabel = RetValueOp; // ret(value) operation
} }
else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional()) else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional())
{ {
opLabel = BrCondOp; // br(cond) operation opLabel = BrCondOp; // br(cond) operation
} } else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) {
else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT)
{
opLabel = SetCCOp; // common label for all SetCC ops opLabel = SetCCOp; // common label for all SetCC ops
} } else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0) {
else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0)
{
opLabel = AllocaN; // Alloca(ptr, N) operation opLabel = AllocaN; // Alloca(ptr, N) operation
} } else if (opLabel == Instruction::GetElementPtr &&
else if (opLabel == Instruction::GetElementPtr && cast<GetElementPtrInst>(I)->hasIndices()) {
cast<GetElementPtrInst>(I)->hasIndices())
{
opLabel = opLabel + 100; // getElem with index vector opLabel = opLabel + 100; // getElem with index vector
} } else if (opLabel == Instruction::Xor &&
else if (opLabel == Instruction::Xor && BinaryOperator::isNot(I)) {
BinaryOperator::isNot(I))
{
opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator
: BNotOp; // bitwise Not operator : BNotOp; // bitwise Not operator
} } else if (opLabel == Instruction::And || opLabel == Instruction::Or ||
else if (opLabel == Instruction::And || opLabel == Instruction::Xor) {
opLabel == Instruction::Or ||
opLabel == Instruction::Xor)
{
// Distinguish bitwise operators from logical operators! // Distinguish bitwise operators from logical operators!
if (I->getType() != Type::BoolTy) if (I->getType() != Type::BoolTy)
opLabel = opLabel + 100; // bitwise operator opLabel = opLabel + 100; // bitwise operator
} } else if (opLabel == Instruction::Cast) {
else if (opLabel == Instruction::Cast)
{
const Type *ITy = I->getType(); const Type *ITy = I->getType();
switch(ITy->getPrimitiveID()) switch(ITy->getPrimitiveID())
{ {
@ -119,18 +102,15 @@ InstructionNode::InstructionNode(Instruction* I)
void void
InstructionNode::dumpNode(int indent) const InstructionNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
std::cerr << getInstruction()->getOpcodeName() std::cerr << getInstruction()->getOpcodeName()
<< " [label " << getOpLabel() << "]" << "\n"; << " [label " << getOpLabel() << "]" << "\n";
} }
void void
VRegListNode::dumpNode(int indent) const VRegListNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -139,8 +119,7 @@ VRegListNode::dumpNode(int indent) const
void void
VRegNode::dumpNode(int indent) const VRegNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -149,8 +128,7 @@ VRegNode::dumpNode(int indent) const
} }
void void
ConstantNode::dumpNode(int indent) const ConstantNode::dumpNode(int indent) const {
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -158,9 +136,7 @@ ConstantNode::dumpNode(int indent) const
<< (int) getValue()->getValueType() << ")" << "\n"; << (int) getValue()->getValueType() << ")" << "\n";
} }
void void LabelNode::dumpNode(int indent) const {
LabelNode::dumpNode(int indent) const
{
for (int i=0; i < indent; i++) for (int i=0; i < indent; i++)
std::cerr << " "; std::cerr << " ";
@ -173,56 +149,46 @@ LabelNode::dumpNode(int indent) const
// A forest of instruction trees, usually for a single method. // A forest of instruction trees, usually for a single method.
//------------------------------------------------------------------------ //------------------------------------------------------------------------
InstrForest::InstrForest(Function *F) InstrForest::InstrForest(Function *F) {
{
for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) { for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
buildTreeForInstruction(I); buildTreeForInstruction(I);
} }
} }
InstrForest::~InstrForest() InstrForest::~InstrForest() {
{
for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>); for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
} }
void void InstrForest::dump() const {
InstrForest::dump() const
{
for (const_root_iterator I = roots_begin(); I != roots_end(); ++I) for (const_root_iterator I = roots_begin(); I != roots_end(); ++I)
(*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0); (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
} }
inline void inline void InstrForest::eraseRoot(InstructionNode* node) {
InstrForest::eraseRoot(InstructionNode* node)
{
for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend(); for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend();
RI != RE; ++RI) RI != RE; ++RI)
if (*RI == node) if (*RI == node)
treeRoots.erase(RI.base()-1); treeRoots.erase(RI.base()-1);
} }
inline void inline void InstrForest::noteTreeNodeForInstr(Instruction *instr,
InstrForest::noteTreeNodeForInstr(Instruction *instr, InstructionNode *treeNode) {
InstructionNode *treeNode)
{
(*this)[instr] = treeNode; (*this)[instr] = treeNode;
treeRoots.push_back(treeNode); // mark node as root of a new tree treeRoots.push_back(treeNode); // mark node as root of a new tree
} }
inline void inline void InstrForest::setLeftChild(InstrTreeNode *parent,
InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child) InstrTreeNode *child) {
{
parent->LeftChild = child; parent->LeftChild = child;
child->Parent = parent; child->Parent = parent;
if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child)) if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
eraseRoot(instrNode); // no longer a tree root eraseRoot(instrNode); // no longer a tree root
} }
inline void inline void InstrForest::setRightChild(InstrTreeNode *parent,
InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child) InstrTreeNode *child) {
{
parent->RightChild = child; parent->RightChild = child;
child->Parent = parent; child->Parent = parent;
if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child)) if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
@ -230,12 +196,9 @@ InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
} }
InstructionNode* InstructionNode* InstrForest::buildTreeForInstruction(Instruction *instr) {
InstrForest::buildTreeForInstruction(Instruction *instr)
{
InstructionNode *treeNode = getTreeNodeForInstr(instr); InstructionNode *treeNode = getTreeNodeForInstr(instr);
if (treeNode) if (treeNode) {
{
// treeNode has already been constructed for this instruction // treeNode has already been constructed for this instruction
assert(treeNode->getInstruction() == instr); assert(treeNode->getInstruction() == instr);
return treeNode; return treeNode;
@ -246,8 +209,8 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
treeNode = new InstructionNode(instr); treeNode = new InstructionNode(instr);
noteTreeNodeForInstr(instr, treeNode); noteTreeNodeForInstr(instr, treeNode);
if (instr->getOpcode() == Instruction::Call) if (instr->getOpcode() == Instruction::Call) {
{ // Operands of call instruction // Operands of call instruction
return treeNode; return treeNode;
} }
@ -311,14 +274,10 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
{ {
// Recursively create a treeNode for it. // Recursively create a treeNode for it.
opTreeNode = buildTreeForInstruction((Instruction*)operand); opTreeNode = buildTreeForInstruction((Instruction*)operand);
} } else if (Constant *CPV = dyn_cast<Constant>(operand)) {
else if (Constant *CPV = dyn_cast<Constant>(operand))
{
// Create a leaf node for a constant // Create a leaf node for a constant
opTreeNode = new ConstantNode(CPV); opTreeNode = new ConstantNode(CPV);
} } else {
else
{
// Create a leaf node for the virtual register // Create a leaf node for the virtual register
opTreeNode = new VRegNode(operand); opTreeNode = new VRegNode(operand);
} }
@ -338,8 +297,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
InstrTreeNode *parent = treeNode; InstrTreeNode *parent = treeNode;
if (numChildren > 2) if (numChildren > 2) {
{
unsigned instrOpcode = treeNode->getInstruction()->getOpcode(); unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
assert(instrOpcode == Instruction::PHI || assert(instrOpcode == Instruction::PHI ||
instrOpcode == Instruction::Call || instrOpcode == Instruction::Call ||
@ -355,8 +313,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
int n; int n;
// Create a list node for children 2 .. N-1, if any // Create a list node for children 2 .. N-1, if any
for (n = numChildren-1; n >= 2; n--) for (n = numChildren-1; n >= 2; n--) {
{
// We have more than two children // We have more than two children
InstrTreeNode *listNode = new VRegListNode(); InstrTreeNode *listNode = new VRegListNode();
setRightChild(parent, listNode); setRightChild(parent, listNode);
@ -365,8 +322,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
} }
// Now insert the last remaining child (if any). // Now insert the last remaining child (if any).
if (numChildren >= 2) if (numChildren >= 2) {
{
assert(n == 1); assert(n == 1);
setRightChild(parent, childArray[numChildren - 1]); setRightChild(parent, childArray[numChildren - 1]);
} }

View File

@ -14,19 +14,19 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/CodeGen/InstrSelection.h"
#include "llvm/CodeGen/InstrSelectionSupport.h"
#include "llvm/CodeGen/InstrForest.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Target/TargetRegInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/iPHINode.h" #include "llvm/iPHINode.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/CodeGen/InstrForest.h"
#include "llvm/CodeGen/InstrSelection.h"
#include "llvm/CodeGen/InstrSelectionSupport.h"
#include "llvm/CodeGen/MachineCodeForInstruction.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegInfo.h"
#include "Support/CommandLine.h" #include "Support/CommandLine.h"
#include "Support/LeakDetector.h" #include "Support/LeakDetector.h"
using std::vector; #include <vector>
std::vector<MachineInstr*> std::vector<MachineInstr*>
FixConstantOperandsForInstr(Instruction* vmInstr, MachineInstr* minstr, FixConstantOperandsForInstr(Instruction* vmInstr, MachineInstr* minstr,
@ -66,7 +66,7 @@ namespace {
TargetMachine &Target; TargetMachine &Target;
void InsertCodeForPhis(Function &F); void InsertCodeForPhis(Function &F);
void InsertPhiElimInstructions(BasicBlock *BB, void InsertPhiElimInstructions(BasicBlock *BB,
const vector<MachineInstr*>& CpVec); const std::vector<MachineInstr*>& CpVec);
void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt); void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt);
void PostprocessMachineCodeForTree(InstructionNode* instrNode, void PostprocessMachineCodeForTree(InstructionNode* instrNode,
int ruleForNode, short* nts); int ruleForNode, short* nts);
@ -89,9 +89,8 @@ TmpInstruction::TmpInstruction(MachineCodeForInstruction& mcfi,
mcfi.addTemp(this); mcfi.addTemp(this);
Operands.push_back(Use(s1, this)); // s1 must be non-null Operands.push_back(Use(s1, this)); // s1 must be non-null
if (s2) { if (s2)
Operands.push_back(Use(s2, this)); Operands.push_back(Use(s2, this));
}
// TmpInstructions should not be garbage checked. // TmpInstructions should not be garbage checked.
LeakDetector::removeGarbageObject(this); LeakDetector::removeGarbageObject(this);
@ -106,8 +105,10 @@ TmpInstruction::TmpInstruction(MachineCodeForInstruction& mcfi,
{ {
mcfi.addTemp(this); mcfi.addTemp(this);
if (s1) { Operands.push_back(Use(s1, this)); } if (s1)
if (s2) { Operands.push_back(Use(s2, this)); } Operands.push_back(Use(s1, this));
if (s2)
Operands.push_back(Use(s2, this));
// TmpInstructions should not be garbage checked. // TmpInstructions should not be garbage checked.
LeakDetector::removeGarbageObject(this); LeakDetector::removeGarbageObject(this);
@ -121,8 +122,7 @@ bool InstructionSelection::runOnFunction(Function &F)
// //
InstrForest instrForest(&F); InstrForest instrForest(&F);
if (SelectDebugLevel >= Select_DebugInstTrees) if (SelectDebugLevel >= Select_DebugInstTrees) {
{
std::cerr << "\n\n*** Input to instruction selection for function " std::cerr << "\n\n*** Input to instruction selection for function "
<< F.getName() << "\n\n" << F << F.getName() << "\n\n" << F
<< "\n\n*** Instruction trees for function " << "\n\n*** Instruction trees for function "
@ -134,16 +134,14 @@ bool InstructionSelection::runOnFunction(Function &F)
// Invoke BURG instruction selection for each tree // Invoke BURG instruction selection for each tree
// //
for (InstrForest::const_root_iterator RI = instrForest.roots_begin(); for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
RI != instrForest.roots_end(); ++RI) RI != instrForest.roots_end(); ++RI) {
{
InstructionNode* basicNode = *RI; InstructionNode* basicNode = *RI;
assert(basicNode->parent() == NULL && "A `root' node has a parent?"); assert(basicNode->parent() == NULL && "A `root' node has a parent?");
// Invoke BURM to label each tree node with a state // Invoke BURM to label each tree node with a state
burm_label(basicNode); burm_label(basicNode);
if (SelectDebugLevel >= Select_DebugBurgTrees) if (SelectDebugLevel >= Select_DebugBurgTrees) {
{
printcover(basicNode, 1, 0); printcover(basicNode, 1, 0);
std::cerr << "\nCover cost == " << treecost(basicNode, 1, 0) <<"\n\n"; std::cerr << "\nCover cost == " << treecost(basicNode, 1, 0) <<"\n\n";
printMatches(basicNode); printMatches(basicNode);
@ -172,8 +170,7 @@ bool InstructionSelection::runOnFunction(Function &F)
// Insert phi elimination code // Insert phi elimination code
InsertCodeForPhis(F); InsertCodeForPhis(F);
if (SelectDebugLevel >= Select_PrintMachineCode) if (SelectDebugLevel >= Select_PrintMachineCode) {
{
std::cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n"; std::cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
MachineFunction::get(&F).dump(); MachineFunction::get(&F).dump();
} }
@ -187,8 +184,7 @@ bool InstructionSelection::runOnFunction(Function &F)
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void void
InstructionSelection::InsertCodeForPhis(Function &F) InstructionSelection::InsertCodeForPhis(Function &F) {
{
// for all basic blocks in function // for all basic blocks in function
// //
MachineFunction &MF = MachineFunction::get(&F); MachineFunction &MF = MachineFunction::get(&F);
@ -207,12 +203,12 @@ InstructionSelection::InsertCodeForPhis(Function &F)
// //
for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
// insert the copy instruction to the predecessor BB // insert the copy instruction to the predecessor BB
vector<MachineInstr*> mvec, CpVec; std::vector<MachineInstr*> mvec, CpVec;
Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes, Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
mvec); mvec);
for (vector<MachineInstr*>::iterator MI=mvec.begin(); for (std::vector<MachineInstr*>::iterator MI=mvec.begin();
MI != mvec.end(); ++MI) { MI != mvec.end(); ++MI) {
vector<MachineInstr*> CpVec2 = std::vector<MachineInstr*> CpVec2 =
FixConstantOperandsForInstr(const_cast<PHINode*>(PN), *MI, Target); FixConstantOperandsForInstr(const_cast<PHINode*>(PN), *MI, Target);
CpVec2.push_back(*MI); CpVec2.push_back(*MI);
CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end()); CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
@ -221,7 +217,7 @@ InstructionSelection::InsertCodeForPhis(Function &F)
InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec); InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
} }
vector<MachineInstr*> mvec; std::vector<MachineInstr*> mvec;
Target.getRegInfo().cpValue2Value(PhiCpRes, const_cast<PHINode*>(PN), Target.getRegInfo().cpValue2Value(PhiCpRes, const_cast<PHINode*>(PN),
mvec); mvec);
BB->insert(BB->begin(), mvec.begin(), mvec.end()); BB->insert(BB->begin(), mvec.begin(), mvec.end());
@ -236,7 +232,7 @@ InstructionSelection::InsertCodeForPhis(Function &F)
void void
InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB, InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
const vector<MachineInstr*>& CpVec) const std::vector<MachineInstr*>& CpVec)
{ {
Instruction *TermInst = (Instruction*)BB->getTerminator(); Instruction *TermInst = (Instruction*)BB->getTerminator();
MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst); MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
@ -304,9 +300,8 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// (If this is a list node, not an instruction, then skip this step). // (If this is a list node, not an instruction, then skip this step).
// This function is specific to the target architecture. // This function is specific to the target architecture.
// //
if (treeRoot->opLabel != VRegListOp) if (treeRoot->opLabel != VRegListOp) {
{ std::vector<MachineInstr*> minstrVec;
vector<MachineInstr*> minstrVec;
InstructionNode* instrNode = (InstructionNode*)treeRoot; InstructionNode* instrNode = (InstructionNode*)treeRoot;
assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode); assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
@ -320,8 +315,8 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// Then, recursively compile the child nodes, if any. // Then, recursively compile the child nodes, if any.
// //
if (nts[0]) if (nts[0]) {
{ // i.e., there is at least one kid // i.e., there is at least one kid
InstrTreeNode* kids[2]; InstrTreeNode* kids[2];
int currentRule = ruleForNode; int currentRule = ruleForNode;
burm_kids(treeRoot, currentRule, kids); burm_kids(treeRoot, currentRule, kids);
@ -329,8 +324,7 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// First skip over any chain rules so that we don't visit // First skip over any chain rules so that we don't visit
// the current node again. // the current node again.
// //
while (ThisIsAChainRule(currentRule)) while (ThisIsAChainRule(currentRule)) {
{
currentRule = burm_rule(treeRoot->state, nts[0]); currentRule = burm_rule(treeRoot->state, nts[0]);
nts = burm_nts[currentRule]; nts = burm_nts[currentRule];
burm_kids(treeRoot, currentRule, kids); burm_kids(treeRoot, currentRule, kids);
@ -339,8 +333,7 @@ InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
// Now we have the first non-chain rule so we have found // Now we have the first non-chain rule so we have found
// the actual child nodes. Recursively compile them. // the actual child nodes. Recursively compile them.
// //
for (unsigned i = 0; nts[i]; i++) for (unsigned i = 0; nts[i]; i++) {
{
assert(i < 2); assert(i < 2);
InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType(); InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
if (nodeType == InstrTreeNode::NTVRegListNode || if (nodeType == InstrTreeNode::NTVRegListNode ||
@ -373,9 +366,8 @@ InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode,
// //
Instruction* vmInstr = instrNode->getInstruction(); Instruction* vmInstr = instrNode->getInstruction();
MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr); MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
for (unsigned i = mvec.size(); i != 0; --i) for (unsigned i = mvec.size(); i != 0; --i) {
{ std::vector<MachineInstr*> loadConstVec =
vector<MachineInstr*> loadConstVec =
FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target); FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target);
mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end()); mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end());

View File

@ -66,14 +66,11 @@ ChooseRegOrImmed(int64_t intValue,
getImmedValue = 0; getImmedValue = 0;
if (canUseImmed && if (canUseImmed &&
target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) {
{
opType = isSigned? MachineOperand::MO_SignExtendedImmed opType = isSigned? MachineOperand::MO_SignExtendedImmed
: MachineOperand::MO_UnextendedImmed; : MachineOperand::MO_UnextendedImmed;
getImmedValue = intValue; getImmedValue = intValue;
} } else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) {
else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
{
opType = MachineOperand::MO_MachineRegister; opType = MachineOperand::MO_MachineRegister;
getMachineRegNum = target.getRegInfo().getZeroRegNum(); getMachineRegNum = target.getRegInfo().getZeroRegNum();
} }
@ -158,8 +155,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
MachineOperand::MO_VirtualRegister; MachineOperand::MO_VirtualRegister;
// Operand may be a virtual register or a compile-time constant // Operand may be a virtual register or a compile-time constant
if (mop.getType() == MachineOperand::MO_VirtualRegister) if (mop.getType() == MachineOperand::MO_VirtualRegister) {
{
assert(mop.getVRegValue() != NULL); assert(mop.getVRegValue() != NULL);
opValue = mop.getVRegValue(); opValue = mop.getVRegValue();
if (Constant *opConst = dyn_cast<Constant>(opValue)) { if (Constant *opConst = dyn_cast<Constant>(opValue)) {
@ -169,9 +165,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
if (opType == MachineOperand::MO_VirtualRegister) if (opType == MachineOperand::MO_VirtualRegister)
constantThatMustBeLoaded = true; constantThatMustBeLoaded = true;
} }
} } else {
else
{
assert(mop.isImmediate()); assert(mop.isImmediate());
bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed; bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
@ -196,8 +190,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
if (opType == mop.getType()) if (opType == mop.getType())
continue; // no change: this is the most common case continue; // no change: this is the most common case
if (opType == MachineOperand::MO_VirtualRegister) if (opType == MachineOperand::MO_VirtualRegister) {
{
constantThatMustBeLoaded = true; constantThatMustBeLoaded = true;
opValue = isSigned opValue = isSigned
? (Value*)ConstantSInt::get(Type::LongTy, immedValue) ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
@ -250,8 +243,8 @@ FixConstantOperandsForInstr(Instruction* vmInstr,
InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target); InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
minstr->setImplicitRef(i, tmpReg); minstr->setImplicitRef(i, tmpReg);
if (isCall) if (isCall) {
{ // find and replace the argument in the CallArgsDescriptor // find and replace the argument in the CallArgsDescriptor
unsigned i=lastCallArgNum; unsigned i=lastCallArgNum;
while (argDesc->getArgInfo(i).getArgVal() != oldVal) while (argDesc->getArgInfo(i).getArgVal() != oldVal)
++i; ++i;