LLVM  mainline
Globals.cpp
Go to the documentation of this file.
00001 //===-- Globals.cpp - Implement the GlobalValue & GlobalVariable class ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the GlobalValue & GlobalVariable classes for the IR
00011 // library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/IR/GlobalValue.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/DerivedTypes.h"
00019 #include "llvm/IR/GlobalAlias.h"
00020 #include "llvm/IR/GlobalVariable.h"
00021 #include "llvm/IR/Module.h"
00022 #include "llvm/IR/Operator.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 using namespace llvm;
00025 
00026 //===----------------------------------------------------------------------===//
00027 //                            GlobalValue Class
00028 //===----------------------------------------------------------------------===//
00029 
00030 bool GlobalValue::isMaterializable() const {
00031   if (const Function *F = dyn_cast<Function>(this))
00032     return F->isMaterializable();
00033   return false;
00034 }
00035 bool GlobalValue::isDematerializable() const {
00036   return getParent() && getParent()->isDematerializable(this);
00037 }
00038 std::error_code GlobalValue::materialize() {
00039   return getParent()->materialize(this);
00040 }
00041 void GlobalValue::dematerialize() {
00042   getParent()->dematerialize(this);
00043 }
00044 
00045 /// Override destroyConstant to make sure it doesn't get called on
00046 /// GlobalValue's because they shouldn't be treated like other constants.
00047 void GlobalValue::destroyConstant() {
00048   llvm_unreachable("You can't GV->destroyConstant()!");
00049 }
00050 
00051 /// copyAttributesFrom - copy all additional attributes (those not needed to
00052 /// create a GlobalValue) from the GlobalValue Src to this one.
00053 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
00054   setVisibility(Src->getVisibility());
00055   setUnnamedAddr(Src->hasUnnamedAddr());
00056   setDLLStorageClass(Src->getDLLStorageClass());
00057 }
00058 
00059 unsigned GlobalValue::getAlignment() const {
00060   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00061     // In general we cannot compute this at the IR level, but we try.
00062     if (const GlobalObject *GO = GA->getBaseObject())
00063       return GO->getAlignment();
00064 
00065     // FIXME: we should also be able to handle:
00066     // Alias = Global + Offset
00067     // Alias = Absolute
00068     return 0;
00069   }
00070   return cast<GlobalObject>(this)->getAlignment();
00071 }
00072 
00073 void GlobalObject::setAlignment(unsigned Align) {
00074   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
00075   assert(Align <= MaximumAlignment &&
00076          "Alignment is greater than MaximumAlignment!");
00077   unsigned AlignmentData = Log2_32(Align) + 1;
00078   unsigned OldData = getGlobalValueSubClassData();
00079   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
00080   assert(getAlignment() == Align && "Alignment representation error!");
00081 }
00082 
00083 unsigned GlobalObject::getGlobalObjectSubClassData() const {
00084   unsigned ValueData = getGlobalValueSubClassData();
00085   return ValueData >> AlignmentBits;
00086 }
00087 
00088 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
00089   unsigned OldData = getGlobalValueSubClassData();
00090   setGlobalValueSubClassData((OldData & AlignmentMask) |
00091                              (Val << AlignmentBits));
00092   assert(getGlobalObjectSubClassData() == Val && "representation error");
00093 }
00094 
00095 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
00096   const auto *GV = cast<GlobalObject>(Src);
00097   GlobalValue::copyAttributesFrom(GV);
00098   setAlignment(GV->getAlignment());
00099   setSection(GV->getSection());
00100 }
00101 
00102 const char *GlobalValue::getSection() const {
00103   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00104     // In general we cannot compute this at the IR level, but we try.
00105     if (const GlobalObject *GO = GA->getBaseObject())
00106       return GO->getSection();
00107     return "";
00108   }
00109   return cast<GlobalObject>(this)->getSection();
00110 }
00111 
00112 Comdat *GlobalValue::getComdat() {
00113   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00114     // In general we cannot compute this at the IR level, but we try.
00115     if (const GlobalObject *GO = GA->getBaseObject())
00116       return const_cast<GlobalObject *>(GO)->getComdat();
00117     return nullptr;
00118   }
00119   return cast<GlobalObject>(this)->getComdat();
00120 }
00121 
00122 void GlobalObject::setSection(StringRef S) { Section = S; }
00123 
00124 bool GlobalValue::isDeclaration() const {
00125   // Globals are definitions if they have an initializer.
00126   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
00127     return GV->getNumOperands() == 0;
00128 
00129   // Functions are definitions if they have a body.
00130   if (const Function *F = dyn_cast<Function>(this))
00131     return F->empty() && !F->isMaterializable();
00132 
00133   // Aliases are always definitions.
00134   assert(isa<GlobalAlias>(this));
00135   return false;
00136 }
00137 
00138 //===----------------------------------------------------------------------===//
00139 // GlobalVariable Implementation
00140 //===----------------------------------------------------------------------===//
00141 
00142 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
00143                                Constant *InitVal, const Twine &Name,
00144                                ThreadLocalMode TLMode, unsigned AddressSpace,
00145                                bool isExternallyInitialized)
00146     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
00147                    OperandTraits<GlobalVariable>::op_begin(this),
00148                    InitVal != nullptr, Link, Name),
00149       isConstantGlobal(constant),
00150       isExternallyInitializedConstant(isExternallyInitialized) {
00151   setThreadLocalMode(TLMode);
00152   if (InitVal) {
00153     assert(InitVal->getType() == Ty &&
00154            "Initializer should be the same type as the GlobalVariable!");
00155     Op<0>() = InitVal;
00156   }
00157 }
00158 
00159 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
00160                                LinkageTypes Link, Constant *InitVal,
00161                                const Twine &Name, GlobalVariable *Before,
00162                                ThreadLocalMode TLMode, unsigned AddressSpace,
00163                                bool isExternallyInitialized)
00164     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
00165                    OperandTraits<GlobalVariable>::op_begin(this),
00166                    InitVal != nullptr, Link, Name),
00167       isConstantGlobal(constant),
00168       isExternallyInitializedConstant(isExternallyInitialized) {
00169   setThreadLocalMode(TLMode);
00170   if (InitVal) {
00171     assert(InitVal->getType() == Ty &&
00172            "Initializer should be the same type as the GlobalVariable!");
00173     Op<0>() = InitVal;
00174   }
00175 
00176   if (Before)
00177     Before->getParent()->getGlobalList().insert(Before, this);
00178   else
00179     M.getGlobalList().push_back(this);
00180 }
00181 
00182 void GlobalVariable::setParent(Module *parent) {
00183   Parent = parent;
00184 }
00185 
00186 void GlobalVariable::removeFromParent() {
00187   getParent()->getGlobalList().remove(this);
00188 }
00189 
00190 void GlobalVariable::eraseFromParent() {
00191   getParent()->getGlobalList().erase(this);
00192 }
00193 
00194 void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To,
00195                                                  Use *U) {
00196   // If you call this, then you better know this GVar has a constant
00197   // initializer worth replacing. Enforce that here.
00198   assert(getNumOperands() == 1 &&
00199          "Attempt to replace uses of Constants on a GVar with no initializer");
00200 
00201   // And, since you know it has an initializer, the From value better be
00202   // the initializer :)
00203   assert(getOperand(0) == From &&
00204          "Attempt to replace wrong constant initializer in GVar");
00205 
00206   // And, you better have a constant for the replacement value
00207   assert(isa<Constant>(To) &&
00208          "Attempt to replace GVar initializer with non-constant");
00209 
00210   // Okay, preconditions out of the way, replace the constant initializer.
00211   this->setOperand(0, cast<Constant>(To));
00212 }
00213 
00214 void GlobalVariable::setInitializer(Constant *InitVal) {
00215   if (!InitVal) {
00216     if (hasInitializer()) {
00217       Op<0>().set(nullptr);
00218       NumOperands = 0;
00219     }
00220   } else {
00221     assert(InitVal->getType() == getType()->getElementType() &&
00222            "Initializer type must match GlobalVariable type");
00223     if (!hasInitializer())
00224       NumOperands = 1;
00225     Op<0>().set(InitVal);
00226   }
00227 }
00228 
00229 /// copyAttributesFrom - copy all additional attributes (those not needed to
00230 /// create a GlobalVariable) from the GlobalVariable Src to this one.
00231 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
00232   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
00233   GlobalObject::copyAttributesFrom(Src);
00234   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
00235   setThreadLocalMode(SrcVar->getThreadLocalMode());
00236   setExternallyInitialized(SrcVar->isExternallyInitialized());
00237 }
00238 
00239 
00240 //===----------------------------------------------------------------------===//
00241 // GlobalAlias Implementation
00242 //===----------------------------------------------------------------------===//
00243 
00244 GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
00245                          Constant *Aliasee, Module *ParentModule)
00246     : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
00247   Op<0>() = Aliasee;
00248 
00249   if (ParentModule)
00250     ParentModule->getAliasList().push_back(this);
00251 }
00252 
00253 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
00254                                  const Twine &Name, Constant *Aliasee,
00255                                  Module *ParentModule) {
00256   return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
00257 }
00258 
00259 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
00260                                  const Twine &Name, Module *Parent) {
00261   return create(Ty, Linkage, Name, nullptr, Parent);
00262 }
00263 
00264 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
00265                                  const Twine &Name, GlobalValue *Aliasee) {
00266   return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
00267 }
00268 
00269 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
00270                                  GlobalValue *Aliasee) {
00271   PointerType *PTy = Aliasee->getType();
00272   return create(PTy, Link, Name, Aliasee);
00273 }
00274 
00275 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
00276   return create(Aliasee->getLinkage(), Name, Aliasee);
00277 }
00278 
00279 void GlobalAlias::setParent(Module *parent) {
00280   Parent = parent;
00281 }
00282 
00283 void GlobalAlias::removeFromParent() {
00284   getParent()->getAliasList().remove(this);
00285 }
00286 
00287 void GlobalAlias::eraseFromParent() {
00288   getParent()->getAliasList().erase(this);
00289 }
00290 
00291 void GlobalAlias::setAliasee(Constant *Aliasee) {
00292   assert((!Aliasee || Aliasee->getType() == getType()) &&
00293          "Alias and aliasee types should match!");
00294   setOperand(0, Aliasee);
00295 }