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 destroyConstantImpl to make sure it doesn't get called on
00046 /// GlobalValue's because they shouldn't be treated like other constants.
00047 void GlobalValue::destroyConstantImpl() {
00048   llvm_unreachable("You can't GV->destroyConstantImpl()!");
00049 }
00050 
00051 void GlobalValue::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
00052   llvm_unreachable("You can't GV->replaceUsesOfWithOnConstant()!");
00053 }
00054 
00055 /// copyAttributesFrom - copy all additional attributes (those not needed to
00056 /// create a GlobalValue) from the GlobalValue Src to this one.
00057 void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
00058   setVisibility(Src->getVisibility());
00059   setUnnamedAddr(Src->hasUnnamedAddr());
00060   setDLLStorageClass(Src->getDLLStorageClass());
00061 }
00062 
00063 unsigned GlobalValue::getAlignment() const {
00064   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00065     // In general we cannot compute this at the IR level, but we try.
00066     if (const GlobalObject *GO = GA->getBaseObject())
00067       return GO->getAlignment();
00068 
00069     // FIXME: we should also be able to handle:
00070     // Alias = Global + Offset
00071     // Alias = Absolute
00072     return 0;
00073   }
00074   return cast<GlobalObject>(this)->getAlignment();
00075 }
00076 
00077 void GlobalObject::setAlignment(unsigned Align) {
00078   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
00079   assert(Align <= MaximumAlignment &&
00080          "Alignment is greater than MaximumAlignment!");
00081   unsigned AlignmentData = Log2_32(Align) + 1;
00082   unsigned OldData = getGlobalValueSubClassData();
00083   setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData);
00084   assert(getAlignment() == Align && "Alignment representation error!");
00085 }
00086 
00087 unsigned GlobalObject::getGlobalObjectSubClassData() const {
00088   unsigned ValueData = getGlobalValueSubClassData();
00089   return ValueData >> AlignmentBits;
00090 }
00091 
00092 void GlobalObject::setGlobalObjectSubClassData(unsigned Val) {
00093   unsigned OldData = getGlobalValueSubClassData();
00094   setGlobalValueSubClassData((OldData & AlignmentMask) |
00095                              (Val << AlignmentBits));
00096   assert(getGlobalObjectSubClassData() == Val && "representation error");
00097 }
00098 
00099 void GlobalObject::copyAttributesFrom(const GlobalValue *Src) {
00100   const auto *GV = cast<GlobalObject>(Src);
00101   GlobalValue::copyAttributesFrom(GV);
00102   setAlignment(GV->getAlignment());
00103   setSection(GV->getSection());
00104 }
00105 
00106 const char *GlobalValue::getSection() const {
00107   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00108     // In general we cannot compute this at the IR level, but we try.
00109     if (const GlobalObject *GO = GA->getBaseObject())
00110       return GO->getSection();
00111     return "";
00112   }
00113   return cast<GlobalObject>(this)->getSection();
00114 }
00115 
00116 Comdat *GlobalValue::getComdat() {
00117   if (auto *GA = dyn_cast<GlobalAlias>(this)) {
00118     // In general we cannot compute this at the IR level, but we try.
00119     if (const GlobalObject *GO = GA->getBaseObject())
00120       return const_cast<GlobalObject *>(GO)->getComdat();
00121     return nullptr;
00122   }
00123   return cast<GlobalObject>(this)->getComdat();
00124 }
00125 
00126 void GlobalObject::setSection(StringRef S) { Section = S; }
00127 
00128 bool GlobalValue::isDeclaration() const {
00129   // Globals are definitions if they have an initializer.
00130   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
00131     return GV->getNumOperands() == 0;
00132 
00133   // Functions are definitions if they have a body.
00134   if (const Function *F = dyn_cast<Function>(this))
00135     return F->empty() && !F->isMaterializable();
00136 
00137   // Aliases are always definitions.
00138   assert(isa<GlobalAlias>(this));
00139   return false;
00140 }
00141 
00142 //===----------------------------------------------------------------------===//
00143 // GlobalVariable Implementation
00144 //===----------------------------------------------------------------------===//
00145 
00146 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
00147                                Constant *InitVal, const Twine &Name,
00148                                ThreadLocalMode TLMode, unsigned AddressSpace,
00149                                bool isExternallyInitialized)
00150     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
00151                    OperandTraits<GlobalVariable>::op_begin(this),
00152                    InitVal != nullptr, Link, Name),
00153       isConstantGlobal(constant),
00154       isExternallyInitializedConstant(isExternallyInitialized) {
00155   setThreadLocalMode(TLMode);
00156   if (InitVal) {
00157     assert(InitVal->getType() == Ty &&
00158            "Initializer should be the same type as the GlobalVariable!");
00159     Op<0>() = InitVal;
00160   }
00161 }
00162 
00163 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
00164                                LinkageTypes Link, Constant *InitVal,
00165                                const Twine &Name, GlobalVariable *Before,
00166                                ThreadLocalMode TLMode, unsigned AddressSpace,
00167                                bool isExternallyInitialized)
00168     : GlobalObject(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
00169                    OperandTraits<GlobalVariable>::op_begin(this),
00170                    InitVal != nullptr, Link, Name),
00171       isConstantGlobal(constant),
00172       isExternallyInitializedConstant(isExternallyInitialized) {
00173   setThreadLocalMode(TLMode);
00174   if (InitVal) {
00175     assert(InitVal->getType() == Ty &&
00176            "Initializer should be the same type as the GlobalVariable!");
00177     Op<0>() = InitVal;
00178   }
00179 
00180   if (Before)
00181     Before->getParent()->getGlobalList().insert(Before, this);
00182   else
00183     M.getGlobalList().push_back(this);
00184 }
00185 
00186 void GlobalVariable::setParent(Module *parent) {
00187   Parent = parent;
00188 }
00189 
00190 void GlobalVariable::removeFromParent() {
00191   getParent()->getGlobalList().remove(this);
00192 }
00193 
00194 void GlobalVariable::eraseFromParent() {
00195   getParent()->getGlobalList().erase(this);
00196 }
00197 
00198 void GlobalVariable::setInitializer(Constant *InitVal) {
00199   if (!InitVal) {
00200     if (hasInitializer()) {
00201       // Note, the num operands is used to compute the offset of the operand, so
00202       // the order here matters.  Clearing the operand then clearing the num
00203       // operands ensures we have the correct offset to the operand.
00204       Op<0>().set(nullptr);
00205       setGlobalVariableNumOperands(0);
00206     }
00207   } else {
00208     assert(InitVal->getType() == getType()->getElementType() &&
00209            "Initializer type must match GlobalVariable type");
00210     // Note, the num operands is used to compute the offset of the operand, so
00211     // the order here matters.  We need to set num operands to 1 first so that
00212     // we get the correct offset to the first operand when we set it.
00213     if (!hasInitializer())
00214       setGlobalVariableNumOperands(1);
00215     Op<0>().set(InitVal);
00216   }
00217 }
00218 
00219 /// copyAttributesFrom - copy all additional attributes (those not needed to
00220 /// create a GlobalVariable) from the GlobalVariable Src to this one.
00221 void GlobalVariable::copyAttributesFrom(const GlobalValue *Src) {
00222   assert(isa<GlobalVariable>(Src) && "Expected a GlobalVariable!");
00223   GlobalObject::copyAttributesFrom(Src);
00224   const GlobalVariable *SrcVar = cast<GlobalVariable>(Src);
00225   setThreadLocalMode(SrcVar->getThreadLocalMode());
00226   setExternallyInitialized(SrcVar->isExternallyInitialized());
00227 }
00228 
00229 
00230 //===----------------------------------------------------------------------===//
00231 // GlobalAlias Implementation
00232 //===----------------------------------------------------------------------===//
00233 
00234 GlobalAlias::GlobalAlias(PointerType *Ty, LinkageTypes Link, const Twine &Name,
00235                          Constant *Aliasee, Module *ParentModule)
00236     : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) {
00237   Op<0>() = Aliasee;
00238 
00239   if (ParentModule)
00240     ParentModule->getAliasList().push_back(this);
00241 }
00242 
00243 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Link,
00244                                  const Twine &Name, Constant *Aliasee,
00245                                  Module *ParentModule) {
00246   return new GlobalAlias(Ty, Link, Name, Aliasee, ParentModule);
00247 }
00248 
00249 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
00250                                  const Twine &Name, Module *Parent) {
00251   return create(Ty, Linkage, Name, nullptr, Parent);
00252 }
00253 
00254 GlobalAlias *GlobalAlias::create(PointerType *Ty, LinkageTypes Linkage,
00255                                  const Twine &Name, GlobalValue *Aliasee) {
00256   return create(Ty, Linkage, Name, Aliasee, Aliasee->getParent());
00257 }
00258 
00259 GlobalAlias *GlobalAlias::create(LinkageTypes Link, const Twine &Name,
00260                                  GlobalValue *Aliasee) {
00261   PointerType *PTy = Aliasee->getType();
00262   return create(PTy, Link, Name, Aliasee);
00263 }
00264 
00265 GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
00266   return create(Aliasee->getLinkage(), Name, Aliasee);
00267 }
00268 
00269 void GlobalAlias::setParent(Module *parent) {
00270   Parent = parent;
00271 }
00272 
00273 void GlobalAlias::removeFromParent() {
00274   getParent()->getAliasList().remove(this);
00275 }
00276 
00277 void GlobalAlias::eraseFromParent() {
00278   getParent()->getAliasList().erase(this);
00279 }
00280 
00281 void GlobalAlias::setAliasee(Constant *Aliasee) {
00282   assert((!Aliasee || Aliasee->getType() == getType()) &&
00283          "Alias and aliasee types should match!");
00284   setOperand(0, Aliasee);
00285 }