blob: ff3b8b546f0cf44461bc85eea98a2ceb74208d70 [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
Chris Lattnerc994c022008-03-13 05:00:21 +0000376bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000377 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000378 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000379 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000380 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000381 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000382}
383
Duncan Sandsaa31b922007-12-19 21:13:37 +0000384void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000385 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000386 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000387 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000388 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000389 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000390 setParamAttrs(PAL);
391}
392
Duncan Sands5208d1a2007-11-28 17:07:01 +0000393
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000394//===----------------------------------------------------------------------===//
395// InvokeInst Implementation
396//===----------------------------------------------------------------------===//
397
398void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000399 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000400 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
401 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 OL[0].init(Fn, this);
403 OL[1].init(IfNormal, this);
404 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000405 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000407 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000408
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000409 assert(((NumArgs == FTy->getNumParams()) ||
410 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000411 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000412
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000413 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000414 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000415 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000416 "Invoking a function with a bad signature!");
417
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000418 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000419 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000420}
421
Misha Brukmanb1c93172005-04-21 23:48:37 +0000422InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000423 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000424 OperandTraits<InvokeInst>::op_end(this)
425 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000426 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000427 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000428 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000429 Use *OL = OperandList, *InOL = II.OperandList;
430 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
431 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432}
433
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000434BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
435 return getSuccessor(idx);
436}
437unsigned InvokeInst::getNumSuccessorsV() const {
438 return getNumSuccessors();
439}
440void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
441 return setSuccessor(idx, B);
442}
443
Chris Lattnerc994c022008-03-13 05:00:21 +0000444bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000445 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000446 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000447 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000448 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000449 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000450}
451
Duncan Sandsaa31b922007-12-19 21:13:37 +0000452void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000453 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000454 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000455 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000456 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000457 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000458 setParamAttrs(PAL);
459}
460
Duncan Sands5208d1a2007-11-28 17:07:01 +0000461
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000462//===----------------------------------------------------------------------===//
463// ReturnInst Implementation
464//===----------------------------------------------------------------------===//
465
Chris Lattner2195fc42007-02-24 00:55:48 +0000466ReturnInst::ReturnInst(const ReturnInst &RI)
467 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000468 OperandTraits<ReturnInst>::op_end(this)
469 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000470 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000471 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000472 if (N == 1)
473 Op<0>().init(RI.Op<0>(), this);
Devang Patelae682fb2008-02-26 17:56:20 +0000474 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000475 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000476 for (unsigned i = 0; i < N; ++i)
477 OL[i].init(RI.getOperand(i), this);
478 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000479}
480
481ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000482 : TerminatorInst(Type::VoidTy, Instruction::Ret,
483 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
484 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000485 if (retVal)
486 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000487}
488ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000489 : TerminatorInst(Type::VoidTy, Instruction::Ret,
490 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
491 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000492 if (retVal)
493 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000494}
495ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000496 : TerminatorInst(Type::VoidTy, Instruction::Ret,
497 OperandTraits<ReturnInst>::op_end(this),
498 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000499}
500
Devang Patela736c002008-02-26 19:38:17 +0000501ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
502 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000503 : TerminatorInst(Type::VoidTy, Instruction::Ret,
504 OperandTraits<ReturnInst>::op_end(this) - N,
505 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000506 if (N != 0)
507 init(retVals, N);
508}
509ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
510 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 : TerminatorInst(Type::VoidTy, Instruction::Ret,
512 OperandTraits<ReturnInst>::op_end(this) - N,
513 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000514 if (N != 0)
515 init(retVals, N);
516}
517
Devang Patel060e79c2008-02-26 19:15:26 +0000518void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000519 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000520
Devang Patelc38eb522008-02-26 18:49:29 +0000521 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000522 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000523 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000524 if (V->getType() == Type::VoidTy)
525 return;
Gabor Greiff6caff662008-05-10 08:32:32 +0000526 Op<0>().init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000527 return;
Devang Patel59643e52008-02-23 00:35:18 +0000528 }
529
Gabor Greiff6caff662008-05-10 08:32:32 +0000530 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000531 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000532 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000533 assert(!isa<BasicBlock>(V) &&
534 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000535 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000536 }
537}
538
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000539unsigned ReturnInst::getNumSuccessorsV() const {
540 return getNumSuccessors();
541}
542
Devang Patelae682fb2008-02-26 17:56:20 +0000543/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
544/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000545void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000546 assert(0 && "ReturnInst has no successors!");
547}
548
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
550 assert(0 && "ReturnInst has no successors!");
551 abort();
552 return 0;
553}
554
Devang Patel59643e52008-02-23 00:35:18 +0000555ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000556}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000557
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000558//===----------------------------------------------------------------------===//
559// UnwindInst Implementation
560//===----------------------------------------------------------------------===//
561
Chris Lattner2195fc42007-02-24 00:55:48 +0000562UnwindInst::UnwindInst(Instruction *InsertBefore)
563 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
564}
565UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
566 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
567}
568
569
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570unsigned UnwindInst::getNumSuccessorsV() const {
571 return getNumSuccessors();
572}
573
574void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575 assert(0 && "UnwindInst has no successors!");
576}
577
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
579 assert(0 && "UnwindInst has no successors!");
580 abort();
581 return 0;
582}
583
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000584//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000585// UnreachableInst Implementation
586//===----------------------------------------------------------------------===//
587
Chris Lattner2195fc42007-02-24 00:55:48 +0000588UnreachableInst::UnreachableInst(Instruction *InsertBefore)
589 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
590}
591UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
592 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
593}
594
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595unsigned UnreachableInst::getNumSuccessorsV() const {
596 return getNumSuccessors();
597}
598
599void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
600 assert(0 && "UnwindInst has no successors!");
601}
602
603BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
604 assert(0 && "UnwindInst has no successors!");
605 abort();
606 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000607}
608
609//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000610// BranchInst Implementation
611//===----------------------------------------------------------------------===//
612
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613void BranchInst::AssertOK() {
614 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000615 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617}
618
Chris Lattner2195fc42007-02-24 00:55:48 +0000619BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000620 : TerminatorInst(Type::VoidTy, Instruction::Br,
621 OperandTraits<BranchInst>::op_end(this) - 1,
622 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000623 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greiff6caff662008-05-10 08:32:32 +0000624 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
626BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
627 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000628 : TerminatorInst(Type::VoidTy, Instruction::Br,
629 OperandTraits<BranchInst>::op_end(this) - 3,
630 3, InsertBefore) {
631 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
632 Op<1>().init(reinterpret_cast<Value*>(IfFalse), this);
633 Op<2>().init(Cond, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000634#ifndef NDEBUG
635 AssertOK();
636#endif
637}
638
639BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000640 : TerminatorInst(Type::VoidTy, Instruction::Br,
641 OperandTraits<BranchInst>::op_end(this) - 1,
642 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000643 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greiff6caff662008-05-10 08:32:32 +0000644 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000645}
646
647BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
648 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000649 : TerminatorInst(Type::VoidTy, Instruction::Br,
650 OperandTraits<BranchInst>::op_end(this) - 3,
651 3, InsertAtEnd) {
652 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
653 Op<1>().init(reinterpret_cast<Value*>(IfFalse), this);
654 Op<2>().init(Cond, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000655#ifndef NDEBUG
656 AssertOK();
657#endif
658}
659
660
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000662 TerminatorInst(Type::VoidTy, Instruction::Br,
663 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
664 BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 OperandList[0].init(BI.getOperand(0), this);
666 if (BI.getNumOperands() != 1) {
667 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
668 OperandList[1].init(BI.getOperand(1), this);
669 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000670 }
671}
672
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
674 return getSuccessor(idx);
675}
676unsigned BranchInst::getNumSuccessorsV() const {
677 return getNumSuccessors();
678}
679void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
680 setSuccessor(idx, B);
681}
682
683
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684//===----------------------------------------------------------------------===//
685// AllocationInst Implementation
686//===----------------------------------------------------------------------===//
687
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688static Value *getAISize(Value *Amt) {
689 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000690 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000691 else {
692 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000693 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000694 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000695 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000696 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000697 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
Misha Brukmanb1c93172005-04-21 23:48:37 +0000700AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000701 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000703 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000704 InsertBefore) {
705 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000707 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000708}
709
Misha Brukmanb1c93172005-04-21 23:48:37 +0000710AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000711 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000713 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000714 InsertAtEnd) {
715 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000717 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718}
719
Gordon Henriksen14a55692007-12-10 02:14:30 +0000720// Out of line virtual method, so the vtable, etc has a home.
721AllocationInst::~AllocationInst() {
722}
723
Dan Gohmanaa583d72008-03-24 16:55:58 +0000724void AllocationInst::setAlignment(unsigned Align) {
725 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
726 SubclassData = Log2_32(Align) + 1;
727 assert(getAlignment() == Align && "Alignment representation error!");
728}
729
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000730bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000731 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
732 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734}
735
736const Type *AllocationInst::getAllocatedType() const {
737 return getType()->getElementType();
738}
739
740AllocaInst::AllocaInst(const AllocaInst &AI)
741 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000742 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
745MallocInst::MallocInst(const MallocInst &MI)
746 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000747 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000748}
749
750//===----------------------------------------------------------------------===//
751// FreeInst Implementation
752//===----------------------------------------------------------------------===//
753
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754void FreeInst::AssertOK() {
755 assert(isa<PointerType>(getOperand(0)->getType()) &&
756 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757}
758
759FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000760 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000761 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
764FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000765 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000767}
768
769
770//===----------------------------------------------------------------------===//
771// LoadInst Implementation
772//===----------------------------------------------------------------------===//
773
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000775 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000777}
778
779LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000781 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000782 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000783 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000785 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786}
787
788LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000791 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000792 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000794 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795}
796
797LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
798 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000801 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000802 setAlignment(0);
803 AssertOK();
804 setName(Name);
805}
806
807LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
808 unsigned Align, Instruction *InsertBef)
809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
810 Load, Ptr, InsertBef) {
811 setVolatile(isVolatile);
812 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000813 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000814 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000815}
816
Dan Gohman68659282007-07-18 20:51:11 +0000817LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
818 unsigned Align, BasicBlock *InsertAE)
819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
820 Load, Ptr, InsertAE) {
821 setVolatile(isVolatile);
822 setAlignment(Align);
823 AssertOK();
824 setName(Name);
825}
826
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
828 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000829 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000830 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000831 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000832 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000833 AssertOK();
834 setName(Name);
835}
836
837
838
839LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000840 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
841 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000842 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000843 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000844 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000845 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000846}
847
848LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000849 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
850 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000851 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000852 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000853 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000855}
856
857LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
858 Instruction *InsertBef)
859: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000860 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000861 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000862 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000863 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000864 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000865}
866
867LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
868 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000869 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
870 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000871 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000872 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000873 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000874 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875}
876
Christopher Lamb84485702007-04-22 19:24:39 +0000877void LoadInst::setAlignment(unsigned Align) {
878 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
879 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
880}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000881
882//===----------------------------------------------------------------------===//
883// StoreInst Implementation
884//===----------------------------------------------------------------------===//
885
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000886void StoreInst::AssertOK() {
887 assert(isa<PointerType>(getOperand(1)->getType()) &&
888 "Ptr must have pointer type!");
889 assert(getOperand(0)->getType() ==
890 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000891 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000894
895StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000896 : Instruction(Type::VoidTy, Store,
897 OperandTraits<StoreInst>::op_begin(this),
898 OperandTraits<StoreInst>::operands(this),
899 InsertBefore) {
900 Op<0>().init(val, this);
901 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000902 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000903 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904 AssertOK();
905}
906
907StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000908 : Instruction(Type::VoidTy, Store,
909 OperandTraits<StoreInst>::op_begin(this),
910 OperandTraits<StoreInst>::operands(this),
911 InsertAtEnd) {
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
Misha Brukmanb1c93172005-04-21 23:48:37 +0000919StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000921 : Instruction(Type::VoidTy, Store,
922 OperandTraits<StoreInst>::op_begin(this),
923 OperandTraits<StoreInst>::operands(this),
924 InsertBefore) {
925 Op<0>().init(val, this);
926 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000927 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000928 setAlignment(0);
929 AssertOK();
930}
931
932StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
933 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000934 : Instruction(Type::VoidTy, Store,
935 OperandTraits<StoreInst>::op_begin(this),
936 OperandTraits<StoreInst>::operands(this),
937 InsertBefore) {
938 Op<0>().init(val, this);
939 Op<1>().init(addr, this);
Christopher Lamb84485702007-04-22 19:24:39 +0000940 setVolatile(isVolatile);
941 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000942 AssertOK();
943}
944
Misha Brukmanb1c93172005-04-21 23:48:37 +0000945StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000946 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000947 : Instruction(Type::VoidTy, Store,
948 OperandTraits<StoreInst>::op_begin(this),
949 OperandTraits<StoreInst>::operands(this),
950 InsertAtEnd) {
951 Op<0>().init(val, this);
952 Op<1>().init(addr, this);
Dan Gohman68659282007-07-18 20:51:11 +0000953 setVolatile(isVolatile);
954 setAlignment(Align);
955 AssertOK();
956}
957
958StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000960 : Instruction(Type::VoidTy, Store,
961 OperandTraits<StoreInst>::op_begin(this),
962 OperandTraits<StoreInst>::operands(this),
963 InsertAtEnd) {
964 Op<0>().init(val, this);
965 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000966 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000967 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000968 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969}
970
Christopher Lamb84485702007-04-22 19:24:39 +0000971void StoreInst::setAlignment(unsigned Align) {
972 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
973 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
974}
975
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976//===----------------------------------------------------------------------===//
977// GetElementPtrInst Implementation
978//===----------------------------------------------------------------------===//
979
Christopher Lambedf07882007-12-17 01:12:55 +0000980static unsigned retrieveAddrSpace(const Value *Val) {
981 return cast<PointerType>(Val->getType())->getAddressSpace();
982}
983
Chris Lattner79807c3d2007-01-31 19:47:18 +0000984void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000985 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
986 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988
Chris Lattner79807c3d2007-01-31 19:47:18 +0000989 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000991}
992
Chris Lattner82981202005-05-03 05:43:30 +0000993void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000994 assert(NumOperands == 2 && "NumOperands not initialized?");
995 Use *OL = OperandList;
Chris Lattner82981202005-05-03 05:43:30 +0000996 OL[0].init(Ptr, this);
997 OL[1].init(Idx, this);
998}
999
Gabor Greiff6caff662008-05-10 08:32:32 +00001000GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1001 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001002 OperandTraits<GetElementPtrInst>::op_end(this)
1003 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001004 GEPI.getNumOperands()) {
1005 Use *OL = OperandList;
1006 Use *GEPIOL = GEPI.OperandList;
1007 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1008 OL[i].init(GEPIOL[i], this);
1009}
1010
Chris Lattner82981202005-05-03 05:43:30 +00001011GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1012 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001013 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001014 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001015 GetElementPtr,
1016 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1017 2, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001018 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001019 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001020}
1021
1022GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1023 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001024 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001025 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001026 GetElementPtr,
1027 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1028 2, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001029 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001030 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001031}
1032
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001033// getIndexedType - Returns the type of the element that would be loaded with
1034// a load instruction with the specified parameters.
1035//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001036// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037// pointer type.
1038//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001039const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001040 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001041 unsigned NumIdx) {
1042 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1043 if (!PTy) return 0; // Type isn't a pointer type!
1044 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045
1046 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001047 if (NumIdx == 0)
1048 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001049
Dan Gohman12fce772008-05-15 19:50:34 +00001050 return ExtractValueInst::getIndexedType(Agg, Idxs+1, Idxs+NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051}
1052
Chris Lattner82981202005-05-03 05:43:30 +00001053const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1054 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1055 if (!PTy) return 0; // Type isn't a pointer type!
1056
1057 // Check the pointer index.
1058 if (!PTy->indexValid(Idx)) return 0;
1059
Chris Lattnerc2233332005-05-03 16:44:45 +00001060 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001061}
1062
Chris Lattner45f15572007-04-14 00:12:57 +00001063
1064/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1065/// zeros. If so, the result pointer and the first operand have the same
1066/// value, just potentially different types.
1067bool GetElementPtrInst::hasAllZeroIndices() const {
1068 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1069 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1070 if (!CI->isZero()) return false;
1071 } else {
1072 return false;
1073 }
1074 }
1075 return true;
1076}
1077
Chris Lattner27058292007-04-27 20:35:56 +00001078/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1079/// constant integers. If so, the result pointer and the first operand have
1080/// a constant offset between them.
1081bool GetElementPtrInst::hasAllConstantIndices() const {
1082 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1083 if (!isa<ConstantInt>(getOperand(i)))
1084 return false;
1085 }
1086 return true;
1087}
1088
Chris Lattner45f15572007-04-14 00:12:57 +00001089
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001091// ExtractElementInst Implementation
1092//===----------------------------------------------------------------------===//
1093
1094ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001095 const std::string &Name,
1096 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001097 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001098 ExtractElement,
1099 OperandTraits<ExtractElementInst>::op_begin(this),
1100 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001101 assert(isValidOperands(Val, Index) &&
1102 "Invalid extractelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001103 Op<0>().init(Val, this);
1104 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001105 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001106}
1107
Chris Lattner65511ff2006-10-05 06:24:58 +00001108ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1109 const std::string &Name,
1110 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001111 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001112 ExtractElement,
1113 OperandTraits<ExtractElementInst>::op_begin(this),
1114 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001115 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001116 assert(isValidOperands(Val, Index) &&
1117 "Invalid extractelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001118 Op<0>().init(Val, this);
1119 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001120 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001121}
1122
1123
Robert Bocchino23004482006-01-10 19:05:34 +00001124ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001125 const std::string &Name,
1126 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001127 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001128 ExtractElement,
1129 OperandTraits<ExtractElementInst>::op_begin(this),
1130 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001131 assert(isValidOperands(Val, Index) &&
1132 "Invalid extractelement instruction operands!");
1133
Gabor Greiff6caff662008-05-10 08:32:32 +00001134 Op<0>().init(Val, this);
1135 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001136 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001137}
1138
Chris Lattner65511ff2006-10-05 06:24:58 +00001139ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1140 const std::string &Name,
1141 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001142 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001143 ExtractElement,
1144 OperandTraits<ExtractElementInst>::op_begin(this),
1145 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001146 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001147 assert(isValidOperands(Val, Index) &&
1148 "Invalid extractelement instruction operands!");
1149
Gabor Greiff6caff662008-05-10 08:32:32 +00001150 Op<0>().init(Val, this);
1151 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001152 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001153}
1154
1155
Chris Lattner54865b32006-04-08 04:05:48 +00001156bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001157 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001158 return false;
1159 return true;
1160}
1161
1162
Robert Bocchino23004482006-01-10 19:05:34 +00001163//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001164// InsertElementInst Implementation
1165//===----------------------------------------------------------------------===//
1166
Chris Lattner0875d942006-04-14 22:20:32 +00001167InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001168 : Instruction(IE.getType(), InsertElement,
1169 OperandTraits<InsertElementInst>::op_begin(this), 3) {
1170 Op<0>().init(IE.Op<0>(), this);
1171 Op<1>().init(IE.Op<1>(), this);
1172 Op<2>().init(IE.Op<2>(), this);
Chris Lattner0875d942006-04-14 22:20:32 +00001173}
Chris Lattner54865b32006-04-08 04:05:48 +00001174InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001175 const std::string &Name,
1176 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001177 : Instruction(Vec->getType(), InsertElement,
1178 OperandTraits<InsertElementInst>::op_begin(this),
1179 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001180 assert(isValidOperands(Vec, Elt, Index) &&
1181 "Invalid insertelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001182 Op<0>().init(Vec, this);
1183 Op<1>().init(Elt, this);
1184 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001185 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001186}
1187
Chris Lattner65511ff2006-10-05 06:24:58 +00001188InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1189 const std::string &Name,
1190 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001191 : Instruction(Vec->getType(), InsertElement,
1192 OperandTraits<InsertElementInst>::op_begin(this),
1193 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001194 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001195 assert(isValidOperands(Vec, Elt, Index) &&
1196 "Invalid insertelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001197 Op<0>().init(Vec, this);
1198 Op<1>().init(Elt, this);
1199 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001200 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001201}
1202
1203
Chris Lattner54865b32006-04-08 04:05:48 +00001204InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001205 const std::string &Name,
1206 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001207 : Instruction(Vec->getType(), InsertElement,
1208 OperandTraits<InsertElementInst>::op_begin(this),
1209 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001210 assert(isValidOperands(Vec, Elt, Index) &&
1211 "Invalid insertelement instruction operands!");
1212
Gabor Greiff6caff662008-05-10 08:32:32 +00001213 Op<0>().init(Vec, this);
1214 Op<1>().init(Elt, this);
1215 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001216 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001217}
1218
Chris Lattner65511ff2006-10-05 06:24:58 +00001219InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1220 const std::string &Name,
1221 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001222: Instruction(Vec->getType(), InsertElement,
1223 OperandTraits<InsertElementInst>::op_begin(this),
1224 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001225 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001226 assert(isValidOperands(Vec, Elt, Index) &&
1227 "Invalid insertelement instruction operands!");
1228
Gabor Greiff6caff662008-05-10 08:32:32 +00001229 Op<0>().init(Vec, this);
1230 Op<1>().init(Elt, this);
1231 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001232 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001233}
1234
Chris Lattner54865b32006-04-08 04:05:48 +00001235bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1236 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001237 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001238 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001239
Reid Spencerd84d35b2007-02-15 02:26:10 +00001240 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001241 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001242
Reid Spencer8d9336d2006-12-31 05:26:44 +00001243 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001244 return false; // Third operand of insertelement must be uint.
1245 return true;
1246}
1247
1248
Robert Bocchinoca27f032006-01-17 20:07:22 +00001249//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001250// ShuffleVectorInst Implementation
1251//===----------------------------------------------------------------------===//
1252
Chris Lattner0875d942006-04-14 22:20:32 +00001253ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001254 : Instruction(SV.getType(), ShuffleVector,
1255 OperandTraits<ShuffleVectorInst>::op_begin(this),
1256 OperandTraits<ShuffleVectorInst>::operands(this)) {
1257 Op<0>().init(SV.Op<0>(), this);
1258 Op<1>().init(SV.Op<1>(), this);
1259 Op<2>().init(SV.Op<2>(), this);
Chris Lattner0875d942006-04-14 22:20:32 +00001260}
1261
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001262ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1263 const std::string &Name,
1264 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001265 : Instruction(V1->getType(), ShuffleVector,
1266 OperandTraits<ShuffleVectorInst>::op_begin(this),
1267 OperandTraits<ShuffleVectorInst>::operands(this),
1268 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001269 assert(isValidOperands(V1, V2, Mask) &&
1270 "Invalid shuffle vector instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001271 Op<0>().init(V1, this);
1272 Op<1>().init(V2, this);
1273 Op<2>().init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001274 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001275}
1276
1277ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1278 const std::string &Name,
1279 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001280 : Instruction(V1->getType(), ShuffleVector,
1281 OperandTraits<ShuffleVectorInst>::op_begin(this),
1282 OperandTraits<ShuffleVectorInst>::operands(this),
1283 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001284 assert(isValidOperands(V1, V2, Mask) &&
1285 "Invalid shuffle vector instruction operands!");
1286
Gabor Greiff6caff662008-05-10 08:32:32 +00001287 Op<0>().init(V1, this);
1288 Op<1>().init(V2, this);
1289 Op<2>().init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001290 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001291}
1292
1293bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1294 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001295 if (!isa<VectorType>(V1->getType()) ||
1296 V1->getType() != V2->getType())
1297 return false;
1298
1299 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1300 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1301 MaskTy->getElementType() != Type::Int32Ty ||
1302 MaskTy->getNumElements() !=
1303 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304 return false;
1305 return true;
1306}
1307
Chris Lattnerf724e342008-03-02 05:28:33 +00001308/// getMaskValue - Return the index from the shuffle mask for the specified
1309/// output result. This is either -1 if the element is undef or a number less
1310/// than 2*numelements.
1311int ShuffleVectorInst::getMaskValue(unsigned i) const {
1312 const Constant *Mask = cast<Constant>(getOperand(2));
1313 if (isa<UndefValue>(Mask)) return -1;
1314 if (isa<ConstantAggregateZero>(Mask)) return 0;
1315 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1316 assert(i < MaskCV->getNumOperands() && "Index out of range");
1317
1318 if (isa<UndefValue>(MaskCV->getOperand(i)))
1319 return -1;
1320 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1321}
1322
Dan Gohman12fce772008-05-15 19:50:34 +00001323//===----------------------------------------------------------------------===//
1324// ExtractValueInst Class
1325//===----------------------------------------------------------------------===//
1326
1327// getIndexedType - Returns the type of the element that would be extracted
1328// with an extractvalue instruction with the specified parameters.
1329//
1330// A null type is returned if the indices are invalid for the specified
1331// pointer type.
1332//
1333const Type* ExtractValueInst::getIndexedType(const Type *Agg,
1334 Value* const *Idxs,
1335 unsigned NumIdx) {
1336 unsigned CurIdx = 0;
1337 for (; CurIdx != NumIdx; ++CurIdx) {
1338 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman2f156ae2008-05-16 00:16:32 +00001339 if (!CT || isa<PointerType>(CT)) return 0;
Dan Gohman12fce772008-05-15 19:50:34 +00001340 Value *Index = Idxs[CurIdx];
1341 if (!CT->indexValid(Index)) return 0;
1342 Agg = CT->getTypeAtIndex(Index);
1343
1344 // If the new type forwards to another type, then it is in the middle
1345 // of being refined to another type (and hence, may have dropped all
1346 // references to what it was using before). So, use the new forwarded
1347 // type.
1348 if (const Type *Ty = Agg->getForwardedType())
1349 Agg = Ty;
1350 }
1351 return CurIdx == NumIdx ? Agg : 0;
1352}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001353
1354//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001355// BinaryOperator Class
1356//===----------------------------------------------------------------------===//
1357
Chris Lattner2195fc42007-02-24 00:55:48 +00001358BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1359 const Type *Ty, const std::string &Name,
1360 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001361 : Instruction(Ty, iType,
1362 OperandTraits<BinaryOperator>::op_begin(this),
1363 OperandTraits<BinaryOperator>::operands(this),
1364 InsertBefore) {
1365 Op<0>().init(S1, this);
1366 Op<1>().init(S2, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001367 init(iType);
1368 setName(Name);
1369}
1370
1371BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1372 const Type *Ty, const std::string &Name,
1373 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001374 : Instruction(Ty, iType,
1375 OperandTraits<BinaryOperator>::op_begin(this),
1376 OperandTraits<BinaryOperator>::operands(this),
1377 InsertAtEnd) {
1378 Op<0>().init(S1, this);
1379 Op<1>().init(S2, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001380 init(iType);
1381 setName(Name);
1382}
1383
1384
1385void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001386 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001387 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001388 assert(LHS->getType() == RHS->getType() &&
1389 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001390#ifndef NDEBUG
1391 switch (iType) {
1392 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001393 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001394 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001395 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001396 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001397 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001398 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001399 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001400 case UDiv:
1401 case SDiv:
1402 assert(getType() == LHS->getType() &&
1403 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001404 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1405 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001406 "Incorrect operand type (not integer) for S/UDIV");
1407 break;
1408 case FDiv:
1409 assert(getType() == LHS->getType() &&
1410 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001411 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1412 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001413 && "Incorrect operand type (not floating point) for FDIV");
1414 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001415 case URem:
1416 case SRem:
1417 assert(getType() == LHS->getType() &&
1418 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001419 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1420 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001421 "Incorrect operand type (not integer) for S/UREM");
1422 break;
1423 case FRem:
1424 assert(getType() == LHS->getType() &&
1425 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001426 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1427 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001428 && "Incorrect operand type (not floating point) for FREM");
1429 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001430 case Shl:
1431 case LShr:
1432 case AShr:
1433 assert(getType() == LHS->getType() &&
1434 "Shift operation should return same type as operands!");
1435 assert(getType()->isInteger() &&
1436 "Shift operation requires integer operands");
1437 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001438 case And: case Or:
1439 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001440 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001441 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001442 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001443 (isa<VectorType>(getType()) &&
1444 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001445 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001446 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447 default:
1448 break;
1449 }
1450#endif
1451}
1452
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001453BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001454 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001455 Instruction *InsertBefore) {
1456 assert(S1->getType() == S2->getType() &&
1457 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001458 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001459}
1460
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001461BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001462 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001463 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001464 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001465 InsertAtEnd->getInstList().push_back(Res);
1466 return Res;
1467}
1468
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001469BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001470 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001471 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1472 return new BinaryOperator(Instruction::Sub,
1473 zero, Op,
1474 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001475}
1476
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001477BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001478 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001479 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1480 return new BinaryOperator(Instruction::Sub,
1481 zero, Op,
1482 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001483}
1484
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001485BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001486 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001487 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001488 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001489 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001490 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001491 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001492 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001493 }
1494
1495 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001496 Op->getType(), Name, InsertBefore);
1497}
1498
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001499BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001500 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001501 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001502 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001503 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001504 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001505 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001506 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001507 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001508 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001509 }
1510
1511 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001512 Op->getType(), Name, InsertAtEnd);
1513}
1514
1515
1516// isConstantAllOnes - Helper function for several functions below
1517static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001518 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1519 return CI->isAllOnesValue();
1520 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1521 return CV->isAllOnesValue();
1522 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001523}
1524
1525bool BinaryOperator::isNeg(const Value *V) {
1526 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1527 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001528 return Bop->getOperand(0) ==
1529 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001530 return false;
1531}
1532
1533bool BinaryOperator::isNot(const Value *V) {
1534 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1535 return (Bop->getOpcode() == Instruction::Xor &&
1536 (isConstantAllOnes(Bop->getOperand(1)) ||
1537 isConstantAllOnes(Bop->getOperand(0))));
1538 return false;
1539}
1540
Chris Lattner2c7d1772005-04-24 07:28:37 +00001541Value *BinaryOperator::getNegArgument(Value *BinOp) {
1542 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1543 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001544}
1545
Chris Lattner2c7d1772005-04-24 07:28:37 +00001546const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1547 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001548}
1549
Chris Lattner2c7d1772005-04-24 07:28:37 +00001550Value *BinaryOperator::getNotArgument(Value *BinOp) {
1551 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1552 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1553 Value *Op0 = BO->getOperand(0);
1554 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 if (isConstantAllOnes(Op0)) return Op1;
1556
1557 assert(isConstantAllOnes(Op1));
1558 return Op0;
1559}
1560
Chris Lattner2c7d1772005-04-24 07:28:37 +00001561const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1562 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001563}
1564
1565
1566// swapOperands - Exchange the two operands to this instruction. This
1567// instruction is safe to use on any binary instruction and does not
1568// modify the semantics of the instruction. If the instruction is
1569// order dependent (SetLT f.e.) the opcode is changed.
1570//
1571bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001572 if (!isCommutative())
1573 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001574 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001575 return false;
1576}
1577
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001578//===----------------------------------------------------------------------===//
1579// CastInst Class
1580//===----------------------------------------------------------------------===//
1581
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001582// Just determine if this cast only deals with integral->integral conversion.
1583bool CastInst::isIntegerCast() const {
1584 switch (getOpcode()) {
1585 default: return false;
1586 case Instruction::ZExt:
1587 case Instruction::SExt:
1588 case Instruction::Trunc:
1589 return true;
1590 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001591 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001592 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001593}
1594
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001595bool CastInst::isLosslessCast() const {
1596 // Only BitCast can be lossless, exit fast if we're not BitCast
1597 if (getOpcode() != Instruction::BitCast)
1598 return false;
1599
1600 // Identity cast is always lossless
1601 const Type* SrcTy = getOperand(0)->getType();
1602 const Type* DstTy = getType();
1603 if (SrcTy == DstTy)
1604 return true;
1605
Reid Spencer8d9336d2006-12-31 05:26:44 +00001606 // Pointer to pointer is always lossless.
1607 if (isa<PointerType>(SrcTy))
1608 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001609 return false; // Other types have no identity values
1610}
1611
1612/// This function determines if the CastInst does not require any bits to be
1613/// changed in order to effect the cast. Essentially, it identifies cases where
1614/// no code gen is necessary for the cast, hence the name no-op cast. For
1615/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001616/// # bitcast i32* %x to i8*
1617/// # bitcast <2 x i32> %x to <4 x i16>
1618/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001619/// @brief Determine if a cast is a no-op.
1620bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1621 switch (getOpcode()) {
1622 default:
1623 assert(!"Invalid CastOp");
1624 case Instruction::Trunc:
1625 case Instruction::ZExt:
1626 case Instruction::SExt:
1627 case Instruction::FPTrunc:
1628 case Instruction::FPExt:
1629 case Instruction::UIToFP:
1630 case Instruction::SIToFP:
1631 case Instruction::FPToUI:
1632 case Instruction::FPToSI:
1633 return false; // These always modify bits
1634 case Instruction::BitCast:
1635 return true; // BitCast never modifies bits.
1636 case Instruction::PtrToInt:
1637 return IntPtrTy->getPrimitiveSizeInBits() ==
1638 getType()->getPrimitiveSizeInBits();
1639 case Instruction::IntToPtr:
1640 return IntPtrTy->getPrimitiveSizeInBits() ==
1641 getOperand(0)->getType()->getPrimitiveSizeInBits();
1642 }
1643}
1644
1645/// This function determines if a pair of casts can be eliminated and what
1646/// opcode should be used in the elimination. This assumes that there are two
1647/// instructions like this:
1648/// * %F = firstOpcode SrcTy %x to MidTy
1649/// * %S = secondOpcode MidTy %F to DstTy
1650/// The function returns a resultOpcode so these two casts can be replaced with:
1651/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1652/// If no such cast is permited, the function returns 0.
1653unsigned CastInst::isEliminableCastPair(
1654 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1655 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1656{
1657 // Define the 144 possibilities for these two cast instructions. The values
1658 // in this matrix determine what to do in a given situation and select the
1659 // case in the switch below. The rows correspond to firstOp, the columns
1660 // correspond to secondOp. In looking at the table below, keep in mind
1661 // the following cast properties:
1662 //
1663 // Size Compare Source Destination
1664 // Operator Src ? Size Type Sign Type Sign
1665 // -------- ------------ ------------------- ---------------------
1666 // TRUNC > Integer Any Integral Any
1667 // ZEXT < Integral Unsigned Integer Any
1668 // SEXT < Integral Signed Integer Any
1669 // FPTOUI n/a FloatPt n/a Integral Unsigned
1670 // FPTOSI n/a FloatPt n/a Integral Signed
1671 // UITOFP n/a Integral Unsigned FloatPt n/a
1672 // SITOFP n/a Integral Signed FloatPt n/a
1673 // FPTRUNC > FloatPt n/a FloatPt n/a
1674 // FPEXT < FloatPt n/a FloatPt n/a
1675 // PTRTOINT n/a Pointer n/a Integral Unsigned
1676 // INTTOPTR n/a Integral Unsigned Pointer n/a
1677 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001678 //
1679 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1680 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1681 // into "fptoui double to ulong", but this loses information about the range
1682 // of the produced value (we no longer know the top-part is all zeros).
1683 // Further this conversion is often much more expensive for typical hardware,
1684 // and causes issues when building libgcc. We disallow fptosi+sext for the
1685 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001686 const unsigned numCastOps =
1687 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1688 static const uint8_t CastResults[numCastOps][numCastOps] = {
1689 // T F F U S F F P I B -+
1690 // R Z S P P I I T P 2 N T |
1691 // U E E 2 2 2 2 R E I T C +- secondOp
1692 // N X X U S F F N X N 2 V |
1693 // C T T I I P P C T T P T -+
1694 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1695 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1696 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001697 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1698 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001699 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1700 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1701 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1702 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1703 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1704 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1705 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1706 };
1707
1708 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1709 [secondOp-Instruction::CastOpsBegin];
1710 switch (ElimCase) {
1711 case 0:
1712 // categorically disallowed
1713 return 0;
1714 case 1:
1715 // allowed, use first cast's opcode
1716 return firstOp;
1717 case 2:
1718 // allowed, use second cast's opcode
1719 return secondOp;
1720 case 3:
1721 // no-op cast in second op implies firstOp as long as the DestTy
1722 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001723 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724 return firstOp;
1725 return 0;
1726 case 4:
1727 // no-op cast in second op implies firstOp as long as the DestTy
1728 // is floating point
1729 if (DstTy->isFloatingPoint())
1730 return firstOp;
1731 return 0;
1732 case 5:
1733 // no-op cast in first op implies secondOp as long as the SrcTy
1734 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001735 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001736 return secondOp;
1737 return 0;
1738 case 6:
1739 // no-op cast in first op implies secondOp as long as the SrcTy
1740 // is a floating point
1741 if (SrcTy->isFloatingPoint())
1742 return secondOp;
1743 return 0;
1744 case 7: {
1745 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1746 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1747 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1748 if (MidSize >= PtrSize)
1749 return Instruction::BitCast;
1750 return 0;
1751 }
1752 case 8: {
1753 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1754 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1755 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1756 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1757 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1758 if (SrcSize == DstSize)
1759 return Instruction::BitCast;
1760 else if (SrcSize < DstSize)
1761 return firstOp;
1762 return secondOp;
1763 }
1764 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1765 return Instruction::ZExt;
1766 case 10:
1767 // fpext followed by ftrunc is allowed if the bit size returned to is
1768 // the same as the original, in which case its just a bitcast
1769 if (SrcTy == DstTy)
1770 return Instruction::BitCast;
1771 return 0; // If the types are not the same we can't eliminate it.
1772 case 11:
1773 // bitcast followed by ptrtoint is allowed as long as the bitcast
1774 // is a pointer to pointer cast.
1775 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1776 return secondOp;
1777 return 0;
1778 case 12:
1779 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1780 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1781 return firstOp;
1782 return 0;
1783 case 13: {
1784 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1785 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1786 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1787 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1788 if (SrcSize <= PtrSize && SrcSize == DstSize)
1789 return Instruction::BitCast;
1790 return 0;
1791 }
1792 case 99:
1793 // cast combination can't happen (error in input). This is for all cases
1794 // where the MidTy is not the same for the two cast instructions.
1795 assert(!"Invalid Cast Combination");
1796 return 0;
1797 default:
1798 assert(!"Error in CastResults table!!!");
1799 return 0;
1800 }
1801 return 0;
1802}
1803
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001804CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001805 const std::string &Name, Instruction *InsertBefore) {
1806 // Construct and return the appropriate CastInst subclass
1807 switch (op) {
1808 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1809 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1810 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1811 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1812 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1813 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1814 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1815 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1816 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1817 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1818 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1819 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1820 default:
1821 assert(!"Invalid opcode provided");
1822 }
1823 return 0;
1824}
1825
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001826CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827 const std::string &Name, BasicBlock *InsertAtEnd) {
1828 // Construct and return the appropriate CastInst subclass
1829 switch (op) {
1830 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1831 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1832 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1833 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1834 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1835 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1836 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1837 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1838 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1839 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1840 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1841 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1842 default:
1843 assert(!"Invalid opcode provided");
1844 }
1845 return 0;
1846}
1847
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001848CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001849 const std::string &Name,
1850 Instruction *InsertBefore) {
1851 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001852 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1853 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001854}
1855
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001856CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001857 const std::string &Name,
1858 BasicBlock *InsertAtEnd) {
1859 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001860 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1861 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001862}
1863
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001864CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001865 const std::string &Name,
1866 Instruction *InsertBefore) {
1867 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001868 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1869 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001870}
1871
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001872CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001873 const std::string &Name,
1874 BasicBlock *InsertAtEnd) {
1875 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001876 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1877 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001878}
1879
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001880CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001881 const std::string &Name,
1882 Instruction *InsertBefore) {
1883 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001884 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1885 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001886}
1887
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001888CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001889 const std::string &Name,
1890 BasicBlock *InsertAtEnd) {
1891 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001892 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1893 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001894}
1895
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001896CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001897 const std::string &Name,
1898 BasicBlock *InsertAtEnd) {
1899 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001900 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001901 "Invalid cast");
1902
Chris Lattner03c49532007-01-15 02:27:26 +00001903 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001904 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1905 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001906}
1907
1908/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001909CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001910 const std::string &Name,
1911 Instruction *InsertBefore) {
1912 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001913 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001914 "Invalid cast");
1915
Chris Lattner03c49532007-01-15 02:27:26 +00001916 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001917 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1918 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001919}
1920
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001921CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001922 bool isSigned, const std::string &Name,
1923 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001924 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001925 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1926 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1927 Instruction::CastOps opcode =
1928 (SrcBits == DstBits ? Instruction::BitCast :
1929 (SrcBits > DstBits ? Instruction::Trunc :
1930 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001931 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00001932}
1933
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001934CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001935 bool isSigned, const std::string &Name,
1936 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001937 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001938 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1939 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1940 Instruction::CastOps opcode =
1941 (SrcBits == DstBits ? Instruction::BitCast :
1942 (SrcBits > DstBits ? Instruction::Trunc :
1943 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001944 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00001945}
1946
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001947CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001948 const std::string &Name,
1949 Instruction *InsertBefore) {
1950 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1951 "Invalid cast");
1952 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1953 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1954 Instruction::CastOps opcode =
1955 (SrcBits == DstBits ? Instruction::BitCast :
1956 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001957 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00001958}
1959
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001960CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001961 const std::string &Name,
1962 BasicBlock *InsertAtEnd) {
1963 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1964 "Invalid cast");
1965 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1966 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1967 Instruction::CastOps opcode =
1968 (SrcBits == DstBits ? Instruction::BitCast :
1969 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001970 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00001971}
1972
Duncan Sands55e50902008-01-06 10:12:28 +00001973// Check whether it is valid to call getCastOpcode for these types.
1974// This routine must be kept in sync with getCastOpcode.
1975bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1976 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1977 return false;
1978
1979 if (SrcTy == DestTy)
1980 return true;
1981
1982 // Get the bit sizes, we'll need these
1983 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1984 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1985
1986 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00001987 if (DestTy->isInteger()) { // Casting to integral
1988 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00001989 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00001990 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00001991 return true;
1992 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00001993 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00001994 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00001995 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00001996 return isa<PointerType>(SrcTy);
1997 }
Gabor Greif697e94c2008-05-15 10:04:30 +00001998 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1999 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002000 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002001 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002002 return true;
2003 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002004 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002005 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002006 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002007 return false;
2008 }
2009 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002010 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002011 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002012 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002013 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002014 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002015 return DestPTy->getBitWidth() == SrcBits;
2016 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002017 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2018 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002019 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002020 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002021 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002022 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002023 return false;
2024 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002025 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002026 return false;
2027 }
2028}
2029
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030// Provide a way to get a "cast" where the cast opcode is inferred from the
2031// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002032// logic in the castIsValid function below. This axiom should hold:
2033// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2034// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002036// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002038CastInst::getCastOpcode(
2039 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040 // Get the bit sizes, we'll need these
2041 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002042 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2043 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044
Duncan Sands55e50902008-01-06 10:12:28 +00002045 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2046 "Only first class types are castable!");
2047
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002049 if (DestTy->isInteger()) { // Casting to integral
2050 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002051 if (DestBits < SrcBits)
2052 return Trunc; // int -> smaller int
2053 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002054 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055 return SExt; // signed -> SEXT
2056 else
2057 return ZExt; // unsigned -> ZEXT
2058 } else {
2059 return BitCast; // Same size, No-op cast
2060 }
2061 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002062 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 return FPToSI; // FP -> sint
2064 else
2065 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002066 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002068 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002069 return BitCast; // Same size, no-op cast
2070 } else {
2071 assert(isa<PointerType>(SrcTy) &&
2072 "Casting from a value that is not first-class type");
2073 return PtrToInt; // ptr -> int
2074 }
2075 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002076 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002077 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 return SIToFP; // sint -> FP
2079 else
2080 return UIToFP; // uint -> FP
2081 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2082 if (DestBits < SrcBits) {
2083 return FPTrunc; // FP -> smaller FP
2084 } else if (DestBits > SrcBits) {
2085 return FPExt; // FP -> larger FP
2086 } else {
2087 return BitCast; // same size, no-op cast
2088 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002089 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002091 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 return BitCast; // same size, no-op cast
2093 } else {
2094 assert(0 && "Casting pointer or non-first class to float");
2095 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002096 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2097 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002098 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002099 "Casting vector to vector of different widths");
2100 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002102 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002104 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002105 }
2106 } else if (isa<PointerType>(DestTy)) {
2107 if (isa<PointerType>(SrcTy)) {
2108 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002109 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 return IntToPtr; // int -> ptr
2111 } else {
2112 assert(!"Casting pointer to other than pointer or int");
2113 }
2114 } else {
2115 assert(!"Casting to type that is not first-class");
2116 }
2117
2118 // If we fall through to here we probably hit an assertion cast above
2119 // and assertions are not turned on. Anything we return is an error, so
2120 // BitCast is as good a choice as any.
2121 return BitCast;
2122}
2123
2124//===----------------------------------------------------------------------===//
2125// CastInst SubClass Constructors
2126//===----------------------------------------------------------------------===//
2127
2128/// Check that the construction parameters for a CastInst are correct. This
2129/// could be broken out into the separate constructors but it is useful to have
2130/// it in one place and to eliminate the redundant code for getting the sizes
2131/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002132bool
2133CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002134
2135 // Check for type sanity on the arguments
2136 const Type *SrcTy = S->getType();
2137 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2138 return false;
2139
2140 // Get the size of the types in bits, we'll need this later
2141 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2142 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2143
2144 // Switch on the opcode provided
2145 switch (op) {
2146 default: return false; // This is an input error
2147 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002148 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002149 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002150 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002152 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002153 case Instruction::FPTrunc:
2154 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2155 SrcBitSize > DstBitSize;
2156 case Instruction::FPExt:
2157 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2158 SrcBitSize < DstBitSize;
2159 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002160 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002161 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2162 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2163 return SVTy->getElementType()->isInteger() &&
2164 DVTy->getElementType()->isFloatingPoint() &&
2165 SVTy->getNumElements() == DVTy->getNumElements();
2166 }
2167 }
Chris Lattner03c49532007-01-15 02:27:26 +00002168 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002171 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2172 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2173 return SVTy->getElementType()->isFloatingPoint() &&
2174 DVTy->getElementType()->isInteger() &&
2175 SVTy->getNumElements() == DVTy->getNumElements();
2176 }
2177 }
Chris Lattner03c49532007-01-15 02:27:26 +00002178 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002180 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002182 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 case Instruction::BitCast:
2184 // BitCast implies a no-op cast of type only. No bits change.
2185 // However, you can't cast pointers to anything but pointers.
2186 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2187 return false;
2188
Duncan Sands55e50902008-01-06 10:12:28 +00002189 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 // these cases, the cast is okay if the source and destination bit widths
2191 // are identical.
2192 return SrcBitSize == DstBitSize;
2193 }
2194}
2195
2196TruncInst::TruncInst(
2197 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2198) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002199 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200}
2201
2202TruncInst::TruncInst(
2203 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2204) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002205 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206}
2207
2208ZExtInst::ZExtInst(
2209 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2210) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002211 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212}
2213
2214ZExtInst::ZExtInst(
2215 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2216) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002217 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218}
2219SExtInst::SExtInst(
2220 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2221) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002222 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223}
2224
Jeff Cohencc08c832006-12-02 02:22:01 +00002225SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2227) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002228 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229}
2230
2231FPTruncInst::FPTruncInst(
2232 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2233) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235}
2236
2237FPTruncInst::FPTruncInst(
2238 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2239) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241}
2242
2243FPExtInst::FPExtInst(
2244 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2245) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247}
2248
2249FPExtInst::FPExtInst(
2250 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2251) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253}
2254
2255UIToFPInst::UIToFPInst(
2256 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2257) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259}
2260
2261UIToFPInst::UIToFPInst(
2262 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2263) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002264 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265}
2266
2267SIToFPInst::SIToFPInst(
2268 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2269) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271}
2272
2273SIToFPInst::SIToFPInst(
2274 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2275) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277}
2278
2279FPToUIInst::FPToUIInst(
2280 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2281) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002282 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283}
2284
2285FPToUIInst::FPToUIInst(
2286 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2287) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002288 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289}
2290
2291FPToSIInst::FPToSIInst(
2292 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2293) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002294 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295}
2296
2297FPToSIInst::FPToSIInst(
2298 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2299) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002300 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301}
2302
2303PtrToIntInst::PtrToIntInst(
2304 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2305) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002306 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307}
2308
2309PtrToIntInst::PtrToIntInst(
2310 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2311) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002312 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313}
2314
2315IntToPtrInst::IntToPtrInst(
2316 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2317) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002318 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319}
2320
2321IntToPtrInst::IntToPtrInst(
2322 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2323) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002324 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325}
2326
2327BitCastInst::BitCastInst(
2328 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2329) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002330 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331}
2332
2333BitCastInst::BitCastInst(
2334 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2335) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002336 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002338
2339//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002340// CmpInst Classes
2341//===----------------------------------------------------------------------===//
2342
Nate Begemand2195702008-05-12 19:01:56 +00002343CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2344 Value *LHS, Value *RHS, const std::string &Name,
2345 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002346 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002347 OperandTraits<CmpInst>::op_begin(this),
2348 OperandTraits<CmpInst>::operands(this),
2349 InsertBefore) {
2350 Op<0>().init(LHS, this);
2351 Op<1>().init(RHS, this);
Reid Spencerd9436b62006-11-20 01:22:35 +00002352 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002353 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002354}
Gabor Greiff6caff662008-05-10 08:32:32 +00002355
Nate Begemand2195702008-05-12 19:01:56 +00002356CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2357 Value *LHS, Value *RHS, const std::string &Name,
2358 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002359 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002360 OperandTraits<CmpInst>::op_begin(this),
2361 OperandTraits<CmpInst>::operands(this),
2362 InsertAtEnd) {
2363 Op<0>().init(LHS, this);
2364 Op<1>().init(RHS, this);
Reid Spencerd9436b62006-11-20 01:22:35 +00002365 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002366 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002367}
2368
2369CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002370CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002371 const std::string &Name, Instruction *InsertBefore) {
2372 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002373 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002374 InsertBefore);
2375 }
Nate Begemand2195702008-05-12 19:01:56 +00002376 if (Op == Instruction::FCmp) {
2377 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2378 InsertBefore);
2379 }
2380 if (Op == Instruction::VICmp) {
2381 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2382 InsertBefore);
2383 }
2384 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2385 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002386}
2387
2388CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002389CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002390 const std::string &Name, BasicBlock *InsertAtEnd) {
2391 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002392 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002393 InsertAtEnd);
2394 }
Nate Begemand2195702008-05-12 19:01:56 +00002395 if (Op == Instruction::FCmp) {
2396 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2397 InsertAtEnd);
2398 }
2399 if (Op == Instruction::VICmp) {
2400 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2401 InsertAtEnd);
2402 }
2403 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2404 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002405}
2406
2407void CmpInst::swapOperands() {
2408 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2409 IC->swapOperands();
2410 else
2411 cast<FCmpInst>(this)->swapOperands();
2412}
2413
2414bool CmpInst::isCommutative() {
2415 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2416 return IC->isCommutative();
2417 return cast<FCmpInst>(this)->isCommutative();
2418}
2419
2420bool CmpInst::isEquality() {
2421 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2422 return IC->isEquality();
2423 return cast<FCmpInst>(this)->isEquality();
2424}
2425
2426
2427ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2428 switch (pred) {
2429 default:
2430 assert(!"Unknown icmp predicate!");
2431 case ICMP_EQ: return ICMP_NE;
2432 case ICMP_NE: return ICMP_EQ;
2433 case ICMP_UGT: return ICMP_ULE;
2434 case ICMP_ULT: return ICMP_UGE;
2435 case ICMP_UGE: return ICMP_ULT;
2436 case ICMP_ULE: return ICMP_UGT;
2437 case ICMP_SGT: return ICMP_SLE;
2438 case ICMP_SLT: return ICMP_SGE;
2439 case ICMP_SGE: return ICMP_SLT;
2440 case ICMP_SLE: return ICMP_SGT;
2441 }
2442}
2443
2444ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2445 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002446 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002447 case ICMP_EQ: case ICMP_NE:
2448 return pred;
2449 case ICMP_SGT: return ICMP_SLT;
2450 case ICMP_SLT: return ICMP_SGT;
2451 case ICMP_SGE: return ICMP_SLE;
2452 case ICMP_SLE: return ICMP_SGE;
2453 case ICMP_UGT: return ICMP_ULT;
2454 case ICMP_ULT: return ICMP_UGT;
2455 case ICMP_UGE: return ICMP_ULE;
2456 case ICMP_ULE: return ICMP_UGE;
2457 }
2458}
2459
Reid Spencer266e42b2006-12-23 06:05:41 +00002460ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2461 switch (pred) {
2462 default: assert(! "Unknown icmp predicate!");
2463 case ICMP_EQ: case ICMP_NE:
2464 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2465 return pred;
2466 case ICMP_UGT: return ICMP_SGT;
2467 case ICMP_ULT: return ICMP_SLT;
2468 case ICMP_UGE: return ICMP_SGE;
2469 case ICMP_ULE: return ICMP_SLE;
2470 }
2471}
2472
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002473ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2474 switch (pred) {
2475 default: assert(! "Unknown icmp predicate!");
2476 case ICMP_EQ: case ICMP_NE:
2477 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2478 return pred;
2479 case ICMP_SGT: return ICMP_UGT;
2480 case ICMP_SLT: return ICMP_ULT;
2481 case ICMP_SGE: return ICMP_UGE;
2482 case ICMP_SLE: return ICMP_ULE;
2483 }
2484}
2485
Reid Spencer266e42b2006-12-23 06:05:41 +00002486bool ICmpInst::isSignedPredicate(Predicate pred) {
2487 switch (pred) {
2488 default: assert(! "Unknown icmp predicate!");
2489 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2490 return true;
2491 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2492 case ICMP_UGE: case ICMP_ULE:
2493 return false;
2494 }
2495}
2496
Reid Spencer0286bc12007-02-28 22:00:54 +00002497/// Initialize a set of values that all satisfy the condition with C.
2498///
2499ConstantRange
2500ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2501 APInt Lower(C);
2502 APInt Upper(C);
2503 uint32_t BitWidth = C.getBitWidth();
2504 switch (pred) {
2505 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2506 case ICmpInst::ICMP_EQ: Upper++; break;
2507 case ICmpInst::ICMP_NE: Lower++; break;
2508 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2509 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2510 case ICmpInst::ICMP_UGT:
2511 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2512 break;
2513 case ICmpInst::ICMP_SGT:
2514 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2515 break;
2516 case ICmpInst::ICMP_ULE:
2517 Lower = APInt::getMinValue(BitWidth); Upper++;
2518 break;
2519 case ICmpInst::ICMP_SLE:
2520 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2521 break;
2522 case ICmpInst::ICMP_UGE:
2523 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2524 break;
2525 case ICmpInst::ICMP_SGE:
2526 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2527 break;
2528 }
2529 return ConstantRange(Lower, Upper);
2530}
2531
Reid Spencerd9436b62006-11-20 01:22:35 +00002532FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2533 switch (pred) {
2534 default:
2535 assert(!"Unknown icmp predicate!");
2536 case FCMP_OEQ: return FCMP_UNE;
2537 case FCMP_ONE: return FCMP_UEQ;
2538 case FCMP_OGT: return FCMP_ULE;
2539 case FCMP_OLT: return FCMP_UGE;
2540 case FCMP_OGE: return FCMP_ULT;
2541 case FCMP_OLE: return FCMP_UGT;
2542 case FCMP_UEQ: return FCMP_ONE;
2543 case FCMP_UNE: return FCMP_OEQ;
2544 case FCMP_UGT: return FCMP_OLE;
2545 case FCMP_ULT: return FCMP_OGE;
2546 case FCMP_UGE: return FCMP_OLT;
2547 case FCMP_ULE: return FCMP_OGT;
2548 case FCMP_ORD: return FCMP_UNO;
2549 case FCMP_UNO: return FCMP_ORD;
2550 case FCMP_TRUE: return FCMP_FALSE;
2551 case FCMP_FALSE: return FCMP_TRUE;
2552 }
2553}
2554
2555FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2556 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002557 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002558 case FCMP_FALSE: case FCMP_TRUE:
2559 case FCMP_OEQ: case FCMP_ONE:
2560 case FCMP_UEQ: case FCMP_UNE:
2561 case FCMP_ORD: case FCMP_UNO:
2562 return pred;
2563 case FCMP_OGT: return FCMP_OLT;
2564 case FCMP_OLT: return FCMP_OGT;
2565 case FCMP_OGE: return FCMP_OLE;
2566 case FCMP_OLE: return FCMP_OGE;
2567 case FCMP_UGT: return FCMP_ULT;
2568 case FCMP_ULT: return FCMP_UGT;
2569 case FCMP_UGE: return FCMP_ULE;
2570 case FCMP_ULE: return FCMP_UGE;
2571 }
2572}
2573
Reid Spencer266e42b2006-12-23 06:05:41 +00002574bool CmpInst::isUnsigned(unsigned short predicate) {
2575 switch (predicate) {
2576 default: return false;
2577 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2578 case ICmpInst::ICMP_UGE: return true;
2579 }
2580}
2581
2582bool CmpInst::isSigned(unsigned short predicate){
2583 switch (predicate) {
2584 default: return false;
2585 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2586 case ICmpInst::ICMP_SGE: return true;
2587 }
2588}
2589
2590bool CmpInst::isOrdered(unsigned short predicate) {
2591 switch (predicate) {
2592 default: return false;
2593 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2594 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2595 case FCmpInst::FCMP_ORD: return true;
2596 }
2597}
2598
2599bool CmpInst::isUnordered(unsigned short predicate) {
2600 switch (predicate) {
2601 default: return false;
2602 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2603 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2604 case FCmpInst::FCMP_UNO: return true;
2605 }
2606}
2607
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002608//===----------------------------------------------------------------------===//
2609// SwitchInst Implementation
2610//===----------------------------------------------------------------------===//
2611
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002612void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002613 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002614 ReservedSpace = 2+NumCases*2;
2615 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002616 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002617
2618 OperandList[0].init(Value, this);
2619 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002620}
2621
Chris Lattner2195fc42007-02-24 00:55:48 +00002622/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2623/// switch on and a default destination. The number of additional cases can
2624/// be specified here to make memory allocation more efficient. This
2625/// constructor can also autoinsert before another instruction.
2626SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2627 Instruction *InsertBefore)
2628 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2629 init(Value, Default, NumCases);
2630}
2631
2632/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2633/// switch on and a default destination. The number of additional cases can
2634/// be specified here to make memory allocation more efficient. This
2635/// constructor also autoinserts at the end of the specified BasicBlock.
2636SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2637 BasicBlock *InsertAtEnd)
2638 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2639 init(Value, Default, NumCases);
2640}
2641
Misha Brukmanb1c93172005-04-21 23:48:37 +00002642SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002643 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002644 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002645 Use *OL = OperandList, *InOL = SI.OperandList;
2646 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2647 OL[i].init(InOL[i], this);
2648 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002649 }
2650}
2651
Gordon Henriksen14a55692007-12-10 02:14:30 +00002652SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002653 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002654}
2655
2656
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002657/// addCase - Add an entry to the switch instruction...
2658///
Chris Lattner47ac1872005-02-24 05:32:09 +00002659void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002660 unsigned OpNo = NumOperands;
2661 if (OpNo+2 > ReservedSpace)
2662 resizeOperands(0); // Get more space!
2663 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002664 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002665 NumOperands = OpNo+2;
2666 OperandList[OpNo].init(OnVal, this);
2667 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002668}
2669
2670/// removeCase - This method removes the specified successor from the switch
2671/// instruction. Note that this cannot be used to remove the default
2672/// destination (successor #0).
2673///
2674void SwitchInst::removeCase(unsigned idx) {
2675 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002676 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2677
2678 unsigned NumOps = getNumOperands();
2679 Use *OL = OperandList;
2680
2681 // Move everything after this operand down.
2682 //
2683 // FIXME: we could just swap with the end of the list, then erase. However,
2684 // client might not expect this to happen. The code as it is thrashes the
2685 // use/def lists, which is kinda lame.
2686 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2687 OL[i-2] = OL[i];
2688 OL[i-2+1] = OL[i+1];
2689 }
2690
2691 // Nuke the last value.
2692 OL[NumOps-2].set(0);
2693 OL[NumOps-2+1].set(0);
2694 NumOperands = NumOps-2;
2695}
2696
2697/// resizeOperands - resize operands - This adjusts the length of the operands
2698/// list according to the following behavior:
2699/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002700/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002701/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2702/// 3. If NumOps == NumOperands, trim the reserved space.
2703///
2704void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002705 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002706 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002707 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002708 } else if (NumOps*2 > NumOperands) {
2709 // No resize needed.
2710 if (ReservedSpace >= NumOps) return;
2711 } else if (NumOps == NumOperands) {
2712 if (ReservedSpace == NumOps) return;
2713 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002714 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002715 }
2716
2717 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002718 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002719 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002720 for (unsigned i = 0; i != e; ++i) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002721 NewOps[i].init(OldOps[i], this);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002722 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002723 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002724 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002725}
2726
2727
2728BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2729 return getSuccessor(idx);
2730}
2731unsigned SwitchInst::getNumSuccessorsV() const {
2732 return getNumSuccessors();
2733}
2734void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2735 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002736}
Chris Lattnerf22be932004-10-15 23:52:53 +00002737
Devang Patel295711f2008-02-19 22:15:16 +00002738//===----------------------------------------------------------------------===//
2739// GetResultInst Implementation
2740//===----------------------------------------------------------------------===//
2741
Devang Patel666d4512008-02-20 19:10:47 +00002742GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002743 const std::string &Name,
2744 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002745 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2746 ->getElementType(Index),
2747 GetResult, Aggregate, InsertBef),
2748 Idx(Index) {
2749 assert(isValidOperands(Aggregate, Index)
2750 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002751 setName(Name);
2752}
2753
Devang Patel666d4512008-02-20 19:10:47 +00002754bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2755 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002756 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002757
Devang Patelcf193b82008-02-20 19:39:41 +00002758 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2759 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002760 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002761 return false;
2762
2763 // getresult aggregate value's element types are restricted to
2764 // avoid nested aggregates.
2765 for (unsigned i = 0; i < NumElements; ++i)
2766 if (!STy->getElementType(i)->isFirstClassType())
2767 return false;
2768
2769 // Otherwise, Aggregate is valid.
2770 return true;
2771 }
Devang Patel666d4512008-02-20 19:10:47 +00002772 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002773}
2774
Chris Lattnerf22be932004-10-15 23:52:53 +00002775// Define these methods here so vtables don't get emitted into every translation
2776// unit that uses these classes.
2777
2778GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002779 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002780}
2781
2782BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002783 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002784}
2785
Chris Lattner0b490b02007-08-24 20:48:18 +00002786FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002787 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002788}
2789ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002790 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002791}
2792
Nate Begemand2195702008-05-12 19:01:56 +00002793VFCmpInst* VFCmpInst::clone() const {
2794 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2795}
2796VICmpInst* VICmpInst::clone() const {
2797 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2798}
2799
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002800MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2801AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2802FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2803LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2804StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2805CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2806CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2807CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2808CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2809CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2810CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2811CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2812CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2813CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2814CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2815CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2816CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002817CallInst *CallInst::clone() const {
2818 return new(getNumOperands()) CallInst(*this);
2819}
2820SelectInst *SelectInst::clone() const {
2821 return new(getNumOperands()) SelectInst(*this);
2822}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2824
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002825ExtractElementInst *ExtractElementInst::clone() const {
2826 return new ExtractElementInst(*this);
2827}
2828InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002829 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002830}
2831ShuffleVectorInst *ShuffleVectorInst::clone() const {
2832 return new ShuffleVectorInst(*this);
2833}
Chris Lattnerf22be932004-10-15 23:52:53 +00002834PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002835ReturnInst *ReturnInst::clone() const {
2836 return new(getNumOperands()) ReturnInst(*this);
2837}
2838BranchInst *BranchInst::clone() const {
2839 return new(getNumOperands()) BranchInst(*this);
2840}
Gabor Greiff6caff662008-05-10 08:32:32 +00002841SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002842InvokeInst *InvokeInst::clone() const {
2843 return new(getNumOperands()) InvokeInst(*this);
2844}
Chris Lattnerf22be932004-10-15 23:52:53 +00002845UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002846UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002847GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }