blob: 5297374e251db649ba75deb6893a4ef3cffbf160 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
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"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattnerf7b6d312005-05-06 20:26:43 +000026unsigned CallSite::getCallingConv() const {
27 if (CallInst *CI = dyn_cast<CallInst>(I))
28 return CI->getCallingConv();
29 else
30 return cast<InvokeInst>(I)->getCallingConv();
31}
32void CallSite::setCallingConv(unsigned CC) {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 CI->setCallingConv(CC);
35 else
36 cast<InvokeInst>(I)->setCallingConv(CC);
37}
38
39
Chris Lattner1c12a882006-06-21 16:53:47 +000040
41
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000042//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000043// TerminatorInst Class
44//===----------------------------------------------------------------------===//
45
Chris Lattner1c12a882006-06-21 16:53:47 +000046// Out of line virtual method, so the vtable, etc has a home.
47TerminatorInst::~TerminatorInst() {
48}
49
50// Out of line virtual method, so the vtable, etc has a home.
51UnaryInstruction::~UnaryInstruction() {
52}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000053
54
55//===----------------------------------------------------------------------===//
56// PHINode Class
57//===----------------------------------------------------------------------===//
58
59PHINode::PHINode(const PHINode &PN)
60 : Instruction(PN.getType(), Instruction::PHI,
61 new Use[PN.getNumOperands()], PN.getNumOperands()),
62 ReservedSpace(PN.getNumOperands()) {
63 Use *OL = OperandList;
64 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
65 OL[i].init(PN.getOperand(i), this);
66 OL[i+1].init(PN.getOperand(i+1), this);
67 }
68}
69
70PHINode::~PHINode() {
71 delete [] OperandList;
72}
73
74// removeIncomingValue - Remove an incoming value. This is useful if a
75// predecessor basic block is deleted.
76Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
77 unsigned NumOps = getNumOperands();
78 Use *OL = OperandList;
79 assert(Idx*2 < NumOps && "BB not in PHI node!");
80 Value *Removed = OL[Idx*2];
81
82 // Move everything after this operand down.
83 //
84 // FIXME: we could just swap with the end of the list, then erase. However,
85 // client might not expect this to happen. The code as it is thrashes the
86 // use/def lists, which is kinda lame.
87 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
88 OL[i-2] = OL[i];
89 OL[i-2+1] = OL[i+1];
90 }
91
92 // Nuke the last value.
93 OL[NumOps-2].set(0);
94 OL[NumOps-2+1].set(0);
95 NumOperands = NumOps-2;
96
97 // If the PHI node is dead, because it has zero entries, nuke it now.
98 if (NumOps == 2 && DeletePHIIfEmpty) {
99 // If anyone is using this PHI, make them use a dummy value instead...
100 replaceAllUsesWith(UndefValue::get(getType()));
101 eraseFromParent();
102 }
103 return Removed;
104}
105
106/// resizeOperands - resize operands - This adjusts the length of the operands
107/// list according to the following behavior:
108/// 1. If NumOps == 0, grow the operand list in response to a push_back style
109/// of operation. This grows the number of ops by 1.5 times.
110/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
111/// 3. If NumOps == NumOperands, trim the reserved space.
112///
113void PHINode::resizeOperands(unsigned NumOps) {
114 if (NumOps == 0) {
115 NumOps = (getNumOperands())*3/2;
116 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
117 } else if (NumOps*2 > NumOperands) {
118 // No resize needed.
119 if (ReservedSpace >= NumOps) return;
120 } else if (NumOps == NumOperands) {
121 if (ReservedSpace == NumOps) return;
122 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124 }
125
126 ReservedSpace = NumOps;
127 Use *NewOps = new Use[NumOps];
128 Use *OldOps = OperandList;
129 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
130 NewOps[i].init(OldOps[i], this);
131 OldOps[i].set(0);
132 }
133 delete [] OldOps;
134 OperandList = NewOps;
135}
136
Nate Begemanb3923212005-08-04 23:24:19 +0000137/// hasConstantValue - If the specified PHI node always merges together the same
138/// value, return the value, otherwise return null.
139///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000140Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000141 // If the PHI node only has one incoming value, eliminate the PHI node...
142 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000143 if (getIncomingValue(0) != this) // not X = phi X
144 return getIncomingValue(0);
145 else
146 return UndefValue::get(getType()); // Self cycle is dead.
147
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // Otherwise if all of the incoming values are the same for the PHI, replace
149 // the PHI node with the incoming value.
150 //
151 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000152 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000153 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000154 if (isa<UndefValue>(getIncomingValue(i)))
155 HasUndefInput = true;
156 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000157 if (InVal && getIncomingValue(i) != InVal)
158 return 0; // Not the same, bail out.
159 else
160 InVal = getIncomingValue(i);
161
162 // The only case that could cause InVal to be null is if we have a PHI node
163 // that only has entries for itself. In this case, there is no entry into the
164 // loop, so kill the PHI.
165 //
166 if (InVal == 0) InVal = UndefValue::get(getType());
167
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000168 // If we have a PHI node like phi(X, undef, X), where X is defined by some
169 // instruction, we cannot always return X as the result of the PHI node. Only
170 // do this if X is not an instruction (thus it must dominate the PHI block),
171 // or if the client is prepared to deal with this possibility.
172 if (HasUndefInput && !AllowNonDominatingInstruction)
173 if (Instruction *IV = dyn_cast<Instruction>(InVal))
174 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000175 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000176 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000177 return 0; // Cannot guarantee that InVal dominates this PHINode.
178
Nate Begemanb3923212005-08-04 23:24:19 +0000179 // All of the incoming values are the same, return the value now.
180 return InVal;
181}
182
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000183
184//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000185// CallInst Implementation
186//===----------------------------------------------------------------------===//
187
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188CallInst::~CallInst() {
189 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000190 if (ParamAttrs)
191 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000192}
193
Chris Lattner054ba2c2007-02-13 00:58:44 +0000194void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000195 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000196 NumOperands = NumParams+1;
197 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000198 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199
Misha Brukmanb1c93172005-04-21 23:48:37 +0000200 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000201 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000202 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Chris Lattner054ba2c2007-02-13 00:58:44 +0000204 assert((NumParams == FTy->getNumParams() ||
205 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000206 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000207 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000208 assert((i >= FTy->getNumParams() ||
209 FTy->getParamType(i) == Params[i]->getType()) &&
210 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000212 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000213}
214
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000216 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000217 NumOperands = 3;
218 Use *OL = OperandList = new Use[3];
219 OL[0].init(Func, this);
220 OL[1].init(Actual1, this);
221 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000222
223 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000225 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000227 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000228 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000230 assert((0 >= FTy->getNumParams() ||
231 FTy->getParamType(0) == Actual1->getType()) &&
232 "Calling a function with a bad signature!");
233 assert((1 >= FTy->getNumParams() ||
234 FTy->getParamType(1) == Actual2->getType()) &&
235 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236}
237
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000238void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000239 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240 NumOperands = 2;
241 Use *OL = OperandList = new Use[2];
242 OL[0].init(Func, this);
243 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000244
245 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000247 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000249 assert((FTy->getNumParams() == 1 ||
250 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000252 assert((0 == FTy->getNumParams() ||
253 FTy->getParamType(0) == Actual->getType()) &&
254 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255}
256
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000258 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000259 NumOperands = 1;
260 Use *OL = OperandList = new Use[1];
261 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000265 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000267 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268}
269
David Greene17a5dfe2007-08-01 03:43:44 +0000270#if 0
271// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000272CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000275 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000276 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000277 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000278 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000279}
280CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
281 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000282 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
283 ->getElementType())->getReturnType(),
284 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000285 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000286 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000287}
288
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
290 const std::string &Name, Instruction *InsertBefore)
291 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
292 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000293 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000295 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
298CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
299 const std::string &Name, BasicBlock *InsertAtEnd)
300 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
301 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000302 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000304 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305}
David Greene17a5dfe2007-08-01 03:43:44 +0000306#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000308 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
310 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000311 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000313 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
316CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
317 BasicBlock *InsertAtEnd)
318 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
319 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000320 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000322 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324CallInst::CallInst(Value *Func, const std::string &Name,
325 Instruction *InsertBefore)
326 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
327 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000330 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
333CallInst::CallInst(Value *Func, const std::string &Name,
334 BasicBlock *InsertAtEnd)
335 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
336 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000337 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000339 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000343 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
344 CI.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000345 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000346 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000347 Use *OL = OperandList;
348 Use *InOL = CI.OperandList;
349 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
350 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
Reid Spencerc6a83842007-04-22 17:28:03 +0000353void CallInst::setParamAttrs(ParamAttrsList *newAttrs) {
354 if (ParamAttrs)
355 ParamAttrs->dropRef();
356
357 if (newAttrs)
358 newAttrs->addRef();
359
360 ParamAttrs = newAttrs;
361}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362
363//===----------------------------------------------------------------------===//
364// InvokeInst Implementation
365//===----------------------------------------------------------------------===//
366
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000367InvokeInst::~InvokeInst() {
368 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000369 if (ParamAttrs)
370 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000371}
372
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000374 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000375 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000376 NumOperands = 3+NumArgs;
377 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000378 OL[0].init(Fn, this);
379 OL[1].init(IfNormal, this);
380 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000381 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000382 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000383 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000384
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000385 assert((NumArgs == FTy->getNumParams()) ||
386 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000388
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000389 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000390 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000391 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000392 "Invoking a function with a bad signature!");
393
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000394 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000395 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000396}
397
398InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
399 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000400 Value* const *Args, unsigned NumArgs,
401 const std::string &Name, Instruction *InsertBefore)
402 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
403 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000404 Instruction::Invoke, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000405 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000406 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000407}
408
409InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
410 BasicBlock *IfException,
411 Value* const *Args, unsigned NumArgs,
412 const std::string &Name, BasicBlock *InsertAtEnd)
413 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
414 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000415 Instruction::Invoke, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000416 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000417 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000418}
419
Misha Brukmanb1c93172005-04-21 23:48:37 +0000420InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000421 : TerminatorInst(II.getType(), Instruction::Invoke,
422 new Use[II.getNumOperands()], II.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000423 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000424 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000425 Use *OL = OperandList, *InOL = II.OperandList;
426 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
427 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000428}
429
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000430BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
431 return getSuccessor(idx);
432}
433unsigned InvokeInst::getNumSuccessorsV() const {
434 return getNumSuccessors();
435}
436void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
437 return setSuccessor(idx, B);
438}
439
Reid Spencerc6a83842007-04-22 17:28:03 +0000440void InvokeInst::setParamAttrs(ParamAttrsList *newAttrs) {
441 if (ParamAttrs)
442 ParamAttrs->dropRef();
443
444 if (newAttrs)
445 newAttrs->addRef();
446
447 ParamAttrs = newAttrs;
448}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000449
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000450//===----------------------------------------------------------------------===//
451// ReturnInst Implementation
452//===----------------------------------------------------------------------===//
453
Chris Lattner2195fc42007-02-24 00:55:48 +0000454ReturnInst::ReturnInst(const ReturnInst &RI)
455 : TerminatorInst(Type::VoidTy, Instruction::Ret,
456 &RetVal, RI.getNumOperands()) {
457 if (RI.getNumOperands())
458 RetVal.init(RI.RetVal, this);
459}
460
461ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
462 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
463 init(retVal);
464}
465ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
466 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
467 init(retVal);
468}
469ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
470 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
471}
472
473
474
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000475void ReturnInst::init(Value *retVal) {
476 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000477 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000478 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000479 NumOperands = 1;
480 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000481 }
482}
483
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000484unsigned ReturnInst::getNumSuccessorsV() const {
485 return getNumSuccessors();
486}
487
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000488// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
489// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000490void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000491 assert(0 && "ReturnInst has no successors!");
492}
493
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000494BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
495 assert(0 && "ReturnInst has no successors!");
496 abort();
497 return 0;
498}
499
500
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000501//===----------------------------------------------------------------------===//
502// UnwindInst Implementation
503//===----------------------------------------------------------------------===//
504
Chris Lattner2195fc42007-02-24 00:55:48 +0000505UnwindInst::UnwindInst(Instruction *InsertBefore)
506 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
507}
508UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
509 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
510}
511
512
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000513unsigned UnwindInst::getNumSuccessorsV() const {
514 return getNumSuccessors();
515}
516
517void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000518 assert(0 && "UnwindInst has no successors!");
519}
520
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000521BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
522 assert(0 && "UnwindInst has no successors!");
523 abort();
524 return 0;
525}
526
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000527//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000528// UnreachableInst Implementation
529//===----------------------------------------------------------------------===//
530
Chris Lattner2195fc42007-02-24 00:55:48 +0000531UnreachableInst::UnreachableInst(Instruction *InsertBefore)
532 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
533}
534UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
535 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
536}
537
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538unsigned UnreachableInst::getNumSuccessorsV() const {
539 return getNumSuccessors();
540}
541
542void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
543 assert(0 && "UnwindInst has no successors!");
544}
545
546BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
547 assert(0 && "UnwindInst has no successors!");
548 abort();
549 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000550}
551
552//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000553// BranchInst Implementation
554//===----------------------------------------------------------------------===//
555
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556void BranchInst::AssertOK() {
557 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000558 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000559 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000560}
561
Chris Lattner2195fc42007-02-24 00:55:48 +0000562BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
563 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
564 assert(IfTrue != 0 && "Branch destination may not be null!");
565 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
566}
567BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
568 Instruction *InsertBefore)
569: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
570 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
571 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
572 Ops[2].init(Cond, this);
573#ifndef NDEBUG
574 AssertOK();
575#endif
576}
577
578BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
579 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
580 assert(IfTrue != 0 && "Branch destination may not be null!");
581 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
582}
583
584BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
585 BasicBlock *InsertAtEnd)
586 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
587 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
588 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
589 Ops[2].init(Cond, this);
590#ifndef NDEBUG
591 AssertOK();
592#endif
593}
594
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000597 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000598 OperandList[0].init(BI.getOperand(0), this);
599 if (BI.getNumOperands() != 1) {
600 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
601 OperandList[1].init(BI.getOperand(1), this);
602 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603 }
604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
607 return getSuccessor(idx);
608}
609unsigned BranchInst::getNumSuccessorsV() const {
610 return getNumSuccessors();
611}
612void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
613 setSuccessor(idx, B);
614}
615
616
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617//===----------------------------------------------------------------------===//
618// AllocationInst Implementation
619//===----------------------------------------------------------------------===//
620
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000621static Value *getAISize(Value *Amt) {
622 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000623 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000624 else {
625 assert(!isa<BasicBlock>(Amt) &&
626 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000627 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000628 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000629 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000630 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000631}
632
Misha Brukmanb1c93172005-04-21 23:48:37 +0000633AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000634 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000637 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000638 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000639 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000640 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641}
642
Misha Brukmanb1c93172005-04-21 23:48:37 +0000643AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000644 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000645 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000647 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000648 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000650 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000651}
652
Chris Lattner1c12a882006-06-21 16:53:47 +0000653// Out of line virtual method, so the vtable, etc has a home.
654AllocationInst::~AllocationInst() {
655}
656
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000657bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000658 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
659 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661}
662
663const Type *AllocationInst::getAllocatedType() const {
664 return getType()->getElementType();
665}
666
667AllocaInst::AllocaInst(const AllocaInst &AI)
668 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000669 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000670}
671
672MallocInst::MallocInst(const MallocInst &MI)
673 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000674 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675}
676
677//===----------------------------------------------------------------------===//
678// FreeInst Implementation
679//===----------------------------------------------------------------------===//
680
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000681void FreeInst::AssertOK() {
682 assert(isa<PointerType>(getOperand(0)->getType()) &&
683 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684}
685
686FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000687 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000689}
690
691FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000692 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
696
697//===----------------------------------------------------------------------===//
698// LoadInst Implementation
699//===----------------------------------------------------------------------===//
700
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000702 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000703 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704}
705
706LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000708 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000709 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000710 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000712 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713}
714
715LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000717 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000718 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000719 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000721 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
724LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
725 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000727 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000728 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000729 setAlignment(0);
730 AssertOK();
731 setName(Name);
732}
733
734LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
735 unsigned Align, Instruction *InsertBef)
736 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
737 Load, Ptr, InsertBef) {
738 setVolatile(isVolatile);
739 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000741 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742}
743
Dan Gohman68659282007-07-18 20:51:11 +0000744LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
745 unsigned Align, BasicBlock *InsertAE)
746 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
747 Load, Ptr, InsertAE) {
748 setVolatile(isVolatile);
749 setAlignment(Align);
750 AssertOK();
751 setName(Name);
752}
753
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
755 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000757 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000758 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000759 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000760 AssertOK();
761 setName(Name);
762}
763
764
765
766LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000767 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
768 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000769 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000770 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000771 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000772 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000773}
774
775LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000776 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
777 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000778 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000779 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000780 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000781 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000782}
783
784LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
785 Instruction *InsertBef)
786: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000787 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000788 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000789 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000790 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000791 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000792}
793
794LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
795 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000796 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
797 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000798 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000799 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000801 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802}
803
Christopher Lamb84485702007-04-22 19:24:39 +0000804void LoadInst::setAlignment(unsigned Align) {
805 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
806 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
807}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000808
809//===----------------------------------------------------------------------===//
810// StoreInst Implementation
811//===----------------------------------------------------------------------===//
812
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000813void StoreInst::AssertOK() {
814 assert(isa<PointerType>(getOperand(1)->getType()) &&
815 "Ptr must have pointer type!");
816 assert(getOperand(0)->getType() ==
817 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000818 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000821
822StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000823 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 Ops[0].init(val, this);
825 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000826 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000827 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000828 AssertOK();
829}
830
831StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000832 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000833 Ops[0].init(val, this);
834 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000835 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000836 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000837 AssertOK();
838}
839
Misha Brukmanb1c93172005-04-21 23:48:37 +0000840StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000842 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000843 Ops[0].init(val, this);
844 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000845 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000846 setAlignment(0);
847 AssertOK();
848}
849
850StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
851 unsigned Align, Instruction *InsertBefore)
852 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
853 Ops[0].init(val, this);
854 Ops[1].init(addr, this);
855 setVolatile(isVolatile);
856 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000857 AssertOK();
858}
859
Misha Brukmanb1c93172005-04-21 23:48:37 +0000860StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000861 unsigned Align, BasicBlock *InsertAtEnd)
862 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
863 Ops[0].init(val, this);
864 Ops[1].init(addr, this);
865 setVolatile(isVolatile);
866 setAlignment(Align);
867 AssertOK();
868}
869
870StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000873 Ops[0].init(val, this);
874 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000875 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000876 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878}
879
Christopher Lamb84485702007-04-22 19:24:39 +0000880void StoreInst::setAlignment(unsigned Align) {
881 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
882 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
883}
884
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885//===----------------------------------------------------------------------===//
886// GetElementPtrInst Implementation
887//===----------------------------------------------------------------------===//
888
889// checkType - Simple wrapper function to give a better assertion failure
890// message on bad indexes for a gep instruction.
891//
892static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000893 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894 return Ty;
895}
896
Chris Lattner79807c3d2007-01-31 19:47:18 +0000897void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
898 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000899 Use *OL = OperandList = new Use[NumOperands];
900 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901
Chris Lattner79807c3d2007-01-31 19:47:18 +0000902 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904}
905
906void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907 NumOperands = 3;
908 Use *OL = OperandList = new Use[3];
909 OL[0].init(Ptr, this);
910 OL[1].init(Idx0, this);
911 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912}
913
Chris Lattner82981202005-05-03 05:43:30 +0000914void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
915 NumOperands = 2;
916 Use *OL = OperandList = new Use[2];
917 OL[0].init(Ptr, this);
918 OL[1].init(Idx, this);
919}
920
Chris Lattner79807c3d2007-01-31 19:47:18 +0000921
922GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
923 unsigned NumIdx,
924 const std::string &Name, Instruction *InBe)
925: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000926 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000927 GetElementPtr, 0, 0, InBe) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000928 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000929 setName(Name);
Chris Lattner79807c3d2007-01-31 19:47:18 +0000930}
931
932GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
933 unsigned NumIdx,
934 const std::string &Name, BasicBlock *IAE)
935: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000936 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000937 GetElementPtr, 0, 0, IAE) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000938 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000939 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000940}
941
Chris Lattner82981202005-05-03 05:43:30 +0000942GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
943 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000944 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
945 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000946 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000947 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000948}
949
950GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
951 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
953 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000954 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000955 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000956}
957
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000958GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
959 const std::string &Name, Instruction *InBe)
960 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
961 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000962 GetElementPtr, 0, 0, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000964 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000965}
966
967GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000968 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
970 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000971 GetElementPtr, 0, 0, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000972 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000973 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974}
975
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976GetElementPtrInst::~GetElementPtrInst() {
977 delete[] OperandList;
978}
979
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980// getIndexedType - Returns the type of the element that would be loaded with
981// a load instruction with the specified parameters.
982//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000983// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984// pointer type.
985//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000986const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000987 Value* const *Idxs,
988 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000989 bool AllowCompositeLeaf) {
990 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
991
992 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000993 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994 if (AllowCompositeLeaf ||
995 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
996 return cast<PointerType>(Ptr)->getElementType();
997 else
998 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000999
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000 unsigned CurIdx = 0;
1001 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001002 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001003 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1004 return 0; // Can't load a whole structure or array!?!?
1005 }
1006
Chris Lattner302116a2007-01-31 04:40:28 +00001007 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001008 if (isa<PointerType>(CT) && CurIdx != 1)
1009 return 0; // Can only index into pointer types at the first index!
1010 if (!CT->indexValid(Index)) return 0;
1011 Ptr = CT->getTypeAtIndex(Index);
1012
1013 // If the new type forwards to another type, then it is in the middle
1014 // of being refined to another type (and hence, may have dropped all
1015 // references to what it was using before). So, use the new forwarded
1016 // type.
1017 if (const Type * Ty = Ptr->getForwardedType()) {
1018 Ptr = Ty;
1019 }
1020 }
Chris Lattner302116a2007-01-31 04:40:28 +00001021 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001022}
1023
Misha Brukmanb1c93172005-04-21 23:48:37 +00001024const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001025 Value *Idx0, Value *Idx1,
1026 bool AllowCompositeLeaf) {
1027 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1028 if (!PTy) return 0; // Type isn't a pointer type!
1029
1030 // Check the pointer index.
1031 if (!PTy->indexValid(Idx0)) return 0;
1032
1033 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
1034 if (!CT || !CT->indexValid(Idx1)) return 0;
1035
1036 const Type *ElTy = CT->getTypeAtIndex(Idx1);
1037 if (AllowCompositeLeaf || ElTy->isFirstClassType())
1038 return ElTy;
1039 return 0;
1040}
1041
Chris Lattner82981202005-05-03 05:43:30 +00001042const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1043 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1044 if (!PTy) return 0; // Type isn't a pointer type!
1045
1046 // Check the pointer index.
1047 if (!PTy->indexValid(Idx)) return 0;
1048
Chris Lattnerc2233332005-05-03 16:44:45 +00001049 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001050}
1051
Chris Lattner45f15572007-04-14 00:12:57 +00001052
1053/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1054/// zeros. If so, the result pointer and the first operand have the same
1055/// value, just potentially different types.
1056bool GetElementPtrInst::hasAllZeroIndices() const {
1057 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1058 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1059 if (!CI->isZero()) return false;
1060 } else {
1061 return false;
1062 }
1063 }
1064 return true;
1065}
1066
Chris Lattner27058292007-04-27 20:35:56 +00001067/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1068/// constant integers. If so, the result pointer and the first operand have
1069/// a constant offset between them.
1070bool GetElementPtrInst::hasAllConstantIndices() const {
1071 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1072 if (!isa<ConstantInt>(getOperand(i)))
1073 return false;
1074 }
1075 return true;
1076}
1077
Chris Lattner45f15572007-04-14 00:12:57 +00001078
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001079//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001080// ExtractElementInst Implementation
1081//===----------------------------------------------------------------------===//
1082
1083ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001084 const std::string &Name,
1085 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001086 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001087 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001088 assert(isValidOperands(Val, Index) &&
1089 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001090 Ops[0].init(Val, this);
1091 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001092 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001093}
1094
Chris Lattner65511ff2006-10-05 06:24:58 +00001095ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1096 const std::string &Name,
1097 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001098 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001099 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001100 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001101 assert(isValidOperands(Val, Index) &&
1102 "Invalid extractelement instruction operands!");
1103 Ops[0].init(Val, this);
1104 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001105 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001106}
1107
1108
Robert Bocchino23004482006-01-10 19:05:34 +00001109ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001110 const std::string &Name,
1111 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001112 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001113 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001114 assert(isValidOperands(Val, Index) &&
1115 "Invalid extractelement instruction operands!");
1116
Robert Bocchino23004482006-01-10 19:05:34 +00001117 Ops[0].init(Val, this);
1118 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001119 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001120}
1121
Chris Lattner65511ff2006-10-05 06:24:58 +00001122ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1123 const std::string &Name,
1124 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001125 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001126 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001127 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001128 assert(isValidOperands(Val, Index) &&
1129 "Invalid extractelement instruction operands!");
1130
1131 Ops[0].init(Val, this);
1132 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001133 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001134}
1135
1136
Chris Lattner54865b32006-04-08 04:05:48 +00001137bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001138 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001139 return false;
1140 return true;
1141}
1142
1143
Robert Bocchino23004482006-01-10 19:05:34 +00001144//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001145// InsertElementInst Implementation
1146//===----------------------------------------------------------------------===//
1147
Chris Lattner0875d942006-04-14 22:20:32 +00001148InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1149 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1150 Ops[0].init(IE.Ops[0], this);
1151 Ops[1].init(IE.Ops[1], this);
1152 Ops[2].init(IE.Ops[2], this);
1153}
Chris Lattner54865b32006-04-08 04:05:48 +00001154InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001155 const std::string &Name,
1156 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001157 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001158 assert(isValidOperands(Vec, Elt, Index) &&
1159 "Invalid insertelement instruction operands!");
1160 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001161 Ops[1].init(Elt, this);
1162 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001163 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001164}
1165
Chris Lattner65511ff2006-10-05 06:24:58 +00001166InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1167 const std::string &Name,
1168 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001169 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001170 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001171 assert(isValidOperands(Vec, Elt, Index) &&
1172 "Invalid insertelement instruction operands!");
1173 Ops[0].init(Vec, this);
1174 Ops[1].init(Elt, this);
1175 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001176 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001177}
1178
1179
Chris Lattner54865b32006-04-08 04:05:48 +00001180InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001181 const std::string &Name,
1182 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001183 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001184 assert(isValidOperands(Vec, Elt, Index) &&
1185 "Invalid insertelement instruction operands!");
1186
1187 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001188 Ops[1].init(Elt, this);
1189 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001190 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001191}
1192
Chris Lattner65511ff2006-10-05 06:24:58 +00001193InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1194 const std::string &Name,
1195 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001196: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001197 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001198 assert(isValidOperands(Vec, Elt, Index) &&
1199 "Invalid insertelement instruction operands!");
1200
1201 Ops[0].init(Vec, this);
1202 Ops[1].init(Elt, this);
1203 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001204 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001205}
1206
Chris Lattner54865b32006-04-08 04:05:48 +00001207bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1208 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001209 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001210 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001211
Reid Spencerd84d35b2007-02-15 02:26:10 +00001212 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001213 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001214
Reid Spencer8d9336d2006-12-31 05:26:44 +00001215 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001216 return false; // Third operand of insertelement must be uint.
1217 return true;
1218}
1219
1220
Robert Bocchinoca27f032006-01-17 20:07:22 +00001221//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001222// ShuffleVectorInst Implementation
1223//===----------------------------------------------------------------------===//
1224
Chris Lattner0875d942006-04-14 22:20:32 +00001225ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1226 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1227 Ops[0].init(SV.Ops[0], this);
1228 Ops[1].init(SV.Ops[1], this);
1229 Ops[2].init(SV.Ops[2], this);
1230}
1231
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001232ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1233 const std::string &Name,
1234 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001235 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001236 assert(isValidOperands(V1, V2, Mask) &&
1237 "Invalid shuffle vector instruction operands!");
1238 Ops[0].init(V1, this);
1239 Ops[1].init(V2, this);
1240 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001241 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001242}
1243
1244ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1245 const std::string &Name,
1246 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001247 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001248 assert(isValidOperands(V1, V2, Mask) &&
1249 "Invalid shuffle vector instruction operands!");
1250
1251 Ops[0].init(V1, this);
1252 Ops[1].init(V2, this);
1253 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001254 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001255}
1256
1257bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1258 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001259 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001260 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001261 if (!isa<VectorType>(Mask->getType()) ||
1262 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1263 cast<VectorType>(Mask->getType())->getNumElements() !=
1264 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001265 return false;
1266 return true;
1267}
1268
1269
1270//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001271// BinaryOperator Class
1272//===----------------------------------------------------------------------===//
1273
Chris Lattner2195fc42007-02-24 00:55:48 +00001274BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1275 const Type *Ty, const std::string &Name,
1276 Instruction *InsertBefore)
1277 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1278 Ops[0].init(S1, this);
1279 Ops[1].init(S2, this);
1280 init(iType);
1281 setName(Name);
1282}
1283
1284BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1285 const Type *Ty, const std::string &Name,
1286 BasicBlock *InsertAtEnd)
1287 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1288 Ops[0].init(S1, this);
1289 Ops[1].init(S2, this);
1290 init(iType);
1291 setName(Name);
1292}
1293
1294
1295void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001296 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001297 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001298 assert(LHS->getType() == RHS->getType() &&
1299 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001300#ifndef NDEBUG
1301 switch (iType) {
1302 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001303 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001304 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001305 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001306 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001307 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001308 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001309 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001310 case UDiv:
1311 case SDiv:
1312 assert(getType() == LHS->getType() &&
1313 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001314 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1315 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001316 "Incorrect operand type (not integer) for S/UDIV");
1317 break;
1318 case FDiv:
1319 assert(getType() == LHS->getType() &&
1320 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001321 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1322 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001323 && "Incorrect operand type (not floating point) for FDIV");
1324 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001325 case URem:
1326 case SRem:
1327 assert(getType() == LHS->getType() &&
1328 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001329 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1330 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001331 "Incorrect operand type (not integer) for S/UREM");
1332 break;
1333 case FRem:
1334 assert(getType() == LHS->getType() &&
1335 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001336 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1337 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001338 && "Incorrect operand type (not floating point) for FREM");
1339 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001340 case Shl:
1341 case LShr:
1342 case AShr:
1343 assert(getType() == LHS->getType() &&
1344 "Shift operation should return same type as operands!");
1345 assert(getType()->isInteger() &&
1346 "Shift operation requires integer operands");
1347 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001348 case And: case Or:
1349 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001350 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001351 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001352 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001353 (isa<VectorType>(getType()) &&
1354 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001355 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001356 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001357 default:
1358 break;
1359 }
1360#endif
1361}
1362
1363BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001364 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001365 Instruction *InsertBefore) {
1366 assert(S1->getType() == S2->getType() &&
1367 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001368 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001369}
1370
1371BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001372 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001373 BasicBlock *InsertAtEnd) {
1374 BinaryOperator *Res = create(Op, S1, S2, Name);
1375 InsertAtEnd->getInstList().push_back(Res);
1376 return Res;
1377}
1378
1379BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1380 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001381 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1382 return new BinaryOperator(Instruction::Sub,
1383 zero, Op,
1384 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385}
1386
1387BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1388 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001389 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1390 return new BinaryOperator(Instruction::Sub,
1391 zero, Op,
1392 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001393}
1394
1395BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1396 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001397 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001398 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001399 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001400 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001401 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001402 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001403 }
1404
1405 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001406 Op->getType(), Name, InsertBefore);
1407}
1408
1409BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1410 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001411 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001412 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001413 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001414 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001415 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001416 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001417 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001418 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001419 }
1420
1421 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001422 Op->getType(), Name, InsertAtEnd);
1423}
1424
1425
1426// isConstantAllOnes - Helper function for several functions below
1427static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001428 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1429 return CI->isAllOnesValue();
1430 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1431 return CV->isAllOnesValue();
1432 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001433}
1434
1435bool BinaryOperator::isNeg(const Value *V) {
1436 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1437 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001438 return Bop->getOperand(0) ==
1439 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001440 return false;
1441}
1442
1443bool BinaryOperator::isNot(const Value *V) {
1444 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1445 return (Bop->getOpcode() == Instruction::Xor &&
1446 (isConstantAllOnes(Bop->getOperand(1)) ||
1447 isConstantAllOnes(Bop->getOperand(0))));
1448 return false;
1449}
1450
Chris Lattner2c7d1772005-04-24 07:28:37 +00001451Value *BinaryOperator::getNegArgument(Value *BinOp) {
1452 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1453 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001454}
1455
Chris Lattner2c7d1772005-04-24 07:28:37 +00001456const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1457 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001458}
1459
Chris Lattner2c7d1772005-04-24 07:28:37 +00001460Value *BinaryOperator::getNotArgument(Value *BinOp) {
1461 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1462 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1463 Value *Op0 = BO->getOperand(0);
1464 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001465 if (isConstantAllOnes(Op0)) return Op1;
1466
1467 assert(isConstantAllOnes(Op1));
1468 return Op0;
1469}
1470
Chris Lattner2c7d1772005-04-24 07:28:37 +00001471const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1472 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001473}
1474
1475
1476// swapOperands - Exchange the two operands to this instruction. This
1477// instruction is safe to use on any binary instruction and does not
1478// modify the semantics of the instruction. If the instruction is
1479// order dependent (SetLT f.e.) the opcode is changed.
1480//
1481bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001482 if (!isCommutative())
1483 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001484 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001485 return false;
1486}
1487
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001488//===----------------------------------------------------------------------===//
1489// CastInst Class
1490//===----------------------------------------------------------------------===//
1491
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001492// Just determine if this cast only deals with integral->integral conversion.
1493bool CastInst::isIntegerCast() const {
1494 switch (getOpcode()) {
1495 default: return false;
1496 case Instruction::ZExt:
1497 case Instruction::SExt:
1498 case Instruction::Trunc:
1499 return true;
1500 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001501 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001502 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001503}
1504
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001505bool CastInst::isLosslessCast() const {
1506 // Only BitCast can be lossless, exit fast if we're not BitCast
1507 if (getOpcode() != Instruction::BitCast)
1508 return false;
1509
1510 // Identity cast is always lossless
1511 const Type* SrcTy = getOperand(0)->getType();
1512 const Type* DstTy = getType();
1513 if (SrcTy == DstTy)
1514 return true;
1515
Reid Spencer8d9336d2006-12-31 05:26:44 +00001516 // Pointer to pointer is always lossless.
1517 if (isa<PointerType>(SrcTy))
1518 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001519 return false; // Other types have no identity values
1520}
1521
1522/// This function determines if the CastInst does not require any bits to be
1523/// changed in order to effect the cast. Essentially, it identifies cases where
1524/// no code gen is necessary for the cast, hence the name no-op cast. For
1525/// example, the following are all no-op casts:
1526/// # bitcast uint %X, int
1527/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001528/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001529/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1530/// @brief Determine if a cast is a no-op.
1531bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1532 switch (getOpcode()) {
1533 default:
1534 assert(!"Invalid CastOp");
1535 case Instruction::Trunc:
1536 case Instruction::ZExt:
1537 case Instruction::SExt:
1538 case Instruction::FPTrunc:
1539 case Instruction::FPExt:
1540 case Instruction::UIToFP:
1541 case Instruction::SIToFP:
1542 case Instruction::FPToUI:
1543 case Instruction::FPToSI:
1544 return false; // These always modify bits
1545 case Instruction::BitCast:
1546 return true; // BitCast never modifies bits.
1547 case Instruction::PtrToInt:
1548 return IntPtrTy->getPrimitiveSizeInBits() ==
1549 getType()->getPrimitiveSizeInBits();
1550 case Instruction::IntToPtr:
1551 return IntPtrTy->getPrimitiveSizeInBits() ==
1552 getOperand(0)->getType()->getPrimitiveSizeInBits();
1553 }
1554}
1555
1556/// This function determines if a pair of casts can be eliminated and what
1557/// opcode should be used in the elimination. This assumes that there are two
1558/// instructions like this:
1559/// * %F = firstOpcode SrcTy %x to MidTy
1560/// * %S = secondOpcode MidTy %F to DstTy
1561/// The function returns a resultOpcode so these two casts can be replaced with:
1562/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1563/// If no such cast is permited, the function returns 0.
1564unsigned CastInst::isEliminableCastPair(
1565 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1566 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1567{
1568 // Define the 144 possibilities for these two cast instructions. The values
1569 // in this matrix determine what to do in a given situation and select the
1570 // case in the switch below. The rows correspond to firstOp, the columns
1571 // correspond to secondOp. In looking at the table below, keep in mind
1572 // the following cast properties:
1573 //
1574 // Size Compare Source Destination
1575 // Operator Src ? Size Type Sign Type Sign
1576 // -------- ------------ ------------------- ---------------------
1577 // TRUNC > Integer Any Integral Any
1578 // ZEXT < Integral Unsigned Integer Any
1579 // SEXT < Integral Signed Integer Any
1580 // FPTOUI n/a FloatPt n/a Integral Unsigned
1581 // FPTOSI n/a FloatPt n/a Integral Signed
1582 // UITOFP n/a Integral Unsigned FloatPt n/a
1583 // SITOFP n/a Integral Signed FloatPt n/a
1584 // FPTRUNC > FloatPt n/a FloatPt n/a
1585 // FPEXT < FloatPt n/a FloatPt n/a
1586 // PTRTOINT n/a Pointer n/a Integral Unsigned
1587 // INTTOPTR n/a Integral Unsigned Pointer n/a
1588 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001589 //
1590 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1591 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1592 // into "fptoui double to ulong", but this loses information about the range
1593 // of the produced value (we no longer know the top-part is all zeros).
1594 // Further this conversion is often much more expensive for typical hardware,
1595 // and causes issues when building libgcc. We disallow fptosi+sext for the
1596 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001597 const unsigned numCastOps =
1598 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1599 static const uint8_t CastResults[numCastOps][numCastOps] = {
1600 // T F F U S F F P I B -+
1601 // R Z S P P I I T P 2 N T |
1602 // U E E 2 2 2 2 R E I T C +- secondOp
1603 // N X X U S F F N X N 2 V |
1604 // C T T I I P P C T T P T -+
1605 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1606 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1607 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001608 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1609 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001610 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1611 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1612 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1613 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1614 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1615 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1616 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1617 };
1618
1619 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1620 [secondOp-Instruction::CastOpsBegin];
1621 switch (ElimCase) {
1622 case 0:
1623 // categorically disallowed
1624 return 0;
1625 case 1:
1626 // allowed, use first cast's opcode
1627 return firstOp;
1628 case 2:
1629 // allowed, use second cast's opcode
1630 return secondOp;
1631 case 3:
1632 // no-op cast in second op implies firstOp as long as the DestTy
1633 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001634 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001635 return firstOp;
1636 return 0;
1637 case 4:
1638 // no-op cast in second op implies firstOp as long as the DestTy
1639 // is floating point
1640 if (DstTy->isFloatingPoint())
1641 return firstOp;
1642 return 0;
1643 case 5:
1644 // no-op cast in first op implies secondOp as long as the SrcTy
1645 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001646 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001647 return secondOp;
1648 return 0;
1649 case 6:
1650 // no-op cast in first op implies secondOp as long as the SrcTy
1651 // is a floating point
1652 if (SrcTy->isFloatingPoint())
1653 return secondOp;
1654 return 0;
1655 case 7: {
1656 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1657 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1658 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1659 if (MidSize >= PtrSize)
1660 return Instruction::BitCast;
1661 return 0;
1662 }
1663 case 8: {
1664 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1665 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1666 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1667 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1668 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1669 if (SrcSize == DstSize)
1670 return Instruction::BitCast;
1671 else if (SrcSize < DstSize)
1672 return firstOp;
1673 return secondOp;
1674 }
1675 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1676 return Instruction::ZExt;
1677 case 10:
1678 // fpext followed by ftrunc is allowed if the bit size returned to is
1679 // the same as the original, in which case its just a bitcast
1680 if (SrcTy == DstTy)
1681 return Instruction::BitCast;
1682 return 0; // If the types are not the same we can't eliminate it.
1683 case 11:
1684 // bitcast followed by ptrtoint is allowed as long as the bitcast
1685 // is a pointer to pointer cast.
1686 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1687 return secondOp;
1688 return 0;
1689 case 12:
1690 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1691 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1692 return firstOp;
1693 return 0;
1694 case 13: {
1695 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1696 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1697 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1698 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1699 if (SrcSize <= PtrSize && SrcSize == DstSize)
1700 return Instruction::BitCast;
1701 return 0;
1702 }
1703 case 99:
1704 // cast combination can't happen (error in input). This is for all cases
1705 // where the MidTy is not the same for the two cast instructions.
1706 assert(!"Invalid Cast Combination");
1707 return 0;
1708 default:
1709 assert(!"Error in CastResults table!!!");
1710 return 0;
1711 }
1712 return 0;
1713}
1714
1715CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1716 const std::string &Name, Instruction *InsertBefore) {
1717 // Construct and return the appropriate CastInst subclass
1718 switch (op) {
1719 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1720 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1721 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1722 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1723 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1724 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1725 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1726 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1727 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1728 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1729 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1730 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1731 default:
1732 assert(!"Invalid opcode provided");
1733 }
1734 return 0;
1735}
1736
1737CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1738 const std::string &Name, BasicBlock *InsertAtEnd) {
1739 // Construct and return the appropriate CastInst subclass
1740 switch (op) {
1741 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1742 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1743 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1744 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1745 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1746 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1747 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1748 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1749 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1750 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1751 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1752 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1753 default:
1754 assert(!"Invalid opcode provided");
1755 }
1756 return 0;
1757}
1758
Reid Spencer5c140882006-12-04 20:17:56 +00001759CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1760 const std::string &Name,
1761 Instruction *InsertBefore) {
1762 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1763 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1764 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1765}
1766
1767CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1768 const std::string &Name,
1769 BasicBlock *InsertAtEnd) {
1770 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1771 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1772 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1773}
1774
1775CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1776 const std::string &Name,
1777 Instruction *InsertBefore) {
1778 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1779 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1780 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1781}
1782
1783CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1784 const std::string &Name,
1785 BasicBlock *InsertAtEnd) {
1786 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1787 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1788 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1789}
1790
1791CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1792 const std::string &Name,
1793 Instruction *InsertBefore) {
1794 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1795 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1796 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1797}
1798
1799CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1800 const std::string &Name,
1801 BasicBlock *InsertAtEnd) {
1802 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1803 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1804 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1805}
1806
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001807CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1808 const std::string &Name,
1809 BasicBlock *InsertAtEnd) {
1810 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001811 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001812 "Invalid cast");
1813
Chris Lattner03c49532007-01-15 02:27:26 +00001814 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001815 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1816 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1817}
1818
1819/// @brief Create a BitCast or a PtrToInt cast instruction
1820CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1821 const std::string &Name,
1822 Instruction *InsertBefore) {
1823 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001824 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001825 "Invalid cast");
1826
Chris Lattner03c49532007-01-15 02:27:26 +00001827 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001828 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1829 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1830}
1831
Reid Spencer7e933472006-12-12 00:49:44 +00001832CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1833 bool isSigned, const std::string &Name,
1834 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001835 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001836 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1837 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1838 Instruction::CastOps opcode =
1839 (SrcBits == DstBits ? Instruction::BitCast :
1840 (SrcBits > DstBits ? Instruction::Trunc :
1841 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1842 return create(opcode, C, Ty, Name, InsertBefore);
1843}
1844
1845CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1846 bool isSigned, const std::string &Name,
1847 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001848 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001849 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1850 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1851 Instruction::CastOps opcode =
1852 (SrcBits == DstBits ? Instruction::BitCast :
1853 (SrcBits > DstBits ? Instruction::Trunc :
1854 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1855 return create(opcode, C, Ty, Name, InsertAtEnd);
1856}
1857
1858CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1859 const std::string &Name,
1860 Instruction *InsertBefore) {
1861 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1862 "Invalid cast");
1863 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1864 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1865 Instruction::CastOps opcode =
1866 (SrcBits == DstBits ? Instruction::BitCast :
1867 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1868 return create(opcode, C, Ty, Name, InsertBefore);
1869}
1870
1871CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1872 const std::string &Name,
1873 BasicBlock *InsertAtEnd) {
1874 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1875 "Invalid cast");
1876 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1877 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1878 Instruction::CastOps opcode =
1879 (SrcBits == DstBits ? Instruction::BitCast :
1880 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1881 return create(opcode, C, Ty, Name, InsertAtEnd);
1882}
1883
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001884// Provide a way to get a "cast" where the cast opcode is inferred from the
1885// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001886// logic in the castIsValid function below. This axiom should hold:
1887// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1888// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889// casting opcode for the arguments passed to it.
1890Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001891CastInst::getCastOpcode(
1892 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893 // Get the bit sizes, we'll need these
1894 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001895 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1896 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897
1898 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001899 if (DestTy->isInteger()) { // Casting to integral
1900 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 if (DestBits < SrcBits)
1902 return Trunc; // int -> smaller int
1903 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001904 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 return SExt; // signed -> SEXT
1906 else
1907 return ZExt; // unsigned -> ZEXT
1908 } else {
1909 return BitCast; // Same size, No-op cast
1910 }
1911 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001912 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001913 return FPToSI; // FP -> sint
1914 else
1915 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001916 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001917 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001918 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919 return BitCast; // Same size, no-op cast
1920 } else {
1921 assert(isa<PointerType>(SrcTy) &&
1922 "Casting from a value that is not first-class type");
1923 return PtrToInt; // ptr -> int
1924 }
1925 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001926 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001927 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001928 return SIToFP; // sint -> FP
1929 else
1930 return UIToFP; // uint -> FP
1931 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1932 if (DestBits < SrcBits) {
1933 return FPTrunc; // FP -> smaller FP
1934 } else if (DestBits > SrcBits) {
1935 return FPExt; // FP -> larger FP
1936 } else {
1937 return BitCast; // same size, no-op cast
1938 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001939 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001940 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001941 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001942 return BitCast; // same size, no-op cast
1943 } else {
1944 assert(0 && "Casting pointer or non-first class to float");
1945 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001946 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1947 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001949 "Casting vector to vector of different widths");
1950 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001951 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001952 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001954 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955 }
1956 } else if (isa<PointerType>(DestTy)) {
1957 if (isa<PointerType>(SrcTy)) {
1958 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001959 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 return IntToPtr; // int -> ptr
1961 } else {
1962 assert(!"Casting pointer to other than pointer or int");
1963 }
1964 } else {
1965 assert(!"Casting to type that is not first-class");
1966 }
1967
1968 // If we fall through to here we probably hit an assertion cast above
1969 // and assertions are not turned on. Anything we return is an error, so
1970 // BitCast is as good a choice as any.
1971 return BitCast;
1972}
1973
1974//===----------------------------------------------------------------------===//
1975// CastInst SubClass Constructors
1976//===----------------------------------------------------------------------===//
1977
1978/// Check that the construction parameters for a CastInst are correct. This
1979/// could be broken out into the separate constructors but it is useful to have
1980/// it in one place and to eliminate the redundant code for getting the sizes
1981/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001982bool
1983CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984
1985 // Check for type sanity on the arguments
1986 const Type *SrcTy = S->getType();
1987 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1988 return false;
1989
1990 // Get the size of the types in bits, we'll need this later
1991 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1992 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1993
1994 // Switch on the opcode provided
1995 switch (op) {
1996 default: return false; // This is an input error
1997 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001998 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002000 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002002 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 case Instruction::FPTrunc:
2004 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2005 SrcBitSize > DstBitSize;
2006 case Instruction::FPExt:
2007 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2008 SrcBitSize < DstBitSize;
2009 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00002010 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00002012 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00002014 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00002016 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002017 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002018 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002019 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002020 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021 case Instruction::BitCast:
2022 // BitCast implies a no-op cast of type only. No bits change.
2023 // However, you can't cast pointers to anything but pointers.
2024 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2025 return false;
2026
2027 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
2028 // these cases, the cast is okay if the source and destination bit widths
2029 // are identical.
2030 return SrcBitSize == DstBitSize;
2031 }
2032}
2033
2034TruncInst::TruncInst(
2035 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2036) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002037 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038}
2039
2040TruncInst::TruncInst(
2041 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2042) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002043 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044}
2045
2046ZExtInst::ZExtInst(
2047 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2048) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002049 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050}
2051
2052ZExtInst::ZExtInst(
2053 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2054) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002055 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056}
2057SExtInst::SExtInst(
2058 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2059) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002060 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061}
2062
Jeff Cohencc08c832006-12-02 02:22:01 +00002063SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002064 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2065) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002066 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067}
2068
2069FPTruncInst::FPTruncInst(
2070 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2071) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002072 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073}
2074
2075FPTruncInst::FPTruncInst(
2076 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2077) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002078 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079}
2080
2081FPExtInst::FPExtInst(
2082 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2083) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002084 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085}
2086
2087FPExtInst::FPExtInst(
2088 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2089) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002090 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091}
2092
2093UIToFPInst::UIToFPInst(
2094 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2095) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002096 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002097}
2098
2099UIToFPInst::UIToFPInst(
2100 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2101) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002102 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103}
2104
2105SIToFPInst::SIToFPInst(
2106 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2107) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002108 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109}
2110
2111SIToFPInst::SIToFPInst(
2112 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2113) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002114 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115}
2116
2117FPToUIInst::FPToUIInst(
2118 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2119) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002120 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121}
2122
2123FPToUIInst::FPToUIInst(
2124 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2125) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002126 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127}
2128
2129FPToSIInst::FPToSIInst(
2130 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2131) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002132 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133}
2134
2135FPToSIInst::FPToSIInst(
2136 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2137) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002138 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002139}
2140
2141PtrToIntInst::PtrToIntInst(
2142 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2143) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002144 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002145}
2146
2147PtrToIntInst::PtrToIntInst(
2148 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2149) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002150 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151}
2152
2153IntToPtrInst::IntToPtrInst(
2154 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2155) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002156 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157}
2158
2159IntToPtrInst::IntToPtrInst(
2160 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2161) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002162 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163}
2164
2165BitCastInst::BitCastInst(
2166 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2167) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002168 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169}
2170
2171BitCastInst::BitCastInst(
2172 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2173) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002174 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002176
2177//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002178// CmpInst Classes
2179//===----------------------------------------------------------------------===//
2180
2181CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2182 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002183 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002184 Ops[0].init(LHS, this);
2185 Ops[1].init(RHS, this);
2186 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002187 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002188 if (op == Instruction::ICmp) {
2189 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2190 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2191 "Invalid ICmp predicate value");
2192 const Type* Op0Ty = getOperand(0)->getType();
2193 const Type* Op1Ty = getOperand(1)->getType();
2194 assert(Op0Ty == Op1Ty &&
2195 "Both operands to ICmp instruction are not of the same type!");
2196 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002197 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002198 "Invalid operand types for ICmp instruction");
2199 return;
2200 }
2201 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2202 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2203 "Invalid FCmp predicate value");
2204 const Type* Op0Ty = getOperand(0)->getType();
2205 const Type* Op1Ty = getOperand(1)->getType();
2206 assert(Op0Ty == Op1Ty &&
2207 "Both operands to FCmp instruction are not of the same type!");
2208 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002209 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002210 "Invalid operand types for FCmp instruction");
2211}
2212
2213CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2214 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002215 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002216 Ops[0].init(LHS, this);
2217 Ops[1].init(RHS, this);
2218 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002219 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002220 if (op == Instruction::ICmp) {
2221 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2222 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2223 "Invalid ICmp predicate value");
2224
2225 const Type* Op0Ty = getOperand(0)->getType();
2226 const Type* Op1Ty = getOperand(1)->getType();
2227 assert(Op0Ty == Op1Ty &&
2228 "Both operands to ICmp instruction are not of the same type!");
2229 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002230 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002231 "Invalid operand types for ICmp instruction");
2232 return;
2233 }
2234 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2235 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2236 "Invalid FCmp predicate value");
2237 const Type* Op0Ty = getOperand(0)->getType();
2238 const Type* Op1Ty = getOperand(1)->getType();
2239 assert(Op0Ty == Op1Ty &&
2240 "Both operands to FCmp instruction are not of the same type!");
2241 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002242 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002243 "Invalid operand types for FCmp instruction");
2244}
2245
2246CmpInst *
2247CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2248 const std::string &Name, Instruction *InsertBefore) {
2249 if (Op == Instruction::ICmp) {
2250 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2251 InsertBefore);
2252 }
2253 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2254 InsertBefore);
2255}
2256
2257CmpInst *
2258CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2259 const std::string &Name, BasicBlock *InsertAtEnd) {
2260 if (Op == Instruction::ICmp) {
2261 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2262 InsertAtEnd);
2263 }
2264 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2265 InsertAtEnd);
2266}
2267
2268void CmpInst::swapOperands() {
2269 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2270 IC->swapOperands();
2271 else
2272 cast<FCmpInst>(this)->swapOperands();
2273}
2274
2275bool CmpInst::isCommutative() {
2276 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2277 return IC->isCommutative();
2278 return cast<FCmpInst>(this)->isCommutative();
2279}
2280
2281bool CmpInst::isEquality() {
2282 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2283 return IC->isEquality();
2284 return cast<FCmpInst>(this)->isEquality();
2285}
2286
2287
2288ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2289 switch (pred) {
2290 default:
2291 assert(!"Unknown icmp predicate!");
2292 case ICMP_EQ: return ICMP_NE;
2293 case ICMP_NE: return ICMP_EQ;
2294 case ICMP_UGT: return ICMP_ULE;
2295 case ICMP_ULT: return ICMP_UGE;
2296 case ICMP_UGE: return ICMP_ULT;
2297 case ICMP_ULE: return ICMP_UGT;
2298 case ICMP_SGT: return ICMP_SLE;
2299 case ICMP_SLT: return ICMP_SGE;
2300 case ICMP_SGE: return ICMP_SLT;
2301 case ICMP_SLE: return ICMP_SGT;
2302 }
2303}
2304
2305ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2306 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002307 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002308 case ICMP_EQ: case ICMP_NE:
2309 return pred;
2310 case ICMP_SGT: return ICMP_SLT;
2311 case ICMP_SLT: return ICMP_SGT;
2312 case ICMP_SGE: return ICMP_SLE;
2313 case ICMP_SLE: return ICMP_SGE;
2314 case ICMP_UGT: return ICMP_ULT;
2315 case ICMP_ULT: return ICMP_UGT;
2316 case ICMP_UGE: return ICMP_ULE;
2317 case ICMP_ULE: return ICMP_UGE;
2318 }
2319}
2320
Reid Spencer266e42b2006-12-23 06:05:41 +00002321ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2322 switch (pred) {
2323 default: assert(! "Unknown icmp predicate!");
2324 case ICMP_EQ: case ICMP_NE:
2325 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2326 return pred;
2327 case ICMP_UGT: return ICMP_SGT;
2328 case ICMP_ULT: return ICMP_SLT;
2329 case ICMP_UGE: return ICMP_SGE;
2330 case ICMP_ULE: return ICMP_SLE;
2331 }
2332}
2333
2334bool ICmpInst::isSignedPredicate(Predicate pred) {
2335 switch (pred) {
2336 default: assert(! "Unknown icmp predicate!");
2337 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2338 return true;
2339 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2340 case ICMP_UGE: case ICMP_ULE:
2341 return false;
2342 }
2343}
2344
Reid Spencer0286bc12007-02-28 22:00:54 +00002345/// Initialize a set of values that all satisfy the condition with C.
2346///
2347ConstantRange
2348ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2349 APInt Lower(C);
2350 APInt Upper(C);
2351 uint32_t BitWidth = C.getBitWidth();
2352 switch (pred) {
2353 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2354 case ICmpInst::ICMP_EQ: Upper++; break;
2355 case ICmpInst::ICMP_NE: Lower++; break;
2356 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2357 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2358 case ICmpInst::ICMP_UGT:
2359 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2360 break;
2361 case ICmpInst::ICMP_SGT:
2362 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2363 break;
2364 case ICmpInst::ICMP_ULE:
2365 Lower = APInt::getMinValue(BitWidth); Upper++;
2366 break;
2367 case ICmpInst::ICMP_SLE:
2368 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2369 break;
2370 case ICmpInst::ICMP_UGE:
2371 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2372 break;
2373 case ICmpInst::ICMP_SGE:
2374 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2375 break;
2376 }
2377 return ConstantRange(Lower, Upper);
2378}
2379
Reid Spencerd9436b62006-11-20 01:22:35 +00002380FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2381 switch (pred) {
2382 default:
2383 assert(!"Unknown icmp predicate!");
2384 case FCMP_OEQ: return FCMP_UNE;
2385 case FCMP_ONE: return FCMP_UEQ;
2386 case FCMP_OGT: return FCMP_ULE;
2387 case FCMP_OLT: return FCMP_UGE;
2388 case FCMP_OGE: return FCMP_ULT;
2389 case FCMP_OLE: return FCMP_UGT;
2390 case FCMP_UEQ: return FCMP_ONE;
2391 case FCMP_UNE: return FCMP_OEQ;
2392 case FCMP_UGT: return FCMP_OLE;
2393 case FCMP_ULT: return FCMP_OGE;
2394 case FCMP_UGE: return FCMP_OLT;
2395 case FCMP_ULE: return FCMP_OGT;
2396 case FCMP_ORD: return FCMP_UNO;
2397 case FCMP_UNO: return FCMP_ORD;
2398 case FCMP_TRUE: return FCMP_FALSE;
2399 case FCMP_FALSE: return FCMP_TRUE;
2400 }
2401}
2402
2403FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2404 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002405 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002406 case FCMP_FALSE: case FCMP_TRUE:
2407 case FCMP_OEQ: case FCMP_ONE:
2408 case FCMP_UEQ: case FCMP_UNE:
2409 case FCMP_ORD: case FCMP_UNO:
2410 return pred;
2411 case FCMP_OGT: return FCMP_OLT;
2412 case FCMP_OLT: return FCMP_OGT;
2413 case FCMP_OGE: return FCMP_OLE;
2414 case FCMP_OLE: return FCMP_OGE;
2415 case FCMP_UGT: return FCMP_ULT;
2416 case FCMP_ULT: return FCMP_UGT;
2417 case FCMP_UGE: return FCMP_ULE;
2418 case FCMP_ULE: return FCMP_UGE;
2419 }
2420}
2421
Reid Spencer266e42b2006-12-23 06:05:41 +00002422bool CmpInst::isUnsigned(unsigned short predicate) {
2423 switch (predicate) {
2424 default: return false;
2425 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2426 case ICmpInst::ICMP_UGE: return true;
2427 }
2428}
2429
2430bool CmpInst::isSigned(unsigned short predicate){
2431 switch (predicate) {
2432 default: return false;
2433 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2434 case ICmpInst::ICMP_SGE: return true;
2435 }
2436}
2437
2438bool CmpInst::isOrdered(unsigned short predicate) {
2439 switch (predicate) {
2440 default: return false;
2441 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2442 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2443 case FCmpInst::FCMP_ORD: return true;
2444 }
2445}
2446
2447bool CmpInst::isUnordered(unsigned short predicate) {
2448 switch (predicate) {
2449 default: return false;
2450 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2451 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2452 case FCmpInst::FCMP_UNO: return true;
2453 }
2454}
2455
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002456//===----------------------------------------------------------------------===//
2457// SwitchInst Implementation
2458//===----------------------------------------------------------------------===//
2459
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002460void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002461 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002462 ReservedSpace = 2+NumCases*2;
2463 NumOperands = 2;
2464 OperandList = new Use[ReservedSpace];
2465
2466 OperandList[0].init(Value, this);
2467 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002468}
2469
Chris Lattner2195fc42007-02-24 00:55:48 +00002470/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2471/// switch on and a default destination. The number of additional cases can
2472/// be specified here to make memory allocation more efficient. This
2473/// constructor can also autoinsert before another instruction.
2474SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2475 Instruction *InsertBefore)
2476 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2477 init(Value, Default, NumCases);
2478}
2479
2480/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2481/// switch on and a default destination. The number of additional cases can
2482/// be specified here to make memory allocation more efficient. This
2483/// constructor also autoinserts at the end of the specified BasicBlock.
2484SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2485 BasicBlock *InsertAtEnd)
2486 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2487 init(Value, Default, NumCases);
2488}
2489
Misha Brukmanb1c93172005-04-21 23:48:37 +00002490SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002491 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2492 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002493 Use *OL = OperandList, *InOL = SI.OperandList;
2494 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2495 OL[i].init(InOL[i], this);
2496 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002497 }
2498}
2499
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002500SwitchInst::~SwitchInst() {
2501 delete [] OperandList;
2502}
2503
2504
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002505/// addCase - Add an entry to the switch instruction...
2506///
Chris Lattner47ac1872005-02-24 05:32:09 +00002507void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002508 unsigned OpNo = NumOperands;
2509 if (OpNo+2 > ReservedSpace)
2510 resizeOperands(0); // Get more space!
2511 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002512 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002513 NumOperands = OpNo+2;
2514 OperandList[OpNo].init(OnVal, this);
2515 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002516}
2517
2518/// removeCase - This method removes the specified successor from the switch
2519/// instruction. Note that this cannot be used to remove the default
2520/// destination (successor #0).
2521///
2522void SwitchInst::removeCase(unsigned idx) {
2523 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002524 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2525
2526 unsigned NumOps = getNumOperands();
2527 Use *OL = OperandList;
2528
2529 // Move everything after this operand down.
2530 //
2531 // FIXME: we could just swap with the end of the list, then erase. However,
2532 // client might not expect this to happen. The code as it is thrashes the
2533 // use/def lists, which is kinda lame.
2534 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2535 OL[i-2] = OL[i];
2536 OL[i-2+1] = OL[i+1];
2537 }
2538
2539 // Nuke the last value.
2540 OL[NumOps-2].set(0);
2541 OL[NumOps-2+1].set(0);
2542 NumOperands = NumOps-2;
2543}
2544
2545/// resizeOperands - resize operands - This adjusts the length of the operands
2546/// list according to the following behavior:
2547/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2548/// of operation. This grows the number of ops by 1.5 times.
2549/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2550/// 3. If NumOps == NumOperands, trim the reserved space.
2551///
2552void SwitchInst::resizeOperands(unsigned NumOps) {
2553 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002554 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002555 } else if (NumOps*2 > NumOperands) {
2556 // No resize needed.
2557 if (ReservedSpace >= NumOps) return;
2558 } else if (NumOps == NumOperands) {
2559 if (ReservedSpace == NumOps) return;
2560 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002561 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002562 }
2563
2564 ReservedSpace = NumOps;
2565 Use *NewOps = new Use[NumOps];
2566 Use *OldOps = OperandList;
2567 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2568 NewOps[i].init(OldOps[i], this);
2569 OldOps[i].set(0);
2570 }
2571 delete [] OldOps;
2572 OperandList = NewOps;
2573}
2574
2575
2576BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2577 return getSuccessor(idx);
2578}
2579unsigned SwitchInst::getNumSuccessorsV() const {
2580 return getNumSuccessors();
2581}
2582void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2583 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002584}
Chris Lattnerf22be932004-10-15 23:52:53 +00002585
2586
2587// Define these methods here so vtables don't get emitted into every translation
2588// unit that uses these classes.
2589
2590GetElementPtrInst *GetElementPtrInst::clone() const {
2591 return new GetElementPtrInst(*this);
2592}
2593
2594BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002595 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002596}
2597
Reid Spencerd9436b62006-11-20 01:22:35 +00002598CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002599 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002600}
2601
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2603AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2604FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2605LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2606StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2607CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2608CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2609CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2610CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2611CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2612CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2613CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2614CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2615CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2616CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2617CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2618CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2619CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2621VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2622
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002623ExtractElementInst *ExtractElementInst::clone() const {
2624 return new ExtractElementInst(*this);
2625}
2626InsertElementInst *InsertElementInst::clone() const {
2627 return new InsertElementInst(*this);
2628}
2629ShuffleVectorInst *ShuffleVectorInst::clone() const {
2630 return new ShuffleVectorInst(*this);
2631}
Chris Lattnerf22be932004-10-15 23:52:53 +00002632PHINode *PHINode::clone() const { return new PHINode(*this); }
2633ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2634BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2635SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2636InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2637UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002638UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}