blob: 40eea138350671f694927ee0a218801e93ea5412 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000021#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000022#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023using namespace llvm;
24
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025//===----------------------------------------------------------------------===//
26// CallSite Class
27//===----------------------------------------------------------------------===//
28
Duncan Sands85fab3a2008-02-18 17:32:13 +000029CallSite::CallSite(Instruction *C) {
30 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
31 I = C;
32}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000033unsigned CallSite::getCallingConv() const {
34 if (CallInst *CI = dyn_cast<CallInst>(I))
35 return CI->getCallingConv();
36 else
37 return cast<InvokeInst>(I)->getCallingConv();
38}
39void CallSite::setCallingConv(unsigned CC) {
40 if (CallInst *CI = dyn_cast<CallInst>(I))
41 CI->setCallingConv(CC);
42 else
43 cast<InvokeInst>(I)->setCallingConv(CC);
44}
Chris Lattner8a923e72008-03-12 17:45:29 +000045const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000046 if (CallInst *CI = dyn_cast<CallInst>(I))
47 return CI->getParamAttrs();
48 else
49 return cast<InvokeInst>(I)->getParamAttrs();
50}
Chris Lattner8a923e72008-03-12 17:45:29 +000051void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000052 if (CallInst *CI = dyn_cast<CallInst>(I))
53 CI->setParamAttrs(PAL);
54 else
55 cast<InvokeInst>(I)->setParamAttrs(PAL);
56}
Dale Johannesen89268bc2008-02-19 21:38:47 +000057bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000058 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000059 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000060 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000061 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000062}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063uint16_t CallSite::getParamAlignment(uint16_t i) const {
64 if (CallInst *CI = dyn_cast<CallInst>(I))
65 return CI->getParamAlignment(i);
66 else
67 return cast<InvokeInst>(I)->getParamAlignment(i);
68}
69
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::doesNotAccessMemory() const {
71 if (CallInst *CI = dyn_cast<CallInst>(I))
72 return CI->doesNotAccessMemory();
73 else
74 return cast<InvokeInst>(I)->doesNotAccessMemory();
75}
76bool CallSite::onlyReadsMemory() const {
77 if (CallInst *CI = dyn_cast<CallInst>(I))
78 return CI->onlyReadsMemory();
79 else
80 return cast<InvokeInst>(I)->onlyReadsMemory();
81}
Duncan Sands3353ed02007-12-18 09:59:50 +000082bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000083 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000084 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000085 else
Duncan Sands3353ed02007-12-18 09:59:50 +000086 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000087}
Duncan Sandsaa31b922007-12-19 21:13:37 +000088void CallSite::setDoesNotThrow(bool doesNotThrow) {
89 if (CallInst *CI = dyn_cast<CallInst>(I))
90 CI->setDoesNotThrow(doesNotThrow);
91 else
92 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
93}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000094
Gordon Henriksen14a55692007-12-10 02:14:30 +000095//===----------------------------------------------------------------------===//
96// TerminatorInst Class
97//===----------------------------------------------------------------------===//
98
99// Out of line virtual method, so the vtable, etc has a home.
100TerminatorInst::~TerminatorInst() {
101}
102
Gabor Greiff6caff662008-05-10 08:32:32 +0000103//===----------------------------------------------------------------------===//
104// UnaryInstruction Class
105//===----------------------------------------------------------------------===//
106
Gordon Henriksen14a55692007-12-10 02:14:30 +0000107// Out of line virtual method, so the vtable, etc has a home.
108UnaryInstruction::~UnaryInstruction() {
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111//===----------------------------------------------------------------------===//
112// PHINode Class
113//===----------------------------------------------------------------------===//
114
115PHINode::PHINode(const PHINode &PN)
116 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000117 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118 ReservedSpace(PN.getNumOperands()) {
119 Use *OL = OperandList;
120 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
121 OL[i].init(PN.getOperand(i), this);
122 OL[i+1].init(PN.getOperand(i+1), this);
123 }
124}
125
Gordon Henriksen14a55692007-12-10 02:14:30 +0000126PHINode::~PHINode() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000127 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128}
129
130// removeIncomingValue - Remove an incoming value. This is useful if a
131// predecessor basic block is deleted.
132Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
133 unsigned NumOps = getNumOperands();
134 Use *OL = OperandList;
135 assert(Idx*2 < NumOps && "BB not in PHI node!");
136 Value *Removed = OL[Idx*2];
137
138 // Move everything after this operand down.
139 //
140 // FIXME: we could just swap with the end of the list, then erase. However,
141 // client might not expect this to happen. The code as it is thrashes the
142 // use/def lists, which is kinda lame.
143 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
144 OL[i-2] = OL[i];
145 OL[i-2+1] = OL[i+1];
146 }
147
148 // Nuke the last value.
149 OL[NumOps-2].set(0);
150 OL[NumOps-2+1].set(0);
151 NumOperands = NumOps-2;
152
153 // If the PHI node is dead, because it has zero entries, nuke it now.
154 if (NumOps == 2 && DeletePHIIfEmpty) {
155 // If anyone is using this PHI, make them use a dummy value instead...
156 replaceAllUsesWith(UndefValue::get(getType()));
157 eraseFromParent();
158 }
159 return Removed;
160}
161
162/// resizeOperands - resize operands - This adjusts the length of the operands
163/// list according to the following behavior:
164/// 1. If NumOps == 0, grow the operand list in response to a push_back style
165/// of operation. This grows the number of ops by 1.5 times.
166/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
167/// 3. If NumOps == NumOperands, trim the reserved space.
168///
169void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000170 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000171 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000172 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000173 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
174 } else if (NumOps*2 > NumOperands) {
175 // No resize needed.
176 if (ReservedSpace >= NumOps) return;
177 } else if (NumOps == NumOperands) {
178 if (ReservedSpace == NumOps) return;
179 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000180 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181 }
182
183 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000184 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000185 Use *NewOps = allocHungoffUses(NumOps);
186 for (unsigned i = 0; i != e; ++i) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187 NewOps[i].init(OldOps[i], this);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000189 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000190 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191}
192
Nate Begemanb3923212005-08-04 23:24:19 +0000193/// hasConstantValue - If the specified PHI node always merges together the same
194/// value, return the value, otherwise return null.
195///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000196Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000197 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000198 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000199 if (getIncomingValue(0) != this) // not X = phi X
200 return getIncomingValue(0);
201 else
202 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000203 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000204
Nate Begemanb3923212005-08-04 23:24:19 +0000205 // Otherwise if all of the incoming values are the same for the PHI, replace
206 // the PHI node with the incoming value.
207 //
208 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000209 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000210 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000211 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000212 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000213 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000214 if (InVal && getIncomingValue(i) != InVal)
215 return 0; // Not the same, bail out.
216 else
217 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000218 }
Nate Begemanb3923212005-08-04 23:24:19 +0000219
220 // The only case that could cause InVal to be null is if we have a PHI node
221 // that only has entries for itself. In this case, there is no entry into the
222 // loop, so kill the PHI.
223 //
224 if (InVal == 0) InVal = UndefValue::get(getType());
225
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000226 // If we have a PHI node like phi(X, undef, X), where X is defined by some
227 // instruction, we cannot always return X as the result of the PHI node. Only
228 // do this if X is not an instruction (thus it must dominate the PHI block),
229 // or if the client is prepared to deal with this possibility.
230 if (HasUndefInput && !AllowNonDominatingInstruction)
231 if (Instruction *IV = dyn_cast<Instruction>(InVal))
232 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000233 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000234 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000235 return 0; // Cannot guarantee that InVal dominates this PHINode.
236
Nate Begemanb3923212005-08-04 23:24:19 +0000237 // All of the incoming values are the same, return the value now.
238 return InVal;
239}
240
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241
242//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243// CallInst Implementation
244//===----------------------------------------------------------------------===//
245
Gordon Henriksen14a55692007-12-10 02:14:30 +0000246CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000247}
248
Chris Lattner054ba2c2007-02-13 00:58:44 +0000249void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000250 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
251 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000252 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000256 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257
Chris Lattner054ba2c2007-02-13 00:58:44 +0000258 assert((NumParams == FTy->getNumParams() ||
259 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000260 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000261 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000262 assert((i >= FTy->getNumParams() ||
263 FTy->getParamType(i) == Params[i]->getType()) &&
264 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000265 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000266 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267}
268
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000269void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000270 assert(NumOperands == 3 && "NumOperands not set up?");
271 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000272 OL[0].init(Func, this);
273 OL[1].init(Actual1, this);
274 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275
276 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000278 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000280 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000281 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000283 assert((0 >= FTy->getNumParams() ||
284 FTy->getParamType(0) == Actual1->getType()) &&
285 "Calling a function with a bad signature!");
286 assert((1 >= FTy->getNumParams() ||
287 FTy->getParamType(1) == Actual2->getType()) &&
288 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289}
290
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000292 assert(NumOperands == 2 && "NumOperands not set up?");
293 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000294 OL[0].init(Func, this);
295 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000296
297 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000299 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301 assert((FTy->getNumParams() == 1 ||
302 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000304 assert((0 == FTy->getNumParams() ||
305 FTy->getParamType(0) == Actual->getType()) &&
306 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000309void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000310 assert(NumOperands == 1 && "NumOperands not set up?");
311 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000312 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000313
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000314 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000316 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000318 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000322 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
324 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 Instruction::Call,
326 OperandTraits<CallInst>::op_end(this) - 2,
327 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000329 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330}
331
332CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000336 Instruction::Call,
337 OperandTraits<CallInst>::op_end(this) - 2,
338 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342CallInst::CallInst(Value *Func, const std::string &Name,
343 Instruction *InsertBefore)
344 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
345 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 Instruction::Call,
347 OperandTraits<CallInst>::op_end(this) - 1,
348 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000350 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
353CallInst::CallInst(Value *Func, const std::string &Name,
354 BasicBlock *InsertAtEnd)
355 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
356 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 Instruction::Call,
358 OperandTraits<CallInst>::op_end(this) - 1,
359 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000361 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362}
363
Misha Brukmanb1c93172005-04-21 23:48:37 +0000364CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000365 : Instruction(CI.getType(), Instruction::Call,
366 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000367 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000368 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000369 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000370 Use *OL = OperandList;
371 Use *InOL = CI.OperandList;
372 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
373 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374}
375
Eric Christopher901b1a72008-05-16 20:39:43 +0000376void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
377 PAListPtr PAL = getParamAttrs();
378 PAL = PAL.addAttr(i, attr);
379 setParamAttrs(PAL);
380}
381
Chris Lattnerc994c022008-03-13 05:00:21 +0000382bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000383 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000384 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000385 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000386 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000387 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000388}
389
Duncan Sandsaa31b922007-12-19 21:13:37 +0000390void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000391 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000392 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000393 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000394 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000395 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000396 setParamAttrs(PAL);
397}
398
Duncan Sands5208d1a2007-11-28 17:07:01 +0000399
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000400//===----------------------------------------------------------------------===//
401// InvokeInst Implementation
402//===----------------------------------------------------------------------===//
403
404void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000405 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000406 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
407 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000408 OL[0].init(Fn, this);
409 OL[1].init(IfNormal, this);
410 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000411 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000412 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000413 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000414
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000415 assert(((NumArgs == FTy->getNumParams()) ||
416 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000417 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000418
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000419 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000420 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000421 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000422 "Invoking a function with a bad signature!");
423
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000424 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000425 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426}
427
Misha Brukmanb1c93172005-04-21 23:48:37 +0000428InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000429 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000430 OperandTraits<InvokeInst>::op_end(this)
431 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000432 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000433 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000434 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000435 Use *OL = OperandList, *InOL = II.OperandList;
436 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
437 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438}
439
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000440BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
441 return getSuccessor(idx);
442}
443unsigned InvokeInst::getNumSuccessorsV() const {
444 return getNumSuccessors();
445}
446void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
447 return setSuccessor(idx, B);
448}
449
Chris Lattnerc994c022008-03-13 05:00:21 +0000450bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000451 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000452 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000453 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000454 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000455 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000456}
457
Eric Christopher901b1a72008-05-16 20:39:43 +0000458void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
459 PAListPtr PAL = getParamAttrs();
460 PAL = PAL.addAttr(i, attr);
461 setParamAttrs(PAL);
462}
463
Duncan Sandsaa31b922007-12-19 21:13:37 +0000464void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000465 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000466 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000467 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000468 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000469 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000470 setParamAttrs(PAL);
471}
472
Duncan Sands5208d1a2007-11-28 17:07:01 +0000473
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000474//===----------------------------------------------------------------------===//
475// ReturnInst Implementation
476//===----------------------------------------------------------------------===//
477
Chris Lattner2195fc42007-02-24 00:55:48 +0000478ReturnInst::ReturnInst(const ReturnInst &RI)
479 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000480 OperandTraits<ReturnInst>::op_end(this)
481 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000482 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000483 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000484 if (N == 1)
485 Op<0>().init(RI.Op<0>(), this);
Devang Patelae682fb2008-02-26 17:56:20 +0000486 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000487 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000488 for (unsigned i = 0; i < N; ++i)
489 OL[i].init(RI.getOperand(i), this);
490 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000491}
492
493ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000494 : TerminatorInst(Type::VoidTy, Instruction::Ret,
495 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
496 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000497 if (retVal)
498 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000499}
500ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000501 : TerminatorInst(Type::VoidTy, Instruction::Ret,
502 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
503 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000504 if (retVal)
505 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000506}
507ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000508 : TerminatorInst(Type::VoidTy, Instruction::Ret,
509 OperandTraits<ReturnInst>::op_end(this),
510 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000511}
512
Devang Patela736c002008-02-26 19:38:17 +0000513ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
514 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000515 : TerminatorInst(Type::VoidTy, Instruction::Ret,
516 OperandTraits<ReturnInst>::op_end(this) - N,
517 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000518 if (N != 0)
519 init(retVals, N);
520}
521ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
522 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000523 : TerminatorInst(Type::VoidTy, Instruction::Ret,
524 OperandTraits<ReturnInst>::op_end(this) - N,
525 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000526 if (N != 0)
527 init(retVals, N);
528}
529
Devang Patel060e79c2008-02-26 19:15:26 +0000530void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000531 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000532
Devang Patelc38eb522008-02-26 18:49:29 +0000533 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000534 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000535 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000536 if (V->getType() == Type::VoidTy)
537 return;
Gabor Greiff6caff662008-05-10 08:32:32 +0000538 Op<0>().init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000539 return;
Devang Patel59643e52008-02-23 00:35:18 +0000540 }
541
Gabor Greiff6caff662008-05-10 08:32:32 +0000542 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000543 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000544 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000545 assert(!isa<BasicBlock>(V) &&
546 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000547 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000548 }
549}
550
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000551unsigned ReturnInst::getNumSuccessorsV() const {
552 return getNumSuccessors();
553}
554
Devang Patelae682fb2008-02-26 17:56:20 +0000555/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
556/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000557void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000558 assert(0 && "ReturnInst has no successors!");
559}
560
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000561BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
562 assert(0 && "ReturnInst has no successors!");
563 abort();
564 return 0;
565}
566
Devang Patel59643e52008-02-23 00:35:18 +0000567ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000568}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570//===----------------------------------------------------------------------===//
571// UnwindInst Implementation
572//===----------------------------------------------------------------------===//
573
Chris Lattner2195fc42007-02-24 00:55:48 +0000574UnwindInst::UnwindInst(Instruction *InsertBefore)
575 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
576}
577UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
578 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
579}
580
581
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582unsigned UnwindInst::getNumSuccessorsV() const {
583 return getNumSuccessors();
584}
585
586void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587 assert(0 && "UnwindInst has no successors!");
588}
589
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
591 assert(0 && "UnwindInst has no successors!");
592 abort();
593 return 0;
594}
595
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000596//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000597// UnreachableInst Implementation
598//===----------------------------------------------------------------------===//
599
Chris Lattner2195fc42007-02-24 00:55:48 +0000600UnreachableInst::UnreachableInst(Instruction *InsertBefore)
601 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
602}
603UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
604 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
605}
606
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607unsigned UnreachableInst::getNumSuccessorsV() const {
608 return getNumSuccessors();
609}
610
611void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
612 assert(0 && "UnwindInst has no successors!");
613}
614
615BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
616 assert(0 && "UnwindInst has no successors!");
617 abort();
618 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000619}
620
621//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000622// BranchInst Implementation
623//===----------------------------------------------------------------------===//
624
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625void BranchInst::AssertOK() {
626 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000627 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629}
630
Chris Lattner2195fc42007-02-24 00:55:48 +0000631BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000632 : TerminatorInst(Type::VoidTy, Instruction::Br,
633 OperandTraits<BranchInst>::op_end(this) - 1,
634 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000635 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greiff6caff662008-05-10 08:32:32 +0000636 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000637}
638BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
639 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000640 : TerminatorInst(Type::VoidTy, Instruction::Br,
641 OperandTraits<BranchInst>::op_end(this) - 3,
642 3, InsertBefore) {
643 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
644 Op<1>().init(reinterpret_cast<Value*>(IfFalse), this);
645 Op<2>().init(Cond, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000646#ifndef NDEBUG
647 AssertOK();
648#endif
649}
650
651BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 : TerminatorInst(Type::VoidTy, Instruction::Br,
653 OperandTraits<BranchInst>::op_end(this) - 1,
654 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000655 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greiff6caff662008-05-10 08:32:32 +0000656 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000657}
658
659BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
660 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000661 : TerminatorInst(Type::VoidTy, Instruction::Br,
662 OperandTraits<BranchInst>::op_end(this) - 3,
663 3, InsertAtEnd) {
664 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
665 Op<1>().init(reinterpret_cast<Value*>(IfFalse), this);
666 Op<2>().init(Cond, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000667#ifndef NDEBUG
668 AssertOK();
669#endif
670}
671
672
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000674 TerminatorInst(Type::VoidTy, Instruction::Br,
675 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
676 BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677 OperandList[0].init(BI.getOperand(0), this);
678 if (BI.getNumOperands() != 1) {
679 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
680 OperandList[1].init(BI.getOperand(1), this);
681 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682 }
683}
684
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
686 return getSuccessor(idx);
687}
688unsigned BranchInst::getNumSuccessorsV() const {
689 return getNumSuccessors();
690}
691void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
692 setSuccessor(idx, B);
693}
694
695
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000696//===----------------------------------------------------------------------===//
697// AllocationInst Implementation
698//===----------------------------------------------------------------------===//
699
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700static Value *getAISize(Value *Amt) {
701 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000702 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000703 else {
704 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000705 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000706 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000707 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000708 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000709 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000710}
711
Misha Brukmanb1c93172005-04-21 23:48:37 +0000712AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000713 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000715 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000716 InsertBefore) {
717 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000718 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000719 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720}
721
Misha Brukmanb1c93172005-04-21 23:48:37 +0000722AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000723 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000725 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000726 InsertAtEnd) {
727 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000728 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000729 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000730}
731
Gordon Henriksen14a55692007-12-10 02:14:30 +0000732// Out of line virtual method, so the vtable, etc has a home.
733AllocationInst::~AllocationInst() {
734}
735
Dan Gohmanaa583d72008-03-24 16:55:58 +0000736void AllocationInst::setAlignment(unsigned Align) {
737 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
738 SubclassData = Log2_32(Align) + 1;
739 assert(getAlignment() == Align && "Alignment representation error!");
740}
741
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000743 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
744 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
748const Type *AllocationInst::getAllocatedType() const {
749 return getType()->getElementType();
750}
751
752AllocaInst::AllocaInst(const AllocaInst &AI)
753 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000754 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755}
756
757MallocInst::MallocInst(const MallocInst &MI)
758 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000759 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760}
761
762//===----------------------------------------------------------------------===//
763// FreeInst Implementation
764//===----------------------------------------------------------------------===//
765
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766void FreeInst::AssertOK() {
767 assert(isa<PointerType>(getOperand(0)->getType()) &&
768 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769}
770
771FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000772 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000773 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774}
775
776FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000777 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779}
780
781
782//===----------------------------------------------------------------------===//
783// LoadInst Implementation
784//===----------------------------------------------------------------------===//
785
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000786void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000787 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789}
790
791LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000793 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000794 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000795 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000797 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798}
799
800LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000802 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000803 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000804 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000806 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807}
808
809LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
810 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000812 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000813 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000814 setAlignment(0);
815 AssertOK();
816 setName(Name);
817}
818
819LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
820 unsigned Align, Instruction *InsertBef)
821 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
822 Load, Ptr, InsertBef) {
823 setVolatile(isVolatile);
824 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000826 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827}
828
Dan Gohman68659282007-07-18 20:51:11 +0000829LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
830 unsigned Align, BasicBlock *InsertAE)
831 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
832 Load, Ptr, InsertAE) {
833 setVolatile(isVolatile);
834 setAlignment(Align);
835 AssertOK();
836 setName(Name);
837}
838
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000839LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
840 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000842 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000843 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000844 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000845 AssertOK();
846 setName(Name);
847}
848
849
850
851LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
853 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000854 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000855 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000856 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000857 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000858}
859
860LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000861 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
862 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000863 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000864 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000865 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000867}
868
869LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
870 Instruction *InsertBef)
871: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000873 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000874 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000875 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000876 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000877}
878
879LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
880 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000881 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
882 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000883 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000884 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000885 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000886 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887}
888
Christopher Lamb84485702007-04-22 19:24:39 +0000889void LoadInst::setAlignment(unsigned Align) {
890 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
891 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
892}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893
894//===----------------------------------------------------------------------===//
895// StoreInst Implementation
896//===----------------------------------------------------------------------===//
897
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000898void StoreInst::AssertOK() {
899 assert(isa<PointerType>(getOperand(1)->getType()) &&
900 "Ptr must have pointer type!");
901 assert(getOperand(0)->getType() ==
902 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000903 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906
907StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000908 : Instruction(Type::VoidTy, Store,
909 OperandTraits<StoreInst>::op_begin(this),
910 OperandTraits<StoreInst>::operands(this),
911 InsertBefore) {
912 Op<0>().init(val, this);
913 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000914 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000915 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 AssertOK();
917}
918
919StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000920 : Instruction(Type::VoidTy, Store,
921 OperandTraits<StoreInst>::op_begin(this),
922 OperandTraits<StoreInst>::operands(this),
923 InsertAtEnd) {
924 Op<0>().init(val, this);
925 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000926 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000927 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928 AssertOK();
929}
930
Misha Brukmanb1c93172005-04-21 23:48:37 +0000931StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000933 : Instruction(Type::VoidTy, Store,
934 OperandTraits<StoreInst>::op_begin(this),
935 OperandTraits<StoreInst>::operands(this),
936 InsertBefore) {
937 Op<0>().init(val, this);
938 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000939 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000940 setAlignment(0);
941 AssertOK();
942}
943
944StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
945 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000946 : Instruction(Type::VoidTy, Store,
947 OperandTraits<StoreInst>::op_begin(this),
948 OperandTraits<StoreInst>::operands(this),
949 InsertBefore) {
950 Op<0>().init(val, this);
951 Op<1>().init(addr, this);
Christopher Lamb84485702007-04-22 19:24:39 +0000952 setVolatile(isVolatile);
953 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000954 AssertOK();
955}
956
Misha Brukmanb1c93172005-04-21 23:48:37 +0000957StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000958 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000959 : Instruction(Type::VoidTy, Store,
960 OperandTraits<StoreInst>::op_begin(this),
961 OperandTraits<StoreInst>::operands(this),
962 InsertAtEnd) {
963 Op<0>().init(val, this);
964 Op<1>().init(addr, this);
Dan Gohman68659282007-07-18 20:51:11 +0000965 setVolatile(isVolatile);
966 setAlignment(Align);
967 AssertOK();
968}
969
970StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000971 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000972 : Instruction(Type::VoidTy, Store,
973 OperandTraits<StoreInst>::op_begin(this),
974 OperandTraits<StoreInst>::operands(this),
975 InsertAtEnd) {
976 Op<0>().init(val, this);
977 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000978 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000979 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000980 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981}
982
Christopher Lamb84485702007-04-22 19:24:39 +0000983void StoreInst::setAlignment(unsigned Align) {
984 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
985 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
986}
987
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988//===----------------------------------------------------------------------===//
989// GetElementPtrInst Implementation
990//===----------------------------------------------------------------------===//
991
Christopher Lambedf07882007-12-17 01:12:55 +0000992static unsigned retrieveAddrSpace(const Value *Val) {
993 return cast<PointerType>(Val->getType())->getAddressSpace();
994}
995
Chris Lattner79807c3d2007-01-31 19:47:18 +0000996void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000997 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
998 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000
Chris Lattner79807c3d2007-01-31 19:47:18 +00001001 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001003}
1004
Chris Lattner82981202005-05-03 05:43:30 +00001005void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001006 assert(NumOperands == 2 && "NumOperands not initialized?");
1007 Use *OL = OperandList;
Chris Lattner82981202005-05-03 05:43:30 +00001008 OL[0].init(Ptr, this);
1009 OL[1].init(Idx, this);
1010}
1011
Gabor Greiff6caff662008-05-10 08:32:32 +00001012GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1013 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001014 OperandTraits<GetElementPtrInst>::op_end(this)
1015 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001016 GEPI.getNumOperands()) {
1017 Use *OL = OperandList;
1018 Use *GEPIOL = GEPI.OperandList;
1019 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1020 OL[i].init(GEPIOL[i], this);
1021}
1022
Chris Lattner82981202005-05-03 05:43:30 +00001023GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1024 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001025 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001026 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001027 GetElementPtr,
1028 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1029 2, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001030 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001031 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001032}
1033
1034GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1035 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001036 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001037 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001038 GetElementPtr,
1039 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1040 2, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001041 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001042 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001043}
1044
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045// getIndexedType - Returns the type of the element that would be loaded with
1046// a load instruction with the specified parameters.
1047//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001048// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001049// pointer type.
1050//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001051const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001052 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001053 unsigned NumIdx) {
1054 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1055 if (!PTy) return 0; // Type isn't a pointer type!
1056 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057
1058 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001059 if (NumIdx == 0)
1060 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001061
Dan Gohman12fce772008-05-15 19:50:34 +00001062 return ExtractValueInst::getIndexedType(Agg, Idxs+1, Idxs+NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001063}
1064
Chris Lattner82981202005-05-03 05:43:30 +00001065const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1066 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1067 if (!PTy) return 0; // Type isn't a pointer type!
1068
1069 // Check the pointer index.
1070 if (!PTy->indexValid(Idx)) return 0;
1071
Chris Lattnerc2233332005-05-03 16:44:45 +00001072 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001073}
1074
Chris Lattner45f15572007-04-14 00:12:57 +00001075
1076/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1077/// zeros. If so, the result pointer and the first operand have the same
1078/// value, just potentially different types.
1079bool GetElementPtrInst::hasAllZeroIndices() const {
1080 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1081 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1082 if (!CI->isZero()) return false;
1083 } else {
1084 return false;
1085 }
1086 }
1087 return true;
1088}
1089
Chris Lattner27058292007-04-27 20:35:56 +00001090/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1091/// constant integers. If so, the result pointer and the first operand have
1092/// a constant offset between them.
1093bool GetElementPtrInst::hasAllConstantIndices() const {
1094 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1095 if (!isa<ConstantInt>(getOperand(i)))
1096 return false;
1097 }
1098 return true;
1099}
1100
Chris Lattner45f15572007-04-14 00:12:57 +00001101
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001102//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001103// ExtractElementInst Implementation
1104//===----------------------------------------------------------------------===//
1105
1106ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001107 const std::string &Name,
1108 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001109 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001110 ExtractElement,
1111 OperandTraits<ExtractElementInst>::op_begin(this),
1112 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001113 assert(isValidOperands(Val, Index) &&
1114 "Invalid extractelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001115 Op<0>().init(Val, this);
1116 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001117 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001118}
1119
Chris Lattner65511ff2006-10-05 06:24:58 +00001120ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1121 const std::string &Name,
1122 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001123 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001124 ExtractElement,
1125 OperandTraits<ExtractElementInst>::op_begin(this),
1126 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001127 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001128 assert(isValidOperands(Val, Index) &&
1129 "Invalid extractelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001130 Op<0>().init(Val, this);
1131 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001132 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001133}
1134
1135
Robert Bocchino23004482006-01-10 19:05:34 +00001136ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001137 const std::string &Name,
1138 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001139 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001140 ExtractElement,
1141 OperandTraits<ExtractElementInst>::op_begin(this),
1142 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001143 assert(isValidOperands(Val, Index) &&
1144 "Invalid extractelement instruction operands!");
1145
Gabor Greiff6caff662008-05-10 08:32:32 +00001146 Op<0>().init(Val, this);
1147 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001148 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001149}
1150
Chris Lattner65511ff2006-10-05 06:24:58 +00001151ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1152 const std::string &Name,
1153 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001154 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001155 ExtractElement,
1156 OperandTraits<ExtractElementInst>::op_begin(this),
1157 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001158 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001159 assert(isValidOperands(Val, Index) &&
1160 "Invalid extractelement instruction operands!");
1161
Gabor Greiff6caff662008-05-10 08:32:32 +00001162 Op<0>().init(Val, this);
1163 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001164 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001165}
1166
1167
Chris Lattner54865b32006-04-08 04:05:48 +00001168bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001169 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001170 return false;
1171 return true;
1172}
1173
1174
Robert Bocchino23004482006-01-10 19:05:34 +00001175//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001176// InsertElementInst Implementation
1177//===----------------------------------------------------------------------===//
1178
Chris Lattner0875d942006-04-14 22:20:32 +00001179InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001180 : Instruction(IE.getType(), InsertElement,
1181 OperandTraits<InsertElementInst>::op_begin(this), 3) {
1182 Op<0>().init(IE.Op<0>(), this);
1183 Op<1>().init(IE.Op<1>(), this);
1184 Op<2>().init(IE.Op<2>(), this);
Chris Lattner0875d942006-04-14 22:20:32 +00001185}
Chris Lattner54865b32006-04-08 04:05:48 +00001186InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001187 const std::string &Name,
1188 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001189 : Instruction(Vec->getType(), InsertElement,
1190 OperandTraits<InsertElementInst>::op_begin(this),
1191 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001192 assert(isValidOperands(Vec, Elt, Index) &&
1193 "Invalid insertelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001194 Op<0>().init(Vec, this);
1195 Op<1>().init(Elt, this);
1196 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001197 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001198}
1199
Chris Lattner65511ff2006-10-05 06:24:58 +00001200InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1201 const std::string &Name,
1202 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001203 : Instruction(Vec->getType(), InsertElement,
1204 OperandTraits<InsertElementInst>::op_begin(this),
1205 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001206 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001207 assert(isValidOperands(Vec, Elt, Index) &&
1208 "Invalid insertelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001209 Op<0>().init(Vec, this);
1210 Op<1>().init(Elt, this);
1211 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001212 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001213}
1214
1215
Chris Lattner54865b32006-04-08 04:05:48 +00001216InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001217 const std::string &Name,
1218 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001219 : Instruction(Vec->getType(), InsertElement,
1220 OperandTraits<InsertElementInst>::op_begin(this),
1221 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001222 assert(isValidOperands(Vec, Elt, Index) &&
1223 "Invalid insertelement instruction operands!");
1224
Gabor Greiff6caff662008-05-10 08:32:32 +00001225 Op<0>().init(Vec, this);
1226 Op<1>().init(Elt, this);
1227 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001228 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001229}
1230
Chris Lattner65511ff2006-10-05 06:24:58 +00001231InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1232 const std::string &Name,
1233 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001234: Instruction(Vec->getType(), InsertElement,
1235 OperandTraits<InsertElementInst>::op_begin(this),
1236 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001237 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001238 assert(isValidOperands(Vec, Elt, Index) &&
1239 "Invalid insertelement instruction operands!");
1240
Gabor Greiff6caff662008-05-10 08:32:32 +00001241 Op<0>().init(Vec, this);
1242 Op<1>().init(Elt, this);
1243 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001244 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001245}
1246
Chris Lattner54865b32006-04-08 04:05:48 +00001247bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1248 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001249 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001250 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001251
Reid Spencerd84d35b2007-02-15 02:26:10 +00001252 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001253 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001254
Reid Spencer8d9336d2006-12-31 05:26:44 +00001255 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001256 return false; // Third operand of insertelement must be uint.
1257 return true;
1258}
1259
1260
Robert Bocchinoca27f032006-01-17 20:07:22 +00001261//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001262// ShuffleVectorInst Implementation
1263//===----------------------------------------------------------------------===//
1264
Chris Lattner0875d942006-04-14 22:20:32 +00001265ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001266 : Instruction(SV.getType(), ShuffleVector,
1267 OperandTraits<ShuffleVectorInst>::op_begin(this),
1268 OperandTraits<ShuffleVectorInst>::operands(this)) {
1269 Op<0>().init(SV.Op<0>(), this);
1270 Op<1>().init(SV.Op<1>(), this);
1271 Op<2>().init(SV.Op<2>(), this);
Chris Lattner0875d942006-04-14 22:20:32 +00001272}
1273
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001274ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1275 const std::string &Name,
1276 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001277 : Instruction(V1->getType(), ShuffleVector,
1278 OperandTraits<ShuffleVectorInst>::op_begin(this),
1279 OperandTraits<ShuffleVectorInst>::operands(this),
1280 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001281 assert(isValidOperands(V1, V2, Mask) &&
1282 "Invalid shuffle vector instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001283 Op<0>().init(V1, this);
1284 Op<1>().init(V2, this);
1285 Op<2>().init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001286 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001287}
1288
1289ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1290 const std::string &Name,
1291 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001292 : Instruction(V1->getType(), ShuffleVector,
1293 OperandTraits<ShuffleVectorInst>::op_begin(this),
1294 OperandTraits<ShuffleVectorInst>::operands(this),
1295 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001296 assert(isValidOperands(V1, V2, Mask) &&
1297 "Invalid shuffle vector instruction operands!");
1298
Gabor Greiff6caff662008-05-10 08:32:32 +00001299 Op<0>().init(V1, this);
1300 Op<1>().init(V2, this);
1301 Op<2>().init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001302 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001303}
1304
1305bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1306 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001307 if (!isa<VectorType>(V1->getType()) ||
1308 V1->getType() != V2->getType())
1309 return false;
1310
1311 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1312 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1313 MaskTy->getElementType() != Type::Int32Ty ||
1314 MaskTy->getNumElements() !=
1315 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001316 return false;
1317 return true;
1318}
1319
Chris Lattnerf724e342008-03-02 05:28:33 +00001320/// getMaskValue - Return the index from the shuffle mask for the specified
1321/// output result. This is either -1 if the element is undef or a number less
1322/// than 2*numelements.
1323int ShuffleVectorInst::getMaskValue(unsigned i) const {
1324 const Constant *Mask = cast<Constant>(getOperand(2));
1325 if (isa<UndefValue>(Mask)) return -1;
1326 if (isa<ConstantAggregateZero>(Mask)) return 0;
1327 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1328 assert(i < MaskCV->getNumOperands() && "Index out of range");
1329
1330 if (isa<UndefValue>(MaskCV->getOperand(i)))
1331 return -1;
1332 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1333}
1334
Dan Gohman12fce772008-05-15 19:50:34 +00001335//===----------------------------------------------------------------------===//
1336// ExtractValueInst Class
1337//===----------------------------------------------------------------------===//
1338
1339// getIndexedType - Returns the type of the element that would be extracted
1340// with an extractvalue instruction with the specified parameters.
1341//
1342// A null type is returned if the indices are invalid for the specified
1343// pointer type.
1344//
1345const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1346 Value* const *Idxs,
1347 unsigned NumIdx) {
1348 unsigned CurIdx = 0;
1349 for (; CurIdx != NumIdx; ++CurIdx) {
1350 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman2f156ae2008-05-16 00:16:32 +00001351 if (!CT || isa<PointerType>(CT)) return 0;
Dan Gohman12fce772008-05-15 19:50:34 +00001352 Value *Index = Idxs[CurIdx];
1353 if (!CT->indexValid(Index)) return 0;
1354 Agg = CT->getTypeAtIndex(Index);
1355
1356 // If the new type forwards to another type, then it is in the middle
1357 // of being refined to another type (and hence, may have dropped all
1358 // references to what it was using before). So, use the new forwarded
1359 // type.
1360 if (const Type *Ty = Agg->getForwardedType())
1361 Agg = Ty;
1362 }
1363 return CurIdx == NumIdx ? Agg : 0;
1364}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001365
1366//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001367// BinaryOperator Class
1368//===----------------------------------------------------------------------===//
1369
Chris Lattner2195fc42007-02-24 00:55:48 +00001370BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1371 const Type *Ty, const std::string &Name,
1372 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001373 : Instruction(Ty, iType,
1374 OperandTraits<BinaryOperator>::op_begin(this),
1375 OperandTraits<BinaryOperator>::operands(this),
1376 InsertBefore) {
1377 Op<0>().init(S1, this);
1378 Op<1>().init(S2, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001379 init(iType);
1380 setName(Name);
1381}
1382
1383BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1384 const Type *Ty, const std::string &Name,
1385 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001386 : Instruction(Ty, iType,
1387 OperandTraits<BinaryOperator>::op_begin(this),
1388 OperandTraits<BinaryOperator>::operands(this),
1389 InsertAtEnd) {
1390 Op<0>().init(S1, this);
1391 Op<1>().init(S2, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001392 init(iType);
1393 setName(Name);
1394}
1395
1396
1397void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001398 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001399 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001400 assert(LHS->getType() == RHS->getType() &&
1401 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001402#ifndef NDEBUG
1403 switch (iType) {
1404 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001405 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001406 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001407 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001408 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001409 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001410 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001411 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001412 case UDiv:
1413 case SDiv:
1414 assert(getType() == LHS->getType() &&
1415 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001416 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1417 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001418 "Incorrect operand type (not integer) for S/UDIV");
1419 break;
1420 case FDiv:
1421 assert(getType() == LHS->getType() &&
1422 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001423 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1424 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001425 && "Incorrect operand type (not floating point) for FDIV");
1426 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001427 case URem:
1428 case SRem:
1429 assert(getType() == LHS->getType() &&
1430 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001431 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1432 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001433 "Incorrect operand type (not integer) for S/UREM");
1434 break;
1435 case FRem:
1436 assert(getType() == LHS->getType() &&
1437 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001438 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1439 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001440 && "Incorrect operand type (not floating point) for FREM");
1441 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001442 case Shl:
1443 case LShr:
1444 case AShr:
1445 assert(getType() == LHS->getType() &&
1446 "Shift operation should return same type as operands!");
1447 assert(getType()->isInteger() &&
1448 "Shift operation requires integer operands");
1449 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001450 case And: case Or:
1451 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001452 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001453 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001454 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001455 (isa<VectorType>(getType()) &&
1456 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001457 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001458 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001459 default:
1460 break;
1461 }
1462#endif
1463}
1464
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001465BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001466 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001467 Instruction *InsertBefore) {
1468 assert(S1->getType() == S2->getType() &&
1469 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001470 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001471}
1472
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001473BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001474 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001475 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001476 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001477 InsertAtEnd->getInstList().push_back(Res);
1478 return Res;
1479}
1480
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001481BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001482 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001483 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1484 return new BinaryOperator(Instruction::Sub,
1485 zero, Op,
1486 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001487}
1488
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001489BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001490 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001491 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1492 return new BinaryOperator(Instruction::Sub,
1493 zero, Op,
1494 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001495}
1496
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001497BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001498 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001499 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001500 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001501 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001502 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001503 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001504 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001505 }
1506
1507 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001508 Op->getType(), Name, InsertBefore);
1509}
1510
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001511BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001512 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001513 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001514 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001515 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001516 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001517 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001518 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001519 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001520 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001521 }
1522
1523 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001524 Op->getType(), Name, InsertAtEnd);
1525}
1526
1527
1528// isConstantAllOnes - Helper function for several functions below
1529static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001530 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1531 return CI->isAllOnesValue();
1532 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1533 return CV->isAllOnesValue();
1534 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001535}
1536
1537bool BinaryOperator::isNeg(const Value *V) {
1538 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1539 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001540 return Bop->getOperand(0) ==
1541 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001542 return false;
1543}
1544
1545bool BinaryOperator::isNot(const Value *V) {
1546 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1547 return (Bop->getOpcode() == Instruction::Xor &&
1548 (isConstantAllOnes(Bop->getOperand(1)) ||
1549 isConstantAllOnes(Bop->getOperand(0))));
1550 return false;
1551}
1552
Chris Lattner2c7d1772005-04-24 07:28:37 +00001553Value *BinaryOperator::getNegArgument(Value *BinOp) {
1554 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1555 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001556}
1557
Chris Lattner2c7d1772005-04-24 07:28:37 +00001558const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1559 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560}
1561
Chris Lattner2c7d1772005-04-24 07:28:37 +00001562Value *BinaryOperator::getNotArgument(Value *BinOp) {
1563 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1564 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1565 Value *Op0 = BO->getOperand(0);
1566 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001567 if (isConstantAllOnes(Op0)) return Op1;
1568
1569 assert(isConstantAllOnes(Op1));
1570 return Op0;
1571}
1572
Chris Lattner2c7d1772005-04-24 07:28:37 +00001573const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1574 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001575}
1576
1577
1578// swapOperands - Exchange the two operands to this instruction. This
1579// instruction is safe to use on any binary instruction and does not
1580// modify the semantics of the instruction. If the instruction is
1581// order dependent (SetLT f.e.) the opcode is changed.
1582//
1583bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001584 if (!isCommutative())
1585 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001586 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 return false;
1588}
1589
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001590//===----------------------------------------------------------------------===//
1591// CastInst Class
1592//===----------------------------------------------------------------------===//
1593
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001594// Just determine if this cast only deals with integral->integral conversion.
1595bool CastInst::isIntegerCast() const {
1596 switch (getOpcode()) {
1597 default: return false;
1598 case Instruction::ZExt:
1599 case Instruction::SExt:
1600 case Instruction::Trunc:
1601 return true;
1602 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001603 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001604 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001605}
1606
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001607bool CastInst::isLosslessCast() const {
1608 // Only BitCast can be lossless, exit fast if we're not BitCast
1609 if (getOpcode() != Instruction::BitCast)
1610 return false;
1611
1612 // Identity cast is always lossless
1613 const Type* SrcTy = getOperand(0)->getType();
1614 const Type* DstTy = getType();
1615 if (SrcTy == DstTy)
1616 return true;
1617
Reid Spencer8d9336d2006-12-31 05:26:44 +00001618 // Pointer to pointer is always lossless.
1619 if (isa<PointerType>(SrcTy))
1620 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001621 return false; // Other types have no identity values
1622}
1623
1624/// This function determines if the CastInst does not require any bits to be
1625/// changed in order to effect the cast. Essentially, it identifies cases where
1626/// no code gen is necessary for the cast, hence the name no-op cast. For
1627/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001628/// # bitcast i32* %x to i8*
1629/// # bitcast <2 x i32> %x to <4 x i16>
1630/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001631/// @brief Determine if a cast is a no-op.
1632bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1633 switch (getOpcode()) {
1634 default:
1635 assert(!"Invalid CastOp");
1636 case Instruction::Trunc:
1637 case Instruction::ZExt:
1638 case Instruction::SExt:
1639 case Instruction::FPTrunc:
1640 case Instruction::FPExt:
1641 case Instruction::UIToFP:
1642 case Instruction::SIToFP:
1643 case Instruction::FPToUI:
1644 case Instruction::FPToSI:
1645 return false; // These always modify bits
1646 case Instruction::BitCast:
1647 return true; // BitCast never modifies bits.
1648 case Instruction::PtrToInt:
1649 return IntPtrTy->getPrimitiveSizeInBits() ==
1650 getType()->getPrimitiveSizeInBits();
1651 case Instruction::IntToPtr:
1652 return IntPtrTy->getPrimitiveSizeInBits() ==
1653 getOperand(0)->getType()->getPrimitiveSizeInBits();
1654 }
1655}
1656
1657/// This function determines if a pair of casts can be eliminated and what
1658/// opcode should be used in the elimination. This assumes that there are two
1659/// instructions like this:
1660/// * %F = firstOpcode SrcTy %x to MidTy
1661/// * %S = secondOpcode MidTy %F to DstTy
1662/// The function returns a resultOpcode so these two casts can be replaced with:
1663/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1664/// If no such cast is permited, the function returns 0.
1665unsigned CastInst::isEliminableCastPair(
1666 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1667 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1668{
1669 // Define the 144 possibilities for these two cast instructions. The values
1670 // in this matrix determine what to do in a given situation and select the
1671 // case in the switch below. The rows correspond to firstOp, the columns
1672 // correspond to secondOp. In looking at the table below, keep in mind
1673 // the following cast properties:
1674 //
1675 // Size Compare Source Destination
1676 // Operator Src ? Size Type Sign Type Sign
1677 // -------- ------------ ------------------- ---------------------
1678 // TRUNC > Integer Any Integral Any
1679 // ZEXT < Integral Unsigned Integer Any
1680 // SEXT < Integral Signed Integer Any
1681 // FPTOUI n/a FloatPt n/a Integral Unsigned
1682 // FPTOSI n/a FloatPt n/a Integral Signed
1683 // UITOFP n/a Integral Unsigned FloatPt n/a
1684 // SITOFP n/a Integral Signed FloatPt n/a
1685 // FPTRUNC > FloatPt n/a FloatPt n/a
1686 // FPEXT < FloatPt n/a FloatPt n/a
1687 // PTRTOINT n/a Pointer n/a Integral Unsigned
1688 // INTTOPTR n/a Integral Unsigned Pointer n/a
1689 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001690 //
1691 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1692 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1693 // into "fptoui double to ulong", but this loses information about the range
1694 // of the produced value (we no longer know the top-part is all zeros).
1695 // Further this conversion is often much more expensive for typical hardware,
1696 // and causes issues when building libgcc. We disallow fptosi+sext for the
1697 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001698 const unsigned numCastOps =
1699 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1700 static const uint8_t CastResults[numCastOps][numCastOps] = {
1701 // T F F U S F F P I B -+
1702 // R Z S P P I I T P 2 N T |
1703 // U E E 2 2 2 2 R E I T C +- secondOp
1704 // N X X U S F F N X N 2 V |
1705 // C T T I I P P C T T P T -+
1706 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1707 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1708 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001709 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1710 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001711 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1712 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1713 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1714 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1715 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1716 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1717 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1718 };
1719
1720 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1721 [secondOp-Instruction::CastOpsBegin];
1722 switch (ElimCase) {
1723 case 0:
1724 // categorically disallowed
1725 return 0;
1726 case 1:
1727 // allowed, use first cast's opcode
1728 return firstOp;
1729 case 2:
1730 // allowed, use second cast's opcode
1731 return secondOp;
1732 case 3:
1733 // no-op cast in second op implies firstOp as long as the DestTy
1734 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001735 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001736 return firstOp;
1737 return 0;
1738 case 4:
1739 // no-op cast in second op implies firstOp as long as the DestTy
1740 // is floating point
1741 if (DstTy->isFloatingPoint())
1742 return firstOp;
1743 return 0;
1744 case 5:
1745 // no-op cast in first op implies secondOp as long as the SrcTy
1746 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001747 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001748 return secondOp;
1749 return 0;
1750 case 6:
1751 // no-op cast in first op implies secondOp as long as the SrcTy
1752 // is a floating point
1753 if (SrcTy->isFloatingPoint())
1754 return secondOp;
1755 return 0;
1756 case 7: {
1757 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1758 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1759 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1760 if (MidSize >= PtrSize)
1761 return Instruction::BitCast;
1762 return 0;
1763 }
1764 case 8: {
1765 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1766 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1767 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1768 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1769 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1770 if (SrcSize == DstSize)
1771 return Instruction::BitCast;
1772 else if (SrcSize < DstSize)
1773 return firstOp;
1774 return secondOp;
1775 }
1776 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1777 return Instruction::ZExt;
1778 case 10:
1779 // fpext followed by ftrunc is allowed if the bit size returned to is
1780 // the same as the original, in which case its just a bitcast
1781 if (SrcTy == DstTy)
1782 return Instruction::BitCast;
1783 return 0; // If the types are not the same we can't eliminate it.
1784 case 11:
1785 // bitcast followed by ptrtoint is allowed as long as the bitcast
1786 // is a pointer to pointer cast.
1787 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1788 return secondOp;
1789 return 0;
1790 case 12:
1791 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1792 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1793 return firstOp;
1794 return 0;
1795 case 13: {
1796 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1797 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1798 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1799 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1800 if (SrcSize <= PtrSize && SrcSize == DstSize)
1801 return Instruction::BitCast;
1802 return 0;
1803 }
1804 case 99:
1805 // cast combination can't happen (error in input). This is for all cases
1806 // where the MidTy is not the same for the two cast instructions.
1807 assert(!"Invalid Cast Combination");
1808 return 0;
1809 default:
1810 assert(!"Error in CastResults table!!!");
1811 return 0;
1812 }
1813 return 0;
1814}
1815
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001816CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817 const std::string &Name, Instruction *InsertBefore) {
1818 // Construct and return the appropriate CastInst subclass
1819 switch (op) {
1820 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1821 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1822 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1823 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1824 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1825 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1826 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1827 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1828 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1829 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1830 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1831 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1832 default:
1833 assert(!"Invalid opcode provided");
1834 }
1835 return 0;
1836}
1837
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001838CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001839 const std::string &Name, BasicBlock *InsertAtEnd) {
1840 // Construct and return the appropriate CastInst subclass
1841 switch (op) {
1842 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1843 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1844 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1845 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1846 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1847 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1848 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1849 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1850 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1851 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1852 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1853 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1854 default:
1855 assert(!"Invalid opcode provided");
1856 }
1857 return 0;
1858}
1859
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001860CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001861 const std::string &Name,
1862 Instruction *InsertBefore) {
1863 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001864 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1865 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001866}
1867
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001868CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001869 const std::string &Name,
1870 BasicBlock *InsertAtEnd) {
1871 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001872 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1873 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001874}
1875
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001876CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001877 const std::string &Name,
1878 Instruction *InsertBefore) {
1879 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001880 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1881 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001882}
1883
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001884CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001885 const std::string &Name,
1886 BasicBlock *InsertAtEnd) {
1887 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001888 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1889 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001890}
1891
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001892CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001893 const std::string &Name,
1894 Instruction *InsertBefore) {
1895 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001896 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1897 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001898}
1899
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001900CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001901 const std::string &Name,
1902 BasicBlock *InsertAtEnd) {
1903 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001904 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1905 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001906}
1907
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001908CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001909 const std::string &Name,
1910 BasicBlock *InsertAtEnd) {
1911 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001912 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001913 "Invalid cast");
1914
Chris Lattner03c49532007-01-15 02:27:26 +00001915 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001916 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1917 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001918}
1919
1920/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001921CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001922 const std::string &Name,
1923 Instruction *InsertBefore) {
1924 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001925 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001926 "Invalid cast");
1927
Chris Lattner03c49532007-01-15 02:27:26 +00001928 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001929 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1930 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001931}
1932
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001933CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001934 bool isSigned, const std::string &Name,
1935 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001936 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001937 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1938 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1939 Instruction::CastOps opcode =
1940 (SrcBits == DstBits ? Instruction::BitCast :
1941 (SrcBits > DstBits ? Instruction::Trunc :
1942 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001943 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00001944}
1945
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001946CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001947 bool isSigned, const std::string &Name,
1948 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001949 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001950 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1951 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1952 Instruction::CastOps opcode =
1953 (SrcBits == DstBits ? Instruction::BitCast :
1954 (SrcBits > DstBits ? Instruction::Trunc :
1955 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001956 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00001957}
1958
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001959CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001960 const std::string &Name,
1961 Instruction *InsertBefore) {
1962 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1963 "Invalid cast");
1964 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1965 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1966 Instruction::CastOps opcode =
1967 (SrcBits == DstBits ? Instruction::BitCast :
1968 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001969 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00001970}
1971
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001972CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001973 const std::string &Name,
1974 BasicBlock *InsertAtEnd) {
1975 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1976 "Invalid cast");
1977 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1978 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1979 Instruction::CastOps opcode =
1980 (SrcBits == DstBits ? Instruction::BitCast :
1981 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00001983}
1984
Duncan Sands55e50902008-01-06 10:12:28 +00001985// Check whether it is valid to call getCastOpcode for these types.
1986// This routine must be kept in sync with getCastOpcode.
1987bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1988 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1989 return false;
1990
1991 if (SrcTy == DestTy)
1992 return true;
1993
1994 // Get the bit sizes, we'll need these
1995 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1996 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1997
1998 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00001999 if (DestTy->isInteger()) { // Casting to integral
2000 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002001 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002002 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002003 return true;
2004 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002005 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002006 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002007 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002008 return isa<PointerType>(SrcTy);
2009 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002010 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2011 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002012 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002013 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002014 return true;
2015 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002016 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002017 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002018 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002019 return false;
2020 }
2021 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002022 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002023 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002024 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002025 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002026 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002027 return DestPTy->getBitWidth() == SrcBits;
2028 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002029 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2030 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002031 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002032 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002033 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002034 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002035 return false;
2036 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002037 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002038 return false;
2039 }
2040}
2041
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002042// Provide a way to get a "cast" where the cast opcode is inferred from the
2043// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002044// logic in the castIsValid function below. This axiom should hold:
2045// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2046// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002048// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002050CastInst::getCastOpcode(
2051 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002052 // Get the bit sizes, we'll need these
2053 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002054 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2055 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056
Duncan Sands55e50902008-01-06 10:12:28 +00002057 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2058 "Only first class types are castable!");
2059
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002061 if (DestTy->isInteger()) { // Casting to integral
2062 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 if (DestBits < SrcBits)
2064 return Trunc; // int -> smaller int
2065 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002066 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067 return SExt; // signed -> SEXT
2068 else
2069 return ZExt; // unsigned -> ZEXT
2070 } else {
2071 return BitCast; // Same size, No-op cast
2072 }
2073 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002074 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002075 return FPToSI; // FP -> sint
2076 else
2077 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002078 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002080 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002081 return BitCast; // Same size, no-op cast
2082 } else {
2083 assert(isa<PointerType>(SrcTy) &&
2084 "Casting from a value that is not first-class type");
2085 return PtrToInt; // ptr -> int
2086 }
2087 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002088 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002089 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090 return SIToFP; // sint -> FP
2091 else
2092 return UIToFP; // uint -> FP
2093 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2094 if (DestBits < SrcBits) {
2095 return FPTrunc; // FP -> smaller FP
2096 } else if (DestBits > SrcBits) {
2097 return FPExt; // FP -> larger FP
2098 } else {
2099 return BitCast; // same size, no-op cast
2100 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002101 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002103 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002104 return BitCast; // same size, no-op cast
2105 } else {
2106 assert(0 && "Casting pointer or non-first class to float");
2107 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002108 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2109 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002111 "Casting vector to vector of different widths");
2112 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002114 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002116 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002117 }
2118 } else if (isa<PointerType>(DestTy)) {
2119 if (isa<PointerType>(SrcTy)) {
2120 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002121 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122 return IntToPtr; // int -> ptr
2123 } else {
2124 assert(!"Casting pointer to other than pointer or int");
2125 }
2126 } else {
2127 assert(!"Casting to type that is not first-class");
2128 }
2129
2130 // If we fall through to here we probably hit an assertion cast above
2131 // and assertions are not turned on. Anything we return is an error, so
2132 // BitCast is as good a choice as any.
2133 return BitCast;
2134}
2135
2136//===----------------------------------------------------------------------===//
2137// CastInst SubClass Constructors
2138//===----------------------------------------------------------------------===//
2139
2140/// Check that the construction parameters for a CastInst are correct. This
2141/// could be broken out into the separate constructors but it is useful to have
2142/// it in one place and to eliminate the redundant code for getting the sizes
2143/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002144bool
2145CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146
2147 // Check for type sanity on the arguments
2148 const Type *SrcTy = S->getType();
2149 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2150 return false;
2151
2152 // Get the size of the types in bits, we'll need this later
2153 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2154 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2155
2156 // Switch on the opcode provided
2157 switch (op) {
2158 default: return false; // This is an input error
2159 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002160 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002162 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002164 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 case Instruction::FPTrunc:
2166 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2167 SrcBitSize > DstBitSize;
2168 case Instruction::FPExt:
2169 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2170 SrcBitSize < DstBitSize;
2171 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002173 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2174 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2175 return SVTy->getElementType()->isInteger() &&
2176 DVTy->getElementType()->isFloatingPoint() &&
2177 SVTy->getNumElements() == DVTy->getNumElements();
2178 }
2179 }
Chris Lattner03c49532007-01-15 02:27:26 +00002180 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002183 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2184 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2185 return SVTy->getElementType()->isFloatingPoint() &&
2186 DVTy->getElementType()->isInteger() &&
2187 SVTy->getNumElements() == DVTy->getNumElements();
2188 }
2189 }
Chris Lattner03c49532007-01-15 02:27:26 +00002190 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002192 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002194 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195 case Instruction::BitCast:
2196 // BitCast implies a no-op cast of type only. No bits change.
2197 // However, you can't cast pointers to anything but pointers.
2198 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2199 return false;
2200
Duncan Sands55e50902008-01-06 10:12:28 +00002201 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202 // these cases, the cast is okay if the source and destination bit widths
2203 // are identical.
2204 return SrcBitSize == DstBitSize;
2205 }
2206}
2207
2208TruncInst::TruncInst(
2209 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2210) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002211 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212}
2213
2214TruncInst::TruncInst(
2215 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2216) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218}
2219
2220ZExtInst::ZExtInst(
2221 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2222) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002223 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224}
2225
2226ZExtInst::ZExtInst(
2227 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2228) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002229 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230}
2231SExtInst::SExtInst(
2232 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2233) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235}
2236
Jeff Cohencc08c832006-12-02 02:22:01 +00002237SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2239) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241}
2242
2243FPTruncInst::FPTruncInst(
2244 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2245) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247}
2248
2249FPTruncInst::FPTruncInst(
2250 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2251) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253}
2254
2255FPExtInst::FPExtInst(
2256 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2257) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259}
2260
2261FPExtInst::FPExtInst(
2262 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2263) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002264 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265}
2266
2267UIToFPInst::UIToFPInst(
2268 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2269) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271}
2272
2273UIToFPInst::UIToFPInst(
2274 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2275) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277}
2278
2279SIToFPInst::SIToFPInst(
2280 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2281) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002282 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283}
2284
2285SIToFPInst::SIToFPInst(
2286 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2287) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002288 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289}
2290
2291FPToUIInst::FPToUIInst(
2292 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2293) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002294 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295}
2296
2297FPToUIInst::FPToUIInst(
2298 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2299) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002300 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301}
2302
2303FPToSIInst::FPToSIInst(
2304 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2305) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002306 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307}
2308
2309FPToSIInst::FPToSIInst(
2310 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2311) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002312 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313}
2314
2315PtrToIntInst::PtrToIntInst(
2316 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2317) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002318 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319}
2320
2321PtrToIntInst::PtrToIntInst(
2322 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2323) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002324 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325}
2326
2327IntToPtrInst::IntToPtrInst(
2328 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2329) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002330 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331}
2332
2333IntToPtrInst::IntToPtrInst(
2334 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2335) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002336 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337}
2338
2339BitCastInst::BitCastInst(
2340 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2341) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002342 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343}
2344
2345BitCastInst::BitCastInst(
2346 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2347) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002348 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002350
2351//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002352// CmpInst Classes
2353//===----------------------------------------------------------------------===//
2354
Nate Begemand2195702008-05-12 19:01:56 +00002355CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2356 Value *LHS, Value *RHS, const std::string &Name,
2357 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002358 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002359 OperandTraits<CmpInst>::op_begin(this),
2360 OperandTraits<CmpInst>::operands(this),
2361 InsertBefore) {
2362 Op<0>().init(LHS, this);
2363 Op<1>().init(RHS, this);
Reid Spencerd9436b62006-11-20 01:22:35 +00002364 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002365 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002366}
Gabor Greiff6caff662008-05-10 08:32:32 +00002367
Nate Begemand2195702008-05-12 19:01:56 +00002368CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2369 Value *LHS, Value *RHS, const std::string &Name,
2370 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002371 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002372 OperandTraits<CmpInst>::op_begin(this),
2373 OperandTraits<CmpInst>::operands(this),
2374 InsertAtEnd) {
2375 Op<0>().init(LHS, this);
2376 Op<1>().init(RHS, this);
Reid Spencerd9436b62006-11-20 01:22:35 +00002377 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002378 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002379}
2380
2381CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002382CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002383 const std::string &Name, Instruction *InsertBefore) {
2384 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002385 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002386 InsertBefore);
2387 }
Nate Begemand2195702008-05-12 19:01:56 +00002388 if (Op == Instruction::FCmp) {
2389 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2390 InsertBefore);
2391 }
2392 if (Op == Instruction::VICmp) {
2393 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2394 InsertBefore);
2395 }
2396 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2397 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002398}
2399
2400CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002401CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002402 const std::string &Name, BasicBlock *InsertAtEnd) {
2403 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002404 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002405 InsertAtEnd);
2406 }
Nate Begemand2195702008-05-12 19:01:56 +00002407 if (Op == Instruction::FCmp) {
2408 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2409 InsertAtEnd);
2410 }
2411 if (Op == Instruction::VICmp) {
2412 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2413 InsertAtEnd);
2414 }
2415 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2416 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002417}
2418
2419void CmpInst::swapOperands() {
2420 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2421 IC->swapOperands();
2422 else
2423 cast<FCmpInst>(this)->swapOperands();
2424}
2425
2426bool CmpInst::isCommutative() {
2427 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2428 return IC->isCommutative();
2429 return cast<FCmpInst>(this)->isCommutative();
2430}
2431
2432bool CmpInst::isEquality() {
2433 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2434 return IC->isEquality();
2435 return cast<FCmpInst>(this)->isEquality();
2436}
2437
2438
2439ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2440 switch (pred) {
2441 default:
2442 assert(!"Unknown icmp predicate!");
2443 case ICMP_EQ: return ICMP_NE;
2444 case ICMP_NE: return ICMP_EQ;
2445 case ICMP_UGT: return ICMP_ULE;
2446 case ICMP_ULT: return ICMP_UGE;
2447 case ICMP_UGE: return ICMP_ULT;
2448 case ICMP_ULE: return ICMP_UGT;
2449 case ICMP_SGT: return ICMP_SLE;
2450 case ICMP_SLT: return ICMP_SGE;
2451 case ICMP_SGE: return ICMP_SLT;
2452 case ICMP_SLE: return ICMP_SGT;
2453 }
2454}
2455
2456ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2457 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002458 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002459 case ICMP_EQ: case ICMP_NE:
2460 return pred;
2461 case ICMP_SGT: return ICMP_SLT;
2462 case ICMP_SLT: return ICMP_SGT;
2463 case ICMP_SGE: return ICMP_SLE;
2464 case ICMP_SLE: return ICMP_SGE;
2465 case ICMP_UGT: return ICMP_ULT;
2466 case ICMP_ULT: return ICMP_UGT;
2467 case ICMP_UGE: return ICMP_ULE;
2468 case ICMP_ULE: return ICMP_UGE;
2469 }
2470}
2471
Reid Spencer266e42b2006-12-23 06:05:41 +00002472ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2473 switch (pred) {
2474 default: assert(! "Unknown icmp predicate!");
2475 case ICMP_EQ: case ICMP_NE:
2476 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2477 return pred;
2478 case ICMP_UGT: return ICMP_SGT;
2479 case ICMP_ULT: return ICMP_SLT;
2480 case ICMP_UGE: return ICMP_SGE;
2481 case ICMP_ULE: return ICMP_SLE;
2482 }
2483}
2484
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002485ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2486 switch (pred) {
2487 default: assert(! "Unknown icmp predicate!");
2488 case ICMP_EQ: case ICMP_NE:
2489 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2490 return pred;
2491 case ICMP_SGT: return ICMP_UGT;
2492 case ICMP_SLT: return ICMP_ULT;
2493 case ICMP_SGE: return ICMP_UGE;
2494 case ICMP_SLE: return ICMP_ULE;
2495 }
2496}
2497
Reid Spencer266e42b2006-12-23 06:05:41 +00002498bool ICmpInst::isSignedPredicate(Predicate pred) {
2499 switch (pred) {
2500 default: assert(! "Unknown icmp predicate!");
2501 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2502 return true;
2503 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2504 case ICMP_UGE: case ICMP_ULE:
2505 return false;
2506 }
2507}
2508
Reid Spencer0286bc12007-02-28 22:00:54 +00002509/// Initialize a set of values that all satisfy the condition with C.
2510///
2511ConstantRange
2512ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2513 APInt Lower(C);
2514 APInt Upper(C);
2515 uint32_t BitWidth = C.getBitWidth();
2516 switch (pred) {
2517 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2518 case ICmpInst::ICMP_EQ: Upper++; break;
2519 case ICmpInst::ICMP_NE: Lower++; break;
2520 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2521 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2522 case ICmpInst::ICMP_UGT:
2523 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2524 break;
2525 case ICmpInst::ICMP_SGT:
2526 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2527 break;
2528 case ICmpInst::ICMP_ULE:
2529 Lower = APInt::getMinValue(BitWidth); Upper++;
2530 break;
2531 case ICmpInst::ICMP_SLE:
2532 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2533 break;
2534 case ICmpInst::ICMP_UGE:
2535 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2536 break;
2537 case ICmpInst::ICMP_SGE:
2538 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2539 break;
2540 }
2541 return ConstantRange(Lower, Upper);
2542}
2543
Reid Spencerd9436b62006-11-20 01:22:35 +00002544FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2545 switch (pred) {
2546 default:
2547 assert(!"Unknown icmp predicate!");
2548 case FCMP_OEQ: return FCMP_UNE;
2549 case FCMP_ONE: return FCMP_UEQ;
2550 case FCMP_OGT: return FCMP_ULE;
2551 case FCMP_OLT: return FCMP_UGE;
2552 case FCMP_OGE: return FCMP_ULT;
2553 case FCMP_OLE: return FCMP_UGT;
2554 case FCMP_UEQ: return FCMP_ONE;
2555 case FCMP_UNE: return FCMP_OEQ;
2556 case FCMP_UGT: return FCMP_OLE;
2557 case FCMP_ULT: return FCMP_OGE;
2558 case FCMP_UGE: return FCMP_OLT;
2559 case FCMP_ULE: return FCMP_OGT;
2560 case FCMP_ORD: return FCMP_UNO;
2561 case FCMP_UNO: return FCMP_ORD;
2562 case FCMP_TRUE: return FCMP_FALSE;
2563 case FCMP_FALSE: return FCMP_TRUE;
2564 }
2565}
2566
2567FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2568 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002569 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002570 case FCMP_FALSE: case FCMP_TRUE:
2571 case FCMP_OEQ: case FCMP_ONE:
2572 case FCMP_UEQ: case FCMP_UNE:
2573 case FCMP_ORD: case FCMP_UNO:
2574 return pred;
2575 case FCMP_OGT: return FCMP_OLT;
2576 case FCMP_OLT: return FCMP_OGT;
2577 case FCMP_OGE: return FCMP_OLE;
2578 case FCMP_OLE: return FCMP_OGE;
2579 case FCMP_UGT: return FCMP_ULT;
2580 case FCMP_ULT: return FCMP_UGT;
2581 case FCMP_UGE: return FCMP_ULE;
2582 case FCMP_ULE: return FCMP_UGE;
2583 }
2584}
2585
Reid Spencer266e42b2006-12-23 06:05:41 +00002586bool CmpInst::isUnsigned(unsigned short predicate) {
2587 switch (predicate) {
2588 default: return false;
2589 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2590 case ICmpInst::ICMP_UGE: return true;
2591 }
2592}
2593
2594bool CmpInst::isSigned(unsigned short predicate){
2595 switch (predicate) {
2596 default: return false;
2597 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2598 case ICmpInst::ICMP_SGE: return true;
2599 }
2600}
2601
2602bool CmpInst::isOrdered(unsigned short predicate) {
2603 switch (predicate) {
2604 default: return false;
2605 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2606 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2607 case FCmpInst::FCMP_ORD: return true;
2608 }
2609}
2610
2611bool CmpInst::isUnordered(unsigned short predicate) {
2612 switch (predicate) {
2613 default: return false;
2614 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2615 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2616 case FCmpInst::FCMP_UNO: return true;
2617 }
2618}
2619
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002620//===----------------------------------------------------------------------===//
2621// SwitchInst Implementation
2622//===----------------------------------------------------------------------===//
2623
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002624void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002625 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002626 ReservedSpace = 2+NumCases*2;
2627 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002628 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002629
2630 OperandList[0].init(Value, this);
2631 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002632}
2633
Chris Lattner2195fc42007-02-24 00:55:48 +00002634/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2635/// switch on and a default destination. The number of additional cases can
2636/// be specified here to make memory allocation more efficient. This
2637/// constructor can also autoinsert before another instruction.
2638SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2639 Instruction *InsertBefore)
2640 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2641 init(Value, Default, NumCases);
2642}
2643
2644/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2645/// switch on and a default destination. The number of additional cases can
2646/// be specified here to make memory allocation more efficient. This
2647/// constructor also autoinserts at the end of the specified BasicBlock.
2648SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2649 BasicBlock *InsertAtEnd)
2650 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2651 init(Value, Default, NumCases);
2652}
2653
Misha Brukmanb1c93172005-04-21 23:48:37 +00002654SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002655 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002656 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002657 Use *OL = OperandList, *InOL = SI.OperandList;
2658 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2659 OL[i].init(InOL[i], this);
2660 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002661 }
2662}
2663
Gordon Henriksen14a55692007-12-10 02:14:30 +00002664SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002665 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002666}
2667
2668
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002669/// addCase - Add an entry to the switch instruction...
2670///
Chris Lattner47ac1872005-02-24 05:32:09 +00002671void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002672 unsigned OpNo = NumOperands;
2673 if (OpNo+2 > ReservedSpace)
2674 resizeOperands(0); // Get more space!
2675 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002676 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002677 NumOperands = OpNo+2;
2678 OperandList[OpNo].init(OnVal, this);
2679 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002680}
2681
2682/// removeCase - This method removes the specified successor from the switch
2683/// instruction. Note that this cannot be used to remove the default
2684/// destination (successor #0).
2685///
2686void SwitchInst::removeCase(unsigned idx) {
2687 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002688 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2689
2690 unsigned NumOps = getNumOperands();
2691 Use *OL = OperandList;
2692
2693 // Move everything after this operand down.
2694 //
2695 // FIXME: we could just swap with the end of the list, then erase. However,
2696 // client might not expect this to happen. The code as it is thrashes the
2697 // use/def lists, which is kinda lame.
2698 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2699 OL[i-2] = OL[i];
2700 OL[i-2+1] = OL[i+1];
2701 }
2702
2703 // Nuke the last value.
2704 OL[NumOps-2].set(0);
2705 OL[NumOps-2+1].set(0);
2706 NumOperands = NumOps-2;
2707}
2708
2709/// resizeOperands - resize operands - This adjusts the length of the operands
2710/// list according to the following behavior:
2711/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002712/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002713/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2714/// 3. If NumOps == NumOperands, trim the reserved space.
2715///
2716void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002717 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002718 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002719 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002720 } else if (NumOps*2 > NumOperands) {
2721 // No resize needed.
2722 if (ReservedSpace >= NumOps) return;
2723 } else if (NumOps == NumOperands) {
2724 if (ReservedSpace == NumOps) return;
2725 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002726 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002727 }
2728
2729 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002730 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002731 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002732 for (unsigned i = 0; i != e; ++i) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002733 NewOps[i].init(OldOps[i], this);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002734 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002735 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002736 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002737}
2738
2739
2740BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2741 return getSuccessor(idx);
2742}
2743unsigned SwitchInst::getNumSuccessorsV() const {
2744 return getNumSuccessors();
2745}
2746void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2747 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002748}
Chris Lattnerf22be932004-10-15 23:52:53 +00002749
Devang Patel295711f2008-02-19 22:15:16 +00002750//===----------------------------------------------------------------------===//
2751// GetResultInst Implementation
2752//===----------------------------------------------------------------------===//
2753
Devang Patel666d4512008-02-20 19:10:47 +00002754GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002755 const std::string &Name,
2756 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002757 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2758 ->getElementType(Index),
2759 GetResult, Aggregate, InsertBef),
2760 Idx(Index) {
2761 assert(isValidOperands(Aggregate, Index)
2762 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002763 setName(Name);
2764}
2765
Devang Patel666d4512008-02-20 19:10:47 +00002766bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2767 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002768 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002769
Devang Patelcf193b82008-02-20 19:39:41 +00002770 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2771 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002772 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002773 return false;
2774
2775 // getresult aggregate value's element types are restricted to
2776 // avoid nested aggregates.
2777 for (unsigned i = 0; i < NumElements; ++i)
2778 if (!STy->getElementType(i)->isFirstClassType())
2779 return false;
2780
2781 // Otherwise, Aggregate is valid.
2782 return true;
2783 }
Devang Patel666d4512008-02-20 19:10:47 +00002784 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002785}
2786
Chris Lattnerf22be932004-10-15 23:52:53 +00002787// Define these methods here so vtables don't get emitted into every translation
2788// unit that uses these classes.
2789
2790GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002791 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002792}
2793
2794BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002795 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002796}
2797
Chris Lattner0b490b02007-08-24 20:48:18 +00002798FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002799 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002800}
2801ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002802 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002803}
2804
Nate Begemand2195702008-05-12 19:01:56 +00002805VFCmpInst* VFCmpInst::clone() const {
2806 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2807}
2808VICmpInst* VICmpInst::clone() const {
2809 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2810}
2811
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002812MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2813AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2814FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2815LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2816StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2817CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2818CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2819CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2820CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2821CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2822CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2823CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2824CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2825CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2826CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2827CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2828CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002829CallInst *CallInst::clone() const {
2830 return new(getNumOperands()) CallInst(*this);
2831}
2832SelectInst *SelectInst::clone() const {
2833 return new(getNumOperands()) SelectInst(*this);
2834}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002835VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2836
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002837ExtractElementInst *ExtractElementInst::clone() const {
2838 return new ExtractElementInst(*this);
2839}
2840InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002841 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002842}
2843ShuffleVectorInst *ShuffleVectorInst::clone() const {
2844 return new ShuffleVectorInst(*this);
2845}
Chris Lattnerf22be932004-10-15 23:52:53 +00002846PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002847ReturnInst *ReturnInst::clone() const {
2848 return new(getNumOperands()) ReturnInst(*this);
2849}
2850BranchInst *BranchInst::clone() const {
2851 return new(getNumOperands()) BranchInst(*this);
2852}
Gabor Greiff6caff662008-05-10 08:32:32 +00002853SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002854InvokeInst *InvokeInst::clone() const {
2855 return new(getNumOperands()) InvokeInst(*this);
2856}
Chris Lattnerf22be932004-10-15 23:52:53 +00002857UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002858UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002859GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }