1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- Constants.cpp - Implement Constant nodes --------------------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the Constant* classes.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "LLVMContextImpl.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "ConstantFold.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/GlobalValue.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instructions.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Module.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Operator.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/FoldingSet.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringExtras.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringMap.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Compiler.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ManagedStatic.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/MathExtras.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/GetElementPtrTypeIterator.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/DenseMap.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallVector.h"
3419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/STLExtras.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <algorithm>
3619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <cstdarg>
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                              Constant Class
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool Constant::isNegativeZeroValue() const {
4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Floating point values have an explicit -0.0 value.
4519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CFP->isZero() && CFP->isNegative();
4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Otherwise, just use +0.0.
4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return isNullValue();
5019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool Constant::isNullValue() const {
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // 0 is null.
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CI->isZero();
5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
5719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // +0.0 is null.
5819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
5919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CFP->isZero() && !CFP->isNegative();
6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // constant zero is zero for aggregates and cpnull is null for pointers.
6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return isa<ConstantAggregateZero>(this) || isa<ConstantPointerNull>(this);
6319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool Constant::isAllOnesValue() const {
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check for -1 integers
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CI->isMinusOne();
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check for FP which are bitcasted from -1 integers
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this))
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CFP->getValueAPF().bitcastToAPInt().isAllOnesValue();
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check for constant vectors
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return CV->isAllOnesValue();
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return false;
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Constructor to create a '0' constant of arbitrary type...
8119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *Constant::getNullValue(Type *Ty) {
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::IntegerTyID:
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantInt::get(Ty, 0);
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FloatTyID:
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantFP::get(Ty->getContext(),
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           APFloat::getZero(APFloat::IEEEsingle));
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID:
8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantFP::get(Ty->getContext(),
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           APFloat::getZero(APFloat::IEEEdouble));
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::X86_FP80TyID:
9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantFP::get(Ty->getContext(),
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           APFloat::getZero(APFloat::x87DoubleExtended));
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FP128TyID:
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantFP::get(Ty->getContext(),
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           APFloat::getZero(APFloat::IEEEquad));
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PPC_FP128TyID:
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantFP::get(Ty->getContext(),
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           APFloat(APInt::getNullValue(128)));
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID:
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantPointerNull::get(cast<PointerType>(Ty));
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::StructTyID:
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::ArrayTyID:
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VectorTyID:
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantAggregateZero::get(Ty);
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Function, Label, or Opaque type?
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(0 && "Cannot create a null constant of that type!");
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *Constant::getIntegerValue(Type *Ty, const APInt &V) {
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ScalarTy = Ty->getScalarType();
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create the base integer constant.
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = ConstantInt::get(Ty->getContext(), V);
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Convert an integer to a pointer, if necessary.
12019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (PointerType *PTy = dyn_cast<PointerType>(ScalarTy))
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    C = ConstantExpr::getIntToPtr(C, PTy);
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Broadcast a scalar to a vector, if necessary.
12419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    C = ConstantVector::get(std::vector<Constant *>(VTy->getNumElements(), C));
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return C;
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *Constant::getAllOnesValue(Type *Ty) {
13119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantInt::get(Ty->getContext(),
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            APInt::getAllOnesValue(ITy->getBitWidth()));
13419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Ty->isFloatingPointTy()) {
13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    APFloat FL = APFloat::getAllOnesValue(Ty->getPrimitiveSizeInBits(),
13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          !Ty->isPPC_FP128Ty());
13819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantFP::get(Ty->getContext(), FL);
13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
14019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<Constant*, 16> Elts;
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  VectorType *VTy = cast<VectorType>(Ty);
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Elts[0] && "Invalid AllOnes value!");
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return cast<ConstantVector>(ConstantVector::get(Elts));
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Constant::destroyConstantImpl() {
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // When a Constant is destroyed, there may be lingering
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // references to the constant by other constants in the constant pool.  These
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // constants are implicitly dependent on the module that is being deleted,
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // but they don't know that.  Because we only find out when the CPV is
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // deleted, we must now notify all of our users (that should only be
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Constants) that they are, in fact, invalid now and should be deleted.
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (!use_empty()) {
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Value *V = use_back();
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG      // Only in -g mode...
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isa<Constant>(V)) {
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      dbgs() << "While deleting: " << *this
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             << "\n\nUse still stuck around after Def is destroyed: "
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             << *V << "\n\n";
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(isa<Constant>(V) && "References remain to Constant being destroyed");
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *CV = cast<Constant>(V);
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CV->destroyConstant();
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // The constant should remove itself from our use list...
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert((use_empty() || use_back() != V) && "Constant not removed!");
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Value has no outstanding references it is safe to delete it now...
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  delete this;
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// canTrap - Return true if evaluation of this constant could trap.  This is
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// true for things like constant expressions that could divide by zero.
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool Constant::canTrap() const {
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The only thing that could possibly trap are constant exprs.
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!CE) return false;
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ConstantExpr traps if any operands can trap.
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CE->getOperand(i)->canTrap())
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, only specific operations can trap.
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (CE->getOpcode()) {
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UDiv:
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SDiv:
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FDiv:
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::URem:
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SRem:
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FRem:
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Div and rem can trap if the RHS is not known to be non-zero.
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isa<ConstantInt>(CE->getOperand(1)) ||CE->getOperand(1)->isNullValue())
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isConstantUsed - Return true if the constant has users other than constant
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// exprs and other dangling things.
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool Constant::isConstantUsed() const {
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (const_use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Constant *UC = dyn_cast<Constant>(*UI);
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (UC == 0 || isa<GlobalValue>(UC))
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (UC->isConstantUsed())
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getRelocationInfo - This method classifies the entry according to
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// whether or not it may generate a relocation entry.  This must be
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// conservative, so if it might codegen to a relocatable entry, it should say
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// so.  The return values are:
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///  NoRelocation: This constant pool entry is guaranteed to never have a
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     relocation applied to it (because it holds a simple constant like
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     '4').
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///  LocalRelocation: This entry has relocations, but the entries are
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     guaranteed to be resolvable by the static linker, so the dynamic
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///     linker will never see them.
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///  GlobalRelocations: This entry may have arbitrary relocations.
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FIXME: This really should not be in VMCore.
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant::PossibleRelocationsTy Constant::getRelocationInfo() const {
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return LocalRelocation;  // Local to this file/library.
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return GlobalRelocations;    // Global reference.
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return BA->getFunction()->getRelocationInfo();
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // While raw uses of blockaddress need to be relocated, differences between
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // two of them don't when they are for labels in the same function.  This is a
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // common idiom when creating a table for the indirect goto extension, so we
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // handle it efficiently here.
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this))
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CE->getOpcode() == Instruction::Sub) {
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ConstantExpr *LHS = dyn_cast<ConstantExpr>(CE->getOperand(0));
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ConstantExpr *RHS = dyn_cast<ConstantExpr>(CE->getOperand(1));
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (LHS && RHS &&
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          LHS->getOpcode() == Instruction::PtrToInt &&
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          RHS->getOpcode() == Instruction::PtrToInt &&
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isa<BlockAddress>(LHS->getOperand(0)) &&
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          isa<BlockAddress>(RHS->getOperand(0)) &&
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          cast<BlockAddress>(LHS->getOperand(0))->getFunction() ==
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            cast<BlockAddress>(RHS->getOperand(0))->getFunction())
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return NoRelocation;
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PossibleRelocationsTy Result = NoRelocation;
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result = std::max(Result,
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      cast<Constant>(getOperand(i))->getRelocationInfo());
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getVectorElements - This method, which is only valid on constant of vector
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// type, returns the elements of the vector in the specified smallvector.
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This handles breaking down a vector undef into undef elements, etc.  For
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// constant exprs and other cases we can't handle, we return an empty vector.
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getType()->isVectorTy() && "Not a vector constant!");
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i)
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Elts.push_back(CV->getOperand(i));
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  VectorType *VT = cast<VectorType>(getType());
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<ConstantAggregateZero>(this)) {
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Elts.assign(VT->getNumElements(),
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                Constant::getNullValue(VT->getElementType()));
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<UndefValue>(this)) {
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Unknown type, must be constant expr etc.
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// removeDeadUsersOfConstant - If the specified constantexpr is dead, remove
30419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// it.  This involves recursively eliminating any dead users of the
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// constantexpr.
30619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool removeDeadUsersOfConstant(const Constant *C) {
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<GlobalValue>(C)) return false; // Cannot remove this
30819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  while (!C->use_empty()) {
31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const Constant *User = dyn_cast<Constant>(C->use_back());
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!User) return false; // Non-constant usage;
31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!removeDeadUsersOfConstant(User))
31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false; // Constant wasn't dead
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const_cast<Constant*>(C)->destroyConstant();
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// removeDeadConstantUsers - If there are any dead constant users dangling
32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// off of this constant, remove them.  This method is useful for clients
32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// that want to check to see if a global is unused, but don't want to deal
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// with potentially dead constants hanging off of the globals.
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid Constant::removeDeadConstantUsers() const {
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Value::const_use_iterator I = use_begin(), E = use_end();
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Value::const_use_iterator LastNonDeadUser = E;
32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  while (I != E) {
32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const Constant *User = dyn_cast<Constant>(*I);
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (User == 0) {
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      LastNonDeadUser = I;
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ++I;
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
33519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!removeDeadUsersOfConstant(User)) {
33719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // If the constant wasn't dead, remember that this was the last live use
33819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // and move on to the next constant.
33919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      LastNonDeadUser = I;
34019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ++I;
34119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
34219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
34319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
34419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If the constant was dead, then the iterator is invalidated.
34519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LastNonDeadUser == E) {
34619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      I = use_begin();
34719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (I == E) break;
34819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
34919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      I = LastNonDeadUser;
35019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ++I;
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
35219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
35319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
35519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                                ConstantInt
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
36119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt::ConstantInt(IntegerType *Ty, const APInt& V)
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : Constant(Ty, ConstantIntVal, 0, 0), Val(V) {
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
36619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = Context.pImpl;
36819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!pImpl->TheTrueVal)
36919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
37019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return pImpl->TheTrueVal;
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
37319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = Context.pImpl;
37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!pImpl->TheFalseVal)
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return pImpl->TheFalseVal;
37819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
37919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
38019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantInt::getTrue(Type *Ty) {
38119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  VectorType *VTy = dyn_cast<VectorType>(Ty);
38219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!VTy) {
38319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(Ty->isIntegerTy(1) && "True must be i1 or vector of i1.");
38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantInt::getTrue(Ty->getContext());
38519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VTy->getElementType()->isIntegerTy(1) &&
38719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "True must be vector of i1 or i1.");
38819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<Constant*, 16> Splat(VTy->getNumElements(),
38919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   ConstantInt::getTrue(Ty->getContext()));
39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ConstantVector::get(Splat);
39119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
39219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
39319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantInt::getFalse(Type *Ty) {
39419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  VectorType *VTy = dyn_cast<VectorType>(Ty);
39519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!VTy) {
39619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(Ty->isIntegerTy(1) && "False must be i1 or vector of i1.");
39719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantInt::getFalse(Ty->getContext());
39819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
39919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(VTy->getElementType()->isIntegerTy(1) &&
40019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "False must be vector of i1 or i1.");
40119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<Constant*, 16> Splat(VTy->getNumElements(),
40219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   ConstantInt::getFalse(Ty->getContext()));
40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ConstantVector::get(Splat);
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// operator== and operator!= to ensure that the DenseMap doesn't attempt to
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// compare APInt's of different widths, which would violate an APInt class
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// invariant which generates an assertion.
41219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the corresponding integer type for the bit width of the value.
41419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // get an existing value or the insertion position
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DenseMapAPIntKeyInfo::KeyTy Key(V, ITy);
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantInt *&Slot = Context.pImpl->IntConstants[Key];
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Slot) Slot = new ConstantInt(ITy, V);
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Slot;
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) {
42319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned);
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For vectors, broadcast the value.
42619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantVector::get(SmallVector<Constant*,
42819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           16>(VTy->getNumElements(), C));
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return C;
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt* ConstantInt::get(IntegerType* Ty, uint64_t V,
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              bool isSigned) {
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Ty->getContext(), APInt(Ty->getBitWidth(), V, isSigned));
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt* ConstantInt::getSigned(IntegerType* Ty, int64_t V) {
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Ty, V, true);
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
44219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantInt::getSigned(Type *Ty, int64_t V) {
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Ty, V, true);
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
44619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantInt::get(Type* Ty, const APInt& V) {
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantInt *C = get(Ty->getContext(), V);
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType() == Ty->getScalarType() &&
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "ConstantInt type doesn't match the type implied by its value!");
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For vectors, broadcast the value.
45219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantVector::get(
45419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SmallVector<Constant *, 16>(VTy->getNumElements(), C));
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return C;
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantInt* ConstantInt::get(IntegerType* Ty, StringRef Str,
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              uint8_t radix) {
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Ty->getContext(), APInt(Ty->getBitWidth(), Str, radix));
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                                ConstantFP
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic const fltSemantics *TypeToFloatSemantics(Type *Ty) {
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isFloatTy())
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &APFloat::IEEEsingle;
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isDoubleTy())
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &APFloat::IEEEdouble;
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isX86_FP80Ty())
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &APFloat::x87DoubleExtended;
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Ty->isFP128Ty())
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &APFloat::IEEEquad;
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Ty->isPPC_FP128Ty() && "Unknown FP format");
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return &APFloat::PPCDoubleDouble;
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// get() - This returns a constant fp for the specified value in the
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specified type.  This should only be used for simple constant values like
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 2.0/1.0 etc, that are known-valid both as double and as the target format.
48519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantFP::get(Type* Ty, double V) {
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext &Context = Ty->getContext();
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APFloat FV(V);
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool ignored;
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FV.convert(*TypeToFloatSemantics(Ty->getScalarType()),
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             APFloat::rmNearestTiesToEven, &ignored);
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = get(Context, FV);
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For vectors, broadcast the value.
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantVector::get(
49719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SmallVector<Constant *, 16>(VTy->getNumElements(), C));
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return C;
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantFP::get(Type* Ty, StringRef Str) {
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext &Context = Ty->getContext();
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APFloat FV(*TypeToFloatSemantics(Ty->getScalarType()), Str);
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = get(Context, FV);
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For vectors, broadcast the value.
51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VTy = dyn_cast<VectorType>(Ty))
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantVector::get(
51219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SmallVector<Constant *, 16>(VTy->getNumElements(), C));
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return C;
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
51819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantFP* ConstantFP::getNegativeZero(Type* Ty) {
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext &Context = Ty->getContext();
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  apf.changeSign();
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Context, apf);
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantFP::getZeroValueForNegation(Type* Ty) {
52719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *PTy = dyn_cast<VectorType>(Ty))
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PTy->getElementType()->isFloatingPointTy()) {
52919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SmallVector<Constant*, 16> zeros(PTy->getNumElements(),
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           getNegativeZero(PTy->getElementType()));
53119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return ConstantVector::get(zeros);
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isFloatingPointTy())
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getNegativeZero(Ty);
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Constant::getNullValue(Ty);
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ConstantFP accessors.
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) {
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DenseMapAPFloatKeyInfo::KeyTy Key(V);
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl* pImpl = Context.pImpl;
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantFP *&Slot = pImpl->FPConstants[Key];
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Slot) {
55019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *Ty;
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (&V.getSemantics() == &APFloat::IEEEsingle)
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ty = Type::getFloatTy(Context);
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (&V.getSemantics() == &APFloat::IEEEdouble)
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ty = Type::getDoubleTy(Context);
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (&V.getSemantics() == &APFloat::x87DoubleExtended)
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ty = Type::getX86_FP80Ty(Context);
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (&V.getSemantics() == &APFloat::IEEEquad)
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ty = Type::getFP128Ty(Context);
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else {
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(&V.getSemantics() == &APFloat::PPCDoubleDouble &&
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             "Unknown FP format");
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ty = Type::getPPC_FP128Ty(Context);
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Slot = new ConstantFP(Ty, V);
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Slot;
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
57019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantFP *ConstantFP::getInfinity(Type *Ty, bool Negative) {
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const fltSemantics &Semantics = *TypeToFloatSemantics(Ty);
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ConstantFP::get(Ty->getContext(),
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         APFloat::getInf(Semantics, Negative));
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
57619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantFP::ConstantFP(Type *Ty, const APFloat& V)
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : Constant(Ty, ConstantFPVal, 0, 0), Val(V) {
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(&V.getSemantics() == TypeToFloatSemantics(Ty) &&
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "FP type Mismatch");
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
58219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool ConstantFP::isExactlyValue(const APFloat &V) const {
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Val.bitwiseIsEqual(V);
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                            ConstantXXX Classes
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
59119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantArray::ConstantArray(ArrayType *T, ArrayRef<Constant *> V)
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : Constant(T, ConstantArrayVal,
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             OperandTraits<ConstantArray>::op_end(this) - V.size(),
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             V.size()) {
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(V.size() == T->getNumElements() &&
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid initializer vector for constant array");
59719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = V.size(); i != e; ++i)
59819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(V[i]->getType() == T->getElementType() &&
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Initializer for array element doesn't match array element type!");
60019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::copy(V.begin(), V.end(), op_begin());
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
60319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantArray::get(ArrayType *Ty, ArrayRef<Constant*> V) {
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = V.size(); i != e; ++i) {
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(V[i]->getType() == Ty->getElementType() &&
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Wrong type in array element initializer");
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = Ty->getContext().pImpl;
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is an all-zero array, return a ConstantAggregateZero object
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!V.empty()) {
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C = V[0];
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!C->isNullValue())
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return pImpl->ArrayConstants.getOrCreate(Ty, V);
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1, e = V.size(); i != e; ++i)
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (V[i] != C)
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return pImpl->ArrayConstants.getOrCreate(Ty, V);
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ConstantAggregateZero::get(Ty);
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ConstantArray::get(const string&) - Return an array that is initialized to
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// contain the specified string.  If length is zero then a null terminator is
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// added to the specified string so that it may be used in a natural way.
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Otherwise, the length parameter specifies how much of the string to use
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// and it won't be null terminated.
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
62919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantArray::get(LLVMContext &Context, StringRef Str,
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             bool AddNull) {
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> ElementVals;
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ElementVals.reserve(Str.size() + size_t(AddNull));
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < Str.size(); ++i)
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), Str[i]));
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add a null terminator to the string...
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AddNull) {
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ElementVals.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArrayType *ATy = ArrayType::get(Type::getInt8Ty(Context), ElementVals.size());
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(ATy, ElementVals);
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
64519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getTypeForElements - Return an anonymous struct type to use for a constant
64619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// with the specified set of elements.  The list must not be empty.
64719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanStructType *ConstantStruct::getTypeForElements(LLVMContext &Context,
64819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                               ArrayRef<Constant*> V,
64919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                               bool Packed) {
65019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<Type*, 16> EltTypes;
65119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = V.size(); i != e; ++i)
65219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EltTypes.push_back(V[i]->getType());
65319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
65419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return StructType::get(Context, EltTypes, Packed);
65519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
65619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
65719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
65819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanStructType *ConstantStruct::getTypeForElements(ArrayRef<Constant*> V,
65919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                               bool Packed) {
66019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(!V.empty() &&
66119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "ConstantStruct::getTypeForElements cannot be called on empty list");
66219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getTypeForElements(V[0]->getContext(), V, Packed);
66319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
66619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantStruct::ConstantStruct(StructType *T, ArrayRef<Constant *> V)
667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : Constant(T, ConstantStructVal,
668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             OperandTraits<ConstantStruct>::op_end(this) - V.size(),
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             V.size()) {
670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(V.size() == T->getNumElements() &&
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid initializer vector for constant structure");
67219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = V.size(); i != e; ++i)
67319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert((T->isOpaque() || V[i]->getType() == T->getElementType(i)) &&
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Initializer for struct element doesn't match struct element type!");
67519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::copy(V.begin(), V.end(), op_begin());
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ConstantStruct accessors.
67919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
68019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Create a ConstantAggregateZero value if all elements are zeros.
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = V.size(); i != e; ++i)
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!V[i]->isNullValue())
68319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
68519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((ST->isOpaque() || ST->getNumElements() == V.size()) &&
68619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Incorrect # elements specified to ConstantStruct::get");
68719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ConstantAggregateZero::get(ST);
688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
69019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantStruct::get(StructType *T, ...) {
69119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  va_list ap;
69219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<Constant*, 8> Values;
69319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  va_start(ap, T);
69419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  while (Constant *Val = va_arg(ap, llvm::Constant*))
69519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Values.push_back(Val);
69619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  va_end(ap);
69719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(T, Values);
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
70019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : Constant(T, ConstantVectorVal,
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             OperandTraits<ConstantVector>::op_end(this) - V.size(),
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             V.size()) {
70419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (size_t i = 0, e = V.size(); i != e; i++)
70519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(V[i]->getType() == T->getElementType() &&
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Initializer for vector element doesn't match vector element type!");
70719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::copy(V.begin(), V.end(), op_begin());
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ConstantVector accessors.
71119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantVector::get(ArrayRef<Constant*> V) {
71219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(!V.empty() && "Vectors can't be empty");
71319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  VectorType *T = VectorType::get(V.front()->getType(), V.size());
71419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = T->getContext().pImpl;
71519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
71619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is an all-undef or all-zero vector, return a
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // ConstantAggregateZero or UndefValue.
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *C = V[0];
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isZero = C->isNullValue();
720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isUndef = isa<UndefValue>(C);
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isZero || isUndef) {
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1, e = V.size(); i != e; ++i)
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (V[i] != C) {
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        isZero = isUndef = false;
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isZero)
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantAggregateZero::get(T);
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isUndef)
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return UndefValue::get(T);
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->VectorConstants.getOrCreate(T, V);
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Utility function for determining if a ConstantExpr is a CastOp or not. This
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// can't be inline because we don't want to #include Instruction.h into
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Constant.h
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantExpr::isCast() const {
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Instruction::isCast(getOpcode());
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantExpr::isCompare() const {
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getOpcode() == Instruction::ICmp || getOpcode() == Instruction::FCmp;
747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantExpr::isGEPWithNoNotionalOverIndexing() const {
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getOpcode() != Instruction::GetElementPtr) return false;
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  gep_type_iterator GEPI = gep_type_begin(this), E = gep_type_end(this);
753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  User::const_op_iterator OI = llvm::next(this->op_begin());
754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Skip the first index, as it has no static limit.
756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++GEPI;
757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++OI;
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The remaining indices must be compile-time known integers within the
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // bounds of the corresponding notional static array types.
761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (; GEPI != E; ++GEPI, ++OI) {
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantInt *CI = dyn_cast<ConstantInt>(*OI);
763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!CI) return false;
76419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ArrayType *ATy = dyn_cast<ArrayType>(*GEPI))
765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CI->getValue().getActiveBits() > 64 ||
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          CI->getZExtValue() >= ATy->getNumElements())
767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // All the indices checked out.
771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantExpr::hasIndices() const {
775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getOpcode() == Instruction::ExtractValue ||
776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         getOpcode() == Instruction::InsertValue;
777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
77919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanArrayRef<unsigned> ConstantExpr::getIndices() const {
780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ExtractValueConstantExpr *EVCE =
781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        dyn_cast<ExtractValueConstantExpr>(this))
782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EVCE->Indices;
783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return cast<InsertValueConstantExpr>(this)->Indices;
785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned ConstantExpr::getPredicate() const {
78819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(isCompare());
789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ((const CompareConstantExpr*)this)->predicate;
790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getWithOperandReplaced - Return a constant expression identical to this
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// one, but with the specified operand set to the specified value.
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstantExpr::getWithOperandReplaced(unsigned OpNo, Constant *Op) const {
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(OpNo < getNumOperands() && "Operand num is out of range!");
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op->getType() == getOperand(OpNo)->getType() &&
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Replacing operand with value of different type!");
799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getOperand(OpNo) == Op)
800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return const_cast<ConstantExpr*>(this);
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Op0, *Op1, *Op2;
803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (getOpcode()) {
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Trunc:
805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ZExt:
806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SExt:
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPTrunc:
808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPExt:
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UIToFP:
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SIToFP:
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToUI:
812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToSI:
813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::PtrToInt:
814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::IntToPtr:
815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::BitCast:
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getCast(getOpcode(), Op, getType());
817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Select:
818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = (OpNo == 0) ? Op : getOperand(0);
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = (OpNo == 1) ? Op : getOperand(1);
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op2 = (OpNo == 2) ? Op : getOperand(2);
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getSelect(Op0, Op1, Op2);
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::InsertElement:
823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = (OpNo == 0) ? Op : getOperand(0);
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = (OpNo == 1) ? Op : getOperand(1);
825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op2 = (OpNo == 2) ? Op : getOperand(2);
826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getInsertElement(Op0, Op1, Op2);
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ExtractElement:
828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = (OpNo == 0) ? Op : getOperand(0);
829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = (OpNo == 1) ? Op : getOperand(1);
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getExtractElement(Op0, Op1);
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ShuffleVector:
832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = (OpNo == 0) ? Op : getOperand(0);
833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = (OpNo == 1) ? Op : getOperand(1);
834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op2 = (OpNo == 2) ? Op : getOperand(2);
835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::GetElementPtr: {
837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<Constant*, 8> Ops;
838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops.resize(getNumOperands()-1);
839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Ops[i-1] = getOperand(i);
841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpNo == 0)
84219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return
84319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ConstantExpr::getGetElementPtr(Op, Ops,
84419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       cast<GEPOperator>(this)->isInBounds());
845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Ops[OpNo-1] = Op;
84619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return
84719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ConstantExpr::getGetElementPtr(getOperand(0), Ops,
84819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     cast<GEPOperator>(this)->isInBounds());
849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(getNumOperands() == 2 && "Must be binary operator?");
852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op0 = (OpNo == 0) ? Op : getOperand(0);
853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Op1 = (OpNo == 1) ? Op : getOperand(1);
854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::get(getOpcode(), Op0, Op1, SubclassOptionalData);
855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getWithOperands - This returns the current constant expression with the
85919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// operands replaced with the specified values.  The specified array must
86019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// have the same number of operands as our current one.
861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *ConstantExpr::
86219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumangetWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const {
86319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
86419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool AnyChange = Ty != getType();
86519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0; i != Ops.size(); ++i)
866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AnyChange |= Ops[i] != getOperand(i);
86719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AnyChange)  // No operands changed, return self.
869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return const_cast<ConstantExpr*>(this);
870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (getOpcode()) {
872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Trunc:
873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ZExt:
874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SExt:
875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPTrunc:
876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPExt:
877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UIToFP:
878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SIToFP:
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToUI:
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToSI:
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::PtrToInt:
882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::IntToPtr:
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::BitCast:
88419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return ConstantExpr::getCast(getOpcode(), Ops[0], Ty);
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Select:
886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::InsertElement:
888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ExtractElement:
890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ShuffleVector:
892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::GetElementPtr:
89419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return
89519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1),
89619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     cast<GEPOperator>(this)->isInBounds());
897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ICmp:
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FCmp:
899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::getCompare(getPredicate(), Ops[0], Ops[1]);
900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(getNumOperands() == 2 && "Must be binary operator?");
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ConstantExpr::get(getOpcode(), Ops[0], Ops[1], SubclassOptionalData);
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                      isValueValidForType implementations
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
91019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool ConstantInt::isValueValidForType(Type *Ty, uint64_t Val) {
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty == Type::getInt1Ty(Ty->getContext()))
913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val == 0 || Val == 1;
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumBits >= 64)
915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true; // always true, has to fit in largest type
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint64_t Max = (1ll << NumBits) - 1;
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Val <= Max;
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool ConstantInt::isValueValidForType(Type *Ty, int64_t Val) {
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); // assert okay
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty == Type::getInt1Ty(Ty->getContext()))
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val == 0 || Val == 1 || Val == -1;
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumBits >= 64)
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true; // always true, has to fit in largest type
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int64_t Min = -(1ll << (NumBits-1));
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int64_t Max = (1ll << (NumBits-1)) - 1;
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return (Val >= Min && Val <= Max);
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
93119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool ConstantFP::isValueValidForType(Type *Ty, const APFloat& Val) {
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // convert modifies in place, so make a copy.
933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APFloat Val2 = APFloat(Val);
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool losesInfo;
935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;         // These can't be represented as floating point!
938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME rounding mode needs to be more flexible
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FloatTyID: {
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (&Val2.getSemantics() == &APFloat::IEEEsingle)
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return !losesInfo;
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID: {
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (&Val2.getSemantics() == &APFloat::IEEEsingle ||
948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        &Val2.getSemantics() == &APFloat::IEEEdouble)
949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return !losesInfo;
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::X86_FP80TyID:
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &Val2.getSemantics() == &APFloat::IEEEsingle ||
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Val2.getSemantics() == &APFloat::IEEEdouble ||
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Val2.getSemantics() == &APFloat::x87DoubleExtended;
957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FP128TyID:
958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &Val2.getSemantics() == &APFloat::IEEEsingle ||
959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Val2.getSemantics() == &APFloat::IEEEdouble ||
960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Val2.getSemantics() == &APFloat::IEEEquad;
961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PPC_FP128TyID:
962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &Val2.getSemantics() == &APFloat::IEEEsingle ||
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Val2.getSemantics() == &APFloat::IEEEdouble ||
964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Val2.getSemantics() == &APFloat::PPCDoubleDouble;
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                      Factory Function Implementation
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantAggregateZero* ConstantAggregateZero::get(Type* Ty) {
97219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert((Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()) &&
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Cannot create an aggregate zero of non-aggregate type!");
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = Ty->getContext().pImpl;
976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->AggZeroConstants.getOrCreate(Ty, 0);
977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// destroyConstant - Remove the constant from the constant table...
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantAggregateZero::destroyConstant() {
98219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->AggZeroConstants.remove(this);
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// destroyConstant - Remove the constant from the constant table...
987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantArray::destroyConstant() {
98919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->ArrayConstants.remove(this);
990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isString - This method returns true if the array is an array of i8, and
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// if the elements of the array are all ConstantInt's.
995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantArray::isString() const {
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check the element type for i8...
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!getType()->getElementType()->isIntegerTy(8))
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check the elements to make sure they are all integers, not constant
1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // expressions.
1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isa<ConstantInt>(getOperand(i)))
1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isCString - This method returns true if the array is a string (see
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isString) and it ends in a null byte \\0 and does not contains any other
1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// null bytes except its terminator.
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantArray::isCString() const {
1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check the element type for i8...
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!getType()->getElementType()->isIntegerTy(8))
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Last element must be a null.
1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!getOperand(getNumOperands()-1)->isNullValue())
1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Other elements must be non-null integers.
1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) {
1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isa<ConstantInt>(getOperand(i)))
1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (getOperand(i)->isNullValue())
1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
102919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// convertToString - Helper function for getAsString() and getAsCString().
103019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic std::string convertToString(const User *U, unsigned len) {
1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string Result;
103219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Result.reserve(len);
103319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0; i != len; ++i)
103419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Result.push_back((char)cast<ConstantInt>(U->getOperand(i))->getZExtValue());
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
103819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getAsString - If this array is isString(), then this method converts the
103919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// array to an std::string and returns it.  Otherwise, it asserts out.
104019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///
104119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstd::string ConstantArray::getAsString() const {
104219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(isString() && "Not a string!");
104319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return convertToString(this, getNumOperands());
104419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getAsCString - If this array is isCString(), then this method converts the
104819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// array (without the trailing null byte) to an std::string and returns it.
104919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// Otherwise, it asserts out.
105019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman///
105119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstd::string ConstantArray::getAsCString() const {
105219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(isCString() && "Not a string!");
105319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return convertToString(this, getNumOperands() - 1);
1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
105619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
105719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//---- ConstantStruct::get() implementation...
1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// destroyConstant - Remove the constant from the constant table...
1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
106219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid ConstantStruct::destroyConstant() {
106319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->StructConstants.remove(this);
1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// destroyConstant - Remove the constant from the constant table...
1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantVector::destroyConstant() {
107019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->VectorConstants.remove(this);
1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This function will return true iff every element in this vector constant
1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// is set to all ones.
107619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// @returns true iff this constant's elements are all set to all ones.
1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// @brief Determine if the value is all ones.
1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ConstantVector::isAllOnesValue() const {
1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check out first element.
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Constant *Elt = getOperand(0);
1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
108219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const ConstantFP *CF = dyn_cast<ConstantFP>(Elt);
108319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Then make sure all remaining elements point to the same value.
108519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
108619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (getOperand(I) != Elt)
108719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return false;
108819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
108919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // First value is all-ones.
109019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return (CI && CI->isAllOnesValue()) ||
109119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         (CF && CF->isAllOnesValue());
1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getSplatValue - If this is a splat constant, where all of the
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// elements have the same value, return that value. Otherwise return null.
109619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantVector::getSplatValue() const {
1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check out first element.
1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Elt = getOperand(0);
1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Then make sure all remaining elements point to the same value.
1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned I = 1, E = getNumOperands(); I < E; ++I)
110119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (getOperand(I) != Elt)
110219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return 0;
1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Elt;
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//---- ConstantPointerNull::get() implementation.
1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
110919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Ty->getContext().pImpl->NullPtrConstants.getOrCreate(Ty, 0);
1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// destroyConstant - Remove the constant from the constant table...
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantPointerNull::destroyConstant() {
111619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->NullPtrConstants.remove(this);
1117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
1118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//---- UndefValue::get() implementation.
1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanUndefValue *UndefValue::get(Type *Ty) {
1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Ty->getContext().pImpl->UndefValueConstants.getOrCreate(Ty, 0);
1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// destroyConstant - Remove the constant from the constant table.
1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid UndefValue::destroyConstant() {
113119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->UndefValueConstants.remove(this);
1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//---- BlockAddress::get() implementation.
1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanBlockAddress *BlockAddress::get(BasicBlock *BB) {
1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(BB->getParent() != 0 && "Block must have a parent");
1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(BB->getParent(), BB);
1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanBlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BlockAddress *&BA =
1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    F->getContext().pImpl->BlockAddresses[std::make_pair(F, BB)];
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BA == 0)
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BA = new BlockAddress(F, BB);
1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(BA->getFunction() == F && "Basic block moved between functions");
1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BA;
1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanBlockAddress::BlockAddress(Function *F, BasicBlock *BB)
1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman: Constant(Type::getInt8PtrTy(F->getContext()), Value::BlockAddressVal,
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           &Op<0>(), 2) {
1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperand(0, F);
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperand(1, BB);
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB->AdjustBlockAddressRefCount(1);
1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// destroyConstant - Remove the constant from the constant table.
1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid BlockAddress::destroyConstant() {
116519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getFunction()->getType()->getContext().pImpl
1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getBasicBlock()->AdjustBlockAddressRefCount(-1);
1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid BlockAddress::replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) {
1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This could be replacing either the Basic Block or the Function.  In either
1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // case, we have to remove the map entry.
1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Function *NewF = getFunction();
1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlock *NewBB = getBasicBlock();
1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (U == &Op<0>())
1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewF = cast<Function>(To);
1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewBB = cast<BasicBlock>(To);
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // See if the 'new' entry already exists, if not, just update this in place
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // and return early.
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BlockAddress *&NewBA =
1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getContext().pImpl->BlockAddresses[std::make_pair(NewF, NewBB)];
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NewBA == 0) {
1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getBasicBlock()->AdjustBlockAddressRefCount(-1);
1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Remove the old entry, this can't cause the map to rehash (just a
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // tombstone will get added).
1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getContext().pImpl->BlockAddresses.erase(std::make_pair(getFunction(),
1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                            getBasicBlock()));
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NewBA = this;
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperand(0, NewF);
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperand(1, NewBB);
1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getBasicBlock()->AdjustBlockAddressRefCount(1);
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, I do need to replace this with an existing value.
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(NewBA != this && "I didn't contain From!");
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everyone using this now uses the replacement.
120419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  replaceAllUsesWith(NewBA);
1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstant();
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//---- ConstantExpr::get() implementations.
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is a utility function to handle folding of casts and lookup of the
1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// cast in the ExprConstants map. It is used by the various get* methods below.
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic inline Constant *getFoldedCast(
121519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Instruction::CastOps opc, Constant *C, Type *Ty) {
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fold a few common cases
1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return FC;
1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = Ty->getContext().pImpl;
1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look up the constant in the table first to ensure uniqueness
1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> argVec(1, C);
1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ExprMapKeyType Key(opc, argVec);
1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->ExprConstants.getOrCreate(Ty, Key);
1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
123019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty) {
1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Instruction::CastOps opc = Instruction::CastOps(oc);
1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Instruction::isCast(opc) && "opcode out of range");
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C && Ty && "Null arguments to getCast");
1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (opc) {
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Invalid cast opcode");
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Trunc:    return getTrunc(C, Ty);
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ZExt:     return getZExt(C, Ty);
1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SExt:     return getSExt(C, Ty);
1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPTrunc:  return getFPTrunc(C, Ty);
1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPExt:    return getFPExtend(C, Ty);
1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UIToFP:   return getUIToFP(C, Ty);
1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SIToFP:   return getSIToFP(C, Ty);
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToUI:   return getFPToUI(C, Ty);
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToSI:   return getFPToSI(C, Ty);
1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::PtrToInt: return getPtrToInt(C, Ty);
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::IntToPtr: return getIntToPtr(C, Ty);
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::BitCast:  return getBitCast(C, Ty);
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
125619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getZExtOrBitCast(Constant *C, Type *Ty) {
1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getBitCast(C, Ty);
1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getZExt(C, Ty);
1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
126219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSExtOrBitCast(Constant *C, Type *Ty) {
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getBitCast(C, Ty);
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getSExt(C, Ty);
1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
126819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) {
1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (C->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getBitCast(C, Ty);
1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getTrunc(C, Ty);
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) {
1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(S->getType()->isPointerTy() && "Invalid cast");
1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((Ty->isIntegerTy() || Ty->isPointerTy()) && "Invalid cast");
1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isIntegerTy())
1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getPtrToInt(S, Ty);
1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getBitCast(S, Ty);
1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getIntegerCast(Constant *C, Type *Ty,
1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       bool isSigned) {
1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() &&
1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         Ty->isIntOrIntVectorTy() && "Invalid cast");
1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned SrcBits = C->getType()->getScalarSizeInBits();
1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned DstBits = Ty->getScalarSizeInBits();
1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Instruction::CastOps opcode =
1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    (SrcBits == DstBits ? Instruction::BitCast :
1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     (SrcBits > DstBits ? Instruction::Trunc :
1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (isSigned ? Instruction::SExt : Instruction::ZExt)));
1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getCast(opcode, C, Ty);
1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
129619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFPCast(Constant *C, Type *Ty) {
1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid cast");
1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned SrcBits = C->getType()->getScalarSizeInBits();
1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned DstBits = Ty->getScalarSizeInBits();
1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcBits == DstBits)
1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return C; // Avoid a useless cast
1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Instruction::CastOps opcode =
130419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt);
1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getCast(opcode, C, Ty);
1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
130819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getTrunc(Constant *C, Type *Ty) {
1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "SrcTy must be larger than DestTy for Trunc!");
1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::Trunc, C, Ty);
1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
132219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSExt(Constant *C, Type *Ty) {
1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() && "SExt operand must be integral");
1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Ty->isIntOrIntVectorTy() && "SExt produces only integer");
1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "SrcTy must be smaller than DestTy for SExt!");
1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::SExt, C, Ty);
1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
133619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getZExt(Constant *C, Type *Ty) {
1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() && "ZEXt operand must be integral");
1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Ty->isIntOrIntVectorTy() && "ZExt produces only integer");
1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "SrcTy must be smaller than DestTy for ZExt!");
1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::ZExt, C, Ty);
1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
135019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFPTrunc(Constant *C, Type *Ty) {
1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         C->getType()->getScalarSizeInBits() > Ty->getScalarSizeInBits()&&
1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "This is an illegal floating point truncation!");
1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::FPTrunc, C, Ty);
1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
136219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFPExtend(Constant *C, Type *Ty) {
1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&&
1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "This is an illegal floating point extension!");
1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::FPExt, C, Ty);
1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
137419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getUIToFP(Constant *C, Type *Ty) {
1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "This is an illegal uint to floating point cast!");
1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::UIToFP, C, Ty);
1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
138519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSIToFP(Constant *C, Type *Ty) {
1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() && Ty->isFPOrFPVectorTy() &&
1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "This is an illegal sint to floating point cast!");
1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::SIToFP, C, Ty);
1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFPToUI(Constant *C, Type *Ty) {
1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "This is an illegal floating point to uint cast!");
1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::FPToUI, C, Ty);
1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
140719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFPToSI(Constant *C, Type *Ty) {
1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool toVec = Ty->getTypeID() == Type::VectorTyID;
1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isFPOrFPVectorTy() && Ty->isIntOrIntVectorTy() &&
1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "This is an illegal floating point to sint cast!");
1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::FPToSI, C, Ty);
1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy) {
1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isPointerTy() && "PtrToInt source must be pointer");
1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(DstTy->isIntegerTy() && "PtrToInt destination must be integral");
1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::PtrToInt, C, DstTy);
1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
142419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy) {
1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntegerTy() && "IntToPtr source must be integral");
1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(DstTy->isPointerTy() && "IntToPtr destination must be a pointer");
1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::IntToPtr, C, DstTy);
1428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
143019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getBitCast(Constant *C, Type *DstTy) {
1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(CastInst::castIsValid(Instruction::BitCast, C, DstTy) &&
1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid constantexpr bitcast!");
1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // It is common to ask for a bitcast of a value to its own type, handle this
1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // speedily.
1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (C->getType() == DstTy) return C;
1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getFoldedCast(Instruction::BitCast, C, DstTy);
1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
144219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            unsigned Flags) {
144319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check the operands for consistency first.
1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Opcode >= Instruction::BinaryOpsBegin &&
1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         Opcode <  Instruction::BinaryOpsEnd   &&
1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid opcode in binary constant expression");
1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C1->getType() == C2->getType() &&
1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Operand types in binary constant expression should match");
1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
1451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Opcode) {
1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Add:
1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Sub:
1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Mul:
1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isIntOrIntVectorTy() &&
1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create an integer operation on a non-integer type!");
1458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FAdd:
1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FSub:
1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FMul:
1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isFPOrFPVectorTy() &&
1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create a floating-point operation on a "
1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "non-floating-point type!");
1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UDiv:
1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SDiv:
1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isIntOrIntVectorTy() &&
1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create an arithmetic operation on a non-arithmetic type!");
1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FDiv:
1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isFPOrFPVectorTy() &&
1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create an arithmetic operation on a non-arithmetic type!");
1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::URem:
1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SRem:
1480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isIntOrIntVectorTy() &&
1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create an arithmetic operation on a non-arithmetic type!");
1483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FRem:
1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isFPOrFPVectorTy() &&
1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create an arithmetic operation on a non-arithmetic type!");
1488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::And:
1490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Or:
1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Xor:
1492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isIntOrIntVectorTy() &&
1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create a logical operation on a non-integral type!");
1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Shl:
1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::LShr:
1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::AShr:
1499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType() == C2->getType() && "Op types should be identical!");
1500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(C1->getType()->isIntOrIntVectorTy() &&
1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Tried to create a shift operation on a non-integer type!");
1502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
1504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
1507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
150819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
150919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return FC;          // Fold a few common cases.
151019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
151119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<Constant*> argVec(1, C1);
151219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  argVec.push_back(C2);
151319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ExprMapKeyType Key(Opcode, argVec, 0, Flags);
151419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
151519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = C1->getContext().pImpl;
151619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return pImpl->ExprConstants.getOrCreate(C1->getType(), Key);
1517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
151919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSizeOf(Type* Ty) {
1520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // sizeof is implemented as: (i64) gep (Ty*)null, 1
1521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Note that a non-inbounds gep is used, as null isn't within any object.
1522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *GEPIdx = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *GEP = getGetElementPtr(
152419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getPtrToInt(GEP,
1526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Type::getInt64Ty(Ty->getContext()));
1527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getAlignOf(Type* Ty) {
1530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
1531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Note that a non-inbounds gep is used, as null isn't within any object.
153219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *AligningTy =
153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StructType::get(Type::getInt1Ty(Ty->getContext()), Ty, NULL);
1534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
1535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
1536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);
1537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Indices[2] = { Zero, One };
153819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Constant *GEP = getGetElementPtr(NullPtr, Indices);
1539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getPtrToInt(GEP,
1540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Type::getInt64Ty(Ty->getContext()));
1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
154319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getOffsetOf(StructType* STy, unsigned FieldNo) {
1544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getOffsetOf(STy, ConstantInt::get(Type::getInt32Ty(STy->getContext()),
1545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           FieldNo));
1546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
154819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getOffsetOf(Type* Ty, Constant *FieldNo) {
1549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // offsetof is implemented as: (i64) gep (Ty*)null, 0, FieldNo
1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Note that a non-inbounds gep is used, as null isn't within any object.
1551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *GEPIdx[] = {
1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0),
1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FieldNo
1554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
1555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *GEP = getGetElementPtr(
155619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                Constant::getNullValue(PointerType::getUnqual(Ty)), GEPIdx);
1557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return getPtrToInt(GEP,
1558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     Type::getInt64Ty(Ty->getContext()));
1559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
156119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getCompare(unsigned short Predicate,
156219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   Constant *C1, Constant *C2) {
1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C1->getType() == C2->getType() && "Op types should be identical!");
156419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
156519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (Predicate) {
156619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: llvm_unreachable("Invalid CmpInst predicate");
156719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::FCMP_FALSE: case CmpInst::FCMP_OEQ: case CmpInst::FCMP_OGT:
156819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::FCMP_OGE:   case CmpInst::FCMP_OLT: case CmpInst::FCMP_OLE:
156919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::FCMP_ONE:   case CmpInst::FCMP_ORD: case CmpInst::FCMP_UNO:
157019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::FCMP_UEQ:   case CmpInst::FCMP_UGT: case CmpInst::FCMP_UGE:
157119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::FCMP_ULT:   case CmpInst::FCMP_ULE: case CmpInst::FCMP_UNE:
157219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::FCMP_TRUE:
157319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getFCmp(Predicate, C1, C2);
157419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
157519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::ICMP_EQ:  case CmpInst::ICMP_NE:  case CmpInst::ICMP_UGT:
157619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::ICMP_UGE: case CmpInst::ICMP_ULT: case CmpInst::ICMP_ULE:
157719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::ICMP_SGT: case CmpInst::ICMP_SGE: case CmpInst::ICMP_SLT:
157819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CmpInst::ICMP_SLE:
157919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return getICmp(Predicate, C1, C2);
158019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
158319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2) {
1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
158619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
158719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return SC;        // Fold common cases
1588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> argVec(3, C);
1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  argVec[1] = V1;
1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  argVec[2] = V2;
1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ExprMapKeyType Key(Instruction::Select, argVec);
1593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = C->getContext().pImpl;
159519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return pImpl->ExprConstants.getOrCreate(V1->getType(), Key);
1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getGetElementPtr(Constant *C, ArrayRef<Value *> Idxs,
159919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         bool InBounds) {
160019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs))
160119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return FC;          // Fold a few common cases.
1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Get the result type of the getelementptr!
160419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), Idxs);
160519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Ty && "GEP indices invalid!");
160619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned AS = cast<PointerType>(C->getType())->getAddressSpace();
160719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ReqTy = Ty->getPointerTo(AS);
160819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isPointerTy() &&
1610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Non-pointer type for constant GetElementPtr expression");
1611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look up the constant in the table first to ensure uniqueness
1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> ArgVec;
161319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ArgVec.reserve(1 + Idxs.size());
1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgVec.push_back(C);
161519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ArgVec.push_back(cast<Constant>(Idxs[i]));
1617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec, 0,
161819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           InBounds ? GEPOperator::IsInBounds : 0);
161919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
162019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = C->getContext().pImpl;
1621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *
1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstantExpr::getICmp(unsigned short pred, Constant *LHS, Constant *RHS) {
1626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(LHS->getType() == RHS->getType());
1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return FC;          // Fold a few common cases...
1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look up the constant in the table first to ensure uniqueness
1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> ArgVec;
1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgVec.push_back(LHS);
1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgVec.push_back(RHS);
1637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the key type with both the opcode and predicate
1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const ExprMapKeyType Key(Instruction::ICmp, ArgVec, pred);
1639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
164019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ResultTy = Type::getInt1Ty(LHS->getContext());
164119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
1645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
1646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *
1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstantExpr::getFCmp(unsigned short pred, Constant *LHS, Constant *RHS) {
1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(LHS->getType() == RHS->getType());
1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
1652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
1654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return FC;          // Fold a few common cases...
1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look up the constant in the table first to ensure uniqueness
1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> ArgVec;
1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgVec.push_back(LHS);
1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgVec.push_back(RHS);
1660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the key type with both the opcode and predicate
1661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const ExprMapKeyType Key(Instruction::FCmp, ArgVec, pred);
1662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
166319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ResultTy = Type::getInt1Ty(LHS->getContext());
166419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VectorType *VT = dyn_cast<VectorType>(LHS->getType()))
1665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ResultTy = VectorType::get(ResultTy, VT->getNumElements());
1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl;
1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->ExprConstants.getOrCreate(ResultTy, Key);
1669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Val->getType()->isVectorTy() &&
1673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Tried to create extractelement operation on non-vector type!");
1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Idx->getType()->isIntegerTy(32) &&
1675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Extractelement index must be i32 type!");
167619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
167719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return FC;          // Fold a few common cases.
167919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look up the constant in the table first to ensure uniqueness
1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> ArgVec(1, Val);
1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgVec.push_back(Idx);
168319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const ExprMapKeyType Key(Instruction::ExtractElement,ArgVec);
1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
168519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = Val->getContext().pImpl;
168619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ReqTy = cast<VectorType>(Val->getType())->getElementType();
1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pImpl->ExprConstants.getOrCreate(ReqTy, Key);
1688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         Constant *Idx) {
1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Val->getType()->isVectorTy() &&
1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Tried to create insertelement operation on non-vector type!");
1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Elt->getType() == cast<VectorType>(Val->getType())->getElementType()
1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         && "Insertelement types must match!");
1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Idx->getType()->isIntegerTy(32) &&
1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Insertelement index must be i32 type!");
1698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
170019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return FC;          // Fold a few common cases.
1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Look up the constant in the table first to ensure uniqueness
170219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<Constant*> ArgVec(1, Val);
170319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ArgVec.push_back(Elt);
170419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ArgVec.push_back(Idx);
170519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const ExprMapKeyType Key(Instruction::InsertElement,ArgVec);
1706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
170719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = Val->getContext().pImpl;
170819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return pImpl->ExprConstants.getOrCreate(Val->getType(), Key);
1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         Constant *Mask) {
1713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid shuffle vector constant expr operands!");
1715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
171619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
171719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return FC;          // Fold a few common cases.
171819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NElts = cast<VectorType>(Mask->getType())->getNumElements();
172019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
172119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ShufTy = VectorType::get(EltTy, NElts);
1722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
172319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Look up the constant in the table first to ensure uniqueness
172419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<Constant*> ArgVec(1, V1);
172519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ArgVec.push_back(V2);
172619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ArgVec.push_back(Mask);
172719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const ExprMapKeyType Key(Instruction::ShuffleVector,ArgVec);
172819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
172919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = ShufTy->getContext().pImpl;
173019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val,
173419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       ArrayRef<unsigned> Idxs) {
173519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(ExtractValueInst::getIndexedType(Agg->getType(),
173619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          Idxs) == Val->getType() &&
173719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "insertvalue indices invalid!");
1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Agg->getType()->isFirstClassType() &&
173919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Non-first-class type for constant insertvalue expression");
174019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs);
174119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(FC && "insertvalue constant expr couldn't be folded!");
1742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return FC;
1743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanConstant *ConstantExpr::getExtractValue(Constant *Agg,
174619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        ArrayRef<unsigned> Idxs) {
1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Agg->getType()->isFirstClassType() &&
1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Tried to create extractelement operation on non-first-class type!");
1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
175019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), Idxs);
175119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  (void)ReqTy;
1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(ReqTy && "extractvalue indices invalid!");
175319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
175419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Agg->getType()->isFirstClassType() &&
175519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Non-first-class type for constant extractvalue expression");
175619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs);
175719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(FC && "ExtractValue constant expr couldn't be folded!");
175819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return FC;
1759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
176119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() &&
1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Cannot NEG a nonintegral value!");
176419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getSub(ConstantFP::getZeroValueForNegation(C->getType()),
176519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                C, HasNUW, HasNSW);
1766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
176819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFNeg(Constant *C) {
1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isFPOrFPVectorTy() &&
1770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Cannot FNEG a non-floating-point value!");
177119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getFSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
1772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
177419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getNot(Constant *C) {
1775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(C->getType()->isIntOrIntVectorTy() &&
1776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Cannot NOT a nonintegral value!");
1777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
1778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getAdd(Constant *C1, Constant *C2,
178119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               bool HasNUW, bool HasNSW) {
178219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
178319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
178419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::Add, C1, C2, Flags);
1785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFAdd(Constant *C1, Constant *C2) {
1788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::FAdd, C1, C2);
1789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
179119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSub(Constant *C1, Constant *C2,
179219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               bool HasNUW, bool HasNSW) {
179319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
179419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
179519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::Sub, C1, C2, Flags);
1796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
179819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFSub(Constant *C1, Constant *C2) {
1799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::FSub, C1, C2);
1800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
180219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getMul(Constant *C1, Constant *C2,
180319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               bool HasNUW, bool HasNSW) {
180419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
180519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
180619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::Mul, C1, C2, Flags);
1807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
180919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFMul(Constant *C1, Constant *C2) {
1810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::FMul, C1, C2);
1811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
181319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getUDiv(Constant *C1, Constant *C2, bool isExact) {
181419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::UDiv, C1, C2,
181519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             isExact ? PossiblyExactOperator::IsExact : 0);
1816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
181819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSDiv(Constant *C1, Constant *C2, bool isExact) {
181919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::SDiv, C1, C2,
182019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             isExact ? PossiblyExactOperator::IsExact : 0);
1821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
182319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFDiv(Constant *C1, Constant *C2) {
1824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::FDiv, C1, C2);
1825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
182719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getURem(Constant *C1, Constant *C2) {
1828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::URem, C1, C2);
1829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getSRem(Constant *C1, Constant *C2) {
1832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::SRem, C1, C2);
1833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getFRem(Constant *C1, Constant *C2) {
1836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::FRem, C1, C2);
1837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
1840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::And, C1, C2);
1841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
184319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
1844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::Or, C1, C2);
1845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
184719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
1848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return get(Instruction::Xor, C1, C2);
1849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getShl(Constant *C1, Constant *C2,
185219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               bool HasNUW, bool HasNSW) {
185319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
185419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                   (HasNSW ? OverflowingBinaryOperator::NoSignedWrap   : 0);
185519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::Shl, C1, C2, Flags);
1856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
185919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::LShr, C1, C2,
186019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             isExact ? PossiblyExactOperator::IsExact : 0);
1861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
186319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanConstant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
186419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return get(Instruction::AShr, C1, C2,
186519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             isExact ? PossiblyExactOperator::IsExact : 0);
1866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// destroyConstant - Remove the constant from the constant table...
1869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantExpr::destroyConstant() {
187119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getType()->getContext().pImpl->ExprConstants.remove(this);
1872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstantImpl();
1873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char *ConstantExpr::getOpcodeName() const {
1876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Instruction::getOpcodeName(getOpcode());
1877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGetElementPtrConstantExpr::
1882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
188319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          Type *DestTy)
1884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : ConstantExpr(DestTy, Instruction::GetElementPtr,
1885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 OperandTraits<GetElementPtrConstantExpr>::op_end(this)
1886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                 - (IdxList.size()+1), IdxList.size()+1) {
1887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OperandList[0] = C;
1888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
1889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandList[i+1] = IdxList[i];
1890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                replaceUsesOfWithOnConstant implementations
1895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// replaceUsesOfWithOnConstant - Update this constant array to change uses of
1897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 'From' to be uses of 'To'.  This must update the uniquing data structures
1898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// etc.
1899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Note that we intentionally replace all uses of From with To here.  Consider
1901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// a large array that uses 'From' 1000 times.  By handling this case all here,
1902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ConstantArray::replaceUsesOfWithOnConstant is only invoked once, and that
1903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// single invocation handles all 1000 uses.  Handling them one at a time would
1904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// work, but would be really slow because it would have to unique each updated
1905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// array instance.
1906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
1908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                Use *U) {
1909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *ToC = cast<Constant>(To);
1911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
191219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = getType()->getContext().pImpl;
1913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::pair<LLVMContextImpl::ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
191519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Lookup.first.first = cast<ArrayType>(getType());
1916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Lookup.second = this;
1917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> &Values = Lookup.first.second;
1919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Values.reserve(getNumOperands());  // Build replacement array.
1920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fill values with the modified operands of the constant array.  Also,
1922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // compute whether this turns into an all-zeros array.
1923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isAllZeros = false;
1924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumUpdated = 0;
1925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!ToC->isNullValue()) {
1926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Constant *Val = cast<Constant>(O->get());
1928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Val == From) {
1929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Val = ToC;
1930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ++NumUpdated;
1931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Values.push_back(Val);
1933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isAllZeros = true;
1936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Constant *Val = cast<Constant>(O->get());
1938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Val == From) {
1939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Val = ToC;
1940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ++NumUpdated;
1941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Values.push_back(Val);
1943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (isAllZeros) isAllZeros = Val->isNullValue();
1944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Replacement = 0;
1948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isAllZeros) {
194919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Replacement = ConstantAggregateZero::get(getType());
1950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check to see if we have this array type already.
1952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Exists;
1953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
1954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
1955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Exists) {
1957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Replacement = I->second;
1958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Okay, the new shape doesn't exist in the system yet.  Instead of
1960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // creating a new constant array, inserting it, replaceallusesof'ing the
1961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // old with the new, then deleting the old... just update the current one
1962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // in place!
1963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
1964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Update to the new value.  Optimize for the case when we have a single
1966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // operand that we're changing, but handle bulk updates efficiently.
1967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NumUpdated == 1) {
1968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned OperandToUpdate = U - OperandList;
1969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        assert(getOperand(OperandToUpdate) == From &&
1970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               "ReplaceAllUsesWith broken!");
1971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        setOperand(OperandToUpdate, ToC);
1972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
1973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (getOperand(i) == From)
1975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            setOperand(i, ToC);
1976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
1978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, I do need to replace this with an existing value.
1982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Replacement != this && "I didn't contain From!");
1983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everyone using this now uses the replacement.
198519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  replaceAllUsesWith(Replacement);
1986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Delete the old constant!
1988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstant();
1989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
1992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Use *U) {
1993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *ToC = cast<Constant>(To);
1995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned OperandToUpdate = U-OperandList;
1997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::pair<LLVMContextImpl::StructConstantsTy::MapKey, ConstantStruct*> Lookup;
200019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Lookup.first.first = cast<StructType>(getType());
2001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Lookup.second = this;
2002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> &Values = Lookup.first.second;
2003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Values.reserve(getNumOperands());  // Build replacement struct.
2004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fill values with the modified operands of the constant struct.  Also,
2007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // compute whether this turns into an all-zeros struct.
2008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isAllZeros = false;
2009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!ToC->isNullValue()) {
2010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Use *O = OperandList, *E = OperandList + getNumOperands(); O != E; ++O)
2011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Values.push_back(cast<Constant>(O->get()));
2012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
2013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isAllZeros = true;
2014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
2015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Constant *Val = cast<Constant>(O->get());
2016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Values.push_back(Val);
2017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (isAllZeros) isAllZeros = Val->isNullValue();
2018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Values[OperandToUpdate] = ToC;
2021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
202219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LLVMContextImpl *pImpl = getContext().pImpl;
2023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Replacement = 0;
2025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isAllZeros) {
202619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Replacement = ConstantAggregateZero::get(getType());
2027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
2028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check to see if we have this struct type already.
2029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Exists;
2030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LLVMContextImpl::StructConstantsTy::MapTy::iterator I =
2031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pImpl->StructConstants.InsertOrGetItem(Lookup, Exists);
2032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Exists) {
2034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Replacement = I->second;
2035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
2036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Okay, the new shape doesn't exist in the system yet.  Instead of
2037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // creating a new constant struct, inserting it, replaceallusesof'ing the
2038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // old with the new, then deleting the old... just update the current one
2039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // in place!
2040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      pImpl->StructConstants.MoveConstantToNewSlot(this, I);
2041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Update to the new value.
2043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      setOperand(OperandToUpdate, ToC);
2044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
2045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Replacement != this && "I didn't contain From!");
2049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everyone using this now uses the replacement.
205119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  replaceAllUsesWith(Replacement);
2052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Delete the old constant!
2054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstant();
2055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantVector::replaceUsesOfWithOnConstant(Value *From, Value *To,
2058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 Use *U) {
2059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
2060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<Constant*> Values;
2062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Values.reserve(getNumOperands());  // Build replacement array...
2063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *Val = getOperand(i);
2065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val == From) Val = cast<Constant>(To);
2066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Values.push_back(Val);
2067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
206919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Constant *Replacement = get(Values);
2070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Replacement != this && "I didn't contain From!");
2071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everyone using this now uses the replacement.
207319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  replaceAllUsesWith(Replacement);
2074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Delete the old constant!
2076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstant();
2077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
2080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               Use *U) {
2081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
2082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *To = cast<Constant>(ToV);
2083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constant *Replacement = 0;
2085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (getOpcode() == Instruction::GetElementPtr) {
2086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<Constant*, 8> Indices;
2087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *Pointer = getOperand(0);
2088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Indices.reserve(getNumOperands()-1);
2089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Pointer == From) Pointer = To;
2090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
2092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Constant *Val = getOperand(i);
2093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Val == From) Val = To;
2094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Indices.push_back(Val);
2095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
209619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices,
209719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         cast<GEPOperator>(this)->isInBounds());
2098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getOpcode() == Instruction::ExtractValue) {
2099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *Agg = getOperand(0);
2100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Agg == From) Agg = To;
2101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
210219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ArrayRef<unsigned> Indices = getIndices();
210319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Replacement = ConstantExpr::getExtractValue(Agg, Indices);
2104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getOpcode() == Instruction::InsertValue) {
2105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *Agg = getOperand(0);
2106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *Val = getOperand(1);
2107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Agg == From) Agg = To;
2108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Val == From) Val = To;
2109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
211019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ArrayRef<unsigned> Indices = getIndices();
211119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Replacement = ConstantExpr::getInsertValue(Agg, Val, Indices);
2112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isCast()) {
2113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(getOperand(0) == From && "Cast only has one use!");
211419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Replacement = ConstantExpr::getCast(getOpcode(), To, getType());
2115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getOpcode() == Instruction::Select) {
2116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C1 = getOperand(0);
2117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C2 = getOperand(1);
2118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C3 = getOperand(2);
2119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C1 == From) C1 = To;
2120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C2 == From) C2 = To;
2121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C3 == From) C3 = To;
2122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Replacement = ConstantExpr::getSelect(C1, C2, C3);
2123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getOpcode() == Instruction::ExtractElement) {
2124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C1 = getOperand(0);
2125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C2 = getOperand(1);
2126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C1 == From) C1 = To;
2127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C2 == From) C2 = To;
2128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Replacement = ConstantExpr::getExtractElement(C1, C2);
2129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getOpcode() == Instruction::InsertElement) {
2130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C1 = getOperand(0);
2131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C2 = getOperand(1);
2132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C3 = getOperand(1);
2133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C1 == From) C1 = To;
2134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C2 == From) C2 = To;
2135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C3 == From) C3 = To;
2136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
2137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getOpcode() == Instruction::ShuffleVector) {
2138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C1 = getOperand(0);
2139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C2 = getOperand(1);
2140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C3 = getOperand(2);
2141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C1 == From) C1 = To;
2142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C2 == From) C2 = To;
2143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C3 == From) C3 = To;
2144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
2145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isCompare()) {
2146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C1 = getOperand(0);
2147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C2 = getOperand(1);
2148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C1 == From) C1 = To;
2149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C2 == From) C2 = To;
2150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (getOpcode() == Instruction::ICmp)
2151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Replacement = ConstantExpr::getICmp(getPredicate(), C1, C2);
2152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else {
2153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(getOpcode() == Instruction::FCmp);
2154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Replacement = ConstantExpr::getFCmp(getPredicate(), C1, C2);
2155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
2156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (getNumOperands() == 2) {
2157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C1 = getOperand(0);
2158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *C2 = getOperand(1);
2159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C1 == From) C1 = To;
2160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (C2 == From) C2 = To;
2161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Replacement = ConstantExpr::get(getOpcode(), C1, C2, SubclassOptionalData);
2162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
2163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Unknown ConstantExpr type!");
2164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
2165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Replacement != this && "I didn't contain From!");
2168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everyone using this now uses the replacement.
217019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  replaceAllUsesWith(Replacement);
2171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Delete the old constant!
2173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  destroyConstant();
2174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2175