Allow the specification of explicit alignments for constant pool entries.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25855 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/CodeGen/MachineConstantPool.h b/include/llvm/CodeGen/MachineConstantPool.h
index c2c3399..1bbf88d 100644
--- a/include/llvm/CodeGen/MachineConstantPool.h
+++ b/include/llvm/CodeGen/MachineConstantPool.h
@@ -30,20 +30,23 @@
 class Constant;
 
 class MachineConstantPool {
-  std::vector<Constant*> Constants;
+  std::vector<std::pair<Constant*,unsigned> > Constants;
 public:
 
   /// getConstantPoolIndex - Create a new entry in the constant pool or return
-  /// an existing one.
+  /// an existing one. User may specify an alignment that is greater than the
+  /// default alignment. If one is not specified, it will be 0.
   ///
-  unsigned getConstantPoolIndex(Constant *C) {
+  unsigned getConstantPoolIndex(Constant *C, unsigned Alignment = 0) {
     // Check to see if we already have this constant.
     //
     // FIXME, this could be made much more efficient for large constant pools.
     for (unsigned i = 0, e = Constants.size(); i != e; ++i)
-      if (Constants[i] == C)
+      if (Constants[i].first == C) {
+        Constants[i].second = std::max(Constants[i].second, Alignment);
         return i;
-    Constants.push_back(C);
+      }
+    Constants.push_back(std::make_pair(C, Alignment));
     return Constants.size()-1;
   }
 
@@ -51,7 +54,9 @@
   ///
   bool isEmpty() const { return Constants.empty(); }
 
-  const std::vector<Constant*> &getConstants() const { return Constants; }
+  const std::vector<std::pair<Constant*,unsigned> > &getConstants() const {
+    return Constants;
+  }
 
   /// print - Used by the MachineFunction printer to print information about
   /// stack objects.  Implemented in MachineFunction.cpp
diff --git a/include/llvm/CodeGen/SelectionDAG.h b/include/llvm/CodeGen/SelectionDAG.h
index 195e8e4..6d75e47 100644
--- a/include/llvm/CodeGen/SelectionDAG.h
+++ b/include/llvm/CodeGen/SelectionDAG.h
@@ -120,8 +120,10 @@
                                    int offset = 0);
   SDOperand getFrameIndex(int FI, MVT::ValueType VT);
   SDOperand getTargetFrameIndex(int FI, MVT::ValueType VT);
-  SDOperand getConstantPool(Constant *C, MVT::ValueType VT);
-  SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT);
+  SDOperand getConstantPool(Constant *C, MVT::ValueType VT,
+                            unsigned Alignment=0);
+  SDOperand getTargetConstantPool(Constant *C, MVT::ValueType VT,
+                                  unsigned Alignment=0);
   SDOperand getBasicBlock(MachineBasicBlock *MBB);
   SDOperand getExternalSymbol(const char *Sym, MVT::ValueType VT);
   SDOperand getTargetExternalSymbol(const char *Sym, MVT::ValueType VT);
@@ -606,8 +608,8 @@
   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> ConstantFPs;
   std::map<std::pair<uint64_t, MVT::ValueType>, SDNode*> TargetConstantFPs;
   std::map<int, SDNode*> FrameIndices, TargetFrameIndices;
-  std::map<Constant *, SDNode*> ConstantPoolIndices;
-  std::map<Constant *, SDNode*> TargetConstantPoolIndices;
+  std::map<std::pair<Constant *, unsigned>, SDNode*> ConstantPoolIndices;
+  std::map<std::pair<Constant *, unsigned>, SDNode*> TargetConstantPoolIndices;
   std::map<MachineBasicBlock *, SDNode*> BBNodes;
   std::vector<SDNode*> ValueTypeNodes;
   std::map<std::string, SDNode*> ExternalSymbols;
diff --git a/include/llvm/CodeGen/SelectionDAGNodes.h b/include/llvm/CodeGen/SelectionDAGNodes.h
index ae2519a..0b850e6 100644
--- a/include/llvm/CodeGen/SelectionDAGNodes.h
+++ b/include/llvm/CodeGen/SelectionDAGNodes.h
@@ -1058,14 +1058,20 @@
 
 class ConstantPoolSDNode : public SDNode {
   Constant *C;
+  unsigned Alignment;
 protected:
   friend class SelectionDAG;
   ConstantPoolSDNode(Constant *c, MVT::ValueType VT, bool isTarget)
     : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
-    C(c) {}
+    C(c), Alignment(0) {}
+  ConstantPoolSDNode(Constant *c, MVT::ValueType VT, unsigned Align,
+                     bool isTarget)
+    : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, VT),
+    C(c), Alignment(Align) {}
 public:
 
   Constant *get() const { return C; }
+  unsigned getAlignment() const { return Alignment; }
 
   static bool classof(const ConstantPoolSDNode *) { return true; }
   static bool classof(const SDNode *N) {
diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp
index ab0465d..df00f47 100644
--- a/lib/CodeGen/AsmPrinter.cpp
+++ b/lib/CodeGen/AsmPrinter.cpp
@@ -103,7 +103,7 @@
 /// the code generator.
 ///
 void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
-  const std::vector<Constant*> &CP = MCP->getConstants();
+  const std::vector<std::pair<Constant*, unsigned> > &CP = MCP->getConstants();
   if (CP.empty()) return;
   const TargetData &TD = TM.getTargetData();
   
@@ -111,13 +111,17 @@
   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
     // FIXME: force doubles to be naturally aligned.  We should handle this
     // more correctly in the future.
-    unsigned Alignment = TD.getTypeAlignmentShift(CP[i]->getType());
-    if (CP[i]->getType() == Type::DoubleTy && Alignment < 3) Alignment = 3;
+    unsigned Alignment = CP[i].second;
+    if (Alignment == 0) {
+      Alignment = TD.getTypeAlignmentShift(CP[i].first->getType());
+      if (CP[i].first->getType() == Type::DoubleTy && Alignment < 3)
+        Alignment = 3;
+    }
     
     EmitAlignment(Alignment);
     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
-      << ":\t\t\t\t\t" << CommentString << *CP[i] << '\n';
-    EmitGlobalConstant(CP[i]);
+      << ":\t\t\t\t\t" << CommentString << *CP[i].first << '\n';
+    EmitGlobalConstant(CP[i].first);
   }
 }
 
diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp
index f0ece6b..3c41dbe 100644
--- a/lib/CodeGen/MachineFunction.cpp
+++ b/lib/CodeGen/MachineFunction.cpp
@@ -346,8 +346,11 @@
 //===----------------------------------------------------------------------===//
 
 void MachineConstantPool::print(std::ostream &OS) const {
-  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
-    OS << "  <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
+  for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
+    OS << "  <cp #" << i << "> is" << *(Value*)Constants[i].first;
+    if (Constants[i].second != 0) OS << " , align=" << Constants[i].second;
+    OS << "\n";
+  }
 }
 
 void MachineConstantPool::dump() const { print(std::cerr); }
diff --git a/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp b/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp
index 9f285d5..c0fd397 100644
--- a/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp
@@ -194,7 +194,8 @@
         MI->addFrameIndexOperand(FI->getIndex());
       } else if (ConstantPoolSDNode *CP = 
                     dyn_cast<ConstantPoolSDNode>(Node->getOperand(i))) {
-        unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
+        unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(),
+                                                       CP->getAlignment());
         MI->addConstantPoolIndexOperand(Idx);
       } else if (ExternalSymbolSDNode *ES = 
                  dyn_cast<ExternalSymbolSDNode>(Node->getOperand(i))) {
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index aa9ea63..6f1a263 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -310,10 +310,14 @@
     Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
     break;
   case ISD::ConstantPool:
-    Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
+    Erased = ConstantPoolIndices.
+      erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
+                           cast<ConstantPoolSDNode>(N)->getAlignment()));
     break;
   case ISD::TargetConstantPool:
-    Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
+    Erased = TargetConstantPoolIndices.
+      erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
+                           cast<ConstantPoolSDNode>(N)->getAlignment()));
     break;
   case ISD::BasicBlock:
     Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
@@ -655,18 +659,20 @@
   return SDOperand(N, 0);
 }
 
-SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
-  SDNode *&N = ConstantPoolIndices[C];
+SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
+                                        unsigned Alignment) {
+  SDNode *&N = ConstantPoolIndices[std::make_pair(C, Alignment)];
   if (N) return SDOperand(N, 0);
-  N = new ConstantPoolSDNode(C, VT, false);
+  N = new ConstantPoolSDNode(C, VT, Alignment, false);
   AllNodes.push_back(N);
   return SDOperand(N, 0);
 }
 
-SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
-  SDNode *&N = TargetConstantPoolIndices[C];
+SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
+                                              unsigned Alignment) {
+  SDNode *&N = TargetConstantPoolIndices[std::make_pair(C, Alignment)];
   if (N) return SDOperand(N, 0);
-  N = new ConstantPoolSDNode(C, VT, true);
+  N = new ConstantPoolSDNode(C, VT, Alignment, true);
   AllNodes.push_back(N);
   return SDOperand(N, 0);
 }
diff --git a/lib/ExecutionEngine/JIT/JITEmitter.cpp b/lib/ExecutionEngine/JIT/JITEmitter.cpp
index fb20471..d2f943f 100644
--- a/lib/ExecutionEngine/JIT/JITEmitter.cpp
+++ b/lib/ExecutionEngine/JIT/JITEmitter.cpp
@@ -566,16 +566,18 @@
 }
 
 void JITEmitter::emitConstantPool(MachineConstantPool *MCP) {
-  const std::vector<Constant*> &Constants = MCP->getConstants();
+  const std::vector<std::pair<Constant*,unsigned> > &Constants = MCP->getConstants();
   if (Constants.empty()) return;
 
   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
-    const Type *Ty = Constants[i]->getType();
+    const Type *Ty = Constants[i].first->getType();
     unsigned Size      = (unsigned)TheJIT->getTargetData().getTypeSize(Ty);
-    unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
+    unsigned Alignment = (Constants[i].second == 0)
+      ? TheJIT->getTargetData().getTypeAlignment(Ty)
+      : Constants[i].second;
 
     void *Addr = MemMgr.allocateConstant(Size, Alignment);
-    TheJIT->InitializeMemory(Constants[i], Addr);
+    TheJIT->InitializeMemory(Constants[i].first, Addr);
     ConstantPoolAddresses.push_back(Addr);
   }
 }
diff --git a/lib/Target/Alpha/AlphaISelLowering.cpp b/lib/Target/Alpha/AlphaISelLowering.cpp
index 517ffae..2990146 100644
--- a/lib/Target/Alpha/AlphaISelLowering.cpp
+++ b/lib/Target/Alpha/AlphaISelLowering.cpp
@@ -482,8 +482,9 @@
       }
   }
   case ISD::ConstantPool: {
-    Constant *C = cast<ConstantPoolSDNode>(Op)->get();
-    SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64);
+    ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
+    Constant *C = CP->get();
+    SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i64, CP->getAlignment());
     
     SDOperand Hi = DAG.getNode(AlphaISD::GPRelHi,  MVT::i64, CPI,
 			       DAG.getNode(AlphaISD::GlobalBaseReg, MVT::i64));
diff --git a/lib/Target/IA64/IA64ISelDAGToDAG.cpp b/lib/Target/IA64/IA64ISelDAGToDAG.cpp
index 072b55b..2b817bb 100644
--- a/lib/Target/IA64/IA64ISelDAGToDAG.cpp
+++ b/lib/Target/IA64/IA64ISelDAGToDAG.cpp
@@ -442,8 +442,10 @@
 
   case ISD::ConstantPool: { // TODO: nuke the constant pool
 			    //       (ia64 doesn't need one)
-    Constant *C = cast<ConstantPoolSDNode>(N)->get();
-    SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
+    ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
+    Constant *C = CP->get();
+    SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
+                                                  CP->getAlignment());
     return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
 	                      CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
   }
diff --git a/lib/Target/PowerPC/PPCISelLowering.cpp b/lib/Target/PowerPC/PPCISelLowering.cpp
index 8b5546d..28beeaf 100644
--- a/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -389,8 +389,9 @@
     return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
   }
   case ISD::ConstantPool: {
-    Constant *C = cast<ConstantPoolSDNode>(Op)->get();
-    SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
+    ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
+    Constant *C = CP->get();
+    SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
     SDOperand Zero = DAG.getConstant(0, MVT::i32);
     
     if (PPCGenerateStaticCode) {
diff --git a/lib/Target/Sparc/SparcISelDAGToDAG.cpp b/lib/Target/Sparc/SparcISelDAGToDAG.cpp
index 38f3c15..c274105 100644
--- a/lib/Target/Sparc/SparcISelDAGToDAG.cpp
+++ b/lib/Target/Sparc/SparcISelDAGToDAG.cpp
@@ -693,7 +693,8 @@
   }
   case ISD::ConstantPool: {
     Constant *C = cast<ConstantPoolSDNode>(Op)->get();
-    SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
+    SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32,
+                                  cast<ConstantPoolSDNode>(Op)->getAlignment());
     SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
     SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
     return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
diff --git a/lib/Target/SparcV8/SparcV8ISelDAGToDAG.cpp b/lib/Target/SparcV8/SparcV8ISelDAGToDAG.cpp
index 38f3c15..c274105 100644
--- a/lib/Target/SparcV8/SparcV8ISelDAGToDAG.cpp
+++ b/lib/Target/SparcV8/SparcV8ISelDAGToDAG.cpp
@@ -693,7 +693,8 @@
   }
   case ISD::ConstantPool: {
     Constant *C = cast<ConstantPoolSDNode>(Op)->get();
-    SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
+    SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32,
+                                  cast<ConstantPoolSDNode>(Op)->getAlignment());
     SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
     SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
     return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
diff --git a/lib/Target/SparcV9/SparcV9AsmPrinter.cpp b/lib/Target/SparcV9/SparcV9AsmPrinter.cpp
index 564c7ed..638a699 100644
--- a/lib/Target/SparcV9/SparcV9AsmPrinter.cpp
+++ b/lib/Target/SparcV9/SparcV9AsmPrinter.cpp
@@ -209,11 +209,14 @@
     // Print a constant (which may be an aggregate) prefixed by all the
     // appropriate directives.  Uses printConstantValueOnly() to print the
     // value or values.
-    void printConstant(const Constant* CV, std::string valID = "") {
+    void printConstant(const Constant* CV, unsigned Alignment,
+                       std::string valID = "") {
       if (valID.length() == 0)
         valID = getID(CV);
 
-      O << "\t.align\t" << ConstantToAlignment(CV, TM) << "\n";
+      if (Alignment == 0)
+        Alignment = ConstantToAlignment(CV, TM);
+      O << "\t.align\t" << Alignment << "\n";
 
       // Print .size and .type only if it is not a string.
       if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
@@ -721,12 +724,12 @@
 
   // Emit constant pool for this function
   const MachineConstantPool *MCP = MF.getConstantPool();
-  const std::vector<Constant*> &CP = MCP->getConstants();
+  const std::vector<std::pair<Constant*, unsigned> > &CP = MCP->getConstants();
 
   enterSection(ReadOnlyData);
   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
     std::string cpiName = ".CPI_" + CurrentFnName + "_" + utostr(i);
-    printConstant(CP[i], cpiName);
+    printConstant(CP[i].first, CP[i].second, cpiName);
   }
 
   enterSection(Text);
@@ -755,7 +758,7 @@
   if (GV->hasInitializer() &&
       !(GV->getInitializer()->isNullValue() ||
         isa<UndefValue>(GV->getInitializer()))) {
-    printConstant(GV->getInitializer(), getID(GV));
+    printConstant(GV->getInitializer(), 0, getID(GV));
   } else {
     O << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
                                                 TM) << "\n";