blob: 05b444573ebf171880a834ded672f227de9ec3b8 [file] [log] [blame]
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001//===-- Constants.cpp - Implement Constant nodes --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3462ae32001-12-03 22:26:30 +000010// This file implements the Constant* classes...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner5a945e32004-01-12 21:13:12 +000015#include "ConstantFolding.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/DerivedTypes.h"
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000017#include "llvm/GlobalValue.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerd7a73302001-10-13 06:57:33 +000020#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/StringExtras.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000023#include "llvm/Support/Compiler.h"
Chris Lattner69edc982006-09-28 00:35:06 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include <algorithm>
Reid Spencercf394bf2004-07-04 11:51:24 +000026#include <iostream>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner2f7c9632001-06-06 20:29:01 +000029//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +000030// Constant Class
Chris Lattner2f7c9632001-06-06 20:29:01 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner3462ae32001-12-03 22:26:30 +000033void Constant::destroyConstantImpl() {
34 // When a Constant is destroyed, there may be lingering
Chris Lattnerd7a73302001-10-13 06:57:33 +000035 // references to the constant by other constants in the constant pool. These
Misha Brukmanbe372b92003-08-21 22:14:26 +000036 // constants are implicitly dependent on the module that is being deleted,
Chris Lattnerd7a73302001-10-13 06:57:33 +000037 // but they don't know that. Because we only find out when the CPV is
38 // deleted, we must now notify all of our users (that should only be
Chris Lattner3462ae32001-12-03 22:26:30 +000039 // Constants) that they are, in fact, invalid now and should be deleted.
Chris Lattnerd7a73302001-10-13 06:57:33 +000040 //
41 while (!use_empty()) {
42 Value *V = use_back();
43#ifndef NDEBUG // Only in -g mode...
Chris Lattnerd9f4ac662002-07-18 00:14:50 +000044 if (!isa<Constant>(V))
45 std::cerr << "While deleting: " << *this
46 << "\n\nUse still stuck around after Def is destroyed: "
47 << *V << "\n\n";
Chris Lattnerd7a73302001-10-13 06:57:33 +000048#endif
Vikram S. Adve4e537b22002-07-14 23:13:17 +000049 assert(isa<Constant>(V) && "References remain to Constant being destroyed");
Reid Spencer1ebe1ab2004-07-17 23:48:33 +000050 Constant *CV = cast<Constant>(V);
51 CV->destroyConstant();
Chris Lattnerd7a73302001-10-13 06:57:33 +000052
53 // The constant should remove itself from our use list...
Vikram S. Adve4e537b22002-07-14 23:13:17 +000054 assert((use_empty() || use_back() != V) && "Constant not removed!");
Chris Lattnerd7a73302001-10-13 06:57:33 +000055 }
56
57 // Value has no outstanding references it is safe to delete it now...
58 delete this;
Chris Lattner38569342001-10-01 20:11:19 +000059}
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattner23dd1f62006-10-20 00:27:06 +000061/// canTrap - Return true if evaluation of this constant could trap. This is
62/// true for things like constant expressions that could divide by zero.
63bool Constant::canTrap() const {
64 assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
65 // The only thing that could possibly trap are constant exprs.
66 const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
67 if (!CE) return false;
68
69 // ConstantExpr traps if any operands can trap.
70 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
71 if (getOperand(i)->canTrap())
72 return true;
73
74 // Otherwise, only specific operations can trap.
75 switch (CE->getOpcode()) {
76 default:
77 return false;
78 case Instruction::Div:
79 case Instruction::Rem:
80 // Div and rem can trap if the RHS is not known to be non-zero.
81 if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
82 return true;
83 return false;
84 }
85}
86
87
Chris Lattnerb1585a92002-08-13 17:50:20 +000088// Static constructor to create a '0' constant of arbitrary type...
89Constant *Constant::getNullValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +000090 switch (Ty->getTypeID()) {
Chris Lattner3e88ef92003-10-03 19:34:51 +000091 case Type::BoolTyID: {
92 static Constant *NullBool = ConstantBool::get(false);
93 return NullBool;
94 }
95 case Type::SByteTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +000096 static Constant *NullSByte = ConstantInt::get(Type::SByteTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +000097 return NullSByte;
98 }
99 case Type::UByteTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000100 static Constant *NullUByte = ConstantInt::get(Type::UByteTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000101 return NullUByte;
102 }
103 case Type::ShortTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000104 static Constant *NullShort = ConstantInt::get(Type::ShortTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000105 return NullShort;
106 }
107 case Type::UShortTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000108 static Constant *NullUShort = ConstantInt::get(Type::UShortTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000109 return NullUShort;
110 }
111 case Type::IntTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000112 static Constant *NullInt = ConstantInt::get(Type::IntTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000113 return NullInt;
114 }
115 case Type::UIntTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000116 static Constant *NullUInt = ConstantInt::get(Type::UIntTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000117 return NullUInt;
118 }
119 case Type::LongTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000120 static Constant *NullLong = ConstantInt::get(Type::LongTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000121 return NullLong;
122 }
123 case Type::ULongTyID: {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000124 static Constant *NullULong = ConstantInt::get(Type::ULongTy, 0);
Chris Lattner3e88ef92003-10-03 19:34:51 +0000125 return NullULong;
126 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000127
Chris Lattner3e88ef92003-10-03 19:34:51 +0000128 case Type::FloatTyID: {
129 static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
130 return NullFloat;
131 }
132 case Type::DoubleTyID: {
133 static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
134 return NullDouble;
135 }
Chris Lattnerb1585a92002-08-13 17:50:20 +0000136
Misha Brukmanb1c93172005-04-21 23:48:37 +0000137 case Type::PointerTyID:
Chris Lattnerb1585a92002-08-13 17:50:20 +0000138 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner3e88ef92003-10-03 19:34:51 +0000139
Chris Lattner9fba3da2004-02-15 05:53:04 +0000140 case Type::StructTyID:
141 case Type::ArrayTyID:
Brian Gaeke02209042004-08-20 06:00:58 +0000142 case Type::PackedTyID:
Chris Lattner9fba3da2004-02-15 05:53:04 +0000143 return ConstantAggregateZero::get(Ty);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000144 default:
Reid Spencercf394bf2004-07-04 11:51:24 +0000145 // Function, Label, or Opaque type?
146 assert(!"Cannot create a null constant of that type!");
Chris Lattnerb1585a92002-08-13 17:50:20 +0000147 return 0;
148 }
149}
150
151// Static constructor to create the maximum constant of an integral type...
152ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000153 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000154 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000155 case Type::SByteTyID:
156 case Type::ShortTyID:
157 case Type::IntTyID:
158 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000159 // Calculate 011111111111111...
Chris Lattnerb1585a92002-08-13 17:50:20 +0000160 unsigned TypeBits = Ty->getPrimitiveSize()*8;
161 int64_t Val = INT64_MAX; // All ones
162 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Reid Spencere0fc4df2006-10-20 07:07:24 +0000163 return ConstantInt::get(Ty, Val);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000164 }
165
166 case Type::UByteTyID:
167 case Type::UShortTyID:
168 case Type::UIntTyID:
169 case Type::ULongTyID: return getAllOnesValue(Ty);
170
Chris Lattner31408f72002-08-14 17:12:13 +0000171 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000172 }
173}
174
175// Static constructor to create the minimum constant for an integral type...
176ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000177 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000178 case Type::BoolTyID: return ConstantBool::getFalse();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000179 case Type::SByteTyID:
180 case Type::ShortTyID:
181 case Type::IntTyID:
182 case Type::LongTyID: {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000183 // Calculate 1111111111000000000000
Chris Lattnerb1585a92002-08-13 17:50:20 +0000184 unsigned TypeBits = Ty->getPrimitiveSize()*8;
185 int64_t Val = -1; // All ones
186 Val <<= TypeBits-1; // Shift over to the right spot
Reid Spencere0fc4df2006-10-20 07:07:24 +0000187 return ConstantInt::get(Ty, Val);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000188 }
189
190 case Type::UByteTyID:
191 case Type::UShortTyID:
192 case Type::UIntTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000193 case Type::ULongTyID: return ConstantInt::get(Ty, 0);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000194
Chris Lattner31408f72002-08-14 17:12:13 +0000195 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000196 }
197}
198
199// Static constructor to create an integral constant with all bits set
200ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
Chris Lattner6b727592004-06-17 18:19:28 +0000201 switch (Ty->getTypeID()) {
Chris Lattnera84df0a22006-09-28 23:36:21 +0000202 case Type::BoolTyID: return ConstantBool::getTrue();
Chris Lattnerb1585a92002-08-13 17:50:20 +0000203 case Type::SByteTyID:
204 case Type::ShortTyID:
205 case Type::IntTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000206 case Type::LongTyID: return ConstantInt::get(Ty, -1);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000207
208 case Type::UByteTyID:
209 case Type::UShortTyID:
210 case Type::UIntTyID:
211 case Type::ULongTyID: {
212 // Calculate ~0 of the right type...
213 unsigned TypeBits = Ty->getPrimitiveSize()*8;
214 uint64_t Val = ~0ULL; // All ones
215 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Reid Spencere0fc4df2006-10-20 07:07:24 +0000216 return ConstantInt::get(Ty, Val);
Chris Lattnerb1585a92002-08-13 17:50:20 +0000217 }
Chris Lattner31408f72002-08-14 17:12:13 +0000218 default: return 0;
Chris Lattnerb1585a92002-08-13 17:50:20 +0000219 }
220}
221
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222//===----------------------------------------------------------------------===//
Chris Lattner3462ae32001-12-03 22:26:30 +0000223// ConstantXXX Classes
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224//===----------------------------------------------------------------------===//
225
226//===----------------------------------------------------------------------===//
227// Normal Constructors
228
Chris Lattnere7e139e2005-09-27 06:09:08 +0000229ConstantIntegral::ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000230 : Constant(Ty, VT, 0, 0), Val(V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231}
Chris Lattner49d855c2001-09-07 16:46:31 +0000232
Chris Lattnere7e139e2005-09-27 06:09:08 +0000233ConstantBool::ConstantBool(bool V)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000234 : ConstantIntegral(Type::BoolTy, ConstantBoolVal, uint64_t(V)) {
Chris Lattner265eb642004-06-21 12:12:12 +0000235}
236
Reid Spencere0fc4df2006-10-20 07:07:24 +0000237ConstantInt::ConstantInt(const Type *Ty, uint64_t V)
238 : ConstantIntegral(Ty, ConstantIntVal, V) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239}
240
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000241ConstantFP::ConstantFP(const Type *Ty, double V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000242 : Constant(Ty, ConstantFPVal, 0, 0) {
Chris Lattner9655e542001-07-20 19:16:02 +0000243 assert(isValueValidForType(Ty, V) && "Value too large for type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244 Val = V;
245}
246
Chris Lattner3462ae32001-12-03 22:26:30 +0000247ConstantArray::ConstantArray(const ArrayType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000248 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000249 : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) {
Alkis Evlogimenos0507ffe2004-09-15 02:32:15 +0000250 assert(V.size() == T->getNumElements() &&
251 "Invalid initializer vector for constant array");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000252 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000253 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
254 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000255 Constant *C = *I;
256 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000257 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000258 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000259 "Initializer for array element doesn't match array element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000260 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261 }
262}
263
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000264ConstantArray::~ConstantArray() {
265 delete [] OperandList;
266}
267
Chris Lattner3462ae32001-12-03 22:26:30 +0000268ConstantStruct::ConstantStruct(const StructType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000269 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000270 : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) {
Chris Lattnerac6db752004-02-09 04:37:31 +0000271 assert(V.size() == T->getNumElements() &&
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000272 "Invalid initializer vector for constant structure");
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000273 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000274 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
275 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000276 Constant *C = *I;
277 assert((C->getType() == T->getElementType(I-V.begin()) ||
Chris Lattner0144fad2005-10-03 21:56:24 +0000278 ((T->getElementType(I-V.begin())->isAbstract() ||
Chris Lattner20a24452005-10-07 05:23:36 +0000279 C->getType()->isAbstract()) &&
Chris Lattner0144fad2005-10-03 21:56:24 +0000280 T->getElementType(I-V.begin())->getTypeID() ==
Chris Lattner20a24452005-10-07 05:23:36 +0000281 C->getType()->getTypeID())) &&
Chris Lattner93c8f142003-06-02 17:42:47 +0000282 "Initializer for struct element doesn't match struct element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000283 OL->init(C, this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284 }
285}
286
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000287ConstantStruct::~ConstantStruct() {
288 delete [] OperandList;
289}
290
291
Brian Gaeke02209042004-08-20 06:00:58 +0000292ConstantPacked::ConstantPacked(const PackedType *T,
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000293 const std::vector<Constant*> &V)
Chris Lattnere7e139e2005-09-27 06:09:08 +0000294 : Constant(T, ConstantPackedVal, new Use[V.size()], V.size()) {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000295 Use *OL = OperandList;
Chris Lattner0144fad2005-10-03 21:56:24 +0000296 for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end();
297 I != E; ++I, ++OL) {
Chris Lattner20a24452005-10-07 05:23:36 +0000298 Constant *C = *I;
299 assert((C->getType() == T->getElementType() ||
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000300 (T->isAbstract() &&
Chris Lattner20a24452005-10-07 05:23:36 +0000301 C->getType()->getTypeID() == T->getElementType()->getTypeID())) &&
Alkis Evlogimenoscb031d92004-09-10 04:16:59 +0000302 "Initializer for packed element doesn't match packed element type!");
Chris Lattner20a24452005-10-07 05:23:36 +0000303 OL->init(C, this);
Brian Gaeke02209042004-08-20 06:00:58 +0000304 }
305}
306
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000307ConstantPacked::~ConstantPacked() {
308 delete [] OperandList;
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000309}
310
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000311/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
312/// behind the scenes to implement unary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000313namespace {
314class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000315 Use Op;
316public:
317 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
318 : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {}
319};
Chris Lattner02157b02006-06-28 21:38:54 +0000320}
Chris Lattner6e415c02004-03-12 05:54:04 +0000321
Chris Lattner22ced562003-06-22 20:48:30 +0000322static bool isSetCC(unsigned Opcode) {
323 return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
324 Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
325 Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
326}
327
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000328/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
329/// behind the scenes to implement binary constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000330namespace {
331class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000332 Use Ops[2];
333public:
334 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
335 : ConstantExpr(isSetCC(Opcode) ? Type::BoolTy : C1->getType(),
336 Opcode, Ops, 2) {
337 Ops[0].init(C1, this);
338 Ops[1].init(C2, this);
339 }
340};
Chris Lattner02157b02006-06-28 21:38:54 +0000341}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000342
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000343/// SelectConstantExpr - This class is private to Constants.cpp, and is used
344/// behind the scenes to implement select constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000345namespace {
346class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000347 Use Ops[3];
348public:
349 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
350 : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) {
351 Ops[0].init(C1, this);
352 Ops[1].init(C2, this);
353 Ops[2].init(C3, this);
354 }
355};
Chris Lattner02157b02006-06-28 21:38:54 +0000356}
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000357
Robert Bocchinoca27f032006-01-17 20:07:22 +0000358/// ExtractElementConstantExpr - This class is private to
359/// Constants.cpp, and is used behind the scenes to implement
360/// extractelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000361namespace {
362class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr {
Robert Bocchino23004482006-01-10 19:05:34 +0000363 Use Ops[2];
364public:
365 ExtractElementConstantExpr(Constant *C1, Constant *C2)
366 : ConstantExpr(cast<PackedType>(C1->getType())->getElementType(),
367 Instruction::ExtractElement, Ops, 2) {
368 Ops[0].init(C1, this);
369 Ops[1].init(C2, this);
370 }
371};
Chris Lattner02157b02006-06-28 21:38:54 +0000372}
Robert Bocchino23004482006-01-10 19:05:34 +0000373
Robert Bocchinoca27f032006-01-17 20:07:22 +0000374/// InsertElementConstantExpr - This class is private to
375/// Constants.cpp, and is used behind the scenes to implement
376/// insertelement constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000377namespace {
378class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000379 Use Ops[3];
380public:
381 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
382 : ConstantExpr(C1->getType(), Instruction::InsertElement,
383 Ops, 3) {
384 Ops[0].init(C1, this);
385 Ops[1].init(C2, this);
386 Ops[2].init(C3, this);
387 }
388};
Chris Lattner02157b02006-06-28 21:38:54 +0000389}
Robert Bocchinoca27f032006-01-17 20:07:22 +0000390
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000391/// ShuffleVectorConstantExpr - This class is private to
392/// Constants.cpp, and is used behind the scenes to implement
393/// shufflevector constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000394namespace {
395class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr {
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000396 Use Ops[3];
397public:
398 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
399 : ConstantExpr(C1->getType(), Instruction::ShuffleVector,
400 Ops, 3) {
401 Ops[0].init(C1, this);
402 Ops[1].init(C2, this);
403 Ops[2].init(C3, this);
404 }
405};
Chris Lattner02157b02006-06-28 21:38:54 +0000406}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000407
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000408/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
409/// used behind the scenes to implement getelementpr constant exprs.
Chris Lattner02157b02006-06-28 21:38:54 +0000410namespace {
411struct VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr {
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000412 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
413 const Type *DestTy)
414 : ConstantExpr(DestTy, Instruction::GetElementPtr,
415 new Use[IdxList.size()+1], IdxList.size()+1) {
416 OperandList[0].init(C, this);
417 for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
418 OperandList[i+1].init(IdxList[i], this);
419 }
420 ~GetElementPtrConstantExpr() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000421 delete [] OperandList;
Chris Lattnerd0df99c2005-01-29 00:34:39 +0000422 }
423};
Chris Lattner02157b02006-06-28 21:38:54 +0000424}
Vikram S. Adve4e537b22002-07-14 23:13:17 +0000425
Chris Lattner817175f2004-03-29 02:37:53 +0000426/// ConstantExpr::get* - Return some common constants without having to
427/// specify the full Instruction::OPCODE identifier.
428///
429Constant *ConstantExpr::getNeg(Constant *C) {
Chris Lattner3cdc27c2004-03-29 19:51:24 +0000430 if (!C->getType()->isFloatingPoint())
431 return get(Instruction::Sub, getNullValue(C->getType()), C);
432 else
433 return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
Chris Lattner817175f2004-03-29 02:37:53 +0000434}
435Constant *ConstantExpr::getNot(Constant *C) {
436 assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
437 return get(Instruction::Xor, C,
438 ConstantIntegral::getAllOnesValue(C->getType()));
439}
440Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
441 return get(Instruction::Add, C1, C2);
442}
443Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
444 return get(Instruction::Sub, C1, C2);
445}
446Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
447 return get(Instruction::Mul, C1, C2);
448}
449Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
450 return get(Instruction::Div, C1, C2);
451}
452Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
453 return get(Instruction::Rem, C1, C2);
454}
455Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
456 return get(Instruction::And, C1, C2);
457}
458Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
459 return get(Instruction::Or, C1, C2);
460}
461Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
462 return get(Instruction::Xor, C1, C2);
463}
464Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
465 return get(Instruction::SetEQ, C1, C2);
466}
467Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
468 return get(Instruction::SetNE, C1, C2);
469}
470Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
471 return get(Instruction::SetLT, C1, C2);
472}
473Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
474 return get(Instruction::SetGT, C1, C2);
475}
476Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
477 return get(Instruction::SetLE, C1, C2);
478}
479Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
480 return get(Instruction::SetGE, C1, C2);
481}
482Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
483 return get(Instruction::Shl, C1, C2);
484}
485Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
486 return get(Instruction::Shr, C1, C2);
487}
488
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000489Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
490 if (C1->getType()->isUnsigned()) return getShr(C1, C2);
491 return getCast(getShr(getCast(C1,
492 C1->getType()->getUnsignedVersion()), C2), C1->getType());
493}
Chris Lattner817175f2004-03-29 02:37:53 +0000494
Chris Lattnerdb8bdba2004-05-25 05:32:43 +0000495Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
496 if (C1->getType()->isSigned()) return getShr(C1, C2);
497 return getCast(getShr(getCast(C1,
498 C1->getType()->getSignedVersion()), C2), C1->getType());
499}
Chris Lattner60e0dd72001-10-03 06:12:09 +0000500
Chris Lattner7c1018a2006-07-14 19:37:40 +0000501/// getWithOperandReplaced - Return a constant expression identical to this
502/// one, but with the specified operand set to the specified value.
503Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo,
504 Constant *Op) const {
505 assert(OpNo < getNumOperands() && "Operand num is out of range!");
506 assert(Op->getType() == getOperand(OpNo)->getType() &&
507 "Replacing operand with value of different type!");
Chris Lattner227816342006-07-14 22:20:01 +0000508 if (getOperand(OpNo) == Op)
509 return const_cast<ConstantExpr*>(this);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000510
Chris Lattner227816342006-07-14 22:20:01 +0000511 Constant *Op0, *Op1, *Op2;
Chris Lattner7c1018a2006-07-14 19:37:40 +0000512 switch (getOpcode()) {
513 case Instruction::Cast:
514 return ConstantExpr::getCast(Op, getType());
Chris Lattner227816342006-07-14 22:20:01 +0000515 case Instruction::Select:
516 Op0 = (OpNo == 0) ? Op : getOperand(0);
517 Op1 = (OpNo == 1) ? Op : getOperand(1);
518 Op2 = (OpNo == 2) ? Op : getOperand(2);
519 return ConstantExpr::getSelect(Op0, Op1, Op2);
520 case Instruction::InsertElement:
521 Op0 = (OpNo == 0) ? Op : getOperand(0);
522 Op1 = (OpNo == 1) ? Op : getOperand(1);
523 Op2 = (OpNo == 2) ? Op : getOperand(2);
524 return ConstantExpr::getInsertElement(Op0, Op1, Op2);
525 case Instruction::ExtractElement:
526 Op0 = (OpNo == 0) ? Op : getOperand(0);
527 Op1 = (OpNo == 1) ? Op : getOperand(1);
528 return ConstantExpr::getExtractElement(Op0, Op1);
529 case Instruction::ShuffleVector:
530 Op0 = (OpNo == 0) ? Op : getOperand(0);
531 Op1 = (OpNo == 1) ? Op : getOperand(1);
532 Op2 = (OpNo == 2) ? Op : getOperand(2);
533 return ConstantExpr::getShuffleVector(Op0, Op1, Op2);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000534 case Instruction::GetElementPtr: {
535 std::vector<Constant*> Ops;
536 for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
537 Ops.push_back(getOperand(i));
538 if (OpNo == 0)
539 return ConstantExpr::getGetElementPtr(Op, Ops);
540 Ops[OpNo-1] = Op;
541 return ConstantExpr::getGetElementPtr(getOperand(0), Ops);
542 }
Chris Lattner7c1018a2006-07-14 19:37:40 +0000543 default:
544 assert(getNumOperands() == 2 && "Must be binary operator?");
Chris Lattner227816342006-07-14 22:20:01 +0000545 Op0 = (OpNo == 0) ? Op : getOperand(0);
546 Op1 = (OpNo == 1) ? Op : getOperand(1);
547 return ConstantExpr::get(getOpcode(), Op0, Op1);
548 }
549}
550
551/// getWithOperands - This returns the current constant expression with the
552/// operands replaced with the specified values. The specified operands must
553/// match count and type with the existing ones.
554Constant *ConstantExpr::
555getWithOperands(const std::vector<Constant*> &Ops) const {
556 assert(Ops.size() == getNumOperands() && "Operand count mismatch!");
557 bool AnyChange = false;
558 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
559 assert(Ops[i]->getType() == getOperand(i)->getType() &&
560 "Operand type mismatch!");
561 AnyChange |= Ops[i] != getOperand(i);
562 }
563 if (!AnyChange) // No operands changed, return self.
564 return const_cast<ConstantExpr*>(this);
565
566 switch (getOpcode()) {
567 case Instruction::Cast:
568 return ConstantExpr::getCast(Ops[0], getType());
569 case Instruction::Select:
570 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
571 case Instruction::InsertElement:
572 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
573 case Instruction::ExtractElement:
574 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
575 case Instruction::ShuffleVector:
576 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
577 case Instruction::GetElementPtr: {
578 std::vector<Constant*> ActualOps(Ops.begin()+1, Ops.end());
579 return ConstantExpr::getGetElementPtr(Ops[0], ActualOps);
580 }
581 default:
582 assert(getNumOperands() == 2 && "Must be binary operator?");
583 return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]);
Chris Lattner7c1018a2006-07-14 19:37:40 +0000584 }
585}
586
Chris Lattner2f7c9632001-06-06 20:29:01 +0000587
588//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589// isValueValidForType implementations
590
Reid Spencere0fc4df2006-10-20 07:07:24 +0000591bool ConstantInt::isValueValidForType(const Type *Ty, int64_t Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000592 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593 default:
594 return false; // These can't be represented as integers!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595 // Signed types...
596 case Type::SByteTyID:
597 return (Val <= INT8_MAX && Val >= INT8_MIN);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000598 case Type::UByteTyID:
599 return (Val >= 0) && (Val <= UINT8_MAX);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600 case Type::ShortTyID:
601 return (Val <= INT16_MAX && Val >= INT16_MIN);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000602 case Type::UShortTyID:
603 return (Val >= 0) && (Val <= UINT16_MAX);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000604 case Type::IntTyID:
Chris Lattner74248512004-06-08 23:21:39 +0000605 return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000606 case Type::UIntTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000607 return (Val >= 0) && (Val <= UINT32_MAX);
608 case Type::LongTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000609 case Type::ULongTyID:
Reid Spencere0fc4df2006-10-20 07:07:24 +0000610 return true; // always true, has to fit in largest type
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000612}
613
Chris Lattner3462ae32001-12-03 22:26:30 +0000614bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
Chris Lattner6b727592004-06-17 18:19:28 +0000615 switch (Ty->getTypeID()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000616 default:
617 return false; // These can't be represented as floating point!
618
Reid Spencerb95f8ab2004-12-07 07:38:08 +0000619 // TODO: Figure out how to test if a double can be cast to a float!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000620 case Type::FloatTyID:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000621 case Type::DoubleTyID:
622 return true; // This is the largest type...
623 }
Chris Lattneraa2372562006-05-24 17:04:05 +0000624}
Chris Lattner9655e542001-07-20 19:16:02 +0000625
Chris Lattner49d855c2001-09-07 16:46:31 +0000626//===----------------------------------------------------------------------===//
Chris Lattner49d855c2001-09-07 16:46:31 +0000627// Factory Function Implementation
628
Chris Lattner98fa07b2003-05-23 20:03:32 +0000629// ConstantCreator - A class that is used to create constants by
630// ValueMap*. This class should be partially specialized if there is
631// something strange that needs to be done to interface to the ctor for the
632// constant.
633//
Chris Lattner189d19f2003-11-21 20:23:48 +0000634namespace llvm {
635 template<class ConstantClass, class TypeClass, class ValType>
Chris Lattner02157b02006-06-28 21:38:54 +0000636 struct VISIBILITY_HIDDEN ConstantCreator {
Chris Lattner189d19f2003-11-21 20:23:48 +0000637 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
638 return new ConstantClass(Ty, V);
639 }
640 };
Misha Brukmanb1c93172005-04-21 23:48:37 +0000641
Chris Lattner189d19f2003-11-21 20:23:48 +0000642 template<class ConstantClass, class TypeClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000643 struct VISIBILITY_HIDDEN ConvertConstantType {
Chris Lattner189d19f2003-11-21 20:23:48 +0000644 static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
645 assert(0 && "This type cannot be converted!\n");
646 abort();
647 }
648 };
Chris Lattnerb50d1352003-10-05 00:17:43 +0000649
Chris Lattner935aa922005-10-04 17:48:46 +0000650 template<class ValType, class TypeClass, class ConstantClass,
651 bool HasLargeKey = false /*true for arrays and structs*/ >
Chris Lattner02157b02006-06-28 21:38:54 +0000652 class VISIBILITY_HIDDEN ValueMap : public AbstractTypeUser {
Chris Lattnerb64419a2005-10-03 22:51:37 +0000653 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000654 typedef std::pair<const Type*, ValType> MapKey;
655 typedef std::map<MapKey, Constant *> MapTy;
656 typedef std::map<Constant*, typename MapTy::iterator> InverseMapTy;
657 typedef std::map<const Type*, typename MapTy::iterator> AbstractTypeMapTy;
Chris Lattnerb64419a2005-10-03 22:51:37 +0000658 private:
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000659 /// Map - This is the main map from the element descriptor to the Constants.
660 /// This is the primary way we avoid creating two of the same shape
661 /// constant.
Chris Lattnerb50d1352003-10-05 00:17:43 +0000662 MapTy Map;
Chris Lattner935aa922005-10-04 17:48:46 +0000663
664 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
665 /// from the constants to their element in Map. This is important for
666 /// removal of constants from the array, which would otherwise have to scan
667 /// through the map with very large keys.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000668 InverseMapTy InverseMap;
Chris Lattnerb50d1352003-10-05 00:17:43 +0000669
Jim Laskeyc03caef2006-07-17 17:38:29 +0000670 /// AbstractTypeMap - Map for abstract type constants.
671 ///
Chris Lattnerb50d1352003-10-05 00:17:43 +0000672 AbstractTypeMapTy AbstractTypeMap;
Chris Lattner99a669b2004-11-19 16:39:44 +0000673
Chris Lattner99a669b2004-11-19 16:39:44 +0000674 private:
675 void clear(std::vector<Constant *> &Constants) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000676 for(typename MapTy::iterator I = Map.begin(); I != Map.end(); ++I)
Chris Lattner99a669b2004-11-19 16:39:44 +0000677 Constants.push_back(I->second);
678 Map.clear();
679 AbstractTypeMap.clear();
Chris Lattner935aa922005-10-04 17:48:46 +0000680 InverseMap.clear();
Chris Lattner99a669b2004-11-19 16:39:44 +0000681 }
682
Chris Lattner98fa07b2003-05-23 20:03:32 +0000683 public:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000684 typename MapTy::iterator map_end() { return Map.end(); }
Chris Lattnerb64419a2005-10-03 22:51:37 +0000685
686 /// InsertOrGetItem - Return an iterator for the specified element.
687 /// If the element exists in the map, the returned iterator points to the
688 /// entry and Exists=true. If not, the iterator points to the newly
689 /// inserted entry and returns Exists=false. Newly inserted entries have
690 /// I->second == 0, and should be filled in.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000691 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, Constant *>
692 &InsertVal,
Chris Lattnerb64419a2005-10-03 22:51:37 +0000693 bool &Exists) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000694 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
Chris Lattnerb64419a2005-10-03 22:51:37 +0000695 Exists = !IP.second;
696 return IP.first;
697 }
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000698
Chris Lattner935aa922005-10-04 17:48:46 +0000699private:
Jim Laskeyc03caef2006-07-17 17:38:29 +0000700 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
Chris Lattner935aa922005-10-04 17:48:46 +0000701 if (HasLargeKey) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000702 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
Chris Lattner935aa922005-10-04 17:48:46 +0000703 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
704 IMI->second->second == CP &&
705 "InverseMap corrupt!");
706 return IMI->second;
707 }
708
Jim Laskeyc03caef2006-07-17 17:38:29 +0000709 typename MapTy::iterator I =
Chris Lattner935aa922005-10-04 17:48:46 +0000710 Map.find(MapKey((TypeClass*)CP->getRawType(), getValType(CP)));
Chris Lattner5bbf60a52005-10-04 16:52:46 +0000711 if (I == Map.end() || I->second != CP) {
712 // FIXME: This should not use a linear scan. If this gets to be a
713 // performance problem, someone should look at this.
714 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
715 /* empty */;
716 }
Chris Lattner935aa922005-10-04 17:48:46 +0000717 return I;
718 }
719public:
720
Chris Lattnerb64419a2005-10-03 22:51:37 +0000721 /// getOrCreate - Return the specified constant from the map, creating it if
722 /// necessary.
Chris Lattner98fa07b2003-05-23 20:03:32 +0000723 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Chris Lattnerb50d1352003-10-05 00:17:43 +0000724 MapKey Lookup(Ty, V);
Jim Laskeyc03caef2006-07-17 17:38:29 +0000725 typename MapTy::iterator I = Map.lower_bound(Lookup);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000726 // Is it in the map?
Chris Lattner98fa07b2003-05-23 20:03:32 +0000727 if (I != Map.end() && I->first == Lookup)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000728 return static_cast<ConstantClass *>(I->second);
Chris Lattner98fa07b2003-05-23 20:03:32 +0000729
730 // If no preexisting value, create one now...
731 ConstantClass *Result =
732 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
733
Chris Lattnerb50d1352003-10-05 00:17:43 +0000734 /// FIXME: why does this assert fail when loading 176.gcc?
735 //assert(Result->getType() == Ty && "Type specified is not correct!");
736 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
737
Chris Lattner935aa922005-10-04 17:48:46 +0000738 if (HasLargeKey) // Remember the reverse mapping if needed.
739 InverseMap.insert(std::make_pair(Result, I));
740
Chris Lattnerb50d1352003-10-05 00:17:43 +0000741 // If the type of the constant is abstract, make sure that an entry exists
742 // for it in the AbstractTypeMap.
743 if (Ty->isAbstract()) {
744 typename AbstractTypeMapTy::iterator TI =
745 AbstractTypeMap.lower_bound(Ty);
746
747 if (TI == AbstractTypeMap.end() || TI->first != Ty) {
748 // Add ourselves to the ATU list of the type.
749 cast<DerivedType>(Ty)->addAbstractTypeUser(this);
750
751 AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
752 }
753 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000754 return Result;
755 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000756
Chris Lattner98fa07b2003-05-23 20:03:32 +0000757 void remove(ConstantClass *CP) {
Jim Laskeyc03caef2006-07-17 17:38:29 +0000758 typename MapTy::iterator I = FindExistingElement(CP);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000759 assert(I != Map.end() && "Constant not found in constant table!");
Chris Lattner3e650af2004-08-04 04:48:01 +0000760 assert(I->second == CP && "Didn't find correct element?");
Chris Lattnerb50d1352003-10-05 00:17:43 +0000761
Chris Lattner935aa922005-10-04 17:48:46 +0000762 if (HasLargeKey) // Remember the reverse mapping if needed.
763 InverseMap.erase(CP);
764
Chris Lattnerb50d1352003-10-05 00:17:43 +0000765 // Now that we found the entry, make sure this isn't the entry that
766 // the AbstractTypeMap points to.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000767 const TypeClass *Ty = static_cast<const TypeClass *>(I->first.first);
Chris Lattnerb50d1352003-10-05 00:17:43 +0000768 if (Ty->isAbstract()) {
769 assert(AbstractTypeMap.count(Ty) &&
770 "Abstract type not in AbstractTypeMap?");
Jim Laskeyc03caef2006-07-17 17:38:29 +0000771 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
Chris Lattnerb50d1352003-10-05 00:17:43 +0000772 if (ATMEntryIt == I) {
773 // Yes, we are removing the representative entry for this type.
774 // See if there are any other entries of the same type.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000775 typename MapTy::iterator TmpIt = ATMEntryIt;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000776
Chris Lattnerb50d1352003-10-05 00:17:43 +0000777 // First check the entry before this one...
778 if (TmpIt != Map.begin()) {
779 --TmpIt;
780 if (TmpIt->first.first != Ty) // Not the same type, move back...
781 ++TmpIt;
782 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000783
Chris Lattnerb50d1352003-10-05 00:17:43 +0000784 // If we didn't find the same type, try to move forward...
785 if (TmpIt == ATMEntryIt) {
786 ++TmpIt;
787 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
788 --TmpIt; // No entry afterwards with the same type
789 }
790
791 // If there is another entry in the map of the same abstract type,
792 // update the AbstractTypeMap entry now.
793 if (TmpIt != ATMEntryIt) {
794 ATMEntryIt = TmpIt;
795 } else {
796 // Otherwise, we are removing the last instance of this type
797 // from the table. Remove from the ATM, and from user list.
798 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
799 AbstractTypeMap.erase(Ty);
800 }
Chris Lattner98fa07b2003-05-23 20:03:32 +0000801 }
Chris Lattnerb50d1352003-10-05 00:17:43 +0000802 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000803
Chris Lattnerb50d1352003-10-05 00:17:43 +0000804 Map.erase(I);
805 }
806
Chris Lattner3b793c62005-10-04 21:35:50 +0000807
808 /// MoveConstantToNewSlot - If we are about to change C to be the element
809 /// specified by I, update our internal data structures to reflect this
810 /// fact.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000811 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
Chris Lattner3b793c62005-10-04 21:35:50 +0000812 // First, remove the old location of the specified constant in the map.
Jim Laskeyc03caef2006-07-17 17:38:29 +0000813 typename MapTy::iterator OldI = FindExistingElement(C);
Chris Lattner3b793c62005-10-04 21:35:50 +0000814 assert(OldI != Map.end() && "Constant not found in constant table!");
815 assert(OldI->second == C && "Didn't find correct element?");
816
817 // If this constant is the representative element for its abstract type,
818 // update the AbstractTypeMap so that the representative element is I.
819 if (C->getType()->isAbstract()) {
820 typename AbstractTypeMapTy::iterator ATI =
821 AbstractTypeMap.find(C->getType());
822 assert(ATI != AbstractTypeMap.end() &&
823 "Abstract type not in AbstractTypeMap?");
824 if (ATI->second == OldI)
825 ATI->second = I;
826 }
827
828 // Remove the old entry from the map.
829 Map.erase(OldI);
830
831 // Update the inverse map so that we know that this constant is now
832 // located at descriptor I.
833 if (HasLargeKey) {
834 assert(I->second == C && "Bad inversemap entry!");
835 InverseMap[C] = I;
836 }
837 }
838
Chris Lattnerb50d1352003-10-05 00:17:43 +0000839 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000840 typename AbstractTypeMapTy::iterator I =
Jim Laskeyc03caef2006-07-17 17:38:29 +0000841 AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000842
843 assert(I != AbstractTypeMap.end() &&
844 "Abstract type not in AbstractTypeMap?");
845
846 // Convert a constant at a time until the last one is gone. The last one
847 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
848 // eliminated eventually.
849 do {
850 ConvertConstantType<ConstantClass,
Jim Laskeyc03caef2006-07-17 17:38:29 +0000851 TypeClass>::convert(
852 static_cast<ConstantClass *>(I->second->second),
Chris Lattnerb50d1352003-10-05 00:17:43 +0000853 cast<TypeClass>(NewTy));
854
Jim Laskeyc03caef2006-07-17 17:38:29 +0000855 I = AbstractTypeMap.find(cast<Type>(OldTy));
Chris Lattnerb50d1352003-10-05 00:17:43 +0000856 } while (I != AbstractTypeMap.end());
857 }
858
859 // If the type became concrete without being refined to any other existing
860 // type, we just remove ourselves from the ATU list.
861 void typeBecameConcrete(const DerivedType *AbsTy) {
862 AbsTy->removeAbstractTypeUser(this);
863 }
864
865 void dump() const {
866 std::cerr << "Constant.cpp: ValueMap\n";
Chris Lattner98fa07b2003-05-23 20:03:32 +0000867 }
868 };
869}
870
Chris Lattnera84df0a22006-09-28 23:36:21 +0000871
872//---- ConstantBool::get*() implementation.
873
874ConstantBool *ConstantBool::getTrue() {
875 static ConstantBool *T = 0;
876 if (T) return T;
877 return T = new ConstantBool(true);
878}
879ConstantBool *ConstantBool::getFalse() {
880 static ConstantBool *F = 0;
881 if (F) return F;
882 return F = new ConstantBool(false);
883}
884
Reid Spencere0fc4df2006-10-20 07:07:24 +0000885//---- ConstantInt::get() implementations...
Chris Lattner49d855c2001-09-07 16:46:31 +0000886//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000887static ManagedStatic<ValueMap<uint64_t, Type, ConstantInt> > IntConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000888
Reid Spencere0fc4df2006-10-20 07:07:24 +0000889// Get a ConstantInt from an int64_t. Note here that we canoncialize the value
890// to a uint64_t value that has been zero extended down to the size of the
891// integer type of the ConstantInt. This allows the getZExtValue method to
892// just return the stored value while getSExtValue has to convert back to sign
893// extended. getZExtValue is more common in LLVM than getSExtValue().
894ConstantInt *ConstantInt::get(const Type *Ty, int64_t V) {
895 unsigned Size = Ty->getPrimitiveSizeInBits();
896 uint64_t ZeroExtendedCanonicalization = V & (~uint64_t(0UL) >> (64-Size));
897 return IntConstants->getOrCreate(Ty, ZeroExtendedCanonicalization );
Chris Lattner49d855c2001-09-07 16:46:31 +0000898}
899
Chris Lattner3462ae32001-12-03 22:26:30 +0000900//---- ConstantFP::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000901//
Chris Lattnerac80ea42004-02-01 22:49:04 +0000902namespace llvm {
903 template<>
904 struct ConstantCreator<ConstantFP, Type, uint64_t> {
905 static ConstantFP *create(const Type *Ty, uint64_t V) {
906 assert(Ty == Type::DoubleTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000907 return new ConstantFP(Ty, BitsToDouble(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000908 }
909 };
910 template<>
911 struct ConstantCreator<ConstantFP, Type, uint32_t> {
912 static ConstantFP *create(const Type *Ty, uint32_t V) {
913 assert(Ty == Type::FloatTy);
Jim Laskeyb74c6662005-08-17 19:34:49 +0000914 return new ConstantFP(Ty, BitsToFloat(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000915 }
916 };
917}
918
Chris Lattner69edc982006-09-28 00:35:06 +0000919static ManagedStatic<ValueMap<uint64_t, Type, ConstantFP> > DoubleConstants;
920static ManagedStatic<ValueMap<uint32_t, Type, ConstantFP> > FloatConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +0000921
Jim Laskey8ad8f712005-08-17 20:06:22 +0000922bool ConstantFP::isNullValue() const {
923 return DoubleToBits(Val) == 0;
924}
925
926bool ConstantFP::isExactlyValue(double V) const {
927 return DoubleToBits(V) == DoubleToBits(Val);
928}
929
930
Chris Lattner3462ae32001-12-03 22:26:30 +0000931ConstantFP *ConstantFP::get(const Type *Ty, double V) {
Chris Lattner241ed4c2004-01-23 00:55:21 +0000932 if (Ty == Type::FloatTy) {
933 // Force the value through memory to normalize it.
Chris Lattner69edc982006-09-28 00:35:06 +0000934 return FloatConstants->getOrCreate(Ty, FloatToBits(V));
Chris Lattnerac80ea42004-02-01 22:49:04 +0000935 } else {
936 assert(Ty == Type::DoubleTy);
Chris Lattner69edc982006-09-28 00:35:06 +0000937 return DoubleConstants->getOrCreate(Ty, DoubleToBits(V));
Chris Lattner241ed4c2004-01-23 00:55:21 +0000938 }
Chris Lattner49d855c2001-09-07 16:46:31 +0000939}
940
Chris Lattner9fba3da2004-02-15 05:53:04 +0000941//---- ConstantAggregateZero::get() implementation...
942//
943namespace llvm {
944 // ConstantAggregateZero does not take extra "value" argument...
945 template<class ValType>
946 struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
947 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
948 return new ConstantAggregateZero(Ty);
949 }
950 };
951
952 template<>
953 struct ConvertConstantType<ConstantAggregateZero, Type> {
954 static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
955 // Make everyone now use a constant of the new type...
956 Constant *New = ConstantAggregateZero::get(NewTy);
957 assert(New != OldC && "Didn't replace constant??");
958 OldC->uncheckedReplaceAllUsesWith(New);
959 OldC->destroyConstant(); // This constant is now dead, destroy it.
960 }
961 };
962}
963
Chris Lattner69edc982006-09-28 00:35:06 +0000964static ManagedStatic<ValueMap<char, Type,
965 ConstantAggregateZero> > AggZeroConstants;
Chris Lattner9fba3da2004-02-15 05:53:04 +0000966
Chris Lattner3e650af2004-08-04 04:48:01 +0000967static char getValType(ConstantAggregateZero *CPZ) { return 0; }
968
Chris Lattner9fba3da2004-02-15 05:53:04 +0000969Constant *ConstantAggregateZero::get(const Type *Ty) {
Chris Lattnerbfd0b6d2006-06-10 04:16:23 +0000970 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<PackedType>(Ty)) &&
971 "Cannot create an aggregate zero of non-aggregate type!");
Chris Lattner69edc982006-09-28 00:35:06 +0000972 return AggZeroConstants->getOrCreate(Ty, 0);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000973}
974
975// destroyConstant - Remove the constant from the constant table...
976//
977void ConstantAggregateZero::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +0000978 AggZeroConstants->remove(this);
Chris Lattner9fba3da2004-02-15 05:53:04 +0000979 destroyConstantImpl();
980}
981
Chris Lattner3462ae32001-12-03 22:26:30 +0000982//---- ConstantArray::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +0000983//
Chris Lattner189d19f2003-11-21 20:23:48 +0000984namespace llvm {
985 template<>
986 struct ConvertConstantType<ConstantArray, ArrayType> {
987 static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
988 // Make everyone now use a constant of the new type...
989 std::vector<Constant*> C;
990 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
991 C.push_back(cast<Constant>(OldC->getOperand(i)));
992 Constant *New = ConstantArray::get(NewTy, C);
993 assert(New != OldC && "Didn't replace constant??");
994 OldC->uncheckedReplaceAllUsesWith(New);
995 OldC->destroyConstant(); // This constant is now dead, destroy it.
996 }
997 };
998}
Chris Lattnerb50d1352003-10-05 00:17:43 +0000999
Chris Lattner3e650af2004-08-04 04:48:01 +00001000static std::vector<Constant*> getValType(ConstantArray *CA) {
1001 std::vector<Constant*> Elements;
1002 Elements.reserve(CA->getNumOperands());
1003 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1004 Elements.push_back(cast<Constant>(CA->getOperand(i)));
1005 return Elements;
1006}
1007
Chris Lattnerb64419a2005-10-03 22:51:37 +00001008typedef ValueMap<std::vector<Constant*>, ArrayType,
Chris Lattner935aa922005-10-04 17:48:46 +00001009 ConstantArray, true /*largekey*/> ArrayConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001010static ManagedStatic<ArrayConstantsTy> ArrayConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001011
Chris Lattner015e8212004-02-15 04:14:47 +00001012Constant *ConstantArray::get(const ArrayType *Ty,
Chris Lattner9fba3da2004-02-15 05:53:04 +00001013 const std::vector<Constant*> &V) {
1014 // If this is an all-zero array, return a ConstantAggregateZero object
1015 if (!V.empty()) {
1016 Constant *C = V[0];
1017 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001018 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001019 for (unsigned i = 1, e = V.size(); i != e; ++i)
1020 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001021 return ArrayConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001022 }
1023 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001024}
1025
Chris Lattner98fa07b2003-05-23 20:03:32 +00001026// destroyConstant - Remove the constant from the constant table...
1027//
1028void ConstantArray::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001029 ArrayConstants->remove(this);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001030 destroyConstantImpl();
1031}
1032
Reid Spencer6f614532006-05-30 08:23:18 +00001033/// ConstantArray::get(const string&) - Return an array that is initialized to
1034/// contain the specified string. If length is zero then a null terminator is
1035/// added to the specified string so that it may be used in a natural way.
1036/// Otherwise, the length parameter specifies how much of the string to use
1037/// and it won't be null terminated.
1038///
Reid Spencer82ebaba2006-05-30 18:15:07 +00001039Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
Chris Lattner7f74a562002-01-20 22:54:45 +00001040 std::vector<Constant*> ElementVals;
Reid Spencer82ebaba2006-05-30 18:15:07 +00001041 for (unsigned i = 0; i < Str.length(); ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001042 ElementVals.push_back(ConstantInt::get(Type::SByteTy, Str[i]));
Chris Lattner8f80fe02001-10-14 23:54:12 +00001043
1044 // Add a null terminator to the string...
Reid Spencer82ebaba2006-05-30 18:15:07 +00001045 if (AddNull) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001046 ElementVals.push_back(ConstantInt::get(Type::SByteTy, 0));
Reid Spencer6f614532006-05-30 08:23:18 +00001047 }
Chris Lattner8f80fe02001-10-14 23:54:12 +00001048
Reid Spencer82ebaba2006-05-30 18:15:07 +00001049 ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
Chris Lattner3462ae32001-12-03 22:26:30 +00001050 return ConstantArray::get(ATy, ElementVals);
Vikram S. Adve34410432001-10-14 23:17:20 +00001051}
1052
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001053/// isString - This method returns true if the array is an array of sbyte or
1054/// ubyte, and if the elements of the array are all ConstantInt's.
1055bool ConstantArray::isString() const {
1056 // Check the element type for sbyte or ubyte...
Chris Lattnere8701f62004-01-14 17:51:53 +00001057 if (getType()->getElementType() != Type::UByteTy &&
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001058 getType()->getElementType() != Type::SByteTy)
1059 return false;
1060 // Check the elements to make sure they are all integers, not constant
1061 // expressions.
1062 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1063 if (!isa<ConstantInt>(getOperand(i)))
1064 return false;
1065 return true;
1066}
1067
Chris Lattner81fabb02002-08-26 17:53:56 +00001068// getAsString - If the sub-element type of this array is either sbyte or ubyte,
1069// then this method converts the array to an std::string and returns it.
1070// Otherwise, it asserts out.
1071//
1072std::string ConstantArray::getAsString() const {
Chris Lattnere8dfcca2004-01-14 17:06:38 +00001073 assert(isString() && "Not a string!");
Chris Lattner81fabb02002-08-26 17:53:56 +00001074 std::string Result;
Chris Lattner6077c312003-07-23 15:22:26 +00001075 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Reid Spencere0fc4df2006-10-20 07:07:24 +00001076 Result += (char)cast<ConstantInt>(getOperand(i))->getZExtValue();
Chris Lattner81fabb02002-08-26 17:53:56 +00001077 return Result;
1078}
1079
1080
Chris Lattner3462ae32001-12-03 22:26:30 +00001081//---- ConstantStruct::get() implementation...
Chris Lattner49d855c2001-09-07 16:46:31 +00001082//
Chris Lattnerb50d1352003-10-05 00:17:43 +00001083
Chris Lattner189d19f2003-11-21 20:23:48 +00001084namespace llvm {
1085 template<>
1086 struct ConvertConstantType<ConstantStruct, StructType> {
1087 static void convert(ConstantStruct *OldC, const StructType *NewTy) {
1088 // Make everyone now use a constant of the new type...
1089 std::vector<Constant*> C;
1090 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1091 C.push_back(cast<Constant>(OldC->getOperand(i)));
1092 Constant *New = ConstantStruct::get(NewTy, C);
1093 assert(New != OldC && "Didn't replace constant??");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001094
Chris Lattner189d19f2003-11-21 20:23:48 +00001095 OldC->uncheckedReplaceAllUsesWith(New);
1096 OldC->destroyConstant(); // This constant is now dead, destroy it.
1097 }
1098 };
1099}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001100
Chris Lattner8760ec72005-10-04 01:17:50 +00001101typedef ValueMap<std::vector<Constant*>, StructType,
Chris Lattner935aa922005-10-04 17:48:46 +00001102 ConstantStruct, true /*largekey*/> StructConstantsTy;
Chris Lattner69edc982006-09-28 00:35:06 +00001103static ManagedStatic<StructConstantsTy> StructConstants;
Chris Lattner49d855c2001-09-07 16:46:31 +00001104
Chris Lattner3e650af2004-08-04 04:48:01 +00001105static std::vector<Constant*> getValType(ConstantStruct *CS) {
1106 std::vector<Constant*> Elements;
1107 Elements.reserve(CS->getNumOperands());
1108 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
1109 Elements.push_back(cast<Constant>(CS->getOperand(i)));
1110 return Elements;
1111}
1112
Chris Lattner015e8212004-02-15 04:14:47 +00001113Constant *ConstantStruct::get(const StructType *Ty,
1114 const std::vector<Constant*> &V) {
Chris Lattner9fba3da2004-02-15 05:53:04 +00001115 // Create a ConstantAggregateZero value if all elements are zeros...
1116 for (unsigned i = 0, e = V.size(); i != e; ++i)
1117 if (!V[i]->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001118 return StructConstants->getOrCreate(Ty, V);
Chris Lattner9fba3da2004-02-15 05:53:04 +00001119
1120 return ConstantAggregateZero::get(Ty);
Chris Lattner49d855c2001-09-07 16:46:31 +00001121}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001122
Chris Lattnerd6108ca2004-07-12 20:35:11 +00001123Constant *ConstantStruct::get(const std::vector<Constant*> &V) {
1124 std::vector<const Type*> StructEls;
1125 StructEls.reserve(V.size());
1126 for (unsigned i = 0, e = V.size(); i != e; ++i)
1127 StructEls.push_back(V[i]->getType());
1128 return get(StructType::get(StructEls), V);
1129}
1130
Chris Lattnerd7a73302001-10-13 06:57:33 +00001131// destroyConstant - Remove the constant from the constant table...
Chris Lattner883ad0b2001-10-03 15:39:36 +00001132//
Chris Lattner3462ae32001-12-03 22:26:30 +00001133void ConstantStruct::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001134 StructConstants->remove(this);
Chris Lattnerd7a73302001-10-13 06:57:33 +00001135 destroyConstantImpl();
1136}
Chris Lattner883ad0b2001-10-03 15:39:36 +00001137
Brian Gaeke02209042004-08-20 06:00:58 +00001138//---- ConstantPacked::get() implementation...
1139//
1140namespace llvm {
1141 template<>
1142 struct ConvertConstantType<ConstantPacked, PackedType> {
1143 static void convert(ConstantPacked *OldC, const PackedType *NewTy) {
1144 // Make everyone now use a constant of the new type...
1145 std::vector<Constant*> C;
1146 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
1147 C.push_back(cast<Constant>(OldC->getOperand(i)));
1148 Constant *New = ConstantPacked::get(NewTy, C);
1149 assert(New != OldC && "Didn't replace constant??");
1150 OldC->uncheckedReplaceAllUsesWith(New);
1151 OldC->destroyConstant(); // This constant is now dead, destroy it.
1152 }
1153 };
1154}
1155
1156static std::vector<Constant*> getValType(ConstantPacked *CP) {
1157 std::vector<Constant*> Elements;
1158 Elements.reserve(CP->getNumOperands());
1159 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1160 Elements.push_back(CP->getOperand(i));
1161 return Elements;
1162}
1163
Chris Lattner69edc982006-09-28 00:35:06 +00001164static ManagedStatic<ValueMap<std::vector<Constant*>, PackedType,
1165 ConstantPacked> > PackedConstants;
Brian Gaeke02209042004-08-20 06:00:58 +00001166
1167Constant *ConstantPacked::get(const PackedType *Ty,
1168 const std::vector<Constant*> &V) {
1169 // If this is an all-zero packed, return a ConstantAggregateZero object
1170 if (!V.empty()) {
1171 Constant *C = V[0];
1172 if (!C->isNullValue())
Chris Lattner69edc982006-09-28 00:35:06 +00001173 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001174 for (unsigned i = 1, e = V.size(); i != e; ++i)
1175 if (V[i] != C)
Chris Lattner69edc982006-09-28 00:35:06 +00001176 return PackedConstants->getOrCreate(Ty, V);
Brian Gaeke02209042004-08-20 06:00:58 +00001177 }
1178 return ConstantAggregateZero::get(Ty);
1179}
1180
1181Constant *ConstantPacked::get(const std::vector<Constant*> &V) {
1182 assert(!V.empty() && "Cannot infer type if V is empty");
1183 return get(PackedType::get(V.front()->getType(),V.size()), V);
1184}
1185
1186// destroyConstant - Remove the constant from the constant table...
1187//
1188void ConstantPacked::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001189 PackedConstants->remove(this);
Brian Gaeke02209042004-08-20 06:00:58 +00001190 destroyConstantImpl();
1191}
1192
Chris Lattner3462ae32001-12-03 22:26:30 +00001193//---- ConstantPointerNull::get() implementation...
Chris Lattnerd7a73302001-10-13 06:57:33 +00001194//
Chris Lattner98fa07b2003-05-23 20:03:32 +00001195
Chris Lattner189d19f2003-11-21 20:23:48 +00001196namespace llvm {
1197 // ConstantPointerNull does not take extra "value" argument...
1198 template<class ValType>
1199 struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1200 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1201 return new ConstantPointerNull(Ty);
1202 }
1203 };
Chris Lattner98fa07b2003-05-23 20:03:32 +00001204
Chris Lattner189d19f2003-11-21 20:23:48 +00001205 template<>
1206 struct ConvertConstantType<ConstantPointerNull, PointerType> {
1207 static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1208 // Make everyone now use a constant of the new type...
1209 Constant *New = ConstantPointerNull::get(NewTy);
1210 assert(New != OldC && "Didn't replace constant??");
1211 OldC->uncheckedReplaceAllUsesWith(New);
1212 OldC->destroyConstant(); // This constant is now dead, destroy it.
1213 }
1214 };
1215}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001216
Chris Lattner69edc982006-09-28 00:35:06 +00001217static ManagedStatic<ValueMap<char, PointerType,
1218 ConstantPointerNull> > NullPtrConstants;
Chris Lattnerd7a73302001-10-13 06:57:33 +00001219
Chris Lattner3e650af2004-08-04 04:48:01 +00001220static char getValType(ConstantPointerNull *) {
1221 return 0;
1222}
1223
1224
Chris Lattner3462ae32001-12-03 22:26:30 +00001225ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001226 return NullPtrConstants->getOrCreate(Ty, 0);
Chris Lattner883ad0b2001-10-03 15:39:36 +00001227}
1228
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001229// destroyConstant - Remove the constant from the constant table...
1230//
1231void ConstantPointerNull::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001232 NullPtrConstants->remove(this);
Chris Lattner0c6e0b92002-08-18 00:40:04 +00001233 destroyConstantImpl();
1234}
1235
1236
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001237//---- UndefValue::get() implementation...
1238//
1239
1240namespace llvm {
1241 // UndefValue does not take extra "value" argument...
1242 template<class ValType>
1243 struct ConstantCreator<UndefValue, Type, ValType> {
1244 static UndefValue *create(const Type *Ty, const ValType &V) {
1245 return new UndefValue(Ty);
1246 }
1247 };
1248
1249 template<>
1250 struct ConvertConstantType<UndefValue, Type> {
1251 static void convert(UndefValue *OldC, const Type *NewTy) {
1252 // Make everyone now use a constant of the new type.
1253 Constant *New = UndefValue::get(NewTy);
1254 assert(New != OldC && "Didn't replace constant??");
1255 OldC->uncheckedReplaceAllUsesWith(New);
1256 OldC->destroyConstant(); // This constant is now dead, destroy it.
1257 }
1258 };
1259}
1260
Chris Lattner69edc982006-09-28 00:35:06 +00001261static ManagedStatic<ValueMap<char, Type, UndefValue> > UndefValueConstants;
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001262
1263static char getValType(UndefValue *) {
1264 return 0;
1265}
1266
1267
1268UndefValue *UndefValue::get(const Type *Ty) {
Chris Lattner69edc982006-09-28 00:35:06 +00001269 return UndefValueConstants->getOrCreate(Ty, 0);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001270}
1271
1272// destroyConstant - Remove the constant from the constant table.
1273//
1274void UndefValue::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001275 UndefValueConstants->remove(this);
Chris Lattnerd5f67d82004-10-16 18:07:16 +00001276 destroyConstantImpl();
1277}
1278
1279
1280
1281
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001282//---- ConstantExpr::get() implementations...
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001283//
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001284typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
Chris Lattner98fa07b2003-05-23 20:03:32 +00001285
Chris Lattner189d19f2003-11-21 20:23:48 +00001286namespace llvm {
1287 template<>
1288 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1289 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1290 if (V.first == Instruction::Cast)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001291 return new UnaryConstantExpr(Instruction::Cast, V.second[0], Ty);
Chris Lattner189d19f2003-11-21 20:23:48 +00001292 if ((V.first >= Instruction::BinaryOpsBegin &&
1293 V.first < Instruction::BinaryOpsEnd) ||
1294 V.first == Instruction::Shl || V.first == Instruction::Shr)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001295 return new BinaryConstantExpr(V.first, V.second[0], V.second[1]);
Chris Lattnerb7897ce2004-03-30 22:51:03 +00001296 if (V.first == Instruction::Select)
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001297 return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
Robert Bocchino23004482006-01-10 19:05:34 +00001298 if (V.first == Instruction::ExtractElement)
1299 return new ExtractElementConstantExpr(V.second[0], V.second[1]);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001300 if (V.first == Instruction::InsertElement)
1301 return new InsertElementConstantExpr(V.second[0], V.second[1],
1302 V.second[2]);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001303 if (V.first == Instruction::ShuffleVector)
1304 return new ShuffleVectorConstantExpr(V.second[0], V.second[1],
1305 V.second[2]);
1306
Chris Lattner189d19f2003-11-21 20:23:48 +00001307 assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001308
Chris Lattner189d19f2003-11-21 20:23:48 +00001309 std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
Chris Lattnerd0df99c2005-01-29 00:34:39 +00001310 return new GetElementPtrConstantExpr(V.second[0], IdxList, Ty);
Chris Lattnerb50d1352003-10-05 00:17:43 +00001311 }
Chris Lattner189d19f2003-11-21 20:23:48 +00001312 };
Chris Lattnerb50d1352003-10-05 00:17:43 +00001313
Chris Lattner189d19f2003-11-21 20:23:48 +00001314 template<>
1315 struct ConvertConstantType<ConstantExpr, Type> {
1316 static void convert(ConstantExpr *OldC, const Type *NewTy) {
1317 Constant *New;
1318 switch (OldC->getOpcode()) {
1319 case Instruction::Cast:
1320 New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1321 break;
Chris Lattner6e415c02004-03-12 05:54:04 +00001322 case Instruction::Select:
1323 New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1324 OldC->getOperand(1),
1325 OldC->getOperand(2));
1326 break;
Chris Lattner189d19f2003-11-21 20:23:48 +00001327 case Instruction::Shl:
1328 case Instruction::Shr:
1329 New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1330 OldC->getOperand(0), OldC->getOperand(1));
1331 break;
1332 default:
1333 assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1334 OldC->getOpcode() < Instruction::BinaryOpsEnd);
1335 New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1336 OldC->getOperand(1));
1337 break;
1338 case Instruction::GetElementPtr:
Misha Brukmanb1c93172005-04-21 23:48:37 +00001339 // Make everyone now use a constant of the new type...
Chris Lattner13128ab2004-10-11 22:52:25 +00001340 std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
1341 New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
Chris Lattner189d19f2003-11-21 20:23:48 +00001342 break;
1343 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001344
Chris Lattner189d19f2003-11-21 20:23:48 +00001345 assert(New != OldC && "Didn't replace constant??");
1346 OldC->uncheckedReplaceAllUsesWith(New);
1347 OldC->destroyConstant(); // This constant is now dead, destroy it.
1348 }
1349 };
1350} // end namespace llvm
Chris Lattnerb50d1352003-10-05 00:17:43 +00001351
1352
Chris Lattner3e650af2004-08-04 04:48:01 +00001353static ExprMapKeyType getValType(ConstantExpr *CE) {
1354 std::vector<Constant*> Operands;
1355 Operands.reserve(CE->getNumOperands());
1356 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
1357 Operands.push_back(cast<Constant>(CE->getOperand(i)));
1358 return ExprMapKeyType(CE->getOpcode(), Operands);
1359}
1360
Chris Lattner69edc982006-09-28 00:35:06 +00001361static ManagedStatic<ValueMap<ExprMapKeyType, Type,
1362 ConstantExpr> > ExprConstants;
Vikram S. Adve4c485332002-07-15 18:19:33 +00001363
Chris Lattner46b3d302003-04-16 22:40:51 +00001364Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
Chris Lattner815ae2b2003-10-07 22:19:19 +00001365 assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1366
Chris Lattneracdbe712003-04-17 19:24:48 +00001367 if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1368 return FC; // Fold a few common cases...
1369
Vikram S. Adve4c485332002-07-15 18:19:33 +00001370 // Look up the constant in the table first to ensure uniqueness
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001371 std::vector<Constant*> argVec(1, C);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001372 ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001373 return ExprConstants->getOrCreate(Ty, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001374}
Chris Lattnerd7a73302001-10-13 06:57:33 +00001375
Chris Lattnerdd284742004-04-04 23:20:30 +00001376Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001377 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001378 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1379 "This is an illegal sign extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001380 if (C->getType() != Type::BoolTy) {
1381 C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1382 return ConstantExpr::getCast(C, Ty);
1383 } else {
Chris Lattnera84df0a22006-09-28 23:36:21 +00001384 if (C == ConstantBool::getTrue())
Chris Lattner1ece6f82005-01-01 15:59:57 +00001385 return ConstantIntegral::getAllOnesValue(Ty);
1386 else
1387 return ConstantIntegral::getNullValue(Ty);
1388 }
Chris Lattnerdd284742004-04-04 23:20:30 +00001389}
1390
1391Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
Chris Lattner1ece6f82005-01-01 15:59:57 +00001392 assert(C->getType()->isIntegral() && Ty->isIntegral() &&
Chris Lattnerdd284742004-04-04 23:20:30 +00001393 C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1394 "This is an illegal zero extension!");
Chris Lattner1ece6f82005-01-01 15:59:57 +00001395 if (C->getType() != Type::BoolTy)
1396 C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
Chris Lattnerdd284742004-04-04 23:20:30 +00001397 return ConstantExpr::getCast(C, Ty);
1398}
1399
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001400Constant *ConstantExpr::getSizeOf(const Type *Ty) {
Chris Lattneracc4e542004-12-13 19:48:51 +00001401 // sizeof is implemented as: (ulong) gep (Ty*)null, 1
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001402 return getCast(
Chris Lattneracc4e542004-12-13 19:48:51 +00001403 getGetElementPtr(getNullValue(PointerType::get(Ty)),
1404 std::vector<Constant*>(1, ConstantInt::get(Type::UIntTy, 1))),
1405 Type::ULongTy);
Alkis Evlogimenosda5de052004-10-24 01:41:10 +00001406}
1407
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001408Constant *ConstantExpr::getPtrPtrFromArrayPtr(Constant *C) {
1409 // pointer from array is implemented as: getelementptr arr ptr, 0, 0
Reid Spencere0fc4df2006-10-20 07:07:24 +00001410 static std::vector<Constant*> Indices(2, ConstantInt::get(Type::UIntTy, 0));
Alkis Evlogimenos9160d5f2005-03-19 11:40:31 +00001411
1412 return ConstantExpr::getGetElementPtr(C, Indices);
1413}
1414
Chris Lattnerb50d1352003-10-05 00:17:43 +00001415Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1416 Constant *C1, Constant *C2) {
Chris Lattner5645e8a2004-01-12 19:04:55 +00001417 if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1418 return getShiftTy(ReqTy, Opcode, C1, C2);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001419 // Check the operands for consistency first
1420 assert((Opcode >= Instruction::BinaryOpsBegin &&
1421 Opcode < Instruction::BinaryOpsEnd) &&
1422 "Invalid opcode in binary constant expression");
1423 assert(C1->getType() == C2->getType() &&
1424 "Operand types in binary constant expression should match");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001425
Chris Lattnere1496fb2006-09-17 19:14:47 +00001426 if (ReqTy == C1->getType() || (Instruction::isComparison(Opcode) &&
Chris Lattner29ca2c62004-08-04 18:50:09 +00001427 ReqTy == Type::BoolTy))
Chris Lattnerb50d1352003-10-05 00:17:43 +00001428 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1429 return FC; // Fold a few common cases...
Chris Lattneracdbe712003-04-17 19:24:48 +00001430
Chris Lattner2b383d2e2003-05-13 21:37:02 +00001431 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001432 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001433 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001434}
1435
Chris Lattner29ca2c62004-08-04 18:50:09 +00001436Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2) {
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001437#ifndef NDEBUG
1438 switch (Opcode) {
1439 case Instruction::Add: case Instruction::Sub:
1440 case Instruction::Mul: case Instruction::Div:
1441 case Instruction::Rem:
1442 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001443 assert((C1->getType()->isInteger() || C1->getType()->isFloatingPoint() ||
1444 isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001445 "Tried to create an arithmetic operation on a non-arithmetic type!");
1446 break;
1447 case Instruction::And:
1448 case Instruction::Or:
1449 case Instruction::Xor:
1450 assert(C1->getType() == C2->getType() && "Op types should be identical!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001451 assert((C1->getType()->isIntegral() || isa<PackedType>(C1->getType())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001452 "Tried to create a logical operation on a non-integral type!");
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001453 break;
1454 case Instruction::SetLT: case Instruction::SetGT: case Instruction::SetLE:
1455 case Instruction::SetGE: case Instruction::SetEQ: case Instruction::SetNE:
1456 assert(C1->getType() == C2->getType() && "Op types should be identical!");
1457 break;
1458 case Instruction::Shl:
1459 case Instruction::Shr:
1460 assert(C2->getType() == Type::UByteTy && "Shift should be by ubyte!");
Chris Lattnerc421a262006-01-04 01:01:04 +00001461 assert((C1->getType()->isInteger() || isa<PackedType>(C1->getType())) &&
Chris Lattnercaf3f3e2004-08-17 17:28:46 +00001462 "Tried to create a shift operation on a non-integer type!");
1463 break;
1464 default:
1465 break;
1466 }
1467#endif
1468
Chris Lattnere1496fb2006-09-17 19:14:47 +00001469 if (Instruction::isComparison(Opcode))
Chris Lattner29ca2c62004-08-04 18:50:09 +00001470 return getTy(Type::BoolTy, Opcode, C1, C2);
1471 else
1472 return getTy(C1->getType(), Opcode, C1, C2);
1473}
1474
Chris Lattner6e415c02004-03-12 05:54:04 +00001475Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1476 Constant *V1, Constant *V2) {
1477 assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1478 assert(V1->getType() == V2->getType() && "Select value types must match!");
1479 assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1480
1481 if (ReqTy == V1->getType())
1482 if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1483 return SC; // Fold common cases
1484
1485 std::vector<Constant*> argVec(3, C);
1486 argVec[1] = V1;
1487 argVec[2] = V2;
1488 ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001489 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner6e415c02004-03-12 05:54:04 +00001490}
1491
Chris Lattner9eb2b522004-01-12 19:12:58 +00001492/// getShiftTy - Return a shift left or shift right constant expr
Chris Lattnerb50d1352003-10-05 00:17:43 +00001493Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1494 Constant *C1, Constant *C2) {
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001495 // Check the operands for consistency first
1496 assert((Opcode == Instruction::Shl ||
1497 Opcode == Instruction::Shr) &&
1498 "Invalid opcode in binary constant expression");
1499 assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1500 "Invalid operand types for Shift constant expr!");
1501
Chris Lattner0bba7712004-01-12 20:40:42 +00001502 if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001503 return FC; // Fold a few common cases...
1504
1505 // Look up the constant in the table first to ensure uniqueness
1506 std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
Chris Lattner98fa07b2003-05-23 20:03:32 +00001507 ExprMapKeyType Key = std::make_pair(Opcode, argVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001508 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattner38a9bcd2003-05-21 17:49:25 +00001509}
1510
1511
Chris Lattnerb50d1352003-10-05 00:17:43 +00001512Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001513 const std::vector<Value*> &IdxList) {
1514 assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
Chris Lattner04b60fe2004-02-16 20:46:13 +00001515 "GEP indices invalid!");
1516
Chris Lattneracdbe712003-04-17 19:24:48 +00001517 if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1518 return FC; // Fold a few common cases...
Chris Lattner04b60fe2004-02-16 20:46:13 +00001519
Chris Lattnerb50d1352003-10-05 00:17:43 +00001520 assert(isa<PointerType>(C->getType()) &&
Chris Lattner98fa07b2003-05-23 20:03:32 +00001521 "Non-pointer type for constant GetElementPtr expression");
Vikram S. Adve4c485332002-07-15 18:19:33 +00001522 // Look up the constant in the table first to ensure uniqueness
Chris Lattner13128ab2004-10-11 22:52:25 +00001523 std::vector<Constant*> ArgVec;
1524 ArgVec.reserve(IdxList.size()+1);
1525 ArgVec.push_back(C);
1526 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
1527 ArgVec.push_back(cast<Constant>(IdxList[i]));
1528 const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001529 return ExprConstants->getOrCreate(ReqTy, Key);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001530}
1531
Chris Lattnerb50d1352003-10-05 00:17:43 +00001532Constant *ConstantExpr::getGetElementPtr(Constant *C,
1533 const std::vector<Constant*> &IdxList){
1534 // Get the result type of the getelementptr!
1535 std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1536
1537 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1538 true);
1539 assert(Ty && "GEP indices invalid!");
Chris Lattner13128ab2004-10-11 22:52:25 +00001540 return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
1541}
1542
1543Constant *ConstantExpr::getGetElementPtr(Constant *C,
1544 const std::vector<Value*> &IdxList) {
1545 // Get the result type of the getelementptr!
1546 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1547 true);
1548 assert(Ty && "GEP indices invalid!");
Chris Lattnerb50d1352003-10-05 00:17:43 +00001549 return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1550}
1551
Robert Bocchino23004482006-01-10 19:05:34 +00001552Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
1553 Constant *Idx) {
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001554 if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
1555 return FC; // Fold a few common cases...
Robert Bocchino23004482006-01-10 19:05:34 +00001556 // Look up the constant in the table first to ensure uniqueness
1557 std::vector<Constant*> ArgVec(1, Val);
1558 ArgVec.push_back(Idx);
1559 const ExprMapKeyType &Key = std::make_pair(Instruction::ExtractElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001560 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchino23004482006-01-10 19:05:34 +00001561}
1562
1563Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
1564 assert(isa<PackedType>(Val->getType()) &&
1565 "Tried to create extractelement operation on non-packed type!");
1566 assert(Idx->getType() == Type::UIntTy &&
Robert Bocchinoca27f032006-01-17 20:07:22 +00001567 "Extractelement index must be uint type!");
Robert Bocchino23004482006-01-10 19:05:34 +00001568 return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
1569 Val, Idx);
1570}
Chris Lattnerb50d1352003-10-05 00:17:43 +00001571
Robert Bocchinoca27f032006-01-17 20:07:22 +00001572Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
1573 Constant *Elt, Constant *Idx) {
1574 if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
1575 return FC; // Fold a few common cases...
1576 // Look up the constant in the table first to ensure uniqueness
1577 std::vector<Constant*> ArgVec(1, Val);
1578 ArgVec.push_back(Elt);
1579 ArgVec.push_back(Idx);
1580 const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001581 return ExprConstants->getOrCreate(ReqTy, Key);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001582}
1583
1584Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
1585 Constant *Idx) {
1586 assert(isa<PackedType>(Val->getType()) &&
1587 "Tried to create insertelement operation on non-packed type!");
1588 assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
1589 && "Insertelement types must match!");
1590 assert(Idx->getType() == Type::UIntTy &&
1591 "Insertelement index must be uint type!");
1592 return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
1593 Val, Elt, Idx);
1594}
1595
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001596Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
1597 Constant *V2, Constant *Mask) {
1598 if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
1599 return FC; // Fold a few common cases...
1600 // Look up the constant in the table first to ensure uniqueness
1601 std::vector<Constant*> ArgVec(1, V1);
1602 ArgVec.push_back(V2);
1603 ArgVec.push_back(Mask);
1604 const ExprMapKeyType &Key = std::make_pair(Instruction::ShuffleVector,ArgVec);
Chris Lattner69edc982006-09-28 00:35:06 +00001605 return ExprConstants->getOrCreate(ReqTy, Key);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001606}
1607
1608Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
1609 Constant *Mask) {
1610 assert(ShuffleVectorInst::isValidOperands(V1, V2, Mask) &&
1611 "Invalid shuffle vector constant expr operands!");
1612 return getShuffleVectorTy(V1->getType(), V1, V2, Mask);
1613}
1614
1615
Vikram S. Adve4c485332002-07-15 18:19:33 +00001616// destroyConstant - Remove the constant from the constant table...
1617//
1618void ConstantExpr::destroyConstant() {
Chris Lattner69edc982006-09-28 00:35:06 +00001619 ExprConstants->remove(this);
Vikram S. Adve4c485332002-07-15 18:19:33 +00001620 destroyConstantImpl();
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001621}
1622
Chris Lattner3cd8c562002-07-30 18:54:25 +00001623const char *ConstantExpr::getOpcodeName() const {
1624 return Instruction::getOpcodeName(getOpcode());
Vikram S. Adve4e537b22002-07-14 23:13:17 +00001625}
Reid Spencer1ebe1ab2004-07-17 23:48:33 +00001626
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001627//===----------------------------------------------------------------------===//
1628// replaceUsesOfWithOnConstant implementations
1629
1630void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001631 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001632 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001633 Constant *ToC = cast<Constant>(To);
Chris Lattnerdff59112005-10-04 18:47:09 +00001634
1635 unsigned OperandToUpdate = U-OperandList;
1636 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1637
Jim Laskeyc03caef2006-07-17 17:38:29 +00001638 std::pair<ArrayConstantsTy::MapKey, Constant*> Lookup;
Chris Lattnerb64419a2005-10-03 22:51:37 +00001639 Lookup.first.first = getType();
1640 Lookup.second = this;
Chris Lattnerdff59112005-10-04 18:47:09 +00001641
Chris Lattnerb64419a2005-10-03 22:51:37 +00001642 std::vector<Constant*> &Values = Lookup.first.second;
1643 Values.reserve(getNumOperands()); // Build replacement array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001644
Chris Lattner8760ec72005-10-04 01:17:50 +00001645 // Fill values with the modified operands of the constant array. Also,
1646 // compute whether this turns into an all-zeros array.
Chris Lattnerdff59112005-10-04 18:47:09 +00001647 bool isAllZeros = false;
1648 if (!ToC->isNullValue()) {
1649 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1650 Values.push_back(cast<Constant>(O->get()));
1651 } else {
1652 isAllZeros = true;
1653 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1654 Constant *Val = cast<Constant>(O->get());
1655 Values.push_back(Val);
1656 if (isAllZeros) isAllZeros = Val->isNullValue();
1657 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001658 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001659 Values[OperandToUpdate] = ToC;
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001660
Chris Lattnerb64419a2005-10-03 22:51:37 +00001661 Constant *Replacement = 0;
1662 if (isAllZeros) {
1663 Replacement = ConstantAggregateZero::get(getType());
1664 } else {
1665 // Check to see if we have this array type already.
1666 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001667 ArrayConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001668 ArrayConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001669
1670 if (Exists) {
1671 Replacement = I->second;
1672 } else {
1673 // Okay, the new shape doesn't exist in the system yet. Instead of
1674 // creating a new constant array, inserting it, replaceallusesof'ing the
1675 // old with the new, then deleting the old... just update the current one
1676 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001677 ArrayConstants->MoveConstantToNewSlot(this, I);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001678
Chris Lattnerdff59112005-10-04 18:47:09 +00001679 // Update to the new value.
1680 setOperand(OperandToUpdate, ToC);
Chris Lattnerb64419a2005-10-03 22:51:37 +00001681 return;
1682 }
1683 }
1684
1685 // Otherwise, I do need to replace this with an existing value.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001686 assert(Replacement != this && "I didn't contain From!");
1687
Chris Lattner7a1450d2005-10-04 18:13:04 +00001688 // Everyone using this now uses the replacement.
1689 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001690
1691 // Delete the old constant!
1692 destroyConstant();
1693}
1694
1695void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001696 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001697 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
Chris Lattner8760ec72005-10-04 01:17:50 +00001698 Constant *ToC = cast<Constant>(To);
1699
Chris Lattnerdff59112005-10-04 18:47:09 +00001700 unsigned OperandToUpdate = U-OperandList;
1701 assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
1702
Jim Laskeyc03caef2006-07-17 17:38:29 +00001703 std::pair<StructConstantsTy::MapKey, Constant*> Lookup;
Chris Lattner8760ec72005-10-04 01:17:50 +00001704 Lookup.first.first = getType();
1705 Lookup.second = this;
1706 std::vector<Constant*> &Values = Lookup.first.second;
1707 Values.reserve(getNumOperands()); // Build replacement struct.
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001708
Chris Lattnerdff59112005-10-04 18:47:09 +00001709
Chris Lattner8760ec72005-10-04 01:17:50 +00001710 // Fill values with the modified operands of the constant struct. Also,
1711 // compute whether this turns into an all-zeros struct.
Chris Lattnerdff59112005-10-04 18:47:09 +00001712 bool isAllZeros = false;
1713 if (!ToC->isNullValue()) {
1714 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
1715 Values.push_back(cast<Constant>(O->get()));
1716 } else {
1717 isAllZeros = true;
1718 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1719 Constant *Val = cast<Constant>(O->get());
1720 Values.push_back(Val);
1721 if (isAllZeros) isAllZeros = Val->isNullValue();
1722 }
Chris Lattner8760ec72005-10-04 01:17:50 +00001723 }
Chris Lattnerdff59112005-10-04 18:47:09 +00001724 Values[OperandToUpdate] = ToC;
1725
Chris Lattner8760ec72005-10-04 01:17:50 +00001726 Constant *Replacement = 0;
1727 if (isAllZeros) {
1728 Replacement = ConstantAggregateZero::get(getType());
1729 } else {
1730 // Check to see if we have this array type already.
1731 bool Exists;
Jim Laskeyc03caef2006-07-17 17:38:29 +00001732 StructConstantsTy::MapTy::iterator I =
Chris Lattner69edc982006-09-28 00:35:06 +00001733 StructConstants->InsertOrGetItem(Lookup, Exists);
Chris Lattner8760ec72005-10-04 01:17:50 +00001734
1735 if (Exists) {
1736 Replacement = I->second;
1737 } else {
1738 // Okay, the new shape doesn't exist in the system yet. Instead of
1739 // creating a new constant struct, inserting it, replaceallusesof'ing the
1740 // old with the new, then deleting the old... just update the current one
1741 // in place!
Chris Lattner69edc982006-09-28 00:35:06 +00001742 StructConstants->MoveConstantToNewSlot(this, I);
Chris Lattner8760ec72005-10-04 01:17:50 +00001743
Chris Lattnerdff59112005-10-04 18:47:09 +00001744 // Update to the new value.
1745 setOperand(OperandToUpdate, ToC);
Chris Lattner8760ec72005-10-04 01:17:50 +00001746 return;
1747 }
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001748 }
1749
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001750 assert(Replacement != this && "I didn't contain From!");
1751
Chris Lattner7a1450d2005-10-04 18:13:04 +00001752 // Everyone using this now uses the replacement.
1753 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001754
1755 // Delete the old constant!
1756 destroyConstant();
1757}
1758
1759void ConstantPacked::replaceUsesOfWithOnConstant(Value *From, Value *To,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001760 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001761 assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
1762
1763 std::vector<Constant*> Values;
1764 Values.reserve(getNumOperands()); // Build replacement array...
1765 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1766 Constant *Val = getOperand(i);
1767 if (Val == From) Val = cast<Constant>(To);
1768 Values.push_back(Val);
1769 }
1770
1771 Constant *Replacement = ConstantPacked::get(getType(), Values);
1772 assert(Replacement != this && "I didn't contain From!");
1773
Chris Lattner7a1450d2005-10-04 18:13:04 +00001774 // Everyone using this now uses the replacement.
1775 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001776
1777 // Delete the old constant!
1778 destroyConstant();
1779}
1780
1781void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
Chris Lattner7a1450d2005-10-04 18:13:04 +00001782 Use *U) {
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001783 assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
1784 Constant *To = cast<Constant>(ToV);
1785
1786 Constant *Replacement = 0;
1787 if (getOpcode() == Instruction::GetElementPtr) {
1788 std::vector<Constant*> Indices;
1789 Constant *Pointer = getOperand(0);
1790 Indices.reserve(getNumOperands()-1);
1791 if (Pointer == From) Pointer = To;
1792
1793 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1794 Constant *Val = getOperand(i);
1795 if (Val == From) Val = To;
1796 Indices.push_back(Val);
1797 }
1798 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
1799 } else if (getOpcode() == Instruction::Cast) {
1800 assert(getOperand(0) == From && "Cast only has one use!");
1801 Replacement = ConstantExpr::getCast(To, getType());
1802 } else if (getOpcode() == Instruction::Select) {
1803 Constant *C1 = getOperand(0);
1804 Constant *C2 = getOperand(1);
1805 Constant *C3 = getOperand(2);
1806 if (C1 == From) C1 = To;
1807 if (C2 == From) C2 = To;
1808 if (C3 == From) C3 = To;
1809 Replacement = ConstantExpr::getSelect(C1, C2, C3);
Robert Bocchino23004482006-01-10 19:05:34 +00001810 } else if (getOpcode() == Instruction::ExtractElement) {
1811 Constant *C1 = getOperand(0);
1812 Constant *C2 = getOperand(1);
1813 if (C1 == From) C1 = To;
1814 if (C2 == From) C2 = To;
1815 Replacement = ConstantExpr::getExtractElement(C1, C2);
Chris Lattnera93b4b52006-04-08 05:09:48 +00001816 } else if (getOpcode() == Instruction::InsertElement) {
1817 Constant *C1 = getOperand(0);
1818 Constant *C2 = getOperand(1);
1819 Constant *C3 = getOperand(1);
1820 if (C1 == From) C1 = To;
1821 if (C2 == From) C2 = To;
1822 if (C3 == From) C3 = To;
1823 Replacement = ConstantExpr::getInsertElement(C1, C2, C3);
1824 } else if (getOpcode() == Instruction::ShuffleVector) {
1825 Constant *C1 = getOperand(0);
1826 Constant *C2 = getOperand(1);
1827 Constant *C3 = getOperand(2);
1828 if (C1 == From) C1 = To;
1829 if (C2 == From) C2 = To;
1830 if (C3 == From) C3 = To;
1831 Replacement = ConstantExpr::getShuffleVector(C1, C2, C3);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001832 } else if (getNumOperands() == 2) {
1833 Constant *C1 = getOperand(0);
1834 Constant *C2 = getOperand(1);
1835 if (C1 == From) C1 = To;
1836 if (C2 == From) C2 = To;
1837 Replacement = ConstantExpr::get(getOpcode(), C1, C2);
1838 } else {
1839 assert(0 && "Unknown ConstantExpr type!");
1840 return;
1841 }
1842
1843 assert(Replacement != this && "I didn't contain From!");
1844
Chris Lattner7a1450d2005-10-04 18:13:04 +00001845 // Everyone using this now uses the replacement.
1846 uncheckedReplaceAllUsesWith(Replacement);
Chris Lattnerc4062ba2005-10-03 21:58:36 +00001847
1848 // Delete the old constant!
1849 destroyConstant();
1850}
1851
1852
Jim Laskey2698f0d2006-03-08 18:11:07 +00001853/// getStringValue - Turn an LLVM constant pointer that eventually points to a
1854/// global into a string value. Return an empty string if we can't do it.
Evan Cheng38280c02006-03-10 23:52:03 +00001855/// Parameter Chop determines if the result is chopped at the first null
1856/// terminator.
Jim Laskey2698f0d2006-03-08 18:11:07 +00001857///
Evan Cheng38280c02006-03-10 23:52:03 +00001858std::string Constant::getStringValue(bool Chop, unsigned Offset) {
Jim Laskey2698f0d2006-03-08 18:11:07 +00001859 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(this)) {
1860 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
1861 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
1862 if (Init->isString()) {
1863 std::string Result = Init->getAsString();
1864 if (Offset < Result.size()) {
1865 // If we are pointing INTO The string, erase the beginning...
1866 Result.erase(Result.begin(), Result.begin()+Offset);
1867
1868 // Take off the null terminator, and any string fragments after it.
Evan Cheng38280c02006-03-10 23:52:03 +00001869 if (Chop) {
1870 std::string::size_type NullPos = Result.find_first_of((char)0);
1871 if (NullPos != std::string::npos)
1872 Result.erase(Result.begin()+NullPos, Result.end());
1873 }
Jim Laskey2698f0d2006-03-08 18:11:07 +00001874 return Result;
1875 }
1876 }
1877 }
1878 } else if (Constant *C = dyn_cast<Constant>(this)) {
1879 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
Evan Cheng2c5e5302006-03-11 00:13:10 +00001880 return GV->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001881 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1882 if (CE->getOpcode() == Instruction::GetElementPtr) {
1883 // Turn a gep into the specified offset.
1884 if (CE->getNumOperands() == 3 &&
1885 cast<Constant>(CE->getOperand(1))->isNullValue() &&
1886 isa<ConstantInt>(CE->getOperand(2))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001887 Offset += cast<ConstantInt>(CE->getOperand(2))->getZExtValue();
Evan Cheng2c5e5302006-03-11 00:13:10 +00001888 return CE->getOperand(0)->getStringValue(Chop, Offset);
Jim Laskey2698f0d2006-03-08 18:11:07 +00001889 }
1890 }
1891 }
1892 }
1893 return "";
1894}
1895