Avoid unnecessary string construction during asm printing.
[oota-llvm.git] / lib / CodeGen / MachineModuleInfo.cpp
index 77fb643b650065eef11048207c3b872818f22570..5c826b5ffc1a86e594af2e724d2b8a51a854bee6 100644 (file)
@@ -2,14 +2,15 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by James M. Laskey and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/MachineModuleInfo.h"
 
 #include "llvm/Constants.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineLocation.h"
@@ -27,9 +28,8 @@ using namespace llvm;
 using namespace llvm::dwarf;
 
 // Handle the Pass registration stuff necessary to use TargetData's.
-namespace {
-  RegisterPass<MachineModuleInfo> X("machinemoduleinfo", "Module Information");
-}
+static RegisterPass<MachineModuleInfo>
+X("machinemoduleinfo", "Module Information");
 char MachineModuleInfo::ID = 0;
 
 //===----------------------------------------------------------------------===//
@@ -52,10 +52,9 @@ getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
 
 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
 /// named GlobalVariable.
-static std::vector<GlobalVariable*>
-getGlobalVariablesUsing(Module &M, const std::string &RootName) {
-  std::vector<GlobalVariable*> Result;  // GlobalVariables matching criteria.
-  
+static void
+getGlobalVariablesUsing(Module &M, const std::string &RootName,
+                        std::vector<GlobalVariable*> &Result) {
   std::vector<const Type*> FieldTypes;
   FieldTypes.push_back(Type::Int32Ty);
   FieldTypes.push_back(Type::Int32Ty);
@@ -65,11 +64,8 @@ getGlobalVariablesUsing(Module &M, const std::string &RootName) {
                                                 StructType::get(FieldTypes));
 
   // If present and linkonce then scan for users.
-  if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
+  if (UseRoot && UseRoot->hasLinkOnceLinkage())
     getGlobalVariablesUsing(UseRoot, Result);
-  }
-  
-  return Result;
 }
   
 /// isStringValue - Return true if the given value can be coerced to a string.
@@ -104,6 +100,12 @@ static GlobalVariable *getGlobalVariable(Value *V) {
   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
     if (CE->getOpcode() == Instruction::BitCast) {
       return dyn_cast<GlobalVariable>(CE->getOperand(0));
+    } else if (CE->getOpcode() == Instruction::GetElementPtr) {
+      for (unsigned int i=1; i<CE->getNumOperands(); i++) {
+        if (!CE->getOperand(i)->isNullValue())
+          return NULL;
+      }
+      return dyn_cast<GlobalVariable>(CE->getOperand(0));
     }
   }
   return NULL;
@@ -117,6 +119,12 @@ static bool isGlobalVariable(Value *V) {
   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
     if (CE->getOpcode() == Instruction::BitCast) {
       return isa<GlobalVariable>(CE->getOperand(0));
+    } else if (CE->getOpcode() == Instruction::GetElementPtr) {
+      for (unsigned int i=1; i<CE->getNumOperands(); i++) {
+        if (!CE->getOperand(i)->isNullValue())
+          return false;
+      }
+      return isa<GlobalVariable>(CE->getOperand(0));
     }
   }
   return false;
@@ -148,6 +156,8 @@ void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
   DD->ApplyToFields(this);
 }
 
+namespace {
+
 //===----------------------------------------------------------------------===//
 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
 /// the supplied DebugInfoDesc.
@@ -187,10 +197,7 @@ private:
 
 public:
   DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
-  : DIVisitor()
-  , DR(D)
-  , I(0)
-  , CI(cast<ConstantStruct>(GV->getInitializer()))
+    : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer()))
   {}
   
   /// Apply - Set the value of each of the fields.
@@ -217,7 +224,9 @@ public:
   }
   virtual void Apply(std::string &Field) {
     Constant *C = CI->getOperand(I++);
-    Field = C->getStringValue();
+    // Fills in the string if it succeeds
+    if (!GetConstantStringInfo(C, Field))
+      Field.clear();
   }
   virtual void Apply(DebugInfoDesc *&Field) {
     Constant *C = CI->getOperand(I++);
@@ -281,7 +290,7 @@ public:
     Elements.push_back(ConstantInt::get(Type::Int1Ty, Field));
   }
   virtual void Apply(std::string &Field) {
-      Elements.push_back(SR.getString(Field));
+    Elements.push_back(SR.getString(Field));
   }
   virtual void Apply(DebugInfoDesc *&Field) {
     GlobalVariable *GV = NULL;
@@ -467,6 +476,7 @@ public:
   }
 };
 
+}
 
 //===----------------------------------------------------------------------===//
 
@@ -658,7 +668,7 @@ const char *CompileUnitDesc::getTypeString() const {
 
 /// getAnchorString - Return a string used to label this descriptor's anchor.
 ///
-const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
+const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units";
 const char *CompileUnitDesc::getAnchorString() const {
   return AnchorString;
 }
@@ -1108,7 +1118,7 @@ const char *GlobalVariableDesc::getTypeString() const {
 
 /// getAnchorString - Return a string used to label this descriptor's anchor.
 ///
-const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
+const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables";
 const char *GlobalVariableDesc::getAnchorString() const {
   return AnchorString;
 }
@@ -1162,7 +1172,7 @@ const char *SubprogramDesc::getTypeString() const {
 
 /// getAnchorString - Return a string used to label this descriptor's anchor.
 ///
-const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
+const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms";
 const char *SubprogramDesc::getAnchorString() const {
   return AnchorString;
 }
@@ -1262,7 +1272,7 @@ const PointerType *DISerializer::getStrPtrType() {
   // If not already defined.
   if (!StrPtrTy) {
     // Construct the pointer to signed bytes.
-    StrPtrTy = PointerType::get(Type::Int8Ty);
+    StrPtrTy = PointerType::getUnqual(Type::Int8Ty);
   }
   
   return StrPtrTy;
@@ -1272,14 +1282,13 @@ const PointerType *DISerializer::getStrPtrType() {
 ///
 const PointerType *DISerializer::getEmptyStructPtrType() {
   // If not already defined.
-  if (!EmptyStructPtrTy) {
-    // Construct the empty structure type.
-    const StructType *EmptyStructTy =
-                                    StructType::get(std::vector<const Type*>());
-    // Construct the pointer to empty structure type.
-    EmptyStructPtrTy = PointerType::get(EmptyStructTy);
-  }
-  
+  if (EmptyStructPtrTy) return EmptyStructPtrTy;
+
+  // Construct the pointer to empty structure type.
+  const StructType *EmptyStructTy = StructType::get(NULL, NULL);
+
+  // Construct the pointer to empty structure type.
+  EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy);
   return EmptyStructPtrTy;
 }
 
@@ -1312,22 +1321,27 @@ const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
 Constant *DISerializer::getString(const std::string &String) {
   // Check string cache for previous edition.
   Constant *&Slot = StringCache[String];
+
   // Return Constant if previously defined.
   if (Slot) return Slot;
+
   // If empty string then use a sbyte* null instead.
   if (String.empty()) {
     Slot = ConstantPointerNull::get(getStrPtrType());
   } else {
     // Construct string as an llvm constant.
     Constant *ConstStr = ConstantArray::get(String);
+
     // Otherwise create and return a new string global.
     GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
                                                GlobalVariable::InternalLinkage,
                                                ConstStr, ".str", M);
     StrGV->setSection("llvm.metadata");
+
     // Convert to generic string pointer.
     Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType());
   }
+
   return Slot;
   
 }
@@ -1357,13 +1371,19 @@ GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
   // Add fields.
   DISerializeVisitor SRAM(*this, Elements);
   SRAM.ApplyToFields(DD);
-  
+
   // Set the globals initializer.
   GV->setInitializer(ConstantStruct::get(Ty, Elements));
   
   return GV;
 }
 
+/// addDescriptor - Directly connect DD with existing GV.
+void DISerializer::addDescriptor(DebugInfoDesc *DD,
+                                 GlobalVariable *GV) {
+  DescGlobals[DD] = GV;
+}
+
 //===----------------------------------------------------------------------===//
 
 /// Verify - Return true if the GlobalVariable appears to be a valid
@@ -1453,6 +1473,14 @@ bool DIVerifier::Verify(GlobalVariable *GV) {
   return true;
 }
 
+/// isVerified - Return true if the specified GV has already been
+/// verified as a debug information descriptor.
+bool DIVerifier::isVerified(GlobalVariable *GV) {
+  unsigned &ValiditySlot = Validity[GV];
+  if (ValiditySlot) return ValiditySlot == Valid;
+  return false;
+}
+
 //===----------------------------------------------------------------------===//
 
 DebugScope::~DebugScope() {
@@ -1536,28 +1564,32 @@ DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) {
   return DR.Deserialize(V);
 }
 
-/// Verify - Verify that a Value is debug information descriptor.
-///
-bool MachineModuleInfo::Verify(Value *V) {
-  return VR.Verify(V);
-}
-
 /// AnalyzeModule - Scan the module for global debug information.
 ///
 void MachineModuleInfo::AnalyzeModule(Module &M) {
   SetupCompileUnits(M);
-}
 
-/// needsFrameInfo - Returns true if we need to gather callee-saved register
-/// move info for the frame.
-bool MachineModuleInfo::needsFrameInfo() const {
-  return hasDebugInfo() || ExceptionHandling;
+  // Insert functions in the llvm.used array into UsedFunctions.
+  GlobalVariable *GV = M.getGlobalVariable("llvm.used");
+  if (!GV || !GV->hasInitializer()) return;
+
+  // Should be an array of 'i8*'.
+  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+  if (InitList == 0) return;
+
+  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
+    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
+      if (CE->getOpcode() == Instruction::BitCast)
+        if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
+          UsedFunctions.insert(F);
+  }
 }
 
 /// SetupCompileUnits - Set up the unique vector of compile units.
 ///
 void MachineModuleInfo::SetupCompileUnits(Module &M) {
-  std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
+  std::vector<CompileUnitDesc *> CU;
+  getAnchoredDescriptors<CompileUnitDesc>(M, CU);
   
   for (unsigned i = 0, N = CU.size(); i < N; i++) {
     CompileUnits.insert(CU[i]);
@@ -1572,17 +1604,18 @@ const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{
 
 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
 /// named GlobalVariable.
-std::vector<GlobalVariable*>
+void
 MachineModuleInfo::getGlobalVariablesUsing(Module &M,
-                                           const std::string &RootName) {
-  return ::getGlobalVariablesUsing(M, RootName);
+                                           const std::string &RootName,
+                                           std::vector<GlobalVariable*>&Result){
+  return ::getGlobalVariablesUsing(M, RootName, Result);
 }
 
-/// RecordLabel - Records location information and associates it with a
+/// RecordSourceLine - Records location information and associates it with a
 /// debug label.  Returns a unique label ID used to generate a label and 
 /// provide correspondence to the source line list.
-unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column,
-                                       unsigned Source) {
+unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column,
+                                             unsigned Source) {
   unsigned ID = NextLabelID();
   Lines.push_back(SourceLineInfo(Line, Column, Source, ID));
   return ID;
@@ -1624,8 +1657,8 @@ unsigned MachineModuleInfo::RecordRegionEnd(Value *V) {
 
 /// RecordVariable - Indicate the declaration of  a local variable.
 ///
-void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) {
-  VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V));
+void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) {
+  VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV));
   DebugScope *Scope = getOrCreateScope(VD->getContext());
   DebugVariable *DV = new DebugVariable(VD, FrameIndex);
   Scope->AddVariable(DV);
@@ -1720,12 +1753,19 @@ void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
                                         std::vector<GlobalVariable *> &TyInfo) {
   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
-  std::vector<unsigned> IdsInFilter (TyInfo.size());
+  std::vector<unsigned> IdsInFilter(TyInfo.size());
   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
 }
 
+/// addCleanup - Add a cleanup action for a landing pad.
+///
+void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
+  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
+  LP.TypeIds.push_back(0);
+}
+
 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
 /// pads.
 void MachineModuleInfo::TidyLandingPads() {
@@ -1733,22 +1773,17 @@ void MachineModuleInfo::TidyLandingPads() {
     LandingPadInfo &LandingPad = LandingPads[i];
     LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
 
-    if (!LandingPad.LandingPadBlock)
-      // Must not have cleanups if no landing pad.
-      LandingPad.TypeIds.clear();
-
     // Special case: we *should* emit LPs with null LP MBB. This indicates
-    // "rethrow" case.
+    // "nounwind" case.
     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
       LandingPads.erase(LandingPads.begin() + i);
       continue;
     }
-          
+
     for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
       unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
       unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
-            
-          
+
       if (!BeginLabel || !EndLabel) {
         LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
         LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
@@ -1759,7 +1794,19 @@ void MachineModuleInfo::TidyLandingPads() {
       LandingPad.EndLabels[j] = EndLabel;
       ++j;
     }
-    
+
+    // Remove landing pads with no try-ranges.
+    if (LandingPads[i].BeginLabels.empty()) {
+      LandingPads.erase(LandingPads.begin() + i);
+      continue;
+    }
+
+    // If there is no landing pad, ensure that the list of typeids is empty.
+    // If the only typeid is a cleanup, this is the same as having no typeids.
+    if (!LandingPad.LandingPadBlock ||
+        (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
+      LandingPad.TypeIds.clear();
+
     ++i;
   }
 }
@@ -1855,9 +1902,6 @@ bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
   // Get machine module info.
   MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>();
   if (!MMI) return false;
-  // Get target instruction info.
-  const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
-  if (!TII) return false;
   
   // Track if change is made.
   bool MadeChange = false;
@@ -1870,7 +1914,7 @@ bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
     // Iterate through instructions.
     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
       // Is it a label.
-      if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
+      if (I->isDebugLabel()) {
         // The label ID # is always operand #0, an immediate.
         unsigned NextLabel = I->getOperand(0).getImm();