//===- SparcV9PreSelection.cpp - Specialize LLVM code for SparcV9 ---------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file defines the PreSelection pass which specializes LLVM code for
//===----------------------------------------------------------------------===//
#include "SparcV9Internals.h"
+#include "SparcV9BurgISel.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
-#include "llvm/iMemory.h"
-#include "llvm/iPHINode.h"
-#include "llvm/iOther.h"
+#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/InstVisitor.h"
//===--------------------------------------------------------------------===//
// PreSelection Pass - Specialize LLVM code for the SparcV9 instr. selector.
- //
+ //
class PreSelection : public FunctionPass, public InstVisitor<PreSelection> {
const TargetInstrInfo &instrInfo;
const char *getPassName() const { return "SparcV9 Instr. Pre-selection"; }
// These methods do the actual work of specializing code
- void visitInstruction(Instruction &I); // common work for every instr.
+ void visitInstruction(Instruction &I); // common work for every instr.
void visitGetElementPtrInst(GetElementPtrInst &I);
void visitCallInst(CallInst &I);
void visitPHINode(PHINode &PN);
+ void visitBasicBlock(BasicBlock &BB) {
+ if (isa<UnreachableInst>(BB.getTerminator())) {
+ BB.getInstList().pop_back();
+ const Type *RetTy = BB.getParent()->getReturnType();
+ Value *RetVal = RetTy == Type::VoidTy ? 0 : UndefValue::get(RetTy);
+ new ReturnInst(RetVal, &BB);
+ }
+ }
+
// Helper functions for visiting operands of every instruction
- //
+ //
// visitOperands() works on every operand in [firstOp, lastOp-1].
// If lastOp==0, lastOp defaults to #operands or #incoming Phi values.
- //
+ //
// visitOneOperand() does all the work for one operand.
- //
+ //
void visitOperands(Instruction &I, int firstOp=0);
void visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
Instruction& insertBefore);
Instruction& insertBefore)
{
Value *getArg1, *getArg2;
-
+
switch(CE->getOpcode())
{
case Instruction::Cast:
return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore);
case Instruction::GetElementPtr:
- assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end()
+ assert(std::find_if(CE->op_begin()+1, CE->op_end(),
+ nonConstant) == CE->op_end()
&& "All indices in ConstantExpr getelementptr must be constant!");
getArg1 = CE->getOperand(0);
if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
return new GetElementPtrInst(getArg1,
std::vector<Value*>(CE->op_begin()+1, CE->op_end()),
"constantGEP:" + getArg1->getName(), &insertBefore);
-
+
case Instruction::Select: {
Value *C, *S1, *S2;
C = CE->getOperand (0);
S2 = DecomposeConstantExpr (CEarg, insertBefore);
return new SelectInst (C, S1, S2, "constantSelect", &insertBefore);
}
-
+
+ case Instruction::Shr: {
+ getArg1 = CE->getOperand(0);
+ if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
+ getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
+ getArg2 = CE->getOperand(1);
+ if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
+ getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
+ return new ShiftInst (static_cast<Instruction::OtherOps>(CE->getOpcode()),
+ getArg1, getArg2,
+ "constantShr:" + getArg1->getName(), &insertBefore);
+ }
+
+ case Instruction::Shl: {
+ getArg1 = CE->getOperand(0);
+ if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
+ getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
+ getArg2 = CE->getOperand(1);
+ if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
+ getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
+ return new ShiftInst (static_cast<Instruction::OtherOps>(CE->getOpcode()),
+ getArg1, getArg2,
+ "constantShl:" + getArg1->getName(), &insertBefore);
+ }
+
default: // must be a binary operator
assert(CE->getOpcode() >= Instruction::BinaryOpsBegin &&
CE->getOpcode() < Instruction::BinaryOpsEnd &&
}
}
+static inline bool ConstantTypeMustBeLoaded(const Type* CVT) {
+ assert(CVT->isPrimitiveType() || isa<PointerType>(CVT));
+ return !(CVT->isIntegral() || isa<PointerType>(CVT));
+}
//------------------------------------------------------------------------------
// Instruction visitor methods to perform instruction-specific operations
// load-time constant: factor it out so we optimize as best we can
Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore);
I.setOperand(opNum, computeConst); // replace expr operand with result
- } else if (instrInfo.ConstantTypeMustBeLoaded(CV)) {
+ } else if (ConstantTypeMustBeLoaded(CV->getType())) {
// load address of constant into a register, then load the constant
// this is now done during instruction selection
// the constant will live in the MachineConstantPool later on
- } else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I)) {
+ } else if (ConstantMayNotFitInImmedField(CV, &I)) {
// put the constant into a virtual register using a cast
CastInst* castI = new CastInst(CV, CV->getType(), "copyConst",
&insertBefore);
/// depends on the type of instruction and on the target architecture.
/// -- For any constants that cannot be put in an immediate field,
/// load address into virtual register first, and then load the constant.
-///
+///
/// firstOp and lastOp can be used to skip leading and trailing operands.
/// If lastOp is 0, it defaults to #operands or #incoming Phi values.
-///
+///
inline void PreSelection::visitOperands(Instruction &I, int firstOp) {
// For any instruction other than PHI, copies go just before the instr.
for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i)
// For a PHI, operand copies must be before the terminator of the
// appropriate predecessor basic block. Remaining logic is simple
// so just handle PHIs and other instructions separately.
- //
+ //
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
visitOneOperand(PN, PN.getIncomingValue(i),
PN.getOperandNumForIncomingValue(i),
// Common work for *all* instructions. This needs to be called explicitly
// by other visit<InstructionType> functions.
-inline void PreSelection::visitInstruction(Instruction &I) {
+inline void PreSelection::visitInstruction(Instruction &I) {
visitOperands(I); // Perform operand transformations
}
// GetElementPtr instructions: check if pointer is a global
-void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) {
+void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) {
Instruction* curI = &I;
// The Sparc backend doesn't handle array indexes that are not long types, so