fix some GCC 4 warnings
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9PreSelection.cpp
index 00cfd1295e6a5f9ce04b41aa88183db65197ea00..a13019e20613143b568713c483973a8a169d8cf7 100644 (file)
@@ -1,10 +1,10 @@
 //===- 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"
@@ -35,7 +34,7 @@ namespace {
 
   //===--------------------------------------------------------------------===//
   // PreSelection Pass - Specialize LLVM code for the SparcV9 instr. selector.
-  // 
+  //
   class PreSelection : public FunctionPass, public InstVisitor<PreSelection> {
     const TargetInstrInfo &instrInfo;
 
@@ -51,18 +50,27 @@ namespace {
     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);
@@ -85,8 +93,6 @@ namespace {
 
 // getGlobalAddr(): Put address of a global into a v. register.
 static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) {
-  if (isa<ConstantPointerRef>(ptr))
-    ptr = cast<ConstantPointerRef>(ptr)->getValue();
 
   return (isa<GlobalVariable>(ptr))
     ? new GetElementPtrInst(ptr,
@@ -104,7 +110,7 @@ static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
                                           Instruction& insertBefore)
 {
   Value *getArg1, *getArg2;
-  
+
   switch(CE->getOpcode())
     {
     case Instruction::Cast:
@@ -114,7 +120,8 @@ static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
       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))
@@ -124,7 +131,7 @@ static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
       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);
@@ -138,7 +145,31 @@ static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
         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 &&
@@ -155,6 +186,10 @@ static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
     }
 }
 
+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
@@ -178,11 +213,11 @@ PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
     // 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);
@@ -195,10 +230,10 @@ PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
 ///    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)
@@ -210,7 +245,7 @@ void PreSelection::visitPHINode(PHINode &PN) {
   // 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),
@@ -220,12 +255,12 @@ void PreSelection::visitPHINode(PHINode &PN) {
 
 // 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